Skip to content

Commit a30abc9

Browse files
Add labels and annotations to pods.
Currently, there is only one deployment, so avoid premature optimization by making these global (and follow the same pattern for imagePullSecrets) Requires more aggressive usage of the `strip-kustomize-helm.sh` script because we have to preserve existing annotations and labels from the config/manager directory. Also update labels and annotations for injected pods (FlagD sidecar, FlagD standalone, and FlagD proxy). Signed-off-by: Christopher Pitstick <cpitstick@lat.ai>
1 parent 917a680 commit a30abc9

File tree

14 files changed

+164
-65
lines changed

14 files changed

+164
-65
lines changed
+19-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
# This script is a hack to support helm flow control in kustomize overlays, which would otherwise break them.
44
# It allows us to render helm template bindings and add newlines.
55
# For instance, it transforms "___{{ .Value.myValue }}___" to {{ .Value.myValue }}.
6-
# It also adds newlines wherever ___newline___ is found.
7-
8-
CHARTS_DIR='./chart/open-feature-operator/templates';
6+
# It also adds newlines wherever ___newline___ is found, and other operations. See
7+
# sed_expressions below.
98

109
echo 'Running strip-kustomize-helm.sh script'
11-
filenames=`find $CHARTS_DIR -name "*.yaml"`
12-
for file in $filenames; do
13-
sed -i "s/___newline___/\\n/g" $file
14-
sed -i "s/\"___//g" $file
15-
sed -i "s/___\"//g" $file
16-
sed -i "s/___//g" $file
10+
CHARTS_DIR='./chart/open-feature-operator/templates'
11+
# Careful! Ordering of these expressions matter!
12+
sed_expressions=(
13+
"s/___newline___/\\n/g"
14+
"s/___space___/ /g"
15+
"s/\"___//g"
16+
"s/___\"//g"
17+
"/___delete_me___/d"
18+
"s/___//g"
19+
)
20+
find $CHARTS_DIR -name "*.yaml" | while read file; do
21+
for expr in "${sed_expressions[@]}"; do
22+
sed -i "$expr" "$file"
23+
done
1724
done
18-
echo 'Done running strip-kustomize-helm.sh script'
25+
26+
echo 'Done running strip-kustomize-helm.sh script'

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ set-helm-overlay:
252252
helm-package: set-helm-overlay generate release-manifests helm
253253
mkdir -p chart/open-feature-operator/templates/crds
254254
mv chart/open-feature-operator/templates/*customresourcedefinition* chart/open-feature-operator/templates/crds
255-
sh .github/scripts/strip-kustomize-helm.sh
255+
.github/scripts/strip-kustomize-helm.sh
256256
$(HELM) package --version $(CHART_VERSION) chart/open-feature-operator
257257
mkdir -p charts && mv open-feature-operator-*.tgz charts
258258
$(HELM) repo index --url https://open-feature.github.io/open-feature-operator/charts charts

chart/open-feature-operator/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ The command removes all the Kubernetes components associated with the chart and
9797
| ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------ |
9898
| `defaultNamespace` | To override the namespace use the `--namespace` flag. This default is provided to ensure that the kustomize build charts in `/templates` deploy correctly when no `namespace` is provided via the `-n` flag. | `open-feature-operator-system` |
9999
| `imagePullSecrets` | Array of ImagePullSecret objects containing credentials for images pulled by the operator (flagdProxyConfiguration.image, flagdConfiguration.image, controllerManager.manager.image, controllerManager.kubeRbacProxy.image). Example: imagePullSecrets: [{"name": "my-secret"}] | `[]` |
100+
| `labels` | Labels to apply to all of the pods in the operator. | `{}` |
101+
| `annotations` | Annotations to apply to all of the pods in the operator. | `{}` |
100102

101103
### Sidecar configuration
102104

@@ -167,7 +169,7 @@ The command removes all the Kubernetes components associated with the chart and
167169
| `controllerManager.kubeRbacProxy.resources.requests.cpu` | Sets cpu resource requests for kube-rbac-proxy. | `5m` |
168170
| `controllerManager.kubeRbacProxy.resources.requests.memory` | Sets memory resource requests for kube-rbac-proxy. | `64Mi` |
169171
| `controllerManager.manager.image.repository` | Sets the image for the operator. | `ghcr.io/open-feature/open-feature-operator` |
170-
| `controllerManager.manager.image.tag` | Sets the version tag for the operator. | `v0.6.1` |
172+
| `controllerManager.manager.image.tag` | Sets the version tag for the operator. | `v0.7.0` |
171173
| `controllerManager.manager.resources.limits.cpu` | Sets cpu resource limits for operator. | `500m` |
172174
| `controllerManager.manager.resources.limits.memory` | Sets memory resource limits for operator. | `128Mi` |
173175
| `controllerManager.manager.resources.requests.cpu` | Sets cpu resource requests for operator. | `10m` |
@@ -180,3 +182,4 @@ The command removes all the Kubernetes components associated with the chart and
180182
| `managerConfig.controllerManagerConfigYaml.metrics.bindAddress` | Sets the bind address for metrics (combined with bindPort). | `127.0.0.1` |
181183
| `managerConfig.controllerManagerConfigYaml.metrics.bindPort` | Sets the bind port for metrics. | `8080` |
182184
| `managerConfig.controllerManagerConfigYaml.webhook.port` | Sets the bind address for webhook. | `9443` |
185+

chart/open-feature-operator/values.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
defaultNamespace: open-feature-operator-system
55
## @param imagePullSecrets Array of ImagePullSecret objects containing credentials for images pulled by the operator (flagdProxyConfiguration.image, flagdConfiguration.image, controllerManager.manager.image, controllerManager.kubeRbacProxy.image). Example: imagePullSecrets: [{"name": "my-secret"}]
66
imagePullSecrets: []
7+
## @param labels Labels to apply to all of the pods in the operator.
8+
labels: {}
9+
## @param annotations Annotations to apply to all of the pods in the operator.
10+
annotations: {}
711

812
## @section Sidecar configuration
913
sidecarConfiguration:

common/flagdproxy/flagdproxy.go

+19-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"reflect"
77

8+
"golang.org/x/exp/maps"
9+
810
"github.com/go-logr/logr"
911
"github.com/open-feature/open-feature-operator/common"
1012
"github.com/open-feature/open-feature-operator/common/types"
@@ -39,9 +41,11 @@ type FlagdProxyConfiguration struct {
3941
Namespace string
4042
OperatorDeploymentName string
4143
ImagePullSecrets []string
44+
Labels map[string]string
45+
Annotations map[string]string
4246
}
4347

44-
func NewFlagdProxyConfiguration(env types.EnvConfig, imagePullSecrets []string) *FlagdProxyConfiguration {
48+
func NewFlagdProxyConfiguration(env types.EnvConfig, imagePullSecrets []string, labels map[string]string, annotations map[string]string) *FlagdProxyConfiguration {
4549
return &FlagdProxyConfiguration{
4650
Image: env.FlagdProxyImage,
4751
Tag: env.FlagdProxyTag,
@@ -51,6 +55,8 @@ func NewFlagdProxyConfiguration(env types.EnvConfig, imagePullSecrets []string)
5155
ManagementPort: env.FlagdProxyManagementPort,
5256
DebugLogging: env.FlagdProxyDebugLogging,
5357
ImagePullSecrets: imagePullSecrets,
58+
Labels: labels,
59+
Annotations: annotations,
5460
}
5561
}
5662

@@ -151,6 +157,16 @@ func (f *FlagdProxyHandler) newFlagdProxyManifest(ownerReference *metav1.OwnerRe
151157
Name: secret,
152158
})
153159
}
160+
flagdLabels := map[string]string{
161+
"app": FlagdProxyDeploymentName,
162+
"app.kubernetes.io/name": FlagdProxyDeploymentName,
163+
"app.kubernetes.io/managed-by": common.ManagedByAnnotationValue,
164+
"app.kubernetes.io/version": f.config.Tag,
165+
}
166+
maps.Copy(flagdLabels, f.config.Labels)
167+
168+
// No "built-in" annotations to merge at this time. If adding them follow the same pattern as labels.
169+
flagdAnnotations := f.config.Annotations
154170

155171
return &appsV1.Deployment{
156172
ObjectMeta: metav1.ObjectMeta{
@@ -172,12 +188,8 @@ func (f *FlagdProxyHandler) newFlagdProxyManifest(ownerReference *metav1.OwnerRe
172188
},
173189
Template: corev1.PodTemplateSpec{
174190
ObjectMeta: metav1.ObjectMeta{
175-
Labels: map[string]string{
176-
"app": FlagdProxyDeploymentName,
177-
"app.kubernetes.io/name": FlagdProxyDeploymentName,
178-
"app.kubernetes.io/managed-by": common.ManagedByAnnotationValue,
179-
"app.kubernetes.io/version": f.config.Tag,
180-
},
191+
Labels: flagdLabels,
192+
Annotations: flagdAnnotations,
181193
},
182194
Spec: corev1.PodSpec{
183195
ServiceAccountName: FlagdProxyServiceAccountName,

common/flagdproxy/flagdproxy_test.go

+25-8
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,22 @@ import (
2121

2222
var pullSecrets = []string{"test-pullSecret"}
2323

24+
var labels = map[string]string{
25+
"label1": "labelValue1",
26+
"label2": "labelValue2",
27+
}
28+
29+
var annotations = map[string]string{
30+
"annotation1": "annotationValue1",
31+
"annotation2": "annotationValue2",
32+
}
33+
2434
func TestNewFlagdProxyConfiguration(t *testing.T) {
2535

2636
kpConfig := NewFlagdProxyConfiguration(types.EnvConfig{
2737
FlagdProxyPort: 8015,
2838
FlagdProxyManagementPort: 8016,
29-
}, pullSecrets)
39+
}, pullSecrets, labels, annotations)
3040

3141
require.NotNil(t, kpConfig)
3242
require.Equal(t, &FlagdProxyConfiguration{
@@ -35,6 +45,8 @@ func TestNewFlagdProxyConfiguration(t *testing.T) {
3545
DebugLogging: false,
3646
OperatorDeploymentName: common.OperatorDeploymentName,
3747
ImagePullSecrets: pullSecrets,
48+
Labels: labels,
49+
Annotations: annotations,
3850
}, kpConfig)
3951
}
4052

@@ -48,7 +60,7 @@ func TestNewFlagdProxyConfiguration_OverrideEnvVars(t *testing.T) {
4860
FlagdProxyDebugLogging: true,
4961
}
5062

51-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
63+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
5264

5365
require.NotNil(t, kpConfig)
5466
require.Equal(t, &FlagdProxyConfiguration{
@@ -60,11 +72,13 @@ func TestNewFlagdProxyConfiguration_OverrideEnvVars(t *testing.T) {
6072
Namespace: "my-namespace",
6173
OperatorDeploymentName: common.OperatorDeploymentName,
6274
ImagePullSecrets: pullSecrets,
75+
Labels: labels,
76+
Annotations: annotations,
6377
}, kpConfig)
6478
}
6579

6680
func TestNewFlagdProxyHandler(t *testing.T) {
67-
kpConfig := NewFlagdProxyConfiguration(types.EnvConfig{}, pullSecrets)
81+
kpConfig := NewFlagdProxyConfiguration(types.EnvConfig{}, pullSecrets, labels, annotations)
6882

6983
require.NotNil(t, kpConfig)
7084

@@ -100,7 +114,7 @@ func TestDoesFlagdProxyExist(t *testing.T) {
100114
},
101115
}
102116

103-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
117+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
104118

105119
require.NotNil(t, kpConfig)
106120

@@ -128,7 +142,7 @@ func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExistsWithBadVersion(t *testing
128142
env := types.EnvConfig{
129143
PodNamespace: "ns",
130144
}
131-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
145+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
132146

133147
require.NotNil(t, kpConfig)
134148

@@ -187,7 +201,7 @@ func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExistsWithoutLabel(t *testing.T
187201
env := types.EnvConfig{
188202
PodNamespace: "ns",
189203
}
190-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
204+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
191205

192206
require.NotNil(t, kpConfig)
193207

@@ -236,7 +250,7 @@ func TestFlagdProxyHandler_HandleFlagdProxy_ProxyExistsWithNewestVersion(t *test
236250
env := types.EnvConfig{
237251
PodNamespace: "ns",
238252
}
239-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
253+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
240254

241255
require.NotNil(t, kpConfig)
242256

@@ -280,7 +294,7 @@ func TestFlagdProxyHandler_HandleFlagdProxy_CreateProxy(t *testing.T) {
280294
FlagdProxyManagementPort: 90,
281295
FlagdProxyDebugLogging: true,
282296
}
283-
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets)
297+
kpConfig := NewFlagdProxyConfiguration(env, pullSecrets, labels, annotations)
284298

285299
require.NotNil(t, kpConfig)
286300

@@ -357,7 +371,10 @@ func TestFlagdProxyHandler_HandleFlagdProxy_CreateProxy(t *testing.T) {
357371
"app.kubernetes.io/name": FlagdProxyDeploymentName,
358372
"app.kubernetes.io/managed-by": common.ManagedByAnnotationValue,
359373
"app.kubernetes.io/version": "tag",
374+
"label1": "labelValue1",
375+
"label2": "labelValue2",
360376
},
377+
Annotations: annotations,
361378
},
362379
Spec: corev1.PodSpec{
363380
ServiceAccountName: FlagdProxyServiceAccountName,

config/overlays/helm/manager.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ metadata:
66
spec:
77
replicas: 0{{ .Values.controllerManager.replicas }}
88
template:
9+
metadata:
10+
# this is transformed by .github/scripts/strip-kustomize-helm.sh
11+
annotations:
12+
___delete_me___: "___ ___newline___{{ if .Values.annotations }}{{___space___toYaml___space___.Values.annotations___space___|___space___indent___space___8___space___}}{{ end }}___"
13+
# this is transformed by .github/scripts/strip-kustomize-helm.sh
14+
labels:
15+
___delete_me___: "___ ___newline___{{ if .Values.labels }}___newline___{{___space___toYaml___space___.Values.labels___space___|___space___indent___space___8___space___}}{{ end }}___"
916
spec:
1017
# this is transformed by .github/scripts/strip-kustomize-helm.sh
11-
___imagePullSecrets___: "___ ___newline___{{ toYaml .Values.imagePullSecrets | indent 8 }}___"
18+
___imagePullSecrets___: "___ ___newline___ {{ toYaml .Values.imagePullSecrets___space___|___space___indent___space___8___space___}}___"
19+
# this is transformed by .github/scripts/strip-kustomize-helm.sh
1220
dnsPolicy: "{{ .Values.controllerManager.manager.dnsPolicy }}"
1321
# this is transformed by .github/scripts/strip-kustomize-helm.sh
1422
hostNetwork: "___{{ .Values.controllerManager.manager.hostNetwork }}___"
@@ -104,6 +112,8 @@ spec:
104112
- --sidecar-ram-request={{ .Values.sidecarConfiguration.resources.requests.memory }}
105113
- --image-pull-secrets={{ range .Values.imagePullSecrets }}{{ .name }},{{- end }}
106114
- --metrics-bind-address=:{{ .Values.managerConfig.controllerManagerConfigYaml.metrics.bindPort }}
115+
- --labels={{- join "," (range $key, $value := .Values.labels -}}{{- printf "%s:%s" $key $value -}}{{- end) -}}
116+
- --annotations={{- join "," (range $key, $value := .Values.annotations -}}{{- printf "%s:%s" $key $value -}}{{- end) -}}
107117
- name: kube-rbac-proxy
108118
image: "{{ .Values.controllerManager.kubeRbacProxy.image.repository }}:{{ .Values.controllerManager.kubeRbacProxy.image.tag }}"
109119
resources:

controllers/core/featureflagsource/controller_test.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ func TestFeatureFlagSourceReconciler_Reconcile(t *testing.T) {
3030
)
3131
var pullSecrets = []string{"test-pullsecret"}
3232

33+
var labels = map[string]string{
34+
"label1": "labelValue1",
35+
"label2": "labelValue2",
36+
}
37+
38+
var annotations = map[string]string{
39+
"annotation1": "annotationValue1",
40+
"annotation2": "annotationValue2",
41+
}
42+
3343
tests := []struct {
3444
name string
3545
fsConfig *api.FeatureFlagSource
@@ -93,7 +103,7 @@ func TestFeatureFlagSourceReconciler_Reconcile(t *testing.T) {
93103
kpConfig := flagdproxy.NewFlagdProxyConfiguration(commontypes.EnvConfig{
94104
FlagdProxyImage: "ghcr.io/open-feature/flagd-proxy",
95105
FlagdProxyTag: flagdProxyTag,
96-
}, pullSecrets)
106+
}, pullSecrets, labels, annotations)
97107

98108
kpConfig.Namespace = testNamespace
99109
kph := flagdproxy.NewFlagdProxyHandler(

controllers/core/flagd/common/common.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ type FlagdConfiguration struct {
99
Image string
1010
Tag string
1111
ImagePullSecrets []string
12+
Labels map[string]string
13+
Annotations map[string]string
1214

1315
OperatorNamespace string
1416
OperatorDeploymentName string

controllers/core/flagd/config.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
resources "github.com/open-feature/open-feature-operator/controllers/core/flagd/common"
77
)
88

9-
func NewFlagdConfiguration(env types.EnvConfig, imagePullSecrets []string) resources.FlagdConfiguration {
9+
func NewFlagdConfiguration(env types.EnvConfig, imagePullSecrets []string, labels map[string]string, annotations map[string]string) resources.FlagdConfiguration {
1010
return resources.FlagdConfiguration{
1111
Image: env.FlagdImage,
1212
Tag: env.FlagdTag,
@@ -17,5 +17,7 @@ func NewFlagdConfiguration(env types.EnvConfig, imagePullSecrets []string) resou
1717
ManagementPort: env.FlagdManagementPort,
1818
DebugLogging: env.FlagdDebugLogging,
1919
ImagePullSecrets: imagePullSecrets,
20+
Labels: labels,
21+
Annotations: annotations,
2022
}
2123
}

controllers/core/flagd/resources/deployment.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ func (r *FlagdDeployment) GetResource(ctx context.Context, flagd *api.Flagd) (cl
7777
}
7878

7979
featureFlagSource := &api.FeatureFlagSource{}
80-
imagePullSecrets := []corev1.LocalObjectReference{}
81-
for _, secret := range r.FlagdConfig.ImagePullSecrets {
82-
imagePullSecrets = append(imagePullSecrets, corev1.LocalObjectReference{
83-
Name: secret,
84-
})
80+
imagePullSecrets := make([]corev1.LocalObjectReference, len(r.FlagdConfig.ImagePullSecrets))
81+
for i, secret := range r.FlagdConfig.ImagePullSecrets {
82+
imagePullSecrets[i] = corev1.LocalObjectReference{Name: secret}
8583
}
8684

8785
if err := r.Client.Get(ctx, client.ObjectKey{
@@ -100,9 +98,10 @@ func (r *FlagdDeployment) GetResource(ctx context.Context, flagd *api.Flagd) (cl
10098
return nil, errors.New("no flagd container has been injected into deployment")
10199
}
102100

103-
deployment.Spec.Template.Spec.ImagePullSecrets = imagePullSecrets
104-
105101
// override settings for the injected container for flagd standalone deployment mode
102+
deployment.Spec.Template.Spec.ImagePullSecrets = imagePullSecrets
103+
deployment.Spec.Template.ObjectMeta.Labels = r.FlagdConfig.Labels
104+
deployment.Spec.Template.ObjectMeta.Annotations = r.FlagdConfig.Annotations
106105
deployment.Spec.Template.Spec.Containers[0].Image = fmt.Sprintf("%s:%s", r.FlagdConfig.Image, r.FlagdConfig.Tag)
107106

108107
deployment.Spec.Template.Spec.Containers[0].Ports = []corev1.ContainerPort{

go.mod

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/open-feature/open-feature-operator/apis v0.2.41-0.20240506125212-c4831a3cdc00
1010
github.com/stretchr/testify v1.8.4
1111
go.uber.org/zap v1.27.0
12+
golang.org/x/exp v0.0.0-20240707233637-46b078467d37
1213
k8s.io/api v0.28.10
1314
k8s.io/apimachinery v0.28.10
1415
k8s.io/client-go v0.28.10
@@ -31,7 +32,7 @@ require (
3132
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
3233
github.com/golang/protobuf v1.5.4 // indirect
3334
github.com/google/gnostic-models v0.6.8 // indirect
34-
github.com/google/go-cmp v0.5.9 // indirect
35+
github.com/google/go-cmp v0.6.0 // indirect
3536
github.com/google/gofuzz v1.2.0 // indirect
3637
github.com/google/uuid v1.3.0 // indirect
3738
github.com/imdario/mergo v0.3.12 // indirect
@@ -54,7 +55,6 @@ require (
5455
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
5556
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
5657
go.uber.org/multierr v1.11.0 // indirect
57-
golang.org/x/exp v0.0.0-20220722155223-a9213eeb770e // indirect
5858
golang.org/x/net v0.23.0 // indirect
5959
golang.org/x/oauth2 v0.8.0 // indirect
6060
golang.org/x/sys v0.19.0 // indirect

0 commit comments

Comments
 (0)