Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions controllers/operandrequest/operandrequest_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,10 @@ func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
return false
},
UpdateFunc: func(e event.UpdateEvent) bool {
// If the object is not updated except the ODLMWatchedLabel label or ODLMReferenceAnnotation annotation, return false
if !r.ObjectIsUpdatedWithException(&e.ObjectOld, &e.ObjectNew) {
return false
}
labels := e.ObjectNew.GetLabels()
if labels != nil {
if labelValue, ok := labels[constant.ODLMWatchedLabel]; ok && labelValue == "true" {
Expand Down
52 changes: 37 additions & 15 deletions controllers/operandrequest/operandrequest_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,13 @@ var _ = Describe("OperandRequest controller", func() {
return err
}, testutil.Timeout, testutil.Interval).Should(Succeed())

By("Checking the status of first OperandRequest")
Eventually(func() operatorv1alpha1.ClusterPhase {
requestInstance1 := &operatorv1alpha1.OperandRequest{}
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
return requestInstance1.Status.Phase
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))

By("Disabling the jaeger operator from first OperandRequest")
requestInstance1 := &operatorv1alpha1.OperandRequest{}
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
Expand All @@ -338,6 +345,13 @@ var _ = Describe("OperandRequest controller", func() {
return err
}, testutil.Timeout, testutil.Interval).Should(Succeed())

By("Checking the status of first OperandRequest after updating the Operand")
Eventually(func() operatorv1alpha1.ClusterPhase {
requestInstance1 := &operatorv1alpha1.OperandRequest{}
Expect(k8sClient.Get(ctx, requestKey1, requestInstance1)).Should(Succeed())
return requestInstance1.Status.Phase
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))

By("Disabling the jaeger operator from second OperandRequest")
requestInstance2 := &operatorv1alpha1.OperandRequest{}
Expect(k8sClient.Get(ctx, requestKey2, requestInstance2)).Should(Succeed())
Expand All @@ -351,20 +365,7 @@ var _ = Describe("OperandRequest controller", func() {
return err != nil && errors.IsNotFound(err)
}, testutil.Timeout, testutil.Interval).Should(BeTrue())

By("Deleting the first OperandRequest")
Expect(k8sClient.Delete(ctx, request1)).Should(Succeed())

By("Checking mongodb operator has not been deleted")
Eventually(func() error {
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)
return err
}, testutil.Timeout, testutil.Interval).Should(Succeed())

By("Deleting the second OperandRequest")
Expect(k8sClient.Delete(ctx, request2)).Should(Succeed())

By("Checking the k8s resource has been deleted")
By("Checking jaeger k8s resource has been deleted")
Eventually(func() bool {
jaegerConfigMap := &corev1.ConfigMap{}
err := k8sClient.Get(context.TODO(), types.NamespacedName{Name: "jaeger-configmap", Namespace: registryNamespaceName}, jaegerConfigMap)
Expand All @@ -376,13 +377,34 @@ var _ = Describe("OperandRequest controller", func() {
return err != nil && errors.IsNotFound(err)
}, testutil.Timeout, testutil.Interval).Should(BeTrue())

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

By("Checking the status of second OperandRequest after updating the Operand")
Eventually(func() operatorv1alpha1.ClusterPhase {
requestInstance2 := &operatorv1alpha1.OperandRequest{}
Expect(k8sClient.Get(ctx, requestKey2, requestInstance2)).Should(Succeed())
return requestInstance2.Status.Phase
}, testutil.Timeout, testutil.Interval).Should(Equal(operatorv1alpha1.ClusterPhaseRunning))

By("Deleting the first OperandRequest")
Expect(k8sClient.Delete(ctx, request1)).Should(Succeed())

By("Checking mongodb operator has not been deleted")
Eventually(func() error {
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)
return err
}, testutil.Timeout, testutil.Interval).Should(Succeed())

By("Deleting the second OperandRequest")
Expect(k8sClient.Delete(ctx, request2)).Should(Succeed())

By("Checking the mongodb-atlas operator has been deleted")
Eventually(func() bool {
mongodbCSV := &olmv1alpha1.ClusterServiceVersion{}
err := k8sClient.Get(ctx, types.NamespacedName{Name: "mongodb-atlas-kubernetes-csv.v0.0.1", Namespace: operatorNamespaceName}, mongodbCSV)
Expand Down
2 changes: 1 addition & 1 deletion controllers/operandrequest/reconcile_operand.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ func (r *Reconciler) reconcileK8sResource(ctx context.Context, res operatorv1alp
return err
}
} else {
klog.V(2).Infof("Skip the k8s resource %s/%s which is not created by ODLM", res.Kind, res.Name)
klog.V(2).Infof("Skip the k8s resource %s %s/%s whose force field is false", res.Kind, k8sResNs, res.Name)
}
}
} else {
Expand Down
40 changes: 38 additions & 2 deletions controllers/operator/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"reflect"
"sort"
"strings"

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

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

func (m *ODLMOperator) GetValueRefFromSecret(ctx context.Context, instanceType, instanceName, instanceNs, secretName, secretNs, secretKey string) (string, error) {
secret := &corev1.Secret{}
if err := m.Client.Get(ctx, types.NamespacedName{Name: secretName, Namespace: secretNs}, secret); err != nil {
if err := m.Reader.Get(ctx, types.NamespacedName{Name: secretName, Namespace: secretNs}, secret); err != nil {
if apierrors.IsNotFound(err) {
klog.V(3).Infof("Secret %s/%s is not found", secretNs, secretName)
return "", nil
Expand Down Expand Up @@ -959,3 +960,38 @@ func (m *ODLMOperator) GetValueRefFromObject(ctx context.Context, instanceType,
klog.V(2).Infof("Get value %s from %s %s/%s", sanitizedString, objKind, obj.GetNamespace(), obj.GetName())
return sanitizedString, nil
}

// ObjectIsUpdatedWithException checks if the object is updated except for the ODLMWatchedLabel and ODLMReferenceAnnotation
func (m *ODLMOperator) ObjectIsUpdatedWithException(oldObj, newObj *client.Object) bool {
oldObject := *oldObj
newObject := *newObj

// Check if labels are the same except for ODLMWatchedLabel
oldLabels := oldObject.GetLabels()
newLabels := newObject.GetLabels()
if oldLabels != nil && newLabels != nil {
delete(oldLabels, constant.ODLMWatchedLabel)
delete(newLabels, constant.ODLMWatchedLabel)
}
if !reflect.DeepEqual(oldLabels, newLabels) {
return true
}

// Check if annotations are the same except for ODLMReferenceAnnotation
oldAnnotations := oldObject.GetAnnotations()
newAnnotations := newObject.GetAnnotations()
if oldAnnotations != nil && newAnnotations != nil {
delete(oldAnnotations, constant.ODLMReferenceAnnotation)
delete(newAnnotations, constant.ODLMReferenceAnnotation)
}
if !reflect.DeepEqual(oldAnnotations, newAnnotations) {
return true
}

// Check if other parts of the object are unchanged
oldObject.SetLabels(nil)
oldObject.SetAnnotations(nil)
newObject.SetLabels(nil)
newObject.SetAnnotations(nil)
return !reflect.DeepEqual(oldObject, newObject)
}
2 changes: 1 addition & 1 deletion controllers/testutil/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func OperandConfigObj(name, namespace string) *apiv1alpha1.OperandConfig {
}
}`),
},
Force: false,
Force: true,
},
},
},
Expand Down