...

Source file src/github.com/chaos-mesh/chaos-mesh/controllers/finalizers/controller.go

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/finalizers

     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 finalizers
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/go-logr/logr"
    20  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    21  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    22  	"k8s.io/client-go/util/retry"
    23  	ctrl "sigs.k8s.io/controller-runtime"
    24  	"sigs.k8s.io/controller-runtime/pkg/client"
    25  
    26  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    27  	"github.com/chaos-mesh/chaos-mesh/controllers/utils/recorder"
    28  )
    29  
    30  const (
    31  	// AnnotationCleanFinalizer key
    32  	AnnotationCleanFinalizer = `chaos-mesh.chaos-mesh.org/cleanFinalizer`
    33  	// AnnotationCleanFinalizerForced value
    34  	AnnotationCleanFinalizerForced = `forced`
    35  
    36  	RecordFinalizer = "chaos-mesh/records"
    37  )
    38  
    39  // Reconciler for common chaos
    40  type Reconciler struct {
    41  	// Object is used to mark the target type of this Reconciler
    42  	Object v1alpha1.InnerObject
    43  
    44  	// Client is used to operate on the Kubernetes cluster
    45  	client.Client
    46  
    47  	Recorder recorder.ChaosRecorder
    48  
    49  	Log logr.Logger
    50  }
    51  
    52  // Reconcile the common chaos
    53  func (r *Reconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
    54  	obj := r.Object.DeepCopyObject().(v1alpha1.InnerObject)
    55  
    56  	if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
    57  		if apierrors.IsNotFound(err) {
    58  			r.Log.Info("chaos not found")
    59  		} else {
    60  			// TODO: handle this error
    61  			r.Log.Error(err, "unable to get chaos")
    62  		}
    63  		return ctrl.Result{}, nil
    64  	}
    65  
    66  	finalizers := obj.GetObjectMeta().Finalizers
    67  	records := obj.GetStatus().Experiment.Records
    68  	shouldUpdate := false
    69  	if obj.IsDeleted() {
    70  		resumed := true
    71  		for _, record := range records {
    72  			if record.Phase != v1alpha1.NotInjected {
    73  				resumed = false
    74  			}
    75  		}
    76  
    77  		if obj.GetObjectMeta().Annotations[AnnotationCleanFinalizer] == AnnotationCleanFinalizerForced || (resumed && len(finalizers) != 0) {
    78  			r.Recorder.Event(obj, recorder.FinalizerRemoved{})
    79  			finalizers = []string{}
    80  			shouldUpdate = true
    81  		}
    82  	} else {
    83  		if !ContainsFinalizer(obj.(metav1.Object), RecordFinalizer) {
    84  			r.Recorder.Event(obj, recorder.FinalizerInited{})
    85  			shouldUpdate = true
    86  			finalizers = append(obj.GetObjectMeta().Finalizers, RecordFinalizer)
    87  		}
    88  	}
    89  
    90  	if shouldUpdate {
    91  		updateError := retry.RetryOnConflict(retry.DefaultBackoff, func() error {
    92  			obj := r.Object.DeepCopyObject().(v1alpha1.InnerObject)
    93  
    94  			if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
    95  				r.Log.Error(err, "unable to get chaos")
    96  				return err
    97  			}
    98  
    99  			obj.GetObjectMeta().Finalizers = finalizers
   100  			return r.Client.Update(context.TODO(), obj)
   101  		})
   102  		if updateError != nil {
   103  			// TODO: handle this error
   104  			r.Log.Error(updateError, "fail to update")
   105  			r.Recorder.Event(obj, recorder.Failed{
   106  				Activity: "update finalizer",
   107  				Err:      "updateError.Error()",
   108  			})
   109  			return ctrl.Result{}, nil
   110  		}
   111  
   112  		r.Recorder.Event(obj, recorder.Updated{
   113  			Field: "finalizer",
   114  		})
   115  	}
   116  	return ctrl.Result{}, nil
   117  }
   118  
   119  // ContainsFinalizer checks an Object that the provided finalizer is present.
   120  func ContainsFinalizer(o metav1.Object, finalizer string) bool {
   121  	f := o.GetFinalizers()
   122  	for _, e := range f {
   123  		if e == finalizer {
   124  			return true
   125  		}
   126  	}
   127  	return false
   128  }
   129