@@ -8,6 +8,7 @@ package datadogcsidriver
88import (
99 "context"
1010 "fmt"
11+ "reflect"
1112 "testing"
1213
1314 "github.com/stretchr/testify/assert"
@@ -28,14 +29,15 @@ import (
2829 "github.com/DataDog/datadog-operator/api/datadoghq/v1alpha1"
2930 "github.com/DataDog/datadog-operator/pkg/images"
3031 "github.com/DataDog/datadog-operator/pkg/kubernetes"
32+ "github.com/DataDog/datadog-operator/pkg/untaint"
3133)
3234
3335const (
3436 testNamespace = "datadog"
3537 testName = "datadog-csi"
3638)
3739
38- func newTestReconciler (t * testing.T , objects ... client.Object ) (* Reconciler , client.Client ) {
40+ func newTestReconciler (t * testing.T , untaintControllerEnabled bool , objects ... client.Object ) (* Reconciler , client.Client ) {
3941 t .Helper ()
4042 s := scheme .Scheme
4143 s .AddKnownTypes (v1alpha1 .GroupVersion ,
@@ -52,7 +54,7 @@ func newTestReconciler(t *testing.T, objects ...client.Object) (*Reconciler, cli
5254 // Set the default controller-runtime logger so ctrl.LoggerFrom(ctx) works in tests
5355 ctrl .SetLogger (zap .New (zap .UseDevMode (true )))
5456 recorder := record .NewFakeRecorder (10 )
55- r := NewReconciler (c , s , recorder )
57+ r := NewReconciler (c , s , recorder , untaintControllerEnabled )
5658
5759 return r , c
5860}
@@ -73,7 +75,7 @@ func defaultCSIDriverCR() *v1alpha1.DatadogCSIDriver {
7375
7476func TestReconcile_CreatesResources (t * testing.T ) {
7577 instance := defaultCSIDriverCR ()
76- r , c := newTestReconciler (t , instance )
78+ r , c := newTestReconciler (t , false , instance )
7779 ctx := context .Background ()
7880
7981 // First reconcile: adds finalizer
@@ -150,7 +152,7 @@ func TestReconcile_CustomSocketPaths(t *testing.T) {
150152 instance .Spec .APMSocketPath = & customAPM
151153 instance .Spec .DSDSocketPath = & customDSD
152154
153- r , c := newTestReconciler (t , instance )
155+ r , c := newTestReconciler (t , false , instance )
154156 ctx := context .Background ()
155157
156158 // Reconcile twice (finalizer + create)
@@ -181,7 +183,7 @@ func TestReconcile_CustomSocketPaths(t *testing.T) {
181183
182184func TestReconcile_Deletion (t * testing.T ) {
183185 instance := defaultCSIDriverCR ()
184- r , c := newTestReconciler (t , instance )
186+ r , c := newTestReconciler (t , false , instance )
185187 ctx := context .Background ()
186188
187189 // Reconcile to add finalizer + create resources
@@ -224,7 +226,7 @@ func TestReconcile_Deletion(t *testing.T) {
224226
225227func TestReconcile_UpdateDaemonSetOnSpecChange (t * testing.T ) {
226228 instance := defaultCSIDriverCR ()
227- r , c := newTestReconciler (t , instance )
229+ r , c := newTestReconciler (t , false , instance )
228230 ctx := context .Background ()
229231
230232 // Reconcile to create resources
@@ -266,7 +268,7 @@ func TestReconcile_UpdateDaemonSetOnSpecChange(t *testing.T) {
266268
267269func TestReconcile_IdempotentNoUpdate (t * testing.T ) {
268270 instance := defaultCSIDriverCR ()
269- r , c := newTestReconciler (t , instance )
271+ r , c := newTestReconciler (t , false , instance )
270272 ctx := context .Background ()
271273
272274 // Reconcile to create resources
@@ -316,7 +318,7 @@ func TestReconcile_CSIDriverLabelsAdoption(t *testing.T) {
316318 },
317319 }
318320
319- r , c := newTestReconciler (t , instance , existingCSIDriver )
321+ r , c := newTestReconciler (t , false , instance , existingCSIDriver )
320322 ctx := context .Background ()
321323
322324 // Reconcile: add finalizer
@@ -369,7 +371,7 @@ func TestReconcile_Overrides(t *testing.T) {
369371 },
370372 }
371373
372- r , c := newTestReconciler (t , instance )
374+ r , c := newTestReconciler (t , false , instance )
373375 ctx := context .Background ()
374376
375377 // Reconcile twice (finalizer + create)
@@ -388,7 +390,7 @@ func TestReconcile_Overrides(t *testing.T) {
388390 // Labels merged into pod template
389391 assert .Equal (t , "containers" , ds .Spec .Template .Labels ["team" ])
390392 // Default labels still present
391- assert .Equal (t , csiDsName , ds .Spec .Template .Labels [appLabelKey ])
393+ assert .Equal (t , csiDsName , ds .Spec .Template .Labels [AppLabelKey ])
392394
393395 // Tolerations applied
394396 require .Len (t , ds .Spec .Template .Spec .Tolerations , 1 )
@@ -423,7 +425,7 @@ func TestReconcile_StatusConditionOnCSIDriverError(t *testing.T) {
423425 // Instead, we test that when the reconcile succeeds, the condition is Ready=True,
424426 // and verify the status structure.
425427 instance := defaultCSIDriverCR ()
426- r , c := newTestReconciler (t , instance )
428+ r , c := newTestReconciler (t , false , instance )
427429 ctx := context .Background ()
428430
429431 // Reconcile to add finalizer
@@ -450,7 +452,7 @@ func TestReconcile_StatusConditionOnCSIDriverError(t *testing.T) {
450452
451453func TestReconcile_CSIDriverSpecDriftIsReconciled (t * testing.T ) {
452454 instance := defaultCSIDriverCR ()
453- r , c := newTestReconciler (t , instance )
455+ r , c := newTestReconciler (t , false , instance )
454456 ctx := context .Background ()
455457
456458 // Reconcile to create resources.
@@ -487,6 +489,49 @@ func TestReconcile_CSIDriverSpecDriftIsReconciled(t *testing.T) {
487489 assert .Contains (t , csiDriver .Spec .VolumeLifecycleModes , storagev1 .VolumeLifecycleEphemeral )
488490}
489491
492+ func TestReconcile_DaemonSetIncludesStartupTolerationWhenUntaintEnabled (t * testing.T ) {
493+ instance := defaultCSIDriverCR ()
494+ r , c := newTestReconciler (t , true , instance )
495+ ctx := context .Background ()
496+
497+ _ , err := r .Reconcile (ctx , instance )
498+ require .NoError (t , err )
499+ require .NoError (t , c .Get (ctx , types.NamespacedName {Name : testName , Namespace : testNamespace }, instance ))
500+
501+ _ , err = r .Reconcile (ctx , instance )
502+ require .NoError (t , err )
503+
504+ ds := & appsv1.DaemonSet {}
505+ require .NoError (t , c .Get (ctx , types.NamespacedName {Name : csiDsName , Namespace : testNamespace }, ds ))
506+ want := untaint .AgentNotReadyEqualToleration ()
507+ assert .True (t , tolerationListContains (ds .Spec .Template .Spec .Tolerations , want ),
508+ "expected %+v in %+v" , want , ds .Spec .Template .Spec .Tolerations )
509+ }
510+
511+ func TestReconcile_DaemonSetOmitsStartupTolerationWhenUntaintDisabled (t * testing.T ) {
512+ instance := defaultCSIDriverCR ()
513+ r , c := newTestReconciler (t , false , instance )
514+ ctx := context .Background ()
515+ _ , err := r .Reconcile (ctx , instance )
516+ require .NoError (t , err )
517+ require .NoError (t , c .Get (ctx , types.NamespacedName {Name : testName , Namespace : testNamespace }, instance ))
518+ _ , err = r .Reconcile (ctx , instance )
519+ require .NoError (t , err )
520+ ds := & appsv1.DaemonSet {}
521+ require .NoError (t , c .Get (ctx , types.NamespacedName {Name : csiDsName , Namespace : testNamespace }, ds ))
522+ want := untaint .AgentNotReadyEqualToleration ()
523+ assert .False (t , tolerationListContains (ds .Spec .Template .Spec .Tolerations , want ))
524+ }
525+
526+ func tolerationListContains (tols []corev1.Toleration , want corev1.Toleration ) bool {
527+ for i := range tols {
528+ if reflect .DeepEqual (tols [i ], want ) {
529+ return true
530+ }
531+ }
532+ return false
533+ }
534+
490535// findCondition returns the condition with the given type, or nil.
491536func findCondition (conditions []metav1.Condition , condType string ) * metav1.Condition {
492537 for i := range conditions {
0 commit comments