@@ -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
2323type PodMutator struct {
@@ -30,17 +30,13 @@ type PodMutator struct {
3030func (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