-
Notifications
You must be signed in to change notification settings - Fork 328
fix: preserve immutable DaemonSet selector on FluentBit label changes #2017
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
Open
pujitha24
wants to merge
3
commits into
fluent:master
Choose a base branch
from
pujitha24:auto/issue-1944
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+155
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| Copyright 2021. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package controllers | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| appsv1 "k8s.io/api/apps/v1" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
|
|
||
| fluentbitv1alpha2 "github.com/fluent/fluent-operator/v3/apis/fluentbit/v1alpha2" | ||
| ) | ||
|
|
||
| // TestFluentBitMutateDaemonSetPreservesImmutableSelector reproduces | ||
| // https://github.com/fluent/fluent-operator/issues/1944: changing | ||
| // spec.labels on a FluentBit CR must not cause the reconciler to try to | ||
| // rewrite the DaemonSet's immutable spec.selector. | ||
| func TestFluentBitMutateDaemonSetPreservesImmutableSelector(t *testing.T) { | ||
| s := runtime.NewScheme() | ||
| if err := scheme.AddToScheme(s); err != nil { | ||
| t.Fatalf("failed to add client-go scheme: %v", err) | ||
| } | ||
| if err := fluentbitv1alpha2.AddToScheme(s); err != nil { | ||
| t.Fatalf("failed to add fluentbit scheme: %v", err) | ||
| } | ||
| r := &FluentBitReconciler{Scheme: s} | ||
|
|
||
| // The DaemonSet as it exists in the cluster today, created when | ||
| // spec.labels only contained "app". | ||
| existing := &appsv1.DaemonSet{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "fluent-bit", Namespace: "default"}, | ||
| Spec: appsv1.DaemonSetSpec{ | ||
| Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "fluentbit"}}, | ||
| Template: corev1.PodTemplateSpec{ | ||
| ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "fluentbit"}}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // The user then adds a label to the FluentBit CR. | ||
| fb := &fluentbitv1alpha2.FluentBit{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "fluent-bit", Namespace: "default"}, | ||
| Spec: fluentbitv1alpha2.FluentBitSpec{ | ||
| Labels: map[string]string{"app": "fluentbit", "fluentbit.fluent.io/enabled": "true"}, | ||
| }, | ||
| } | ||
|
|
||
| if err := r.mutate(existing, fb)(); err != nil { | ||
| t.Fatalf("mutate returned an unexpected error: %v", err) | ||
| } | ||
|
|
||
| if got := existing.Spec.Selector.MatchLabels; len(got) != 1 || got["app"] != "fluentbit" { | ||
| t.Fatalf("expected the immutable selector to stay unchanged, got %v", got) | ||
| } | ||
|
|
||
| // The pod template must still satisfy the preserved selector. | ||
| if existing.Spec.Template.Labels["app"] != "fluentbit" { | ||
| t.Fatalf("expected pod template to retain the selector label, got %v", existing.Spec.Template.Labels) | ||
| } | ||
| // The new label should still be applied to the pod template. | ||
| if existing.Spec.Template.Labels["fluentbit.fluent.io/enabled"] != "true" { | ||
| t.Fatalf("expected new label to be applied to pod template, got %v", existing.Spec.Template.Labels) | ||
| } | ||
| } | ||
|
|
||
| // TestFluentBitMutateDaemonSetRemovedLabelDoesNotMutateSpec covers removing a | ||
| // label that was part of the original selector: the pod template must still | ||
| // carry it (to satisfy the preserved selector) without mutating fb.Spec.Labels | ||
| // in place, since MakeDaemonSet reuses that map for the DaemonSet's own labels | ||
| // and selector. | ||
| func TestFluentBitMutateDaemonSetRemovedLabelDoesNotMutateSpec(t *testing.T) { | ||
| s := runtime.NewScheme() | ||
| if err := scheme.AddToScheme(s); err != nil { | ||
| t.Fatalf("failed to add client-go scheme: %v", err) | ||
| } | ||
| if err := fluentbitv1alpha2.AddToScheme(s); err != nil { | ||
| t.Fatalf("failed to add fluentbit scheme: %v", err) | ||
| } | ||
| r := &FluentBitReconciler{Scheme: s} | ||
|
|
||
| existing := &appsv1.DaemonSet{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "fluent-bit", Namespace: "default"}, | ||
| Spec: appsv1.DaemonSetSpec{ | ||
| Selector: &metav1.LabelSelector{MatchLabels: map[string]string{"app": "fluentbit", "team": "logging"}}, | ||
| Template: corev1.PodTemplateSpec{ | ||
| ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "fluentbit", "team": "logging"}}, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| // The user removes "team" from the FluentBit CR's labels. | ||
| fbLabels := map[string]string{"app": "fluentbit"} | ||
| fb := &fluentbitv1alpha2.FluentBit{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: "fluent-bit", Namespace: "default"}, | ||
| Spec: fluentbitv1alpha2.FluentBitSpec{Labels: fbLabels}, | ||
| } | ||
|
|
||
| if err := r.mutate(existing, fb)(); err != nil { | ||
| t.Fatalf("mutate returned an unexpected error: %v", err) | ||
| } | ||
|
|
||
| if got := existing.Spec.Selector.MatchLabels; len(got) != 2 || got["team"] != "logging" { | ||
| t.Fatalf("expected the immutable selector to stay unchanged, got %v", got) | ||
| } | ||
| if existing.Spec.Template.Labels["team"] != "logging" { | ||
| t.Fatalf("expected pod template to retain the removed selector label, got %v", existing.Spec.Template.Labels) | ||
| } | ||
|
|
||
| // fb.Spec.Labels must not have been mutated as a side effect: MakeDaemonSet | ||
| // reuses it directly as the DaemonSet's labels/selector map. | ||
| if _, ok := fbLabels["team"]; ok { | ||
| t.Fatalf("mutate must not write back into fb.Spec.Labels, got %v", fbLabels) | ||
| } | ||
| if len(fbLabels) != 1 { | ||
| t.Fatalf("expected fb.Spec.Labels to remain untouched, got %v", fbLabels) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.