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 e796502
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions controllers/notificationsconfiguration/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package notificationsconfiguration
import (
"context"
"fmt"
"reflect"

"github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"reflect"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/argoproj-labs/argocd-operator/api/v1alpha1"
"github.com/argoproj-labs/argocd-operator/controllers/argoutil"
"strings"
)

const (
Expand Down Expand Up @@ -65,7 +64,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 +84,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 e796502

Please sign in to comment.