Skip to content

Commit ea69d06

Browse files
committed
label and annotation changes with mergeWithOverride; adjust tests
1 parent 142eb5c commit ea69d06

12 files changed

+190
-23
lines changed

internal/manifests/mutate.go

+67-4
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func MutateFuncFor(existing, desired client.Object) controllerutil.MutateFn {
7878
// Get the existing labels and override any conflicts with the desired labels
7979
// This will preserve any labels on the existing set.
8080
existingLabels := existing.GetLabels()
81-
if err := mergeWithOverwriteWithEmptyValue(&existingLabels, desired.GetLabels()); err != nil {
81+
if err := mergeWithOverride(&existingLabels, desired.GetLabels()); err != nil {
8282
return err
8383
}
8484
existing.SetLabels(existingLabels)
@@ -281,7 +281,18 @@ func mutateDaemonset(existing, desired *appsv1.DaemonSet) error {
281281
}
282282
}
283283

284-
return mergeWithOverwriteWithEmptyValue(&existing.Spec, desired.Spec)
284+
existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
285+
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
286+
287+
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.UpdateStrategy, desired.Spec.UpdateStrategy); err != nil {
288+
return err
289+
}
290+
291+
if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
292+
return err
293+
}
294+
295+
return nil
285296
}
286297

287298
func mutateDeployment(existing, desired *appsv1.Deployment) error {
@@ -293,7 +304,22 @@ func mutateDeployment(existing, desired *appsv1.Deployment) error {
293304
return err
294305
}
295306
}
296-
return mergeWithOverwriteWithEmptyValue(&existing.Spec, desired.Spec)
307+
308+
existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
309+
existing.Spec.Paused = desired.Spec.Paused
310+
existing.Spec.ProgressDeadlineSeconds = desired.Spec.ProgressDeadlineSeconds
311+
existing.Spec.Replicas = desired.Spec.Replicas
312+
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
313+
314+
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.Strategy, desired.Spec.Strategy); err != nil {
315+
return err
316+
}
317+
318+
if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
319+
return err
320+
}
321+
322+
return nil
297323
}
298324

299325
func mutateStatefulSet(existing, desired *appsv1.StatefulSet) error {
@@ -309,7 +335,44 @@ func mutateStatefulSet(existing, desired *appsv1.StatefulSet) error {
309335
}
310336
}
311337

312-
return mergeWithOverwriteWithEmptyValue(&existing.Spec, desired.Spec)
338+
existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
339+
existing.Spec.Ordinals = desired.Spec.Ordinals
340+
existing.Spec.PersistentVolumeClaimRetentionPolicy = desired.Spec.PersistentVolumeClaimRetentionPolicy
341+
existing.Spec.PodManagementPolicy = desired.Spec.PodManagementPolicy
342+
existing.Spec.Replicas = desired.Spec.Replicas
343+
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
344+
existing.Spec.ServiceName = desired.Spec.ServiceName
345+
346+
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.UpdateStrategy, desired.Spec.UpdateStrategy); err != nil {
347+
return err
348+
}
349+
350+
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.VolumeClaimTemplates, desired.Spec.VolumeClaimTemplates); err != nil {
351+
return err
352+
}
353+
354+
if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
355+
return err
356+
}
357+
358+
return nil
359+
}
360+
361+
func mutatePodTemplate(existing, desired *corev1.PodTemplateSpec) error {
362+
if err := mergeWithOverride(&existing.Labels, desired.Labels); err != nil {
363+
return err
364+
}
365+
366+
if err := mergeWithOverride(&existing.Annotations, desired.Annotations); err != nil {
367+
return err
368+
}
369+
370+
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec, desired.Spec); err != nil {
371+
return err
372+
}
373+
374+
return nil
375+
313376
}
314377

