-
Notifications
You must be signed in to change notification settings - Fork 609
/
configmap.go
84 lines (69 loc) · 2.22 KB
/
configmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
const (
ArgoCDNotificationsConfigMap = "argocd-notifications-cm"
)
// reconcileNotificationsConfigmap will ensure that the notifications configuration is updated
func (r *NotificationsConfigurationReconciler) reconcileNotificationsConfigmap(cr *v1alpha1.NotificationsConfiguration) error {
NotificationsConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: ArgoCDNotificationsConfigMap,
Namespace: cr.Namespace,
},
}
if err := argoutil.FetchObject(r.Client, cr.Namespace, NotificationsConfigMap.Name, NotificationsConfigMap); err != nil {
if !errors.IsNotFound(err) {
return fmt.Errorf("failed to get the configmap %s : %s", NotificationsConfigMap.Name, err)
}
if err := controllerutil.SetControllerReference(cr, NotificationsConfigMap, r.Scheme); err != nil {
return err
}
err := r.Client.Create(context.TODO(), NotificationsConfigMap)
if err != nil {
return err
}
}
// Verify if Notifications Configmap data is up to date with NotificationsConfiguration CR data
expectedConfiguration := make(map[string]string)
for k, v := range cr.Spec.Triggers {
expectedConfiguration[k] = v
}
for k, v := range cr.Spec.Templates {
expectedConfiguration[k] = v
}
for k, v := range cr.Spec.Services {
expectedConfiguration[k] = v
}
for k, v := range cr.Spec.Subscriptions {
expectedConfiguration[k] = v
}
if cr.Spec.Context != nil {
expectedConfiguration["context"] = mapToString(cr.Spec.Context)
}
if !reflect.DeepEqual(expectedConfiguration, NotificationsConfigMap.Data) {
NotificationsConfigMap.Data = expectedConfiguration
err := r.Client.Update(context.TODO(), NotificationsConfigMap)
if err != nil {
return err
}
}
// 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
}