...

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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package podkill
    15  
    16  import (
    17  	"context"
    18  
    19  	v1 "k8s.io/api/core/v1"
    20  	"sigs.k8s.io/controller-runtime/pkg/client"
    21  
    22  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    23  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/controller"
    24  )
    25  
    26  type Impl struct {
    27  	client.Client
    28  }
    29  
    30  func (impl *Impl) Apply(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    31  	podchaos := obj.(*v1alpha1.PodChaos)
    32  
    33  	var pod v1.Pod
    34  	err := impl.Get(ctx, controller.ParseNamespacedName(records[index].Id), &pod)
    35  	if err != nil {
    36  		// TODO: handle this error
    37  		return v1alpha1.NotInjected, err
    38  	}
    39  
    40  	err = impl.Delete(ctx, &pod, &client.DeleteOptions{
    41  		GracePeriodSeconds: &podchaos.Spec.GracePeriod, // PeriodSeconds has to be set specifically
    42  	})
    43  	if err != nil {
    44  		// TODO: handle this error
    45  		return v1alpha1.NotInjected, err
    46  	}
    47  
    48  	return v1alpha1.Injected, nil
    49  }
    50  
    51  func (impl *Impl) Recover(ctx context.Context, index int, records []*v1alpha1.Record, obj v1alpha1.InnerObject) (v1alpha1.Phase, error) {
    52  	return v1alpha1.NotInjected, nil
    53  }
    54  
    55  func NewImpl(c client.Client) *Impl {
    56  	return &Impl{
    57  		Client: c,
    58  	}
    59  }
    60