...

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

Documentation: github.com/chaos-mesh/chaos-mesh/controllers/multicluster/remotechaos

     1  // Copyright 2022 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 remotechaos
    17  
    18  import (
    19  	"context"
    20  	"reflect"
    21  
    22  	"github.com/go-logr/logr"
    23  	apierrors "k8s.io/apimachinery/pkg/api/errors"
    24  	"k8s.io/client-go/util/retry"
    25  	ctrl "sigs.k8s.io/controller-runtime"
    26  	"sigs.k8s.io/controller-runtime/pkg/client"
    27  
    28  	"github.com/chaos-mesh/chaos-mesh/api/v1alpha1"
    29  	"github.com/chaos-mesh/chaos-mesh/controllers/multicluster/clusterregistry"
    30  )
    31  
    32  type Reconciler struct {
    33  	client.Client
    34  	Log logr.Logger
    35  
    36  	Object v1alpha1.InnerObject
    37  
    38  	registry *clusterregistry.RemoteClusterRegistry
    39  }
    40  
    41  func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    42  	obj := r.Object.DeepCopyObject().(v1alpha1.RemoteObject)
    43  
    44  	if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
    45  		if apierrors.IsNotFound(err) {
    46  			r.Log.Info("chaos not found")
    47  		} else {
    48  			// TODO: handle this error
    49  			r.Log.Error(err, "unable to get chaos")
    50  		}
    51  		return ctrl.Result{}, nil
    52  	}
    53  
    54  	err := r.registry.WithClient(obj.GetRemoteCluster(), func(c client.Client) error {
    55  		r.Log.Info("handling chaos with remote client", "cluster", obj.GetRemoteCluster())
    56  
    57  		localObj := obj.DeepCopyObject().(v1alpha1.RemoteObject)
    58  
    59  		remoteObj := obj.DeepCopyObject().(v1alpha1.RemoteObject)
    60  		err := c.Get(ctx, req.NamespacedName, remoteObj)
    61  		if err != nil {
    62  			if apierrors.IsNotFound(err) {
    63  				// remote chaos doesn't exist, while the local one is being deleted
    64  				if localObj.GetDeletionTimestamp() != nil {
    65  					return retry.RetryOnConflict(retry.DefaultRetry, func() error {
    66  						var obj v1alpha1.RemoteObject
    67  						r.Log.Info("resetting finalizers of local objects")
    68  						if err := r.Client.Get(context.TODO(), req.NamespacedName, obj); err != nil {
    69  							if apierrors.IsNotFound(err) {
    70  								r.Log.Info("chaos has been removed")
    71  								return nil
    72  							}
    73  							// TODO: handle this error
    74  							r.Log.Error(err, "unable to get chaos")
    75  
    76  							return err
    77  						}
    78  
    79  						obj.SetFinalizers([]string{})
    80  						return r.Client.Update(ctx, obj)
    81  					})
    82  				}
    83  
    84  				// omit the remoteCluster
    85  				localSpecValue := reflect.Indirect(reflect.ValueOf(localObj)).FieldByName("Spec")
    86  				localSpecValue.FieldByName("RemoteCluster").Set(reflect.ValueOf(""))
    87  
    88  				// only Spec, Name, Namespace and a label will be initialized
    89  				newObj := r.Object.DeepCopyObject().(v1alpha1.RemoteObject)
    90  				reflect.Indirect(reflect.ValueOf(newObj)).FieldByName("Spec").Set(localSpecValue)
    91  
    92  				newObj.SetLabels(map[string]string{
    93  					"chaos-mesh.org/controlled-by": "remote-chaos",
    94  				})
    95  				newObj.SetName(obj.GetName())
    96  				newObj.SetNamespace(obj.GetNamespace())
    97  
    98  				return c.Create(ctx, newObj)
    99  			}
   100  
   101  			// TODO: handle this error
   102  			r.Log.Error(err, "unable to get chaos")
   103  			return nil
   104  		}
   105  
   106  		// remote chaos exists
   107  		if localObj.GetDeletionTimestamp() != nil {
   108  			r.Log.Info("deleting remote obj", "namespace", remoteObj.GetNamespace(), "name", remoteObj.GetName())
   109  			err := c.Delete(ctx, remoteObj)
   110  			if err != nil {
   111  				if apierrors.IsNotFound(err) {
   112  					return nil
   113  				}
   114  				return err
   115  			}
   116  		}
   117  		return nil
   118  	})
   119  	if err != nil {
   120  		r.Log.Error(err, "unable to handle chaos")
   121  		// TODO: handle the error
   122  		// TODO: retry
   123  	}
   124  
   125  	return ctrl.Result{}, nil
   126  }
   127