Skip to content

Commit

Permalink
build: allow using cache explicitly with --squash-all using --layers
Browse files Browse the repository at this point in the history
Buildah already supports using `--layers` with `--squash` after containers/buildah#3674
if user wants to do so hence podman must honor similar configuration
in `--squash-all` behaviour if user wants to using cache.

PS: We cannot alter behaviour of `podman build --squash` for
docker-compat reasons hence this feature can be easily supported by
`--squash-all`.

Closes: containers/buildah#4011

Signed-off-by: Aditya R <arajan@redhat.com>
  • Loading branch information
flouthoc committed May 26, 2022
1 parent b730e73 commit dc8b95a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cmd/podman/images/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,8 @@ func buildFlags(cmd *cobra.Command) {
// build executes the build command.
func build(cmd *cobra.Command, args []string) error {
if (cmd.Flags().Changed("squash") && cmd.Flags().Changed("layers")) ||
(cmd.Flags().Changed("squash-all") && cmd.Flags().Changed("layers")) ||
(cmd.Flags().Changed("squash-all") && cmd.Flags().Changed("squash")) {
return errors.New("cannot specify --squash, --squash-all and --layers options together")
return errors.New("cannot specify --squash with --layers and --squash-all with --squash")
}

if cmd.Flag("output").Changed && registry.IsRemote() {
Expand Down Expand Up @@ -418,7 +417,13 @@ func buildFlagsWrapperToOptions(c *cobra.Command, contextDir string, flags *buil
// Squash-all invoked, squash both new and old layers into one.
if c.Flags().Changed("squash-all") {
flags.Squash = true
flags.Layers = false
if !c.Flags().Changed("layers") {
// Buildah supports using layers and --squash togather
// after https://github.com/containers/buildah/pull/3674
// so podman must honor if user wants to still use layers
// with --squash-all.
flags.Layers = false
}
}

var stdin io.Reader
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,32 @@ var _ = Describe("Podman build", func() {
Expect(session).Should(Exit(0))
})

It("podman build verify explicit cache use with squash-all and --layers", func() {
session := podmanTest.Podman([]string{"build", "--pull-never", "-f", "build/squash/Dockerfile.squash-c", "--squash-all", "--layers", "-t", "test-squash-d:latest", "build/squash"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))

session = podmanTest.Podman([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-d"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// Check for one layers
Expect(strings.Fields(session.OutputToString())).To(HaveLen(1))

// Second build must use last squashed build from cache
session = podmanTest.Podman([]string{"build", "--pull-never", "-f", "build/squash/Dockerfile.squash-c", "--squash-all", "--layers", "-t", "test", "build/squash"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// Test if entire build is used from cache
Expect(session.OutputToString()).To(ContainSubstring("Using cache"))

session = podmanTest.Podman([]string{"inspect", "--format", "{{.RootFS.Layers}}", "test-squash-d"})
session.WaitWithDefaultTimeout()
Expect(session).Should(Exit(0))
// Check for one layers
Expect(strings.Fields(session.OutputToString())).To(HaveLen(1))

})

It("podman build Containerfile locations", func() {
// Given
// Switch to temp dir and restore it afterwards
Expand Down

0 comments on commit dc8b95a

Please sign in to comment.