@@ -283,6 +283,19 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
283283 var configMaps []v1.ConfigMap
284284
285285 ranContainers := false
286+ // set the ranContainers bool to true if at least one container was successfully started.
287+ setRanContainers := func (r * entities.PlayKubeReport ) {
288+ if ! ranContainers {
289+ for _ , p := range r .Pods {
290+ // If the list of container errors is less then the total number of pod containers then we know it didn't start.
291+ if len (p .ContainerErrors ) < len (p .Containers )+ len (p .InitContainers ) {
292+ ranContainers = true
293+ break
294+ }
295+ }
296+ }
297+ }
298+
286299 // FIXME: both, the service container and the proxies, should ideally
287300 // be _state_ of an object. The Kube code below is quite Spaghetti-code
288301 // which we should refactor at some point to make it easier to extend
@@ -364,7 +377,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
364377
365378 report .Pods = append (report .Pods , r .Pods ... )
366379 validKinds ++
367- ranContainers = true
380+ setRanContainers ( r )
368381 case "DaemonSet" :
369382 var daemonSetYAML v1apps.DaemonSet
370383
@@ -380,7 +393,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
380393
381394 report .Pods = append (report .Pods , r .Pods ... )
382395 validKinds ++
383- ranContainers = true
396+ setRanContainers ( r )
384397 case "Deployment" :
385398 var deploymentYAML v1apps.Deployment
386399
@@ -396,7 +409,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
396409
397410 report .Pods = append (report .Pods , r .Pods ... )
398411 validKinds ++
399- ranContainers = true
412+ setRanContainers ( r )
400413 case "Job" :
401414 var jobYAML v1.Job
402415
@@ -412,7 +425,7 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
412425
413426 report .Pods = append (report .Pods , r .Pods ... )
414427 validKinds ++
415- ranContainers = true
428+ setRanContainers ( r )
416429 case "PersistentVolumeClaim" :
417430 var pvcYAML v1.PersistentVolumeClaim
418431
@@ -473,10 +486,14 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
473486 return nil , fmt .Errorf ("YAML document does not contain any supported kube kind" )
474487 }
475488
489+ if ! options .ServiceContainer {
490+ return report , nil
491+ }
492+
476493 // If we started containers along with a service container, we are
477494 // running inside a systemd unit and need to set the main PID.
478495
479- if options . ServiceContainer && ranContainers {
496+ if ranContainers {
480497 switch len (notifyProxies ) {
481498 case 0 : // Optimization for containers/podman/issues/17345
482499 // No container needs sdnotify, so we can mark the
@@ -509,6 +526,12 @@ func (ic *ContainerEngine) PlayKube(ctx context.Context, body io.Reader, options
509526 }
510527
511528 report .ServiceContainerID = serviceContainer .ID ()
529+ } else if serviceContainer != nil {
530+ // No containers started, make sure to stop the service container.
531+ // Note because the pods still do exists and are not removed by default we cannot remove it.
532+ if err := serviceContainer .StopWithTimeout (0 ); err != nil {
533+ logrus .Errorf ("Failed to stop service container: %v" , err )
534+ }
512535 }
513536
514537 return report , nil
@@ -715,9 +738,6 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
715738 }
716739
717740 p := specgen .NewPodSpecGenerator ()
718- if err != nil {
719- return nil , nil , err
720- }
721741
722742 p , err = entities .ToPodSpecGen (* p , & podOpt )
723743 if err != nil {
@@ -1143,7 +1163,6 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
11431163 }
11441164 for id , err := range podStartErrors {
11451165 playKubePod .ContainerErrors = append (playKubePod .ContainerErrors , fmt .Errorf ("starting container %s: %w" , id , err ).Error ())
1146- fmt .Println (playKubePod .ContainerErrors )
11471166 }
11481167
11491168 // Wait for each proxy to receive a READY message. Use a wait
@@ -1633,7 +1652,7 @@ func getBuildFile(imageName string, cwd string) (string, error) {
16331652 // If the error is not because the file does not exist, take
16341653 // a mulligan and try Dockerfile. If that also fails, return that
16351654 // error
1636- if err != nil && ! os . IsNotExist (err ) {
1655+ if ! errors . Is (err , os . ErrNotExist ) {
16371656 logrus .Error (err .Error ())
16381657 }
16391658
@@ -1643,7 +1662,7 @@ func getBuildFile(imageName string, cwd string) (string, error) {
16431662 return dockerfilePath , nil
16441663 }
16451664 // Strike two
1646- if os . IsNotExist (err ) {
1665+ if errors . Is (err , os . ErrNotExist ) {
16471666 return "" , nil
16481667 }
16491668 return "" , err
0 commit comments