Skip to content

Commit 1dec80d

Browse files
authored
Merge pull request #26 from thschue/main
feat: create configmap if manually deleted
2 parents 3b5f7b4 + eb75f63 commit 1dec80d

File tree

3 files changed

+48
-13
lines changed

3 files changed

+48
-13
lines changed

controllers/featureflagconfiguration_controller.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/open-feature/open-feature-operator/pkg/utils"
2323
corev1 "k8s.io/api/core/v1"
2424
"k8s.io/apimachinery/pkg/api/errors"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2526
"k8s.io/client-go/tools/record"
2627
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2728
"time"
@@ -109,11 +110,13 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
109110
return r.finishReconcile(err, false)
110111
}
111112

113+
cmExists := false
112114
// Get list of configmaps with annotation
113115
for _, cm := range configMapList.Items {
114116
val, ok := cm.GetAnnotations()["openfeature.dev/featureflagconfiguration"]
115117
if ok && val == ffconf.Name {
116118
ffConfigMapList = append(ffConfigMapList, cm)
119+
cmExists = true
117120
}
118121
}
119122

@@ -143,6 +146,32 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
143146
}
144147
}
145148

149+
if !cmExists {
150+
ffOwnerRefs := []metav1.OwnerReference{
151+
utils.GetFfReference(ffconf),
152+
}
153+
cm := utils.GenerateFfConfigMap(ffconf.Name, ffconf.Namespace, ffOwnerRefs, ffconf.Spec)
154+
155+
podList := &corev1.PodList{}
156+
if err := r.List(ctx, podList); err != nil {
157+
return r.finishReconcile(err, false)
158+
}
159+
for _, pod := range podList.Items {
160+
val, ok := pod.GetAnnotations()["openfeature.dev/featureflagconfiguration"]
161+
if ok && val == ffconf.Name {
162+
reference := pod.OwnerReferences[0]
163+
reference.Controller = utils.FalseVal()
164+
cm.OwnerReferences = append(cm.OwnerReferences, reference)
165+
}
166+
}
167+
// Create ConfigMap only if there is a pod which uses it
168+
if len(cm.OwnerReferences) > 1 {
169+
err := r.Client.Create(ctx, &cm)
170+
return r.finishReconcile(err, true)
171+
}
172+
173+
}
174+
146175
return r.finishReconcile(nil, false)
147176
}
148177

pkg/utils/utils.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
configv1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
5+
corev1 "k8s.io/api/core/v1"
56
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
67
)
78

@@ -33,3 +34,19 @@ func GetFfReference(ff *configv1alpha1.FeatureFlagConfiguration) metav1.OwnerRef
3334
Controller: TrueVal(),
3435
}
3536
}
37+
38+
func GenerateFfConfigMap(name string, namespace string, references []metav1.OwnerReference, spec configv1alpha1.FeatureFlagConfigurationSpec) corev1.ConfigMap {
39+
return corev1.ConfigMap{
40+
ObjectMeta: metav1.ObjectMeta{
41+
Name: name,
42+
Namespace: namespace,
43+
Annotations: map[string]string{
44+
"openfeature.dev/featureflagconfiguration": name,
45+
},
46+
OwnerReferences: references,
47+
},
48+
Data: map[string]string{
49+
"config.yaml": spec.FeatureFlagSpec,
50+
},
51+
}
52+
}

webhooks/mutating_admission_webhook.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,8 @@ func (m *PodMutator) createConfigMap(ctx context.Context, name string, namespace
114114
references = append(references, utils.GetFfReference(&ff))
115115
}
116116

117-
cm := corev1.ConfigMap{
118-
ObjectMeta: metav1.ObjectMeta{
119-
Name: name,
120-
Namespace: namespace,
121-
Annotations: map[string]string{
122-
"openfeature.dev/featureflagconfiguration": name,
123-
},
124-
OwnerReferences: references,
125-
},
126-
Data: map[string]string{
127-
"config.yaml": ff.Spec.FeatureFlagSpec,
128-
},
129-
}
117+
cm := utils.GenerateFfConfigMap(name, namespace, references, ff.Spec)
118+
130119
return m.Client.Create(ctx, &cm)
131120
}
132121

0 commit comments

Comments
 (0)