@@ -2272,56 +2272,41 @@ 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-
2301- var originalDaemonSet * appsv1.DaemonSet
2281+ t .Logf ("Updating daemonset %s/%s with args %v" , NVSentinelNamespace , daemonsetName , args )
23022282
23032283 err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
23042284 daemonSet := & appsv1.DaemonSet {}
23052285 if err := client .Resources ().Get (ctx , daemonsetName , NVSentinelNamespace , daemonSet ); err != nil {
23062286 return err
23072287 }
23082288
2309- if originalDaemonSet == nil {
2310- originalDaemonSet = daemonSet .DeepCopy ()
2311- }
2289+ containers := daemonSet .Spec .Template .Spec .Containers
2290+ containerFound := false
2291+
2292+ for i := range containers {
2293+ if containers [i ].Name == containerName {
2294+ setArgsOnContainer (t , & containers [i ], args )
2295+
2296+ containerFound = true
23122297
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 )
23172298 break
23182299 }
23192300 }
23202301
2302+ if ! containerFound {
2303+ return fmt .Errorf ("container %q not found in daemonset %s/%s" , containerName , NVSentinelNamespace , daemonsetName )
2304+ }
2305+
23212306 return client .Resources ().Update (ctx , daemonSet )
23222307 })
23232308 if err != nil {
2324- return nil , err
2309+ return err
23252310 }
23262311
23272312 t .Logf ("Waiting for daemonset %s/%s rollout to complete" , NVSentinelNamespace , daemonsetName )
@@ -2330,41 +2315,130 @@ func UpdateDaemonSetProcessingStrategy(ctx context.Context, t *testing.T,
23302315 t .Logf ("Waiting 10 seconds for daemonset pods to start" )
23312316 time .Sleep (10 * time .Second )
23322317
2333- return originalDaemonSet , nil
2318+ return nil
23342319}
23352320
2336- func RestoreDaemonSet (ctx context.Context , t * testing.T , client klient.Client ,
2337- originalDaemonSet * appsv1.DaemonSet , daemonsetName string ,
2338- ) error {
2321+ func RemoveDaemonSetArgs (ctx context.Context , t * testing.T , client klient.Client ,
2322+ daemonsetName string ,
2323+ containerName string , args map [string ]string ,
2324+ ) {
23392325 t .Helper ()
23402326
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 )
2327+ t .Logf ("Removing args %v from daemonset %s/%s" , args , NVSentinelNamespace , daemonsetName )
23472328
23482329 err := retry .RetryOnConflict (retry .DefaultRetry , func () error {
23492330 daemonSet := & appsv1.DaemonSet {}
23502331 if err := client .Resources ().Get (ctx , daemonsetName , NVSentinelNamespace , daemonSet ); err != nil {
23512332 return err
23522333 }
23532334
2354- daemonSet .Spec .Template .Spec .Containers = originalDaemonSet .Spec .Template .Spec .Containers
2335+ containers := daemonSet .Spec .Template .Spec .Containers
2336+ containerFound := false
2337+
2338+ for i := range containers {
2339+ if containers [i ].Name == containerName {
2340+ removeArgsFromContainer (& containers [i ], args )
2341+
2342+ containerFound = true
2343+
2344+ break
2345+ }
2346+ }
2347+
2348+ if ! containerFound {
2349+ return fmt .Errorf ("container %q not found in daemonset %s/%s" , containerName , NVSentinelNamespace , daemonsetName )
2350+ }
23552351
23562352 return client .Resources ().Update (ctx , daemonSet )
23572353 })
2358- if err != nil {
2359- return err
2360- }
2354+ require .NoError (t , err , "failed to remove args from daemonset %s/%s" , NVSentinelNamespace , daemonsetName )
23612355
23622356 t .Logf ("Waiting for daemonset %s/%s rollout to complete after restoration" , NVSentinelNamespace , daemonsetName )
23632357 waitForDaemonSetRollout (ctx , t , client , daemonsetName )
23642358
23652359 t .Log ("DaemonSet restored successfully" )
2360+ }
23662361
2367- return nil
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+ }
23682442}
23692443
23702444func GetDaemonSetPodOnWorkerNode (ctx context.Context , t * testing.T , client klient.Client ,
0 commit comments