315378
func hasImmutableLabelChange(existingSelectorLabels, desiredLabels map[string]string) error {

internal/manifests/mutate_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -2139,6 +2139,7 @@ func TestMutateDaemonsetLabelChange(t *testing.T) {
21392139
"app.kubernetes.io/instance": "default.daemonset",
21402140
"app.kubernetes.io/managed-by": "opentelemetry-operator",
21412141
"app.kubernetes.io/part-of": "opentelemetry",
2142+
"user-label": "existing",
21422143
"new-user-label": "desired",
21432144
},
21442145
},
@@ -2301,6 +2302,7 @@ func TestMutateDeploymentLabelChange(t *testing.T) {
23012302
"app.kubernetes.io/instance": "default.deployment",
23022303
"app.kubernetes.io/managed-by": "opentelemetry-operator",
23032304
"app.kubernetes.io/part-of": "opentelemetry",
2305+
"user-label": "existing",
23042306
"new-user-label": "desired",
23052307
},
23062308
},
@@ -2463,6 +2465,7 @@ func TestMutateStatefulSetLabelChange(t *testing.T) {
24632465
"app.kubernetes.io/instance": "default.statefulset",
24642466
"app.kubernetes.io/managed-by": "opentelemetry-operator",
24652467
"app.kubernetes.io/part-of": "opentelemetry",
2468+
"user-label": "existing",
24662469
"new-user-label": "desired",
24672470
},
24682471
},

tests/e2e/annotation-change-collector/02-assert-daemonset-without-extra-annotation.yaml

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ apiVersion: apps/v1
22
kind: DaemonSet
33
metadata:
44
name: daemonset-collector
5+
(contains(keys(annotations), 'user-annotation')): true
6+
(contains(keys(annotations), 'new-annotation')): true
7+
annotations:
8+
manual-annotation: "true"
59
spec:
610
template:
711
metadata:
8-
(contains(keys(annotations), 'user-annotation')): false
9-
(contains(keys(annotations), 'new-annotation')): false
12+
(contains(keys(annotations), 'user-annotation')): true
13+
(contains(keys(annotations), 'new-annotation')): true
14+
annotations:
15+
manual-annotation: "true"

tests/e2e/annotation-change-collector/02-assert-deployment-without-extra-annotation.yaml

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ apiVersion: apps/v1
22
kind: Deployment
33
metadata:
44
name: deployment-collector
5+
(contains(keys(annotations), 'user-annotation')): true
6+
(contains(keys(annotations), 'new-annotation')): true
7+
annotations:
8+
manual-annotation: "true"
59
spec:
610
template:
711
metadata:
8-
(contains(keys(annotations), 'user-annotation')): false
9-
(contains(keys(annotations), 'new-annotation')): false
12+
(contains(keys(annotations), 'user-annotation')): true
13+
(contains(keys(annotations), 'new-annotation')): true
14+
annotations:
15+
manual-annotation: "true"

tests/e2e/annotation-change-collector/02-assert-statefulset-without-extra-annotation.yaml

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ apiVersion: apps/v1
22
kind: StatefulSet
33
metadata:
44
name: statefulset-collector
5+
(contains(keys(annotations), 'user-annotation')): true
6+
(contains(keys(annotations), 'new-annotation')): true
7+
annotations:
8+
manual-annotation: "true"
59
spec:
610
template:
711
metadata:
8-
(contains(keys(annotations), 'user-annotation')): false
9-
(contains(keys(annotations), 'new-annotation')): false
12+
(contains(keys(annotations), 'user-annotation')): true
13+
(contains(keys(annotations), 'new-annotation')): true
14+
annotations:
15+
manual-annotation: "true"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: daemonset-collector
5+
annotations:
6+
manual-annotation: "true"
7+
spec:
8+
template:
9+
metadata:
10+
annotations:
11+
manual-annotation: "true"
12+
---
13+
apiVersion: apps/v1
14+
kind: Deployment
15+
metadata:
16+
name: deployment-collector
17+
annotations:
18+
manual-annotation: "true"
19+
spec:
20+
template:
21+
metadata:
22+
annotations:
23+
manual-annotation: "true"
24+
---
25+
apiVersion: apps/v1
26+
kind: StatefulSet
27+
metadata:
28+
name: statefulset-collector
29+
annotations:
30+
manual-annotation: "true"
31+
spec:
32+
template:
33+
metadata:
34+
annotations:
35+
manual-annotation: "true"

tests/e2e/annotation-change-collector/chainsaw-test.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,10 @@ spec:
3636
file: 01-assert-statefulset-with-annotation-change.yaml
3737

3838
- name: step-02
39-
description: delete extra annotation from collector
39+
description: manually annotate resources and delete extra annotation from collector
4040
try:
41+
- apply:
42+
file: 02-manual-annotation-resources.yaml
4143
- update:
4244
file: 02-install-collectors-without-extra-annotation.yaml
4345
# deployment
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
apiVersion: apps/v1
22
kind: DaemonSet
33
metadata:
4-
(contains(keys(labels),'user-label')): false
5-
(contains(keys(labels),'new-label')): false
4+
(contains(keys(labels),'user-label')): true
5+
(contains(keys(labels),'new-label')): true
66
labels:
77
app.kubernetes.io/component: opentelemetry-collector
88
app.kubernetes.io/managed-by: opentelemetry-operator
99
app.kubernetes.io/name: daemonset-collector
1010
app.kubernetes.io/part-of: opentelemetry
11+
manual-label: "true"
1112
spec:
1213
template:
1314
metadata:
14-
(contains(keys(labels),'user-label')): false
15-
(contains(keys(labels),'new-label')): false
15+
(contains(keys(labels),'user-label')): true
16+
(contains(keys(labels),'new-label')): true
17+
labels:
18+
manual-label: "true"
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
apiVersion: apps/v1
22
kind: Deployment
33
metadata:
4-
(contains(keys(labels),'user-label')): false
5-
(contains(keys(labels),'new-label')): false
4+
(contains(keys(labels),'user-label')): true
5+
(contains(keys(labels),'new-label')): true
66
labels:
77
app.kubernetes.io/component: opentelemetry-collector
88
app.kubernetes.io/managed-by: opentelemetry-operator
99
app.kubernetes.io/name: deployment-collector
1010
app.kubernetes.io/part-of: opentelemetry
11+
manual-label: "true"
1112
spec:
1213
template:
1314
metadata:
14-
(contains(keys(labels),'user-label')): false
15-
(contains(keys(labels),'new-label')): false
15+
(contains(keys(labels),'user-label')): true
16+
(contains(keys(labels),'new-label')): true
17+
labels:
18+
manual-label: "true"
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
apiVersion: apps/v1
22
kind: StatefulSet
33
metadata:
4-
(contains(keys(labels),'user-label')): false
5-
(contains(keys(labels),'new-label')): false
4+
(contains(keys(labels),'user-label')): true
5+
(contains(keys(labels),'new-label')): true
66
labels:
77
app.kubernetes.io/component: opentelemetry-collector
88
app.kubernetes.io/managed-by: opentelemetry-operator
99
app.kubernetes.io/name: statefulset-collector
1010
app.kubernetes.io/part-of: opentelemetry
11+
manual-label: "true"
1112
spec:
1213
template:
1314
metadata:
14-
(contains(keys(labels),'user-label')): false
15-
(contains(keys(labels),'new-label')): false
15+
(contains(keys(labels),'user-label')): true
16+
(contains(keys(labels),'new-label')): true
17+
labels:
18+
manual-label: "true"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: apps/v1
2+
kind: DaemonSet
3+
metadata:
4+
name: daemonset-collector
5+
labels:
6+
manual-label: "true"
7+
spec:
8+
template:
9+
metadata:
10+
labels:
11+
manual-label: "true"
12+
---
13+
apiVersion: apps/v1
14+
kind: Deployment
15+
metadata:
16+
name: deployment-collector
17+
labels:
18+
manual-label: "true"
19+
spec:
20+
template:
21+
metadata:
22+
labels:
23+
manual-label: "true"
24+
---
25+
apiVersion: apps/v1
26+
kind: StatefulSet
27+
metadata:
28+
name: statefulset-collector
29+
labels:
30+
manual-label: "true"
31+
spec:
32+
template:
33+
metadata:
34+
labels:
35+
manual-label: "true"

tests/e2e/label-change-collector/chainsaw-test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ spec:
3838
- name: step-02
3939
description: delete extra label from collector
4040
try:
41+
- apply:
42+
file: 02-manual-labeling-resources.yaml
4143
- update:
4244
file: 02-install-collectors-without-extra-label.yaml
4345
# deployment

0 commit comments

Comments
 (0)