Skip to content

Commit 3dcd7a6

Browse files
author
Thomas Schuetz
committed
feat: made some methods private, added utils
Signed-off-by: Thomas Schuetz <[email protected]>
1 parent 3d5acc9 commit 3dcd7a6

File tree

3 files changed

+55
-68
lines changed

3 files changed

+55
-68
lines changed

controllers/featureflagconfiguration_controller.go

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ package controllers
1919
import (
2020
"context"
2121
"github.com/go-logr/logr"
22+
"github.com/open-feature/open-feature-operator/pkg/utils"
2223
corev1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/errors"
24-
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2525
"k8s.io/client-go/tools/record"
2626
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
2727
"time"
@@ -84,15 +84,15 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
8484
// The object is not being deleted, so if it does not have our finalizer,
8585
// then lets add the finalizer and update the object. This is equivalent
8686
// registering our finalizer.
87-
if !ContainsString(ffconf.GetFinalizers(), finalizerName) {
87+
if !utils.ContainsString(ffconf.GetFinalizers(), finalizerName) {
8888
controllerutil.AddFinalizer(ffconf, finalizerName)
8989
if err := r.Update(ctx, ffconf); err != nil {
9090
return r.finishReconcile(err, false)
9191
}
9292
}
9393
} else {
9494
// The object is being deleted
95-
if ContainsString(ffconf.GetFinalizers(), finalizerName) {
95+
if utils.ContainsString(ffconf.GetFinalizers(), finalizerName) {
9696
controllerutil.RemoveFinalizer(ffconf, finalizerName)
9797
if err := r.Update(ctx, ffconf); err != nil {
9898
return ctrl.Result{}, err
@@ -119,9 +119,9 @@ func (r *FeatureFlagConfigurationReconciler) Reconcile(ctx context.Context, req
119119

120120
for _, cm := range ffConfigMapList {
121121
// Append OwnerReference if not set
122-
if !r.CheckOwnerReference(ffconf, cm) {
122+
if !r.featureFlagResourceIsOwner(ffconf, cm) {
123123
r.Log.Info("Setting owner reference for " + cm.Name)
124-
cm.OwnerReferences = append(cm.OwnerReferences, r.GetFfReference(ffconf))
124+
cm.OwnerReferences = append(cm.OwnerReferences, utils.GetFfReference(ffconf))
125125
err := r.Client.Update(ctx, &cm)
126126
if err != nil {
127127
return r.finishReconcile(err, true)
@@ -154,15 +154,6 @@ func (r *FeatureFlagConfigurationReconciler) SetupWithManager(mgr ctrl.Manager)
154154
Complete(r)
155155
}
156156

157-
func ContainsString(slice []string, s string) bool {
158-
for _, item := range slice {
159-
if item == s {
160-
return true
161-
}
162-
}
163-
return false
164-
}
165-
166157
func (r *FeatureFlagConfigurationReconciler) finishReconcile(err error, requeueImmediate bool) (ctrl.Result, error) {
167158
if err != nil {
168159
interval := reconcileErrorInterval
@@ -180,31 +171,11 @@ func (r *FeatureFlagConfigurationReconciler) finishReconcile(err error, requeueI
180171
return ctrl.Result{Requeue: true, RequeueAfter: interval}, nil
181172
}
182173

183-
func (r *FeatureFlagConfigurationReconciler) GetFfReference(ff *configv1alpha1.FeatureFlagConfiguration) v1.OwnerReference {
184-
return v1.OwnerReference{
185-
APIVersion: ff.APIVersion,
186-
Kind: ff.Kind,
187-
Name: ff.Name,
188-
UID: ff.UID,
189-
Controller: r.trueVal(),
190-
}
191-
}
192-
193-
func (r *FeatureFlagConfigurationReconciler) CheckOwnerReference(ff *configv1alpha1.FeatureFlagConfiguration, cm corev1.ConfigMap) bool {
174+
func (r *FeatureFlagConfigurationReconciler) featureFlagResourceIsOwner(ff *configv1alpha1.FeatureFlagConfiguration, cm corev1.ConfigMap) bool {
194175
for _, cmOwner := range cm.OwnerReferences {
195-
if cmOwner.UID == r.GetFfReference(ff).UID {
176+
if cmOwner.UID == utils.GetFfReference(ff).UID {
196177
return true
197178
}
198179
}
199180
return false
200181
}
201-
202-
func (r *FeatureFlagConfigurationReconciler) falseVal() *bool {
203-
b := false
204-
return &b
205-
}
206-
207-
func (r *FeatureFlagConfigurationReconciler) trueVal() *bool {
208-
b := true
209-
return &b
210-
}

pkg/utils/utils.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package utils
2+
3+
import (
4+
configv1alpha1 "github.com/open-feature/open-feature-operator/apis/core/v1alpha1"
5+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
6+
)
7+
8+
func TrueVal() *bool {
9+
b := true
10+
return &b
11+
}
12+
13+
func FalseVal() *bool {
14+
b := false
15+
return &b
16+
}
17+
18+
func ContainsString(slice []string, s string) bool {
19+
for _, item := range slice {
20+
if item == s {
21+
return true
22+
}
23+
}
24+
return false
25+
}
26+
27+
func GetFfReference(ff *configv1alpha1.FeatureFlagConfiguration) metav1.OwnerReference {
28+
return metav1.OwnerReference{
29+
APIVersion: ff.APIVersion,
30+
Kind: ff.Kind,
31+
Name: ff.Name,
32+
UID: ff.UID,
33+
Controller: TrueVal(),
34+
}
35+
}

webhooks/mutating_admission_webhook.go

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
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.
3031
func (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,7 +92,7 @@ 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 {
9798
if cmOwner.UID == podOwner.UID {
@@ -102,15 +103,15 @@ func CheckOwnerReference(pod *corev1.Pod, cm corev1.ConfigMap) bool {
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))
107108
references := []metav1.OwnerReference{
108109
pod.OwnerReferences[0],
109110
}
110-
references[0].Controller = m.falseVal()
111-
ff := m.GetFeatureFlag(ctx, name, namespace)
111+
references[0].Controller = utils.FalseVal()
112+
ff := m.getFeatureFlag(ctx, name, namespace)
112113
if ff.Name != "" {
113-
references = append(references, m.GetFfReference(&ff))
114+
references = append(references, utils.GetFfReference(&ff))
114115
}
115116

116117
cm := corev1.ConfigMap{
@@ -129,15 +130,15 @@ func (m *PodMutator) CreateConfigMap(ctx context.Context, name string, namespace
129130
return m.Client.Create(ctx, &cm)
130131
}
131132

132-
func (m *PodMutator) GetFeatureFlag(ctx context.Context, name string, namespace string) configv1alpha1.FeatureFlagConfiguration {
133+
func (m *PodMutator) getFeatureFlag(ctx context.Context, name string, namespace string) configv1alpha1.FeatureFlagConfiguration {
133134
ffConfig := configv1alpha1.FeatureFlagConfiguration{}
134135
if err := m.Client.Get(ctx, client.ObjectKey{Name: name, Namespace: namespace}, &ffConfig); errors.IsNotFound(err) {
135136
return configv1alpha1.FeatureFlagConfiguration{}
136137
}
137138
return ffConfig
138139
}
139140

140-
func (m *PodMutator) InjectSidecar(pod *corev1.Pod, configMap string) ([]byte, error) {
141+
func (m *PodMutator) injectSidecar(pod *corev1.Pod, configMap string) ([]byte, error) {
141142
m.Log.V(1).Info(fmt.Sprintf("Creating sidecar for pod %s/%s", pod.Namespace, pod.Name))
142143
// Inject the agent
143144
pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{
@@ -165,23 +166,3 @@ func (m *PodMutator) InjectSidecar(pod *corev1.Pod, configMap string) ([]byte, e
165166
})
166167
return json.Marshal(pod)
167168
}
168-
169-
func (m *PodMutator) GetFfReference(ff *configv1alpha1.FeatureFlagConfiguration) metav1.OwnerReference {
170-
return metav1.OwnerReference{
171-
APIVersion: ff.APIVersion,
172-
Kind: ff.Kind,
173-
Name: ff.Name,
174-
UID: ff.UID,
175-
Controller: m.trueVal(),
176-
}
177-
}
178-
179-
func (m *PodMutator) trueVal() *bool {
180-
b := true
181-
return &b
182-
}
183-
184-
func (m *PodMutator) falseVal() *bool {
185-
b := false
186-
return &b
187-
}

0 commit comments

Comments
 (0)