From 19a475906612b603b11fbbf6ee6a6978a4bd9586 Mon Sep 17 00:00:00 2001
From: Daniel J Walsh <dwalsh@redhat.com>
Date: Sun, 9 Jan 2022 10:28:21 -0500
Subject: Use the InfraImage defined in containers.conf

Remove hard code use of the DefaultInfraImage and rely on
getting this from containers.conf.

Fixes: https://github.com/containers/podman/issues/12771

Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
---
 cmd/podman/containers/create.go  |  5 +----
 cmd/podman/pods/create.go        | 10 ++++++++--
 libpod/kube.go                   |  2 +-
 pkg/api/handlers/libpod/pods.go  | 12 ++----------
 test/e2e/containers_conf_test.go | 32 ++++++++++++++++++++++++++++++++
 test/system/200-pod.bats         |  5 ++++-
 6 files changed, 48 insertions(+), 18 deletions(-)

diff --git a/cmd/podman/containers/create.go b/cmd/podman/containers/create.go
index 9610c29dc..694b97fe5 100644
--- a/cmd/podman/containers/create.go
+++ b/cmd/podman/containers/create.go
@@ -388,10 +388,7 @@ func createPodIfNecessary(cmd *cobra.Command, s *specgen.SpecGenerator, netOpts
 	if err != nil {
 		return nil, err
 	}
-	imageName := config.DefaultInfraImage
-	podGen.InfraImage = imageName
-	podGen.InfraContainerSpec = specgen.NewSpecGenerator(imageName, false)
-	podGen.InfraContainerSpec.RawImageName = imageName
+	podGen.InfraContainerSpec = specgen.NewSpecGenerator("", false)
 	podGen.InfraContainerSpec.NetworkOptions = podGen.NetworkOptions
 	err = specgenutil.FillOutSpecGen(podGen.InfraContainerSpec, &infraOpts, []string{})
 	if err != nil {
diff --git a/cmd/podman/pods/create.go b/cmd/podman/pods/create.go
index f844812c2..6a28f7a79 100644
--- a/cmd/podman/pods/create.go
+++ b/cmd/podman/pods/create.go
@@ -71,7 +71,11 @@ func init() {
 	_ = createCommand.RegisterFlagCompletionFunc(nameFlagName, completion.AutocompleteNone)
 
 	infraImageFlagName := "infra-image"
-	flags.StringVar(&infraImage, infraImageFlagName, containerConfig.Engine.InfraImage, "The image of the infra container to associate with the pod")
+	var defInfraImage string
+	if !registry.IsRemote() {
+		defInfraImage = containerConfig.Engine.InfraImage
+	}
+	flags.StringVar(&infraImage, infraImageFlagName, defInfraImage, "The image of the infra container to associate with the pod")
 	_ = createCommand.RegisterFlagCompletionFunc(infraImageFlagName, common.AutocompleteImages)
 
 	podIDFileFlagName := "pod-id-file"
@@ -109,7 +113,9 @@ func create(cmd *cobra.Command, args []string) error {
 		return errors.Wrapf(err, "unable to process labels")
 	}
 
-	imageName = infraImage
+	if cmd.Flag("infra-image").Changed {
+		imageName = infraImage
+	}
 	img := imageName
 	if !createOptions.Infra {
 		if cmd.Flag("no-hosts").Changed {
diff --git a/libpod/kube.go b/libpod/kube.go
index d667616d0..f465fc776 100644
--- a/libpod/kube.go
+++ b/libpod/kube.go
@@ -595,7 +595,7 @@ func containerToV1Container(ctx context.Context, c *Container) (v1.Container, []
 	// pause one and make sure it's in the storage by pulling it down if
 	// missing.
 	if image == "" && c.IsInfra() {
-		image = config.DefaultInfraImage
+		image = c.runtime.config.Engine.InfraImage
 		if _, err := c.runtime.libimageRuntime.Pull(ctx, image, config.PullPolicyMissing, nil); err != nil {
 			return kubeContainer, nil, nil, nil, err
 		}
diff --git a/pkg/api/handlers/libpod/pods.go b/pkg/api/handlers/libpod/pods.go
index 1b29831b4..43b39060b 100644
--- a/pkg/api/handlers/libpod/pods.go
+++ b/pkg/api/handlers/libpod/pods.go
@@ -7,7 +7,6 @@ import (
 	"strings"
 	"time"
 
-	"github.com/containers/common/pkg/config"
 	"github.com/containers/podman/v3/libpod"
 	"github.com/containers/podman/v3/libpod/define"
 	"github.com/containers/podman/v3/pkg/api/handlers"
@@ -62,15 +61,8 @@ func PodCreate(w http.ResponseWriter, r *http.Request) {
 		psg.InfraContainerSpec.Name = psg.InfraName
 		psg.InfraContainerSpec.ConmonPidFile = psg.InfraConmonPidFile
 		psg.InfraContainerSpec.ContainerCreateCommand = psg.InfraCommand
-		imageName := psg.InfraImage
-		rawImageName := psg.InfraImage
-		if imageName == "" {
-			imageName = config.DefaultInfraImage
-			rawImageName = config.DefaultInfraImage
-		}
-		psg.InfraImage = imageName
-		psg.InfraContainerSpec.Image = imageName
-		psg.InfraContainerSpec.RawImageName = rawImageName
+		psg.InfraContainerSpec.Image = psg.InfraImage
+		psg.InfraContainerSpec.RawImageName = psg.InfraImage
 	}
 	podSpecComplete := entities.PodSpec{PodSpecGen: psg}
 	pod, err := generate.MakePod(&podSpecComplete, runtime)
diff --git a/test/e2e/containers_conf_test.go b/test/e2e/containers_conf_test.go
index 2a6f177f2..838221dd5 100644
--- a/test/e2e/containers_conf_test.go
+++ b/test/e2e/containers_conf_test.go
@@ -452,4 +452,36 @@ var _ = Describe("Podman run", func() {
 		Expect(result).Should(Exit(0))
 		Expect(result.OutputToString()).To(ContainSubstring("(default 1234)"))
 	})
+
+	It("podman bad infra_image name in containers.conf", func() {
+		infra1 := "i.do/not/exist:image"
+		infra2 := "i.still.do/not/exist:image"
+		errorString := "initializing source docker://" + infra1
+		error2String := "initializing source docker://" + infra2
+		configPath := filepath.Join(podmanTest.TempDir, "containers.conf")
+		os.Setenv("CONTAINERS_CONF", configPath)
+
+		containersConf := []byte("[engine]\ninfra_image=\"" + infra1 + "\"")
+		err = ioutil.WriteFile(configPath, containersConf, os.ModePerm)
+		Expect(err).To(BeNil())
+
+		if IsRemote() {
+			podmanTest.RestartRemoteService()
+		}
+
+		result := podmanTest.Podman([]string{"pod", "create", "--infra-image", infra2})
+		result.WaitWithDefaultTimeout()
+		Expect(result).Should(Exit(125))
+		Expect(result.ErrorToString()).To(ContainSubstring(error2String))
+
+		result = podmanTest.Podman([]string{"pod", "create"})
+		result.WaitWithDefaultTimeout()
+		Expect(result).Should(Exit(125))
+		Expect(result.ErrorToString()).To(ContainSubstring(errorString))
+
+		result = podmanTest.Podman([]string{"create", "--pod", "new:pod1", ALPINE})
+		result.WaitWithDefaultTimeout()
+		Expect(result).Should(Exit(125))
+		Expect(result.ErrorToString()).To(ContainSubstring(errorString))
+	})
 })
diff --git a/test/system/200-pod.bats b/test/system/200-pod.bats
index 57d052ce2..6abdf9779 100644
--- a/test/system/200-pod.bats
+++ b/test/system/200-pod.bats
@@ -62,8 +62,8 @@ function teardown() {
 
 
 @test "podman pod create - custom infra image" {
+    skip_if_remote "CONTAINERS_CONF only effects server side"
     image="i.do/not/exist:image"
-
     tmpdir=$PODMAN_TMPDIR/pod-test
     run mkdir -p $tmpdir
     containersconf=$tmpdir/containers.conf
@@ -77,6 +77,9 @@ EOF
 
     CONTAINERS_CONF=$containersconf run_podman 125 pod create
     is "$output" ".*initializing source docker://$image:.*"
+
+    CONTAINERS_CONF=$containersconf run_podman 125 create --pod new:test $IMAGE
+    is "$output" ".*initializing source docker://$image:.*"
 }
 
 @test "podman pod - communicating between pods" {
-- 
cgit v1.2.3-54-g00ecf