...
1
2
3
4
5
6
7
8
9
10
11
12
13
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
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
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
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
85 localSpecValue := reflect.Indirect(reflect.ValueOf(localObj)).FieldByName("Spec")
86 localSpecValue.FieldByName("RemoteCluster").Set(reflect.ValueOf(""))
87
88
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
102 r.Log.Error(err, "unable to get chaos")
103 return nil
104 }
105
106
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
122
123 }
124
125 return ctrl.Result{}, nil
126 }
127