Skip to content

Commit

Permalink
Merge pull request containers#13286 from flouthoc/kube-build-false-de…
Browse files Browse the repository at this point in the history
…fault

kube: honor `--build=false` if specified.
  • Loading branch information
openshift-merge-robot committed Feb 21, 2022
2 parents c3a9505 + 9ce61e3 commit a746a61
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 5 deletions.
6 changes: 5 additions & 1 deletion cmd/podman/play/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type playKubeOptionsWrapper struct {
TLSVerifyCLI bool
CredentialsCLI string
StartCLI bool
BuildCLI bool
}

var (
Expand Down Expand Up @@ -117,7 +118,7 @@ func init() {
_ = kubeCmd.RegisterFlagCompletionFunc(configmapFlagName, completion.AutocompleteDefault)

buildFlagName := "build"
flags.BoolVar(&kubeOptions.Build, buildFlagName, false, "Build all images in a YAML (given Containerfiles exist)")
flags.BoolVar(&kubeOptions.BuildCLI, buildFlagName, false, "Build all images in a YAML (given Containerfiles exist)")
}

if !registry.IsRemote() {
Expand All @@ -138,6 +139,9 @@ func kube(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("start") {
kubeOptions.Start = types.NewOptionalBool(kubeOptions.StartCLI)
}
if cmd.Flags().Changed("build") {
kubeOptions.Build = types.NewOptionalBool(kubeOptions.BuildCLI)
}
if kubeOptions.Authfile != "" {
if _, err := os.Stat(kubeOptions.Authfile); err != nil {
return err
Expand Down
5 changes: 3 additions & 2 deletions docs/source/markdown/podman-play-kube.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ like:
```

The build will consider `foobar` to be the context directory for the build. If there is an image in local storage
called `foobar`, the image will not be built unless the `--build` flag is used.
called `foobar`, the image will not be built unless the `--build` flag is used. Use `--build=false` to completely
disable builds.

`Kubernetes ConfigMap`

Expand Down Expand Up @@ -115,7 +116,7 @@ environment variable. `export REGISTRY_AUTH_FILE=path`

#### **--build**

Build images even if they are found in the local storage.
Build images even if they are found in the local storage. Use `--build=false` to completely disable builds.

#### **--cert-dir**=*path*

Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/entities/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type PlayKubeOptions struct {
// Authfile - path to an authentication file.
Authfile string
// Indicator to build all images with Containerfile or Dockerfile
Build bool
Build types.OptionalBool
// CertDir - to a directory containing TLS certifications and keys.
CertDir string
// Down indicates whether to bring contents of a yaml file "down"
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/infra/abi/play.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ func (ic *ContainerEngine) getImageAndLabelInfo(ctx context.Context, cwd string,
if err != nil {
return nil, nil, err
}
if (len(buildFile) > 0 && !existsLocally) || (len(buildFile) > 0 && options.Build) {
if (len(buildFile) > 0) && ((!existsLocally && options.Build != types.OptionalBoolFalse) || (options.Build == types.OptionalBoolTrue)) {
buildOpts := new(buildahDefine.BuildOptions)
commonOpts := new(buildahDefine.CommonBuildOptions)
buildOpts.ConfigureNetwork = buildahDefine.NetworkDefault
Expand Down
47 changes: 47 additions & 0 deletions test/e2e/play_build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,53 @@ LABEL marge=mom
Expect(inspectData[0].Config.Labels).To(HaveKeyWithValue("marge", "mom"))
})

It("Do not build image at all if --build=false", func() {
// Setup
yamlDir := filepath.Join(tempdir, RandomString(12))
err := os.Mkdir(yamlDir, 0755)
Expect(err).To(BeNil(), "mkdir "+yamlDir)
err = writeYaml(testYAML, filepath.Join(yamlDir, "top.yaml"))
Expect(err).To(BeNil())

// build an image called foobar but make sure it doesn't have
// the same label as the yaml buildfile, so we can check that
// the image is NOT rebuilt.
err = writeYaml(prebuiltImage, filepath.Join(yamlDir, "Containerfile"))
Expect(err).To(BeNil())

app1Dir := filepath.Join(yamlDir, "foobar")
err = os.Mkdir(app1Dir, 0755)
Expect(err).To(BeNil())
err = writeYaml(playBuildFile, filepath.Join(app1Dir, "Containerfile"))
Expect(err).To(BeNil())
// Write a file to be copied
err = writeYaml(copyFile, filepath.Join(app1Dir, "copyfile"))
Expect(err).To(BeNil())

// Switch to temp dir and restore it afterwards
cwd, err := os.Getwd()
Expect(err).To(BeNil())
Expect(os.Chdir(yamlDir)).To(BeNil())
defer func() { (Expect(os.Chdir(cwd)).To(BeNil())) }()

// Build the image into the local store
build := podmanTest.Podman([]string{"build", "-t", "foobar", "-f", "Containerfile"})
build.WaitWithDefaultTimeout()
Expect(build).Should(Exit(0))

session := podmanTest.Podman([]string{"play", "kube", "--build=false", "top.yaml"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

inspect := podmanTest.Podman([]string{"container", "inspect", "top_pod-foobar"})
inspect.WaitWithDefaultTimeout()
Expect(inspect).Should(Exit(0))
inspectData := inspect.InspectContainerToJSON()
Expect(len(inspectData)).To(BeNumerically(">", 0))
Expect(inspectData[0].Config.Labels).To(Not(HaveKey("homer")))
Expect(inspectData[0].Config.Labels).To(HaveKeyWithValue("marge", "mom"))
})

It("--build should override image in store", func() {
// Setup
yamlDir := filepath.Join(tempdir, RandomString(12))
Expand Down

0 comments on commit a746a61

Please sign in to comment.