...

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

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

     1  // Copyright 2020 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 recover
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/go-logr/logr"
    20  	"github.com/hashicorp/go-multierror"
    21  	v1 "k8s.io/api/core/v1"
    22  	k8serror "k8s.io/apimachinery/pkg/api/errors"
    23  	"k8s.io/apimachinery/pkg/types"
    24  	"k8s.io/client-go/tools/cache"
    25  	"sigs.k8s.io/controller-runtime/pkg/client"
    26  
    27  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    28  	"github.com/chaos-mesh/chaos-mesh/controllers/common"
    29  	"github.com/chaos-mesh/chaos-mesh/pkg/finalizer"
    30  )
    31  
    32  type Delegate struct {
    33  	client.Client
    34  	Log logr.Logger
    35  	RecoverIntf
    36  }
    37  
    38  type RecoverIntf interface {
    39  	RecoverPod(context.Context, *v1.Pod, v1alpha1.InnerObject) error
    40  }
    41  
    42  func (r *Delegate) CleanFinalizersAndRecover(ctx context.Context, chaos v1alpha1.InnerObject, finalizers []string, annotations map[string]string) ([]string, error) {
    43  	var result error
    44  
    45  	for _, key := range finalizers {
    46  		ns, name, err := cache.SplitMetaNamespaceKey(key)
    47  		if err != nil {
    48  			result = multierror.Append(result, err)
    49  			continue
    50  		}
    51  
    52  		var pod v1.Pod
    53  		err = r.Client.Get(ctx, types.NamespacedName{
    54  			Namespace: ns,
    55  			Name:      name,
    56  		}, &pod)
    57  
    58  		if err != nil {
    59  			if !k8serror.IsNotFound(err) {
    60  				result = multierror.Append(result, err)
    61  				continue
    62  			}
    63  
    64  			r.Log.Info("Pod not found", "namespace", ns, "name", name)
    65  			finalizers = finalizer.RemoveFromFinalizer(finalizers, key)
    66  			continue
    67  		}
    68  
    69  		err = r.RecoverPod(ctx, &pod, chaos)
    70  		if err != nil {
    71  			result = multierror.Append(result, err)
    72  			continue
    73  		}
    74  
    75  		finalizers = finalizer.RemoveFromFinalizer(finalizers, key)
    76  	}
    77  
    78  	if annotations[common.AnnotationCleanFinalizer] == common.AnnotationCleanFinalizerForced {
    79  		r.Log.Info("Force cleanup all finalizers", "chaos", chaos)
    80  		finalizers = finalizers[:0]
    81  		return finalizers, nil
    82  	}
    83  
    84  	return finalizers, result
    85  }
    86