Skip to content

Commit f8d0d9f

Browse files
committed
enabling a single configmap per deployment
1 parent 36b01f8 commit f8d0d9f

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
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

webhooks/mutating_admission_webhook.go

Lines changed: 12 additions & 11 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,28 +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+
}
63+
6364
// TODO: this should be a short sha to avoid collisions
64-
configName := fmt.Sprintf("%s-%s-config", pod.Name, pod.Namespace)
65+
configName := name
6566
// Create the agent configmap
6667
m.Client.Delete(context.TODO(), &corev1.ConfigMap{
6768
ObjectMeta: metav1.ObjectMeta{
6869
Name: configName,
6970
Namespace: req.Namespace,
7071
},
7172
}) // Delete the configmap if it exists
72-
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))
7374
if err := m.Client.Create(ctx, &corev1.ConfigMap{
7475
ObjectMeta: metav1.ObjectMeta{
7576
Name: configName,
76-
Namespace: pod.Namespace,
77+
Namespace: req.Namespace,
7778
},
7879
//TODO
7980
Data: map[string]string{
8081
"config.yaml": featureFlagCustomResource.Spec.FeatureFlagSpec,
8182
},
8283
}); err != nil {
8384

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

0 commit comments

Comments
 (0)