66 "fmt"
77 "github.com/go-logr/logr"
88 configv1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
9+ "github.com/open-feature/open-feature-operator/pkg/utils"
910 corev1 "k8s.io/api/core/v1"
1011 "k8s.io/apimachinery/pkg/api/errors"
1112 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -28,7 +29,6 @@ type PodMutator struct {
2829
2930// PodMutator adds an annotation to every incoming pods.
3031func (m * PodMutator ) Handle (ctx context.Context , req admission.Request ) admission.Response {
31-
3232 pod := & corev1.Pod {}
3333 err := m .decoder .Decode (req , pod )
3434 if err != nil {
@@ -57,24 +57,25 @@ func (m *PodMutator) Handle(ctx context.Context, req admission.Request) admissio
5757 // Check for ConfigMap and create it if it doesn't exist
5858 cm := corev1.ConfigMap {}
5959 if err := m .Client .Get (ctx , client.ObjectKey {Name : val , Namespace : req .Namespace }, & cm ); errors .IsNotFound (err ) {
60- err := m .CreateConfigMap (ctx , val , req .Namespace , pod )
60+ err := m .createConfigMap (ctx , val , req .Namespace , pod )
6161 if err != nil {
6262 m .Log .V (1 ).Info (fmt .Sprintf ("failed to create config map %s error: %s" , val , err .Error ()))
6363 return admission .Errored (http .StatusInternalServerError , err )
6464 }
6565 }
6666
67- if ! CheckOwnerReference (pod , cm ) {
67+ // Add owner reference of the pod's owner
68+ if ! podOwnerIsOwner (pod , cm ) {
6869 reference := pod .OwnerReferences [0 ]
69- reference .Controller = m . falseVal ()
70+ reference .Controller = utils . FalseVal ()
7071 cm .OwnerReferences = append (cm .OwnerReferences , reference )
7172 err := m .Client .Update (ctx , & cm )
7273 if err != nil {
7374 m .Log .V (1 ).Info (fmt .Sprintf ("failed to update owner reference for %s error: %s" , val , err .Error ()))
7475 }
7576 }
7677
77- marshaledPod , err := m .InjectSidecar (pod , val )
78+ marshaledPod , err := m .injectSidecar (pod , val )
7879 if err != nil {
7980 return admission .Errored (http .StatusInternalServerError , err )
8081 }
@@ -91,50 +92,53 @@ func (m *PodMutator) InjectDecoder(d *admission.Decoder) error {
9192 return nil
9293}
9394
94- func CheckOwnerReference (pod * corev1.Pod , cm corev1.ConfigMap ) bool {
95+ func podOwnerIsOwner (pod * corev1.Pod , cm corev1.ConfigMap ) bool {
9596 for _ , cmOwner := range cm .OwnerReferences {
9697 for _ , podOwner := range pod .OwnerReferences {
97- if cmOwner == podOwner {
98+ if cmOwner . UID == podOwner . UID {
9899 return true
99100 }
100101 }
101102 }
102103 return false
103104}
104105
105- func (m * PodMutator ) CreateConfigMap (ctx context.Context , name string , namespace string , pod * corev1.Pod ) error {
106+ func (m * PodMutator ) createConfigMap (ctx context.Context , name string , namespace string , pod * corev1.Pod ) error {
106107 m .Log .V (1 ).Info (fmt .Sprintf ("Creating configmap %s" , name ))
107- reference := pod .OwnerReferences [0 ]
108- reference .Controller = m .falseVal ()
108+ references := []metav1.OwnerReference {
109+ pod .OwnerReferences [0 ],
110+ }
111+ references [0 ].Controller = utils .FalseVal ()
112+ ff := m .getFeatureFlag (ctx , name , namespace )
113+ if ff .Name != "" {
114+ references = append (references , utils .GetFfReference (& ff ))
115+ }
109116
110- spec := m .GetFeatureFlagSpec (ctx , name , namespace )
111117 cm := corev1.ConfigMap {
112118 ObjectMeta : metav1.ObjectMeta {
113119 Name : name ,
114120 Namespace : namespace ,
115121 Annotations : map [string ]string {
116122 "openfeature.dev/featureflagconfiguration" : name ,
117123 },
118- OwnerReferences : []metav1.OwnerReference {
119- reference ,
120- },
124+ OwnerReferences : references ,
121125 },
122126 Data : map [string ]string {
123- "config.yaml" : spec .FeatureFlagSpec ,
127+ "config.yaml" : ff . Spec .FeatureFlagSpec ,
124128 },
125129 }
126130 return m .Client .Create (ctx , & cm )
127131}
128132
129- func (m * PodMutator ) GetFeatureFlagSpec (ctx context.Context , name string , namespace string ) configv1alpha1.FeatureFlagConfigurationSpec {
133+ func (m * PodMutator ) getFeatureFlag (ctx context.Context , name string , namespace string ) configv1alpha1.FeatureFlagConfiguration {
130134 ffConfig := configv1alpha1.FeatureFlagConfiguration {}
131135 if err := m .Client .Get (ctx , client.ObjectKey {Name : name , Namespace : namespace }, & ffConfig ); errors .IsNotFound (err ) {
132- return configv1alpha1.FeatureFlagConfigurationSpec {}
136+ return configv1alpha1.FeatureFlagConfiguration {}
133137 }
134- return ffConfig . Spec
138+ return ffConfig
135139}
136140
137- func (m * PodMutator ) InjectSidecar (pod * corev1.Pod , configMap string ) ([]byte , error ) {
141+ func (m * PodMutator ) injectSidecar (pod * corev1.Pod , configMap string ) ([]byte , error ) {
138142 m .Log .V (1 ).Info (fmt .Sprintf ("Creating sidecar for pod %s/%s" , pod .Namespace , pod .Name ))
139143 // Inject the agent
140144 pod .Spec .Volumes = append (pod .Spec .Volumes , corev1.Volume {
@@ -162,8 +166,3 @@ func (m *PodMutator) InjectSidecar(pod *corev1.Pod, configMap string) ([]byte, e
162166 })
163167 return json .Marshal (pod )
164168}
165-
166- func (m * PodMutator ) falseVal () * bool {
167- b := false
168- return & b
169- }
0 commit comments