Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion cmd/podman/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

var (
Expand All @@ -27,6 +28,7 @@ var (
fromAndBudValues buildahcli.FromAndBudResults
userNSValues buildahcli.UserNSResults
namespaceValues buildahcli.NameSpaceResults
podBuildValues cliconfig.PodmanBuildResults

_buildCommand = &cobra.Command{
Use: "build [flags] CONTEXT",
Expand All @@ -40,6 +42,7 @@ var (
buildCommand.FromAndBudResults = &fromAndBudValues
buildCommand.LayerResults = &layerValues
buildCommand.NameSpaceResults = &namespaceValues
buildCommand.PodmanBuildResults = &podBuildValues
buildCommand.Remote = remoteclient
return buildCmd(&buildCommand)
},
Expand Down Expand Up @@ -73,15 +76,30 @@ func init() {
logrus.Error("unable to set force-rm flag to true")
}
flag.DefValue = "true"
podmanBuildFlags := GetPodmanBuildFlags(&podBuildValues)
flag = podmanBuildFlags.Lookup("squash-all")
if err := flag.Value.Set("false"); err != nil {
logrus.Error("unable to set squash-all flag to false")
}

flag.DefValue = "true"
fromAndBugFlags := buildahcli.GetFromAndBudFlags(&fromAndBudValues, &userNSValues, &namespaceValues)

flags.AddFlagSet(&budFlags)
flags.AddFlagSet(&layerFlags)
flags.AddFlagSet(&fromAndBugFlags)
flags.AddFlagSet(&layerFlags)
flags.AddFlagSet(&podmanBuildFlags)
markFlagHidden(flags, "signature-policy")
}

// GetPodmanBuildFlags flags used only by `podman build` and not by
// `buildah bud`.
func GetPodmanBuildFlags(flags *cliconfig.PodmanBuildResults) pflag.FlagSet {
fs := pflag.FlagSet{}
fs.BoolVar(&flags.SquashAll, "squash-all", false, "Squash all layers into a single layer.")
return fs
}

func getContainerfiles(files []string) []string {
var containerfiles []string
for _, f := range files {
Expand Down Expand Up @@ -119,6 +137,12 @@ func getNsValues(c *cliconfig.BuildValues) ([]buildah.NamespaceOption, error) {
}

func buildCmd(c *cliconfig.BuildValues) error {
if (c.Flags().Changed("squash") && c.Flags().Changed("layers")) ||
(c.Flags().Changed("squash-all") && c.Flags().Changed("layers")) ||
(c.Flags().Changed("squash-all") && c.Flags().Changed("squash")) {
return fmt.Errorf("cannot specify squash, squash-all and layers options together")
}

// The following was taken directly from containers/buildah/cmd/bud.go
// TODO Find a away to vendor more of this in rather than copy from bud
output := ""
Expand Down Expand Up @@ -293,6 +317,22 @@ func buildCmd(c *cliconfig.BuildValues) error {
Volumes: c.Volumes,
}

// `buildah bud --layers=false` acts like `docker build --squash` does.
// That is all of the new layers created during the build process are
// condensed into one, any layers present prior to this build are retained
// without condensing. `buildah bud --squash` squashes both new and old
// layers down into one. Translate Podman commands into Buildah.
// Squash invoked, retain old layers, squash new layers into one.
if c.Flags().Changed("squash") && c.Squash {
c.Squash = false
layers = false
}
// Squash-all invoked, squash both new and old layers into one.
if c.Flags().Changed("squash-all") {
c.Squash = true
layers = false
}

options := imagebuildah.BuildOptions{
CommonBuildOpts: &buildOpts,
AdditionalTags: tags,
Expand Down
9 changes: 8 additions & 1 deletion cmd/podman/cliconfig/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ type RunValues struct {
PodmanCommand
}

// PodmanBuildResults represents the results for Podman Build flags
// that are unique to Podman.
type PodmanBuildResults struct {
SquashAll bool
}

type BuildValues struct {
PodmanCommand
*buildahcli.BudResults
*buildahcli.UserNSResults
*buildahcli.FromAndBudResults
*buildahcli.NameSpaceResults
*buildahcli.LayerResults
*buildahcli.NameSpaceResults
*PodmanBuildResults
}

type CpValues struct {
Expand Down
1 change: 1 addition & 0 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,7 @@ _podman_build() {
-q
--rm
--squash
--squash-all
--tls-verify
"

Expand Down
5 changes: 5 additions & 0 deletions docs/podman-build.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,11 @@ If you omit the unit, the system uses bytes. If you omit the size entirely, the

**--squash**

Squash all of the image's new layers into a single new layer; any preexisting layers
are not squashed.

**--squash-all**

Squash all of the new image's layers (including those inherited from a base image) into a single new layer.

**--tag**, **-t**=*imageName*
Expand Down