-
Notifications
You must be signed in to change notification settings - Fork 149
[CONTP-1609] Auto-inject agent toleration when untaint controller is enabled #3086
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
Merged
adel121
merged 9 commits into
main
from
adelhajhassan/auto-add-untaint-toleration-to-agent
Jun 5, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6a003f6
[CONTP-1609] Auto-inject agent toleration when untaint controller is …
adel121 ab96c1e
extract taint key and value to a shared package
adel121 7abc20a
use builtin toleration detection api
adel121 31dae4d
fix lint
adel121 7ba9b6d
increase test coverage
adel121 9303804
fix formatting
adel121 63ee7a8
fix lint
adel121 972b436
Update docs/untaint_controller.md
adel121 87e8b9d
address review comments
adel121 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
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
36 changes: 36 additions & 0 deletions
36
internal/controller/datadogagent/component/agent/untaint_toleration.go
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,36 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| package agent | ||
|
|
||
| import ( | ||
| "github.com/go-logr/logr" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| "github.com/DataDog/datadog-operator/pkg/untaint" | ||
| ) | ||
|
|
||
| // podToleratesAgentNotReadyStartup reports whether tolerations tolerate the | ||
| // agent-not-ready startup taint, using the same rules as the scheduler/kubelet | ||
| // (corev1.Toleration.ToleratesTaint). Comparison-operator tolerations (Lt/Gt) are | ||
| // ignored unless the cluster enables that feature (we pass false). | ||
| func podToleratesAgentNotReadyStartup(logger logr.Logger, tolerations []corev1.Toleration) bool { | ||
| taint := untaint.AgentNotReadyTaint() | ||
| for i := range tolerations { | ||
| if tolerations[i].ToleratesTaint(logger, &taint, false) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // EnsureAgentNotReadyStartupToleration appends the agent-not-ready Equal toleration | ||
| // when not already tolerated per Kubernetes toleration matching. | ||
| func EnsureAgentNotReadyStartupToleration(logger logr.Logger, spec *corev1.PodSpec) { | ||
| if podToleratesAgentNotReadyStartup(logger, spec.Tolerations) { | ||
| return | ||
| } | ||
| spec.Tolerations = append(spec.Tolerations, untaint.AgentNotReadyEqualToleration()) | ||
| } |
71 changes: 71 additions & 0 deletions
71
internal/controller/datadogagent/component/agent/untaint_toleration_test.go
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,71 @@ | ||
| // Unless explicitly stated otherwise all files in this repository are licensed | ||
| // under the Apache License Version 2.0. | ||
| // This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
| // Copyright 2016-present Datadog, Inc. | ||
|
|
||
| package agent | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
|
|
||
| "github.com/DataDog/datadog-operator/pkg/untaint" | ||
| ) | ||
|
|
||
| func TestEnsureAgentNotReadyStartupToleration_idempotent(t *testing.T) { | ||
| spec := &corev1.PodSpec{} | ||
| want := untaint.AgentNotReadyEqualToleration() | ||
| log := logr.Discard() | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(log, spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| require.Equal(t, want, spec.Tolerations[0]) | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(log, spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| } | ||
|
|
||
| func TestEnsureAgentNotReadyStartupToleration_skipIfUserEqual(t *testing.T) { | ||
| want := untaint.AgentNotReadyEqualToleration() | ||
| spec := &corev1.PodSpec{Tolerations: []corev1.Toleration{want}} | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(logr.Discard(), spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| } | ||
|
|
||
| func TestEnsureAgentNotReadyStartupToleration_skipIfUserExists(t *testing.T) { | ||
| spec := &corev1.PodSpec{ | ||
| Tolerations: []corev1.Toleration{ | ||
| {Key: untaint.AgentNotReadyTaintKey, Operator: corev1.TolerationOpExists, Effect: corev1.TaintEffectNoSchedule}, | ||
| }, | ||
| } | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(logr.Discard(), spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| } | ||
|
|
||
| func TestEnsureAgentNotReadyStartupToleration_equalEmptyOperator(t *testing.T) { | ||
| spec := &corev1.PodSpec{ | ||
| Tolerations: []corev1.Toleration{ | ||
| {Key: untaint.AgentNotReadyTaintKey, Operator: "", Value: untaint.AgentNotReadyTaintValue, Effect: corev1.TaintEffectNoSchedule}, | ||
| }, | ||
| } | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(logr.Discard(), spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| } | ||
|
|
||
| func TestEnsureAgentNotReadyStartupToleration_skipIfExistsAllKeys(t *testing.T) { | ||
| // Empty key + Exists matches every taint (corev1.Toleration.ToleratesTaint). | ||
| spec := &corev1.PodSpec{ | ||
| Tolerations: []corev1.Toleration{ | ||
| {Operator: corev1.TolerationOpExists}, | ||
| }, | ||
| } | ||
|
|
||
| EnsureAgentNotReadyStartupToleration(logr.Discard(), spec) | ||
| require.Len(t, spec.Tolerations, 1) | ||
| } |
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 |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ import ( | |
| "context" | ||
| "fmt" | ||
| "reflect" | ||
| "slices" | ||
| "sort" | ||
| "testing" | ||
| "time" | ||
|
|
@@ -44,6 +45,7 @@ import ( | |
| "github.com/DataDog/datadog-operator/pkg/images" | ||
| "github.com/DataDog/datadog-operator/pkg/kubernetes" | ||
| "github.com/DataDog/datadog-operator/pkg/testutils" | ||
| "github.com/DataDog/datadog-operator/pkg/untaint" | ||
| pkgutils "github.com/DataDog/datadog-operator/pkg/utils" | ||
| ) | ||
|
|
||
|
|
@@ -61,6 +63,17 @@ type testCase struct { | |
| introspectionEnabled bool // For introspection tests | ||
| } | ||
|
|
||
| // ddaiReconcilerOptionsFromDDA mirrors setup.go wiring so DDAI tests behave like production | ||
| // for flags shared with the DatadogAgent reconciler (e.g. UntaintControllerEnabled). | ||
| func ddaiReconcilerOptionsFromDDA(opts ReconcilerOptions) datadogagentinternal.ReconcilerOptions { | ||
| return datadogagentinternal.ReconcilerOptions{ | ||
| ExtendedDaemonsetOptions: opts.ExtendedDaemonsetOptions, | ||
| SupportCilium: opts.SupportCilium, | ||
| OperatorMetricsEnabled: opts.OperatorMetricsEnabled, | ||
| UntaintControllerEnabled: opts.UntaintControllerEnabled, | ||
| } | ||
| } | ||
|
|
||
| // runTestCases runs test cases | ||
| func runTestCases(t *testing.T, tests []testCase, testFunc func(t *testing.T, tt testCase, opts ReconcilerOptions)) { | ||
| for _, tt := range tests { | ||
|
|
@@ -106,7 +119,7 @@ func runDDAReconcilerTest(t *testing.T, tt testCase, opts ReconcilerOptions) { | |
| r.initializeComponentRegistry() | ||
|
|
||
| ri := datadogagentinternal.NewReconciler( | ||
| datadogagentinternal.ReconcilerOptions{}, | ||
| ddaiReconcilerOptionsFromDDA(opts), | ||
| c, | ||
| kubernetes.PlatformInfo{}, | ||
| s, | ||
|
|
@@ -179,7 +192,7 @@ func runFullReconcilerTest(t *testing.T, tt testCase, opts ReconcilerOptions) { | |
| r.initializeComponentRegistry() | ||
|
|
||
| ri := datadogagentinternal.NewReconciler( | ||
| datadogagentinternal.ReconcilerOptions{}, | ||
| ddaiReconcilerOptionsFromDDA(opts), | ||
| c, | ||
| kubernetes.PlatformInfo{}, | ||
| s, | ||
|
|
@@ -248,6 +261,64 @@ func buildClient(t *testing.T, tt testCase, s *runtime.Scheme) client.Client { | |
| return builder.Build() | ||
| } | ||
|
|
||
| func TestReconcileDDA_UntaintController_injectsAgentNotReadyToleration(t *testing.T) { | ||
| const resourcesName = "foo" | ||
| const resourcesNamespace = "bar" | ||
| const dsName = "foo-agent" | ||
| defaultRequeueDuration := 15 * time.Second | ||
|
|
||
| wantTol := untaint.AgentNotReadyEqualToleration() | ||
| tt := testCase{ | ||
| name: "untaint controller enabled injects agent-not-ready toleration on node agent DS", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for completeness would be nice to have case asserting tolerations aren't added when feature is disabled. |
||
| loadFunc: func(c client.Client) *v2alpha1.DatadogAgent { | ||
| dda := testutils.NewInitializedDatadogAgentBuilder(resourcesNamespace, resourcesName). | ||
| Build() | ||
| _ = c.Create(context.TODO(), dda) | ||
| return dda | ||
| }, | ||
| want: reconcile.Result{RequeueAfter: defaultRequeueDuration}, | ||
| wantErr: false, | ||
| wantFunc: func(t *testing.T, c client.Client) { | ||
| ds := &appsv1.DaemonSet{} | ||
| err := c.Get(context.TODO(), types.NamespacedName{Namespace: resourcesNamespace, Name: dsName}, ds) | ||
| assert.NoError(t, err) | ||
| assert.True(t, slices.ContainsFunc(ds.Spec.Template.Spec.Tolerations, func(tol corev1.Toleration) bool { | ||
| return reflect.DeepEqual(tol, wantTol) | ||
| }), "expected injected toleration %+v in %+v", wantTol, ds.Spec.Template.Spec.Tolerations) | ||
| }, | ||
| } | ||
| runDDAReconcilerTest(t, tt, ReconcilerOptions{UntaintControllerEnabled: true}) | ||
| } | ||
|
|
||
| func TestReconcileDDA_UntaintController_disabledDoesNotInjectAgentNotReadyToleration(t *testing.T) { | ||
| const resourcesName = "foo" | ||
| const resourcesNamespace = "bar" | ||
| const dsName = "foo-agent" | ||
| defaultRequeueDuration := 15 * time.Second | ||
|
|
||
| wantTol := untaint.AgentNotReadyEqualToleration() | ||
| tt := testCase{ | ||
| name: "untaint controller disabled does not inject agent-not-ready toleration on node agent DS", | ||
| loadFunc: func(c client.Client) *v2alpha1.DatadogAgent { | ||
| dda := testutils.NewInitializedDatadogAgentBuilder(resourcesNamespace, resourcesName). | ||
| Build() | ||
| _ = c.Create(context.TODO(), dda) | ||
| return dda | ||
| }, | ||
| want: reconcile.Result{RequeueAfter: defaultRequeueDuration}, | ||
| wantErr: false, | ||
| wantFunc: func(t *testing.T, c client.Client) { | ||
| ds := &appsv1.DaemonSet{} | ||
| err := c.Get(context.TODO(), types.NamespacedName{Namespace: resourcesNamespace, Name: dsName}, ds) | ||
| assert.NoError(t, err) | ||
| assert.False(t, slices.ContainsFunc(ds.Spec.Template.Spec.Tolerations, func(tol corev1.Toleration) bool { | ||
| return reflect.DeepEqual(tol, wantTol) | ||
| }), "did not expect injected toleration %+v in %+v", wantTol, ds.Spec.Template.Spec.Tolerations) | ||
| }, | ||
| } | ||
| runDDAReconcilerTest(t, tt, ReconcilerOptions{UntaintControllerEnabled: false}) | ||
| } | ||
|
|
||
| func TestReconcileDatadogAgentV2_Reconcile(t *testing.T) { | ||
| const resourcesName = "foo" | ||
| const resourcesNamespace = "bar" | ||
|
|
||
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
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
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
Oops, something went wrong.
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.