@@ -420,20 +420,28 @@ func (w *executor) removeContainersInNamespace(ctx context.Context, includes, ex
420420}
421421
422422func (w * executor ) runAndWaitContainers (ctx context.Context , containers ... model.ContainerDefinition ) error {
423+ group := sync .NewGroup (0 )
424+
423425 for item := range containers {
424426 // fix: Implicit memory aliasing in for loop
425427 c := containers [item ]
426428 w .Infof ("start and wait container %s" , c .Name )
427- err := w .runtime .CreateContainer (ctx , toRuntimeContainer (& c , model .RestartPolicyNever ), true )
428- if err != nil {
429- return err
430- }
431- err = w .runtime .RemoveContainer (ctx , c .Name )
432- if err != nil {
433- return err
434- }
429+ probe := w .loadContainerProbe (c .StartupProbe )
430+ group .Go (func () error {
431+ return wait .PollImmediate (startProbeCheckInterval , time .Duration (probe .ProbeTimeout )* time .Second , func () (bool , error ) {
432+ err := w .runtime .CreateContainer (ctx , toRuntimeContainer (& c , model .RestartPolicyNever ), true )
433+ if err != nil {
434+ return false , err
435+ }
436+ err = w .runtime .RemoveContainer (ctx , c .Name )
437+ if err != nil {
438+ return false , err
439+ }
440+ return true , nil
441+ })
442+ })
435443 }
436- return nil
444+ return group . WaitResult ()
437445}
438446
439447const (
@@ -487,47 +495,57 @@ func (w *executor) loadContainerProbe(probe *model.ContainerProbe) *model.Contai
487495}
488496
489497func (w * executor ) runContainers (ctx context.Context , containers ... model.ContainerDefinition ) error {
498+ group := sync .NewGroup (0 )
499+
490500 for item := range containers {
491501 // fix: Implicit memory aliasing in for loop
492502 c := containers [item ]
493- updatePolicyMode := model .UpdatePolicyModeRestart
494- if ! GetForceUpdate (ctx ) && c .UpdatePolicy != nil && c .UpdatePolicy .OnNoChange != "" {
495- updatePolicyMode = c .UpdatePolicy .OnNoChange
496- }
497- mc := toRuntimeContainer (& c , model .RestartPolicyAlways )
498- switch updatePolicyMode {
499- case model .UpdatePolicyModeSkip :
500- can , err := canSkipRestart (ctx , w .runtime , mc )
501- if err != nil {
502- return err
503- }
504- if can {
505- w .Infof ("update container %s and skip restart" , c .Name )
506- err = w .runtime .UpdateContainer (ctx , mc , & client.ContainerUpdateOptions {})
507- if err != nil {
508- return err
503+ probe := w .loadContainerProbe (c .StartupProbe )
504+ group .Go (func () error {
505+ return wait .PollImmediate (startProbeCheckInterval , time .Duration (probe .ProbeTimeout )* time .Second , func () (bool , error ) {
506+ w .Infof ("try to handle container running process" , c .Name )
507+
508+ updatePolicyMode := model .UpdatePolicyModeRestart
509+ if ! GetForceUpdate (ctx ) && c .UpdatePolicy != nil && c .UpdatePolicy .OnNoChange != "" {
510+ updatePolicyMode = c .UpdatePolicy .OnNoChange
509511 }
510- continue
511- }
512- fallthrough
513- case model .UpdatePolicyModeRestart :
514- if err := w .doContainerPreRestart (ctx , c ); err != nil {
515- return fmt .Errorf ("pre-restart container %s: %w" , c .Name , err )
516- }
517- if _ , err := w .runtime .GetContainer (ctx , c .Name ); err == nil || ! errdefs .IsNotFound (err ) {
518- w .Infof ("remove container %s from containerd" , c .Name )
519- _ = w .runtime .RemoveContainer (ctx , c .Name )
520- }
521- w .Infof ("start and run container %s" , c .Name )
522- err := w .runtime .CreateContainer (ctx , mc , false )
523- if err != nil {
524- return err
525- }
526- default :
527- return fmt .Errorf ("unknown update policy mode: %s" , updatePolicyMode )
528- }
512+ mc := toRuntimeContainer (& c , model .RestartPolicyAlways )
513+ switch updatePolicyMode {
514+ case model .UpdatePolicyModeSkip :
515+ can , err := canSkipRestart (ctx , w .runtime , mc )
516+ if err != nil {
517+ return false , err
518+ }
519+ if can {
520+ w .Infof ("update container %s and skip restart" , c .Name )
521+ err = w .runtime .UpdateContainer (ctx , mc , & client.ContainerUpdateOptions {})
522+ if err != nil {
523+ return false , err
524+ }
525+ return true , nil
526+ }
527+ fallthrough
528+ case model .UpdatePolicyModeRestart :
529+ if err := w .doContainerPreRestart (ctx , c ); err != nil {
530+ return false , fmt .Errorf ("pre-restart container %s: %w" , c .Name , err )
531+ }
532+ if _ , err := w .runtime .GetContainer (ctx , c .Name ); err == nil || ! errdefs .IsNotFound (err ) {
533+ w .Infof ("remove container %s from containerd" , c .Name )
534+ _ = w .runtime .RemoveContainer (ctx , c .Name )
535+ }
536+ w .Infof ("start and run container %s" , c .Name )
537+ err := w .runtime .CreateContainer (ctx , mc , false )
538+ if err != nil {
539+ return false , err
540+ }
541+ default :
542+ return false , fmt .Errorf ("unknown update policy mode: %s" , updatePolicyMode )
543+ }
544+ return true , nil
545+ })
546+ })
529547 }
530- return nil
548+ return group . WaitResult ()
531549}
532550
533551func (w * executor ) removeAllInNamespace (ctx context.Context ) error {
0 commit comments