Merge pull request #13286 from flouthoc/kube-build-false-default

kube: honor `--build=false` if specified.
This commit is contained in:
OpenShift Merge Robot
2022-02-21 15:33:39 -05:00
committed by GitHub
5 changed files with 57 additions and 5 deletions

View File

@ -27,6 +27,7 @@ type playKubeOptionsWrapper struct {
TLSVerifyCLI bool TLSVerifyCLI bool
CredentialsCLI string CredentialsCLI string
StartCLI bool StartCLI bool
BuildCLI bool
} }
var ( var (
@ -117,7 +118,7 @@ func init() {
_ = kubeCmd.RegisterFlagCompletionFunc(configmapFlagName, completion.AutocompleteDefault) _ = kubeCmd.RegisterFlagCompletionFunc(configmapFlagName, completion.AutocompleteDefault)
buildFlagName := "build" 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() { if !registry.IsRemote() {
@ -138,6 +139,9 @@ func kube(cmd *cobra.Command, args []string) error {
if cmd.Flags().Changed("start") { if cmd.Flags().Changed("start") {
kubeOptions.Start = types.NewOptionalBool(kubeOptions.StartCLI) kubeOptions.Start = types.NewOptionalBool(kubeOptions.StartCLI)
} }
if cmd.Flags().Changed("build") {
kubeOptions.Build = types.NewOptionalBool(kubeOptions.BuildCLI)
}
if kubeOptions.Authfile != "" { if kubeOptions.Authfile != "" {
if _, err := os.Stat(kubeOptions.Authfile); err != nil { if _, err := os.Stat(kubeOptions.Authfile); err != nil {
return err return err

View File

@ -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 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` `Kubernetes ConfigMap`
@ -115,7 +116,7 @@ environment variable. `export REGISTRY_AUTH_FILE=path`
#### **--build** #### **--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* #### **--cert-dir**=*path*

View File

@ -11,7 +11,7 @@ type PlayKubeOptions struct {
// Authfile - path to an authentication file. // Authfile - path to an authentication file.
Authfile string Authfile string
// Indicator to build all images with Containerfile or Dockerfile // Indicator to build all images with Containerfile or Dockerfile
Build bool Build types.OptionalBool
// CertDir - to a directory containing TLS certifications and keys. // CertDir - to a directory containing TLS certifications and keys.
CertDir string CertDir string
// Down indicates whether to bring contents of a yaml file "down" // Down indicates whether to bring contents of a yaml file "down"

View File

@ -486,7 +486,7 @@ func (ic *ContainerEngine) getImageAndLabelInfo(ctx context.Context, cwd string,
if err != nil { if err != nil {
return nil, nil, err 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) buildOpts := new(buildahDefine.BuildOptions)
commonOpts := new(buildahDefine.CommonBuildOptions) commonOpts := new(buildahDefine.CommonBuildOptions)
buildOpts.ConfigureNetwork = buildahDefine.NetworkDefault buildOpts.ConfigureNetwork = buildahDefine.NetworkDefault

View File

@ -212,6 +212,53 @@ LABEL marge=mom
Expect(inspectData[0].Config.Labels).To(HaveKeyWithValue("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() { It("--build should override image in store", func() {
// Setup // Setup
yamlDir := filepath.Join(tempdir, RandomString(12)) yamlDir := filepath.Join(tempdir, RandomString(12))