Skip to content

Commit

Permalink
Only overwrite certain fields of the PodSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
xperimental committed Jul 12, 2023
1 parent eb9dac8 commit 0150a95
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
20 changes: 18 additions & 2 deletions operator/internal/manifests/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ func mutateDeployment(existing, desired *appsv1.Deployment) {
existing.Spec.Selector = desired.Spec.Selector
}
existing.Spec.Replicas = desired.Spec.Replicas
existing.Spec.Template = desired.Spec.Template
existing.Spec.Strategy = desired.Spec.Strategy
mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template)
}

func mutateStatefulSet(existing, desired *appsv1.StatefulSet) {
Expand All @@ -242,11 +242,27 @@ func mutateStatefulSet(existing, desired *appsv1.StatefulSet) {
existing.Spec.Selector = desired.Spec.Selector
}
existing.Spec.Replicas = desired.Spec.Replicas
existing.Spec.Template = desired.Spec.Template
mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template)
}

func mutatePodDisruptionBudget(existing, desired *policyv1.PodDisruptionBudget) {
existing.Annotations = desired.Annotations
existing.Labels = desired.Labels
existing.Spec = desired.Spec
}

func mutatePodTemplate(existing, desired *corev1.PodTemplateSpec) {
existing.Annotations = desired.Annotations
existing.Labels = desired.Labels
mutatePodSpec(&existing.Spec, &desired.Spec)
}

func mutatePodSpec(existing *corev1.PodSpec, desired *corev1.PodSpec) {
existing.Affinity = desired.Affinity
existing.Containers = desired.Containers
existing.InitContainers = desired.InitContainers
existing.NodeSelector = desired.NodeSelector
existing.Tolerations = desired.Tolerations
existing.TopologySpreadConstraints = desired.TopologySpreadConstraints
existing.Volumes = desired.Volumes
}
66 changes: 66 additions & 0 deletions operator/internal/manifests/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,39 @@ func TestMutateFuncFor_MutateDeploymentSpec(t *testing.T) {
},
},
},
{
name: "remove extra annotations and labels on pod",
got: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"first-key": "first-value",
"second-key": "second-value",
},
Labels: map[string]string{
"first-key": "first-value",
"second-key": "second-value",
},
},
},
},
},
want: &appsv1.Deployment{
Spec: appsv1.DeploymentSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"first-key": "first-value",
},
Labels: map[string]string{
"first-key": "first-value",
},
},
},
},
},
},
}
for _, tst := range table {
tst := tst
Expand Down Expand Up @@ -748,6 +781,39 @@ func TestMutateFuncFor_MutateStatefulSetSpec(t *testing.T) {
},
},
},
{
name: "remove extra annotations and labels on pod",
got: &appsv1.StatefulSet{
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"first-key": "first-value",
"second-key": "second-value",
},
Labels: map[string]string{
"first-key": "first-value",
"second-key": "second-value",
},
},
},
},
},
want: &appsv1.StatefulSet{
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"first-key": "first-value",
},
Labels: map[string]string{
"first-key": "first-value",
},
},
},
},
},
},
}
for _, tst := range table {
tst := tst
Expand Down

0 comments on commit 0150a95

Please sign in to comment.