Skip to content

Commit

Permalink
fix: broken check for context in notificationController
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Kumar Singh <[email protected]>
  • Loading branch information
anandrkskd committed Dec 18, 2024
1 parent 0ca1920 commit 1574590
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion controllers/notificationsconfiguration/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"strings"

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

if !reflect.DeepEqual(expectedConfiguration, NotificationsConfigMap.Data) {
// check context separately as converting context map to string produce different string due to random serialization of map value
changed := checkIfContextEquals(cr, NotificationsConfigMap)

for k, _ := range expectedConfiguration {
if !reflect.DeepEqual(expectedConfiguration[k], NotificationsConfigMap.Data[k]) && k != "context" {
changed = true
}
}

if changed {
NotificationsConfigMap.Data = expectedConfiguration
err := r.Client.Update(context.TODO(), NotificationsConfigMap)
if err != nil {
Expand All @@ -76,10 +86,34 @@ func (r *NotificationsConfigurationReconciler) reconcileNotificationsConfigmap(c
// Do nothing
return nil
}

func mapToString(m map[string]string) string {
result := ""
for key, value := range m {
result += fmt.Sprintf("%s: %s\n", key, value)
}
return result
}

// checkIfContextEquals checks if context value in NotificationConfiguration and notificationConfigMap context have same value
// return true if there is difference, and false if no changes observed
func checkIfContextEquals(cr *v1alpha1.NotificationsConfiguration, notificationConfigMap *corev1.ConfigMap) bool {
cmContext := strings.Split(strings.TrimSuffix(notificationConfigMap.Data["context"], "\n"), "\n")
if len(cmContext) == len(cr.Spec.Context) {
// Create a map for quick lookups
stringMap := make(map[string]bool)
for _, item := range cmContext {
stringMap[item] = true
}

// Check for each item in array1
for key, value := range cr.Spec.Context {
if !stringMap[fmt.Sprintf("%s: %s", key, value)] {
return true
}
}
} else {
return true
}
return false
}

0 comments on commit 1574590

Please sign in to comment.