Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator: Fix update of labels and annotations of PodTemplates #9860

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 44 additions & 37 deletions operator/internal/manifests/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

// MutateFuncFor returns a mutate function based on the
// existing resource's concrete type. It supports currently
// only the following types or else panics:
// - ConfigMap
// - Service
// - Deployment
// - StatefulSet
// - ServiceMonitor
// MutateFuncFor returns a mutate function based on the existing resource's concrete type.
// It currently supports the following types and will return an error for other types:
//
// - ConfigMap
// - Secret
// - Service
// - ServiceAccount
// - ClusterRole
// - ClusterRoleBinding
// - Role
// - RoleBinding
// - Deployment
// - StatefulSet
// - ServiceMonitor
// - Ingress
// - Route
// - PrometheusRule
// - PodDisruptionBudget
func MutateFuncFor(existing, desired client.Object, depAnnotations map[string]string) controllerutil.MutateFn {
return func() error {
existingAnnotations := existing.GetAnnotations()
Expand Down Expand Up @@ -61,7 +71,7 @@ func MutateFuncFor(existing, desired client.Object, depAnnotations map[string]st
case *corev1.Service:
svc := existing.(*corev1.Service)
wantSvc := desired.(*corev1.Service)
return mutateService(svc, wantSvc)
mutateService(svc, wantSvc)

case *corev1.ServiceAccount:
sa := existing.(*corev1.ServiceAccount)
Expand Down Expand Up @@ -91,12 +101,12 @@ func MutateFuncFor(existing, desired client.Object, depAnnotations map[string]st
case *appsv1.Deployment:
dpl := existing.(*appsv1.Deployment)
wantDpl := desired.(*appsv1.Deployment)
return mutateDeployment(dpl, wantDpl)
mutateDeployment(dpl, wantDpl)

case *appsv1.StatefulSet:
sts := existing.(*appsv1.StatefulSet)
wantSts := desired.(*appsv1.StatefulSet)
return mutateStatefulSet(sts, wantSts)
mutateStatefulSet(sts, wantSts)

case *monitoringv1.ServiceMonitor:
svcMonitor := existing.(*monitoringv1.ServiceMonitor)
Expand Down Expand Up @@ -138,14 +148,6 @@ func mergeWithOverride(dst, src interface{}) error {
return nil
}

func mergeWithOverrideEmpty(dst, src interface{}) error {
err := mergo.Merge(dst, src, mergo.WithOverwriteWithEmptyValue)
if err != nil {
return kverrors.Wrap(err, "unable to mergeWithOverrideEmpty", "dst", dst, "src", src)
}
return nil
}

func mutateConfigMap(existing, desired *corev1.ConfigMap) {
existing.Annotations = desired.Annotations
existing.Labels = desired.Labels
Expand Down Expand Up @@ -217,45 +219,50 @@ func mutatePrometheusRule(existing, desired *monitoringv1.PrometheusRule) {
existing.Spec = desired.Spec
}

func mutateService(existing, desired *corev1.Service) error {
func mutateService(existing, desired *corev1.Service) {
existing.Spec.Ports = desired.Spec.Ports
if err := mergeWithOverride(&existing.Spec.Selector, desired.Spec.Selector); err != nil {
return err
}
return nil
existing.Spec.Selector = desired.Spec.Selector
}

func mutateDeployment(existing, desired *appsv1.Deployment) error {
func mutateDeployment(existing, desired *appsv1.Deployment) {
// Deployment selector is immutable so we set this value only if
// a new object is going to be created
if existing.CreationTimestamp.IsZero() {
existing.Spec.Selector = desired.Spec.Selector
}
existing.Spec.Replicas = desired.Spec.Replicas
if err := mergeWithOverrideEmpty(&existing.Spec.Template, desired.Spec.Template); err != nil {
return err
}
if err := mergeWithOverride(&existing.Spec.Strategy, desired.Spec.Strategy); err != nil {
return err
}
return nil
existing.Spec.Strategy = desired.Spec.Strategy
mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template)
}

func mutateStatefulSet(existing, desired *appsv1.StatefulSet) error {
func mutateStatefulSet(existing, desired *appsv1.StatefulSet) {
// StatefulSet selector is immutable so we set this value only if
// a new object is going to be created
if existing.CreationTimestamp.IsZero() {
existing.Spec.Selector = desired.Spec.Selector
}
existing.Spec.Replicas = desired.Spec.Replicas
if err := mergeWithOverrideEmpty(&existing.Spec.Template, desired.Spec.Template); err != nil {
return err
}
return nil
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
}
70 changes: 68 additions & 2 deletions operator/internal/manifests/mutate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ func TestGetMutateFunc_MutateRoleBinding(t *testing.T) {
require.Exactly(t, got.Subjects, want.Subjects)
}

func TestGeMutateFunc_MutateDeploymentSpec(t *testing.T) {
func TestMutateFuncFor_MutateDeploymentSpec(t *testing.T) {
type test struct {
name string
got *appsv1.Deployment
Expand Down Expand Up @@ -591,6 +591,39 @@ func TestGeMutateFunc_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 All @@ -615,7 +648,7 @@ func TestGeMutateFunc_MutateDeploymentSpec(t *testing.T) {
}
}

func TestGeMutateFunc_MutateStatefulSetSpec(t *testing.T) {
func TestMutateFuncFor_MutateStatefulSetSpec(t *testing.T) {
type test struct {
name string
got *appsv1.StatefulSet
Expand Down Expand Up @@ -748,6 +781,39 @@ func TestGeMutateFunc_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