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

Updating spec mutation logic of daemonset, statefulset, and deployment with mergeWithOverwriteWithEmptyValue #3324

Merged
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
fcca8ac
e2e tests for additionalContainers added
davidhaja Sep 27, 2024
5e26452
additionalContainers related unit tests added for mutation
davidhaja Sep 27, 2024
f0645f0
Changed apiversion to v1beta1 in nodeselector e2e test
davidhaja Sep 28, 2024
69e79d0
removed explicit zero value for additionalContainers and changed appl…
davidhaja Sep 28, 2024
89cda21
added affinity in collector e2e tests
davidhaja Sep 28, 2024
5a713c3
affinity unit tests added for daemonset, deployment, statefulset duri…
davidhaja Sep 29, 2024
e676581
collector container mutate args e2e tests added
davidhaja Sep 29, 2024
9911706
Unit tests added for additional args mutation of collector container
davidhaja Sep 29, 2024
6e72bf5
e2e tests for changing labels in collectors
davidhaja Oct 2, 2024
4ae3398
e2e tests for changing annotations in collectors
davidhaja Oct 2, 2024
74fbdc1
fix annotation change e2e test asserts
davidhaja Oct 2, 2024
15f2113
Error and label change related unit tests added for resource mutation
davidhaja Oct 3, 2024
88ef17b
fix label change e2e tests for mutation
davidhaja Oct 3, 2024
fc1e3ad
mutating the spec and labels of deployment, daemonset, statefulset wi…
davidhaja Oct 3, 2024
eedc548
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
davidhaja Oct 3, 2024
9060661
Adjust reconcile tests to new mutation logic
davidhaja Oct 4, 2024
8f5fd10
Added chlog entry for new mutation logic
davidhaja Oct 4, 2024
62dc693
fix typo in mutate_test.go
davidhaja Oct 4, 2024
764f054
Fix G601: Implicit memory aliasing in mutate_test.go
davidhaja Oct 4, 2024
142eb5c
Revert "Adjust reconcile tests to new mutation logic"
davidhaja Oct 8, 2024
ea69d06
label and annotation changes with mergeWithOverride; adjust tests
davidhaja Oct 9, 2024
f4ab9aa
copy over desired.spec.template.spec to existing.spec.template.spec
davidhaja Oct 11, 2024
c04bfd9
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
davidhaja Oct 11, 2024
1d1ebb1
volumeClaimTemplates mutation through range
davidhaja Oct 16, 2024
2ae1acf
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
davidhaja Oct 16, 2024
e16acf1
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
davidhaja Oct 25, 2024
0d42bec
Change type to bugfix
davidhaja Oct 25, 2024
49ab4da
Fix volume-claim-label e2e test
davidhaja Oct 25, 2024
f7b265e
Merge branch 'main' into 2947-updating-ds-sf-depl-mutation
iblancasa Oct 28, 2024
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
16 changes: 16 additions & 0 deletions .chloggen/2947-updating-ds-sf-depl-mutation.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this shouldn't be a bug_fix. The current behaviour looks like a bug to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it.


# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Improve mutation of deployments, statefulsets, and daemonsets allowing to remove fields on update"

# One or more tracking issues related to the change
issues: [2947]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
2 changes: 1 addition & 1 deletion controllers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func reconcileDesiredObjects(ctx context.Context, kubeClient client.Client, logg
op = result
return createOrUpdateErr
})
if crudErr != nil && errors.Is(crudErr, manifests.ImmutableChangeErr) {
if crudErr != nil && errors.As(crudErr, &manifests.ImmutableChangeErr) {
davidhaja marked this conversation as resolved.
Show resolved Hide resolved
l.Error(crudErr, "detected immutable field change, trying to delete, new object will be created on next reconcile", "existing", existing.GetName())
delErr := kubeClient.Delete(ctx, existing)
if delErr != nil {
Expand Down
133 changes: 77 additions & 56 deletions internal/manifests/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package manifests

import (
"errors"
"fmt"
"reflect"

Expand All @@ -34,8 +33,16 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)

type ImmutableFieldChangeErr struct {
Field string
}

func (e *ImmutableFieldChangeErr) Error() string {
return fmt.Sprintf("Immutable field change attempted: %s", e.Field)
}

var (
ImmutableChangeErr = errors.New("immutable field change attempted")
ImmutableChangeErr *ImmutableFieldChangeErr
)

// MutateFuncFor returns a mutate function based on the
Expand Down Expand Up @@ -189,10 +196,6 @@ func mergeWithOverride(dst, src interface{}) error {
return mergo.Merge(dst, src, mergo.WithOverride)
}

func mergeWithOverwriteWithEmptyValue(dst, src interface{}) error {
return mergo.Merge(dst, src, mergo.WithOverwriteWithEmptyValue)
}

func mutateSecret(existing, desired *corev1.Secret) {
existing.Labels = desired.Labels
existing.Annotations = desired.Annotations
Expand Down Expand Up @@ -276,68 +279,77 @@ func mutateService(existing, desired *corev1.Service) {
}

func mutateDaemonset(existing, desired *appsv1.DaemonSet) error {
if !existing.CreationTimestamp.IsZero() && !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return ImmutableChangeErr
}
// Daemonset 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
}
if err := mergeWithOverride(&existing.Spec, desired.Spec); err != nil {
return err
if !existing.CreationTimestamp.IsZero() {
if !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return &ImmutableFieldChangeErr{Field: "Spec.Selector"}
}
if err := hasImmutableLabelChange(existing.Spec.Selector.MatchLabels, desired.Spec.Template.Labels); err != nil {
return err
}
}
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.Template.Spec.NodeSelector, desired.Spec.Template.Spec.NodeSelector); err != nil {

existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
existing.Spec.UpdateStrategy = desired.Spec.UpdateStrategy

if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
return err
}

return nil
}

func mutateDeployment(existing, desired *appsv1.Deployment) error {
if !existing.CreationTimestamp.IsZero() && !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return ImmutableChangeErr
}
// 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
if !existing.CreationTimestamp.IsZero() {
if !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return &ImmutableFieldChangeErr{Field: "Spec.Selector"}
}
if err := hasImmutableLabelChange(existing.Spec.Selector.MatchLabels, desired.Spec.Template.Labels); err != nil {
return err
}
}

existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
existing.Spec.Paused = desired.Spec.Paused
existing.Spec.ProgressDeadlineSeconds = desired.Spec.ProgressDeadlineSeconds
existing.Spec.Replicas = desired.Spec.Replicas
if err := mergeWithOverride(&existing.Spec.Template, desired.Spec.Template); err != nil {
return err
}
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.Template.Spec.NodeSelector, desired.Spec.Template.Spec.NodeSelector); err != nil {
return err
}
if err := mergeWithOverride(&existing.Spec.Strategy, desired.Spec.Strategy); err != nil {
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
existing.Spec.Strategy = desired.Spec.Strategy

if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
return err
}

return nil
}

func mutateStatefulSet(existing, desired *appsv1.StatefulSet) error {
if hasChange, field := hasImmutableFieldChange(existing, desired); hasChange {
return fmt.Errorf("%s is being changed, %w", field, ImmutableChangeErr)
}
// 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
if !existing.CreationTimestamp.IsZero() {
if !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return &ImmutableFieldChangeErr{Field: "Spec.Selector"}
}
if err := hasImmutableLabelChange(existing.Spec.Selector.MatchLabels, desired.Spec.Template.Labels); err != nil {
return err
}
if hasVolumeClaimsTemplatesChanged(existing, desired) {
return &ImmutableFieldChangeErr{Field: "Spec.VolumeClaimTemplates"}
}
}

existing.Spec.MinReadySeconds = desired.Spec.MinReadySeconds
existing.Spec.Ordinals = desired.Spec.Ordinals
existing.Spec.PersistentVolumeClaimRetentionPolicy = desired.Spec.PersistentVolumeClaimRetentionPolicy
existing.Spec.PodManagementPolicy = desired.Spec.PodManagementPolicy
existing.Spec.Replicas = desired.Spec.Replicas
existing.Spec.RevisionHistoryLimit = desired.Spec.RevisionHistoryLimit
existing.Spec.ServiceName = desired.Spec.ServiceName
existing.Spec.UpdateStrategy = desired.Spec.UpdateStrategy
existing.Spec.VolumeClaimTemplates = desired.Spec.VolumeClaimTemplates
davidhaja marked this conversation as resolved.
Show resolved Hide resolved

for i := range existing.Spec.VolumeClaimTemplates {
existing.Spec.VolumeClaimTemplates[i].TypeMeta = desired.Spec.VolumeClaimTemplates[i].TypeMeta
existing.Spec.VolumeClaimTemplates[i].ObjectMeta = desired.Spec.VolumeClaimTemplates[i].ObjectMeta
existing.Spec.VolumeClaimTemplates[i].Spec = desired.Spec.VolumeClaimTemplates[i].Spec
}
if err := mergeWithOverride(&existing.Spec.Template, desired.Spec.Template); err != nil {
return err
}
if err := mergeWithOverwriteWithEmptyValue(&existing.Spec.Template.Spec.NodeSelector, desired.Spec.Template.Spec.NodeSelector); err != nil {
if err := mutatePodTemplate(&existing.Spec.Template, &desired.Spec.Template); err != nil {
return err
}

return nil
}

Expand All @@ -353,19 +365,28 @@ func mutateIssuer(existing, desired *cmv1.Issuer) {
existing.Spec = desired.Spec
}

func hasImmutableFieldChange(existing, desired *appsv1.StatefulSet) (bool, string) {
if existing.CreationTimestamp.IsZero() {
return false, ""
}
if !apiequality.Semantic.DeepEqual(desired.Spec.Selector, existing.Spec.Selector) {
return true, fmt.Sprintf("Spec.Selector: desired: %s existing: %s", desired.Spec.Selector, existing.Spec.Selector)
func mutatePodTemplate(existing, desired *corev1.PodTemplateSpec) error {
if err := mergeWithOverride(&existing.Labels, desired.Labels); err != nil {
return err
}

if hasVolumeClaimsTemplatesChanged(existing, desired) {
return true, "Spec.VolumeClaimTemplates"
if err := mergeWithOverride(&existing.Annotations, desired.Annotations); err != nil {
return err
}

return false, ""
existing.Spec = desired.Spec

return nil

}

func hasImmutableLabelChange(existingSelectorLabels, desiredLabels map[string]string) error {
for k, v := range existingSelectorLabels {
if vv, ok := desiredLabels[k]; !ok || vv != v {
return &ImmutableFieldChangeErr{Field: "Spec.Template.Metadata.Labels"}
}
}
return nil
}

// hasVolumeClaimsTemplatesChanged if volume claims template change has been detected.
Expand Down
Loading
Loading