Skip to content

Commit a78dcf8

Browse files
authored
fix: broken check for context in notificationController (#1626) (#1657)
* fix: broken check for context in notificationController (#1626) * fix: broken check for context in notificationController Signed-off-by: Anand Kumar Singh <[email protected]> * add unit tests Signed-off-by: Anand Kumar Singh <[email protected]> * update test Signed-off-by: Anand Kumar Singh <[email protected]> --------- Signed-off-by: Anand Kumar Singh <[email protected]> (cherry picked from commit eace21c) * update logic to simplify the code Signed-off-by: Anand Kumar Singh <[email protected]> --------- Signed-off-by: Anand Kumar Singh <[email protected]>
1 parent 66246bc commit a78dcf8

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

controllers/notificationsconfiguration/configmap.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"reflect"
7+
"strings"
78

89
corev1 "k8s.io/api/core/v1"
910
"k8s.io/apimachinery/pkg/api/errors"
@@ -65,7 +66,16 @@ func (r *NotificationsConfigurationReconciler) reconcileNotificationsConfigmap(c
6566
expectedConfiguration["context"] = mapToString(cr.Spec.Context)
6667
}
6768

68-
if !reflect.DeepEqual(expectedConfiguration, NotificationsConfigMap.Data) {
69+
// check context separately as converting context map to string produce different string due to random serialization of map value
70+
changed := checkIfContextChanged(cr, NotificationsConfigMap)
71+
72+
for k, _ := range expectedConfiguration {
73+
if !reflect.DeepEqual(expectedConfiguration[k], NotificationsConfigMap.Data[k]) && k != "context" {
74+
changed = true
75+
}
76+
}
77+
78+
if changed {
6979
NotificationsConfigMap.Data = expectedConfiguration
7080
err := r.Client.Update(context.TODO(), NotificationsConfigMap)
7181
if err != nil {
@@ -76,10 +86,34 @@ func (r *NotificationsConfigurationReconciler) reconcileNotificationsConfigmap(c
7686
// Do nothing
7787
return nil
7888
}
89+
7990
func mapToString(m map[string]string) string {
8091
result := ""
8192
for key, value := range m {
8293
result += fmt.Sprintf("%s: %s\n", key, value)
8394
}
8495
return result
8596
}
97+
98+
// checkIfContextChanged checks if context value in NotificationConfiguration and notificationConfigMap context have same value
99+
// return true if there is difference, and false if no changes observed
100+
func checkIfContextChanged(cr *v1alpha1.NotificationsConfiguration, notificationConfigMap *corev1.ConfigMap) bool {
101+
cmContext := strings.Split(strings.TrimSuffix(notificationConfigMap.Data["context"], "\n"), "\n")
102+
if len(cmContext) != len(cr.Spec.Context) {
103+
return true
104+
} else {
105+
// Create a map for quick lookups
106+
stringMap := make(map[string]bool)
107+
for _, item := range cmContext {
108+
stringMap[item] = true
109+
}
110+
111+
// Check for each item in array1
112+
for key, value := range cr.Spec.Context {
113+
if !stringMap[fmt.Sprintf("%s: %s", key, value)] {
114+
return true
115+
}
116+
}
117+
}
118+
return false
119+
}

controllers/notificationsconfiguration/configmap_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,38 @@ func TestReconcileNotifications_DeleteConfigMap(t *testing.T) {
226226
assert.Equal(t, testCM.Data["trigger.on-sync-status-test"],
227227
"- when: app.status.sync.status == 'Unknown' \n send: [my-custom-template]")
228228
}
229+
230+
func Test_checkIfContextEquals(t *testing.T) {
231+
a := makeTestNotificationsConfiguration(func(a *v1alpha1.NotificationsConfiguration) {})
232+
a.Spec = v1alpha1.NotificationsConfigurationSpec{
233+
Context: map[string]string{"key1": "value1",
234+
"key2": "value2",
235+
"key4": "value4",
236+
"key3": "value3",
237+
"key6": "value6"},
238+
}
239+
240+
var testmap = []struct {
241+
testcase string
242+
cm corev1.ConfigMap
243+
result bool
244+
}{
245+
{"equal context",
246+
corev1.ConfigMap{Data: map[string]string{"context": "key4: value4\nkey2: value2\nkey6: value6\nkey1: value1\nkey3: value3\n"}},
247+
false,
248+
},
249+
{"context is not equal",
250+
corev1.ConfigMap{Data: map[string]string{"context": "key1: value1\nkey4: value4\nkey9: value9\nkey6: value6\n"}},
251+
true,
252+
},
253+
{"context of same length but not equal",
254+
corev1.ConfigMap{Data: map[string]string{"context": "key2: value2\nkey1: value1\nkey4: value4\nkey9: value9\nkey6: value6\n"}},
255+
true,
256+
},
257+
}
258+
for _, tt := range testmap {
259+
t.Run(tt.testcase, func(t *testing.T) {
260+
assert.Equal(t, tt.result, checkIfContextChanged(a, &tt.cm))
261+
})
262+
}
263+
}

0 commit comments

Comments
 (0)