@@ -2272,34 +2272,16 @@ func waitForDaemonSetRollout(ctx context.Context, t *testing.T, client klient.Cl
22722272 t .Logf ("DaemonSet %s/%s rollout completed successfully" , NVSentinelNamespace , name )
22732273}
22742274
2275- // updateContainerProcessingStrategy updates the processing strategy argument for a specific container.
2276- func updateContainerProcessingStrategy (container * v1.Container ) {
2277- processingStrategyArg := "--processing-strategy=STORE_ONLY"
2278-
2279- for j , arg := range container .Args {
2280- if strings .HasPrefix (arg , "--processing-strategy=" ) {
2281- container .Args [j ] = processingStrategyArg
2282- return
2283- }
2284-
2285- if arg == "--processing-strategy" && j + 1 < len (container .Args ) {
2286- container .Args [j + 1 ] = "STORE_ONLY"
2287- return
2288- }
2289- }
2290-
2291- container .Args = append (container .Args , processingStrategyArg )
2292- }
2293-
2294- // UpdateDaemonSetProcessingStrategy updates the daemonset to use STORE_ONLY processing strategy.
2295- func UpdateDaemonSetProcessingStrategy (ctx context.Context , t * testing.T ,
2296- client klient.Client , daemonsetName string , containerName string ) (* appsv1.DaemonSet , error ) {
2275+ // UpdateDaemonSetArgs updates the daemonset with the specified arguments and complete the rollout.
2276+ func UpdateDaemonSetArgs (ctx context.Context , t * testing.T ,
2277+ client klient.Client , daemonsetName string , containerName string ,
2278+ args map [string ]string ) error {
22972279 t .Helper ()
22982280
2299- t .Logf ("Updating daemonset %s/%s to use STORE_ONLY processing strategy" , NVSentinelNamespace , daemonsetName )
2300-
23012281 var originalDaemonSet * appsv1.DaemonSet
23022282
2283+ t .Logf ("Updating daemonset %s/%s with args %v" , NVSentinelNamespace , daemonsetName , args )
2284+
23032285 err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
23042286 daemonSet := & appsv1.DaemonSet {}
23052287 if err := client .Resources ().Get (ctx , daemonsetName , NVSentinelNamespace , daemonSet ); err != nil {
@@ -2310,18 +2292,23 @@ func UpdateDaemonSetProcessingStrategy(ctx context.Context, t *testing.T,
23102292 originalDaemonSet = daemonSet .DeepCopy ()
23112293 }
23122294
2313- for i := range daemonSet .Spec .Template .Spec .Containers {
2314- container := & daemonSet .Spec .Template .Spec .Containers [i ]
2315- if container .Name == containerName {
2316- updateContainerProcessingStrategy (container )
2295+ containers := daemonSet .Spec .Template .Spec .Containers
2296+ containerFound := false
2297+ for i := range containers {
2298+ if containers [i ].Name == containerName {
2299+ setArgsOnContainer (t , & containers [i ], args )
2300+ containerFound = true
23172301 break
23182302 }
23192303 }
2304+ if ! containerFound {
2305+ return fmt .Errorf ("container %q not found in daemonset %s/%s" , containerName , NVSentinelNamespace , daemonsetName )
2306+ }
23202307
23212308 return client .Resources ().Update (ctx , daemonSet )
23222309 })
23232310 if err != nil {
2324- return nil , err
2311+ return err
23252312 }
23262313
23272314 t .Logf ("Waiting for daemonset %s/%s rollout to complete" , NVSentinelNamespace , daemonsetName )
@@ -2330,34 +2317,39 @@ func UpdateDaemonSetProcessingStrategy(ctx context.Context, t *testing.T,
23302317 t .Logf ("Waiting 10 seconds for daemonset pods to start" )
23312318 time .Sleep (10 * time .Second )
23322319
2333- return originalDaemonSet , nil
2320+ return nil
23342321}
23352322
2336- func RestoreDaemonSet (ctx context.Context , t * testing.T , client klient.Client ,
2337- originalDaemonSet * appsv1.DaemonSet , daemonsetName string ,
2323+ func RemoveDaemonSetArgs (ctx context.Context , t * testing.T , client klient.Client ,
2324+ daemonsetName string ,
2325+ containerName string , args map [string ]string ,
23382326) error {
23392327 t .Helper ()
23402328
2341- if originalDaemonSet == nil {
2342- t .Log ("No original daemonset to restore, skipping" )
2343- return nil
2344- }
2345-
2346- t .Logf ("Restoring daemonset %s/%s to original state" , NVSentinelNamespace , daemonsetName )
2329+ t .Logf ("Removing args %v from daemonset %s/%s" , args , NVSentinelNamespace , daemonsetName )
23472330
23482331 err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
23492332 daemonSet := & appsv1.DaemonSet {}
23502333 if err := client .Resources ().Get (ctx , daemonsetName , NVSentinelNamespace , daemonSet ); err != nil {
23512334 return err
23522335 }
23532336
2354- daemonSet .Spec .Template .Spec .Containers = originalDaemonSet .Spec .Template .Spec .Containers
2337+ containers := daemonSet .Spec .Template .Spec .Containers
2338+ containerFound := false
2339+ for i := range containers {
2340+ if containers [i ].Name == containerName {
2341+ removeArgsFromContainer (& containers [i ], args )
2342+ containerFound = true
2343+ break
2344+ }
2345+ }
2346+ if ! containerFound {
2347+ return fmt .Errorf ("container %q not found in daemonset %s/%s" , containerName , NVSentinelNamespace , daemonsetName )
2348+ }
23552349
23562350 return client .Resources ().Update (ctx , daemonSet )
23572351 })
2358- if err != nil {
2359- return err
2360- }
2352+ require .NoError (t , err , "failed to remove args from daemonset %s/%s" , NVSentinelNamespace , daemonsetName )
23612353
23622354 t .Logf ("Waiting for daemonset %s/%s rollout to complete after restoration" , NVSentinelNamespace , daemonsetName )
23632355 waitForDaemonSetRollout (ctx , t , client , daemonsetName )
@@ -2367,6 +2359,88 @@ func RestoreDaemonSet(ctx context.Context, t *testing.T, client klient.Client,
23672359 return nil
23682360}
23692361
2362+ // tryUpdateExistingArg attempts to update an existing argument at position j.
2363+ // Returns true if the argument was found and updated.
2364+ func tryUpdateExistingArg (container * v1.Container , j int , flag , value string ) bool {
2365+ existingArg := container .Args [j ]
2366+
2367+ // Match --flag=value style
2368+ if strings .HasPrefix (existingArg , flag + "=" ) {
2369+ if value != "" {
2370+ container .Args [j ] = flag + "=" + value
2371+ } else {
2372+ container .Args [j ] = flag
2373+ }
2374+
2375+ return true
2376+ }
2377+
2378+ // Match --flag or --flag value style
2379+ if existingArg == flag {
2380+ if value != "" {
2381+ if j + 1 < len (container .Args ) && ! strings .HasPrefix (container .Args [j + 1 ], "-" ) {
2382+ container .Args [j + 1 ] = value
2383+ } else {
2384+ container .Args = append (container .Args [:j + 1 ], append ([]string {value }, container .Args [j + 1 :]... )... )
2385+ }
2386+ }
2387+
2388+ return true
2389+ }
2390+
2391+ return false
2392+ }
2393+
2394+ func setArgsOnContainer (t * testing.T , container * v1.Container , args map [string ]string ) {
2395+ t .Helper ()
2396+ t .Logf ("Setting args %v on container %s" , args , container .Name )
2397+
2398+ for flag , value := range args {
2399+ found := false
2400+
2401+ for j := 0 ; j < len (container .Args ); j ++ {
2402+ if tryUpdateExistingArg (container , j , flag , value ) {
2403+ found = true
2404+ break
2405+ }
2406+ }
2407+
2408+ if ! found {
2409+ if value != "" {
2410+ container .Args = append (container .Args , flag + "=" + value )
2411+ } else {
2412+ container .Args = append (container .Args , flag )
2413+ }
2414+ }
2415+ }
2416+ }
2417+
2418+ func removeArgsFromContainer (container * v1.Container , args map [string ]string ) {
2419+ for flag := range args {
2420+ for j := 0 ; j < len (container .Args ); j ++ {
2421+ existingArg := container .Args [j ]
2422+
2423+ // Match --flag=value style
2424+ if strings .HasPrefix (existingArg , flag + "=" ) {
2425+ container .Args = append (container .Args [:j ], container .Args [j + 1 :]... )
2426+ break
2427+ }
2428+
2429+ // Match --flag or --flag value style
2430+
2431+ if existingArg == flag {
2432+ if j + 1 < len (container .Args ) && ! strings .HasPrefix (container .Args [j + 1 ], "-" ) {
2433+ container .Args = append (container .Args [:j ], container .Args [j + 2 :]... )
2434+ } else {
2435+ container .Args = append (container .Args [:j ], container .Args [j + 1 :]... )
2436+ }
2437+
2438+ break
2439+ }
2440+ }
2441+ }
2442+ }
2443+
23702444func GetDaemonSetPodOnWorkerNode (ctx context.Context , t * testing.T , client klient.Client ,
23712445 daemonsetName string , podNamePattern string ) (* v1.Pod , error ) {
23722446 t .Helper ()
0 commit comments