Skip to content

Commit d7bf88e

Browse files
authored
Check if Object is updated except certain label and annotations (#1104)
* Check if Object is updated except certain label and annotations Signed-off-by: Daniel Fan <[email protected]> * update test case to include more checks Signed-off-by: Daniel Fan <[email protected]> * correct log info Signed-off-by: Daniel Fan <[email protected]> * use Reader instead of Client to get ConfigMap/Secret that are referred Signed-off-by: Daniel Fan <[email protected]> --------- Signed-off-by: Daniel Fan <[email protected]>
1 parent a61b000 commit d7bf88e

File tree

5 files changed

+81
-19
lines changed

5 files changed

+81
-19
lines changed

controllers/operandrequest/operandrequest_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,10 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
376376
return false
377377
},
378378
UpdateFunc: func(e event.UpdateEvent) bool {
379+
// If the object is not updated except the ODLMWatchedLabel label or ODLMReferenceAnnotation annotation, return false
380+
if !r.ObjectIsUpdatedWithException(&e.ObjectOld, &e.ObjectNew) {
381+
return false
382+
}
379383
labels := e.ObjectNew.GetLabels()
380384
if labels != nil {
381385
if labelValue, ok := labels[constant.ODLMWatchedLabel]; ok && labelValue == "true" {

controllers/operandrequest/operandrequest_controller_test.go

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,13 @@ var _ = Describe("OperandRequest controller", func() {
325325
return err
326326
}, testutil.Timeout, testutil.Interval).Should(Succeed())
327327

328+
By("Checking the status of first OperandRequest")
329+
Eventually(func() operatorv1alpha1.ClusterPhase {
330+
requestInstance1 := &operatorv1alpha1.OperandRequest{}
331+
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
332+
return requestInstance1.Status.Phase
333+
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))
334+
328335
By("Disabling the jaeger operator from first OperandRequest")
329336
requestInstance1 := &operatorv1alpha1.OperandRequest{}
330337
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
@@ -338,6 +345,13 @@ var _ = Describe("OperandRequest controller", func() {
338345
return err
339346
}, testutil.Timeout, testutil.Interval).Should(Succeed())
340347

348+
By("Checking the status of first OperandRequest after updating the Operand")
349+
Eventually(func() operatorv1alpha1.ClusterPhase {
350+
requestInstance1 := &operatorv1alpha1.OperandRequest{}
351+
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
352+
return requestInstance1.Status.Phase
353+
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))
354+
341355
By("Disabling the jaeger operator from second OperandRequest")
342356
requestInstance2 := &operatorv1alpha1.OperandRequest{}
343357
Expect(k8sClient.Get(ctx, requestKey2, requestInstance2)).Should(Succeed())
@@ -351,20 +365,7 @@ var _ = Describe("OperandRequest controller", func() {
351365
return err != nil && errors.IsNotFound(err)
352366
}, testutil.Timeout, testutil.Interval).Should(BeTrue())
353367

354-
By("Deleting the first OperandRequest")
355-
Expect(k8sClient.Delete(ctx, request1)).Should(Succeed())
356-
357-
By("Checking mongodb operator has not been deleted")
358-
Eventually(func() error {
359-
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
360-
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)
361-
return err
362-
}, testutil.Timeout, testutil.Interval).Should(Succeed())
363-
364-
By("Deleting the second OperandRequest")
365-
Expect(k8sClient.Delete(ctx, request2)).Should(Succeed())
366-
367-
By("Checking the k8s resource has been deleted")
368+
By("Checking jaeger k8s resource has been deleted")
368369
Eventually(func() bool {
369370
jaegerConfigMap := &corev1.ConfigMap{}
370371
err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: "jaeger-configmap", Namespace: registryNamespaceName}, jaegerConfigMap)
@@ -376,13 +377,34 @@ var _ = Describe("OperandRequest controller", func() {
376377
return err != nil && errors.IsNotFound(err)
377378
}, testutil.Timeout, testutil.Interval).Should(BeTrue())
378379

379-
By("Checking operators have been deleted")
380+
By("Checking jaeger operators have been deleted")
380381
Eventually(func() bool {
381382
jaegerCSV := &olmv1alpha1.ClusterServiceVersion{}
382383
err := k8sClient.Get(ctx, types.NamespacedName{Name: "jaeger-csv.v0.0.1", Namespace: operatorNamespaceName}, jaegerCSV)
383384
return err != nil && errors.IsNotFound(err)
384385
}, testutil.Timeout, testutil.Interval).Should(BeTrue())
385386

387+
By("Checking the status of second OperandRequest after updating the Operand")
388+
Eventually(func() operatorv1alpha1.ClusterPhase {
389+
requestInstance2 := &operatorv1alpha1.OperandRequest{}
390+
Expect(k8sClient.Get(ctx, requestKey2, requestInstance2)).Should(Succeed())
391+
return requestInstance2.Status.Phase
392+
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))
393+
394+
By("Deleting the first OperandRequest")
395+
Expect(k8sClient.Delete(ctx, request1)).Should(Succeed())
396+
397+
By("Checking mongodb operator has not been deleted")
398+
Eventually(func() error {
399+
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
400+
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)
401+
return err
402+
}, testutil.Timeout, testutil.Interval).Should(Succeed())
403+
404+
By("Deleting the second OperandRequest")
405+
Expect(k8sClient.Delete(ctx, request2)).Should(Succeed())
406+
407+
By("Checking the mongodb-atlas operator has been deleted")
386408
Eventually(func() bool {
387409
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
388410
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)

controllers/operandrequest/reconcile_operand.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,7 @@ func (r *Reconciler) reconcileK8sResource(ctx context.Context, res operatorv1alp
576576
return err
577577
}
578578
} else {
579-
klog.V(2).Infof("Skip the k8s resource %s/%s which is not created by ODLM", res.Kind, res.Name)
579+
klog.V(2).Infof("Skip the k8s resource %s %s/%s whose force field is false", res.Kind, k8sResNs, res.Name)
580580
}
581581
}
582582
} else {

controllers/operator/manager.go

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"reflect"
2324
"sort"
2425
"strings"
2526

@@ -859,7 +860,7 @@ func (m *ODLMOperator) ParseObjectRef(ctx context.Context, obj *util.ObjectRef,
859860

860861
func (m *ODLMOperator) GetValueRefFromConfigMap(ctx context.Context, instanceType, instanceName, instanceNs, cmName, cmNs, configMapKey string) (string, error) {
861862
cm := &corev1.ConfigMap{}
862-
if err := m.Client.Get(ctx, types.NamespacedName{Name: cmName, Namespace: cmNs}, cm); err != nil {
863+
if err := m.Reader.Get(ctx, types.NamespacedName{Name: cmName, Namespace: cmNs}, cm); err != nil {
863864
if apierrors.IsNotFound(err) {
864865
klog.V(2).Infof("Configmap %s/%s is not found", cmNs, cmName)
865866
return "", nil
@@ -891,7 +892,7 @@ func (m *ODLMOperator) GetValueRefFromConfigMap(ctx context.Context, instanceTyp
891892

892893
func (m *ODLMOperator) GetValueRefFromSecret(ctx context.Context, instanceType, instanceName, instanceNs, secretName, secretNs, secretKey string) (string, error) {
893894
secret := &corev1.Secret{}
894-
if err := m.Client.Get(ctx, types.NamespacedName{Name: secretName, Namespace: secretNs}, secret); err != nil {
895+
if err := m.Reader.Get(ctx, types.NamespacedName{Name: secretName, Namespace: secretNs}, secret); err != nil {
895896
if apierrors.IsNotFound(err) {
896897
klog.V(3).Infof("Secret %s/%s is not found", secretNs, secretName)
897898
return "", nil
@@ -959,3 +960,38 @@ func (m *ODLMOperator) GetValueRefFromObject(ctx context.Context, instanceType,
959960
klog.V(2).Infof("Get value %s from %s %s/%s", sanitizedString, objKind, obj.GetNamespace(), obj.GetName())
960961
return sanitizedString, nil
961962
}
963+
964+
// ObjectIsUpdatedWithException checks if the object is updated except for the ODLMWatchedLabel and ODLMReferenceAnnotation
965+
func (m *ODLMOperator) ObjectIsUpdatedWithException(oldObj, newObj *client.Object) bool {
966+
oldObject := *oldObj
967+
newObject := *newObj
968+
969+
// Check if labels are the same except for ODLMWatchedLabel
970+
oldLabels := oldObject.GetLabels()
971+
newLabels := newObject.GetLabels()
972+
if oldLabels != nil && newLabels != nil {
973+
delete(oldLabels, constant.ODLMWatchedLabel)
974+
delete(newLabels, constant.ODLMWatchedLabel)
975+
}
976+
if !reflect.DeepEqual(oldLabels, newLabels) {
977+
return true
978+
}
979+
980+
// Check if annotations are the same except for ODLMReferenceAnnotation
981+
oldAnnotations := oldObject.GetAnnotations()
982+
newAnnotations := newObject.GetAnnotations()
983+
if oldAnnotations != nil && newAnnotations != nil {
984+
delete(oldAnnotations, constant.ODLMReferenceAnnotation)
985+
delete(newAnnotations, constant.ODLMReferenceAnnotation)
986+
}
987+
if !reflect.DeepEqual(oldAnnotations, newAnnotations) {
988+
return true
989+
}
990+
991+
// Check if other parts of the object are unchanged
992+
oldObject.SetLabels(nil)
993+
oldObject.SetAnnotations(nil)
994+
newObject.SetLabels(nil)
995+
newObject.SetAnnotations(nil)
996+
return !reflect.DeepEqual(oldObject, newObject)
997+
}

controllers/testutil/test_util.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ func OperandConfigObj(name, namespace string) *apiv1alpha1.OperandConfig {
200200
}
201201
}`),
202202
},
203-
Force: false,
203+
Force: true,
204204
},
205205
},
206206
},

0 commit comments

Comments
 (0)