Skip to content

Commit 9cbfa7f

Browse files
authored
Merge pull request #10 from open-feature/fix/single-configmap-deployment
Fix/single configmap deployment
2 parents 8a848d5 + f8d0d9f commit 9cbfa7f

File tree

6 files changed

+21
-41
lines changed

6 files changed

+21
-41
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,7 @@ root@nginx:/# curl localhost:8080
6666
3. `kubectl apply -f config/webhook/certificate.yaml`
6767
4. `IMG=ghcr.io/open-feature/open-feature-operator:main make deploy`
6868

69+
#### Run the example
70+
71+
1. `kubectl apply -f config/samples/featureflagconfiguration-sample.yaml`
72+
2. `kubectl apply -f config/samples/pod.yaml`

config/manager/kustomization.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ apiVersion: kustomize.config.k8s.io/v1beta1
1212
kind: Kustomization
1313
images:
1414
- name: controller
15-
newName: ghcr.io/open-feature/open-feature-operator
16-
newTag: main
15+
newName: tibbar/of-operator
16+
newTag: v0.0.2.1

config/samples/deployment.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ apiVersion: apps/v1
22
kind: Deployment
33
metadata:
44
name: nginx-deployment
5-
annotations:
6-
openfeature.dev: "enabled"
7-
openfeature.dev/featureflagconfiguration: "featureflagconfiguration-sample"
85
spec:
96
selector:
107
matchLabels:
118
app: nginx
12-
replicas: 2 # tells deployment to run 2 pods matching the template
9+
replicas: 5 # tells deployment to run 2 pods matching the template
1310
template:
1411
metadata:
1512
labels:
1613
app: nginx
14+
annotations:
15+
openfeature.dev: "enabled"
16+
openfeature.dev/featureflagconfiguration: "featureflagconfiguration-sample"
1717
spec:
1818
containers:
1919
- name: nginx

config/webhook/manifests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ webhooks:
2424
- UPDATE
2525
resources:
2626
- pods
27-
- deployments
2827
sideEffects: NoneOnDryRun

examples/deployment.yaml

Lines changed: 0 additions & 22 deletions
This file was deleted.

webhooks/mutating_admission_webhook.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// NOTE: RBAC not needed here.
1818
//+kubebuilder:rbac:groups="",resources=pods,verbs=get;list;watch;create;update;patch;delete
1919
//+kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
20-
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=Ignore,groups="",resources=pods;deployments,verbs=create;update,versions=v1,name=mpod.kb.io,admissionReviewVersions=v1,sideEffects=NoneOnDryRun
20+
// +kubebuilder:webhook:path=/mutate-v1-pod,mutating=true,failurePolicy=Ignore,groups="",resources=pods,verbs=create;update,versions=v1,name=mpod.kb.io,admissionReviewVersions=v1,sideEffects=NoneOnDryRun
2121

2222
// PodMutator annotates Pods
2323
type PodMutator struct {
@@ -30,17 +30,13 @@ type PodMutator struct {
3030
func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admission.Response {
3131

3232
pod := &corev1.Pod{}
33-
m.Log.V(2).Info("Handling pod %s/%s", req.Namespace, req.Name)
3433
err := m.decoder.Decode(req, pod)
3534
if err != nil {
3635
return admission.Errored(http.StatusBadRequest, err)
3736
}
38-
3937
// Check enablement
4038
val, ok := pod.GetAnnotations()["openfeature.dev"]
41-
if !ok {
42-
return admission.Allowed("no annotation")
43-
} else {
39+
if ok {
4440
if val != "enabled" {
4541
m.Log.V(2).Info("openfeature.dev Annotation is not enabled")
4642
return admission.Allowed("openfeature is disabled")
@@ -50,7 +46,7 @@ func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admissio
5046
// Check CustomResource
5147
val, ok = pod.GetAnnotations()["openfeature.dev/featureflagconfiguration"]
5248
if !ok {
53-
return admission.Denied("FeatureFlagConfiguration not found")
49+
return admission.Allowed("FeatureFlagConfiguration not found")
5450
} else {
5551
// Current limitation is to use the same namespace, this is easy to fix though
5652
// e.g. namespace/name check
@@ -60,30 +56,33 @@ func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admissio
6056
return admission.Denied("FeatureFlagConfiguration not found")
6157
}
6258
}
59+
name := pod.Name
60+
if len(pod.GetOwnerReferences()) != 0 {
61+
name = pod.GetOwnerReferences()[0].Name
62+
}
6363

6464
// TODO: this should be a short sha to avoid collisions
65-
configName := fmt.Sprintf("%s-%s-config", pod.Name, pod.Namespace)
65+
configName := name
6666
// Create the agent configmap
6767
m.Client.Delete(context.TODO(), &corev1.ConfigMap{
6868
ObjectMeta: metav1.ObjectMeta{
6969
Name: configName,
7070
Namespace: req.Namespace,
7171
},
7272
}) // Delete the configmap if it exists
73-
m.Log.V(1).Info(fmt.Sprintf("Creating configmap %s/%s", pod.Namespace, configName))
73+
m.Log.V(1).Info(fmt.Sprintf("Creating configmap %s", configName))
7474
if err := m.Client.Create(ctx, &corev1.ConfigMap{
7575
ObjectMeta: metav1.ObjectMeta{
7676
Name: configName,
77-
Namespace: pod.Namespace,
77+
Namespace: req.Namespace,
7878
},
7979
//TODO
8080
Data: map[string]string{
8181
"config.yaml": featureFlagCustomResource.Spec.FeatureFlagSpec,
8282
},
8383
}); err != nil {
8484

85-
m.Log.V(1).Info(fmt.Sprintf("failed to create config map %s", configName))
86-
85+
m.Log.V(1).Info(fmt.Sprintf("failed to create config map %s error: %s", configName, err.Error()))
8786
return admission.Errored(http.StatusInternalServerError, err)
8887
}
8988

0 commit comments

Comments
 (0)