...

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

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

     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 podkill
    17  
    18  import (
    19  	"context"
    20  
    21  	v1 "k8s.io/api/core/v1"
    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/utils/controller"
    27  )
    28  
    29  var _ impltypes.ChaosImpl = (*Impl)(nil)
    30  
    31  type Impl struct {
    32  	client.Client
    33  }
    34  
    35  func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    36  	podchaos := obj.(*v1alpha1.PodChaos)
    37  
    38  	var pod v1.Pod
    39  	namespacedName, err := controller.ParseNamespacedName(records[index].Id)
    40  	if err != nil {
    41  		return v1alpha1.NotInjected, err
    42  	}
    43  	err = impl.Get(ctx, namespacedName, &pod)
    44  	if err != nil {
    45  		// TODO: handle this error
    46  		return v1alpha1.NotInjected, err
    47  	}
    48  
    49  	err = impl.Delete(ctx, &pod, &client.DeleteOptions{
    50  		GracePeriodSeconds: &podchaos.Spec.GracePeriod, // PeriodSeconds has to be set specifically
    51  	})
    52  	if err != nil {
    53  		// TODO: handle this error
    54  		return v1alpha1.NotInjected, err
    55  	}
    56  
    57  	return v1alpha1.Injected, nil
    58  }
    59  
    60  func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    61  	return v1alpha1.NotInjected, nil
    62  }
    63  
    64  func NewImpl(c client.Client) *Impl {
    65  	return &Impl{
    66  		Client: c,
    67  	}
    68  }
    69