...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/podchaos/containerkill/impl.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/podchaos/containerkill

     1  // Copyright 2021 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  // http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package containerkill
    17  
    18  import (
    19  	"context"
    20  
    21  	"github.com/go-logr/logr"
    22  	"sigs.k8s.io/controller-runtime/pkg/client"
    23  
    24  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    25  	impltypes "github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/types"
    26  	"github.com/chaos-mesh/chaos-mesh/controllers/chaosimpl/utils"
    27  	"github.com/chaos-mesh/chaos-mesh/pkg/chaosdaemon/pb"
    28  )
    29  
    30  var _ impltypes.ChaosImpl = (*Impl)(nil)
    31  
    32  type Impl struct {
    33  	client.Client
    34  
    35  	Log logr.Logger
    36  
    37  	decoder *utils.ContainerRecordDecoder
    38  }
    39  
    40  func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    41  	decodedContainer, err := impl.decoder.DecodeContainerRecord(ctx, records[index], obj)
    42  	pbClient := decodedContainer.PbClient
    43  	containerId := decodedContainer.ContainerId
    44  	if pbClient != nil {
    45  		defer pbClient.Close()
    46  	}
    47  	if err != nil {
    48  		return v1alpha1.NotInjected, err
    49  	}
    50  
    51  	if _, err = pbClient.ContainerKill(ctx, &pb.ContainerRequest{
    52  		Action: &pb.ContainerAction{
    53  			Action: pb.ContainerAction_KILL,
    54  		},
    55  		ContainerId: containerId,
    56  	}); err != nil {
    57  		impl.Log.Error(err, "kill container error", "containerID", containerId)
    58  		return v1alpha1.NotInjected, err
    59  	}
    60  
    61  	return v1alpha1.Injected, nil
    62  }
    63  
    64  func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    65  	return v1alpha1.NotInjected, nil
    66  }
    67  
    68  func NewImpl(c client.Client, log logr.Logger, decoder *utils.ContainerRecordDecoder) *Impl {
    69  	return &Impl{
    70  		Client:  c,
    71  		Log:     log.WithName("containerkill"),
    72  		decoder: decoder,
    73  	}
    74  }
    75