From 9ea2d6dcd9a8081aa1f7c6beec682ec8f3125377 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Tue, 1 Nov 2022 12:51:20 +0200 Subject: [PATCH] rationalize pull build Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/pkg_build.go | 15 +++++++++++++-- src/cmd/linuxkit/pkglib/build.go | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/cmd/linuxkit/pkg_build.go b/src/cmd/linuxkit/pkg_build.go index 0544f53e0..ff67029d9 100644 --- a/src/cmd/linuxkit/pkg_build.go +++ b/src/cmd/linuxkit/pkg_build.go @@ -38,6 +38,7 @@ func pkgBuildPush(args []string, withPush bool) { } force := flags.Bool("force", false, "Force rebuild even if image is in local cache") + pull := flags.Bool("pull", false, "Pull image if in registry but not in local cache; conflicts with --force") ignoreCache := flags.Bool("ignore-cached", false, "Ignore cached intermediate images, always pulling from registry") docker := flags.Bool("docker", false, "Store the built image in the docker image cache instead of the default linuxkit cache") platforms := flags.String("platforms", "", "Which platforms to build for, defaults to all of those for which the package can be built") @@ -49,12 +50,15 @@ func pkgBuildPush(args []string, withPush bool) { flags.Var(&cacheDir, "cache", fmt.Sprintf("Directory for caching and finding cached image, overrides env var %s", envVarCacheDir)) // some logic clarification: - // pkg build - always builds unless is in cache - // pkg build --force - always builds even if is in cache + // pkg build - builds unless is in cache or published in registry + // pkg build --pull - builds unless is in cache or published in registry; pulls from registry if not in cache + // pkg build --force - always builds even if is in cache or published in registry + // pkg build --force --pull - always builds even if is in cache or published in registry; --pull ignored // pkg push - always builds unless is in cache // pkg push --force - always builds even if is in cache // pkg push --nobuild - skips build; if not in cache, fails // pkg push --nobuild --force - nonsensical + // pkg pull - pull from registry if available, otherwise fail var ( release *string @@ -78,6 +82,10 @@ func pkgBuildPush(args []string, withPush bool) { fmt.Fprint(os.Stderr, "flags -force and -nobuild conflict") os.Exit(1) } + if *pull && *force { + fmt.Fprint(os.Stderr, "flags -force and -pull conflict") + os.Exit(1) + } var opts []pkglib.BuildOpt if *force { @@ -86,6 +94,9 @@ func pkgBuildPush(args []string, withPush bool) { if *ignoreCache { opts = append(opts, pkglib.WithBuildIgnoreCache()) } + if *pull { + opts = append(opts, pkglib.WithBuildPull()) + } opts = append(opts, pkglib.WithBuildCacheDir(cacheDir.String())) diff --git a/src/cmd/linuxkit/pkglib/build.go b/src/cmd/linuxkit/pkglib/build.go index 1fd571570..1d31f1e7d 100644 --- a/src/cmd/linuxkit/pkglib/build.go +++ b/src/cmd/linuxkit/pkglib/build.go @@ -27,6 +27,7 @@ import ( type buildOpts struct { skipBuild bool force bool + pull bool ignoreCache bool push bool release string @@ -61,6 +62,14 @@ func WithBuildForce() BuildOpt { } } +// WithBuildPull pull down the image to cache if it already exists in registry +func WithBuildPull() BuildOpt { + return func(bo *buildOpts) error { + bo.pull = true + return nil + } +} + // WithBuildPush pushes the result of the build to the registry func WithBuildPush() BuildOpt { return func(bo *buildOpts) error { @@ -225,6 +234,12 @@ func (p Pkg) Build(bos ...BuildOpt) error { } var platformsToBuild []imagespec.Platform + switch { + case bo.force: + case bo.pull: + case !bo.skipBuild: + } + if bo.force { platformsToBuild = bo.platforms } else if !bo.skipBuild {