From 83a8c5cae2c4aa56141a413204bb835e06a57ed4 Mon Sep 17 00:00:00 2001 From: Avi Deitcher Date: Wed, 21 Feb 2024 21:33:40 +0200 Subject: [PATCH] add support for tag templates Signed-off-by: Avi Deitcher --- src/cmd/linuxkit/pkg.go | 3 +++ src/cmd/linuxkit/pkglib/build_test.go | 1 + src/cmd/linuxkit/pkglib/pkglib.go | 17 ++++++++++++++++- src/cmd/linuxkit/pkglib/testdata/Dockerfile | 0 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 src/cmd/linuxkit/pkglib/testdata/Dockerfile diff --git a/src/cmd/linuxkit/pkg.go b/src/cmd/linuxkit/pkg.go index 1a1ee8680..e16a1bc5f 100644 --- a/src/cmd/linuxkit/pkg.go +++ b/src/cmd/linuxkit/pkg.go @@ -22,6 +22,7 @@ func pkgCmd() *cobra.Command { hashPath string dirty bool devMode bool + tag string ) cmd := &cobra.Command{ @@ -36,6 +37,7 @@ func pkgCmd() *cobra.Command { HashPath: hashPath, Dirty: dirty, Dev: devMode, + Tag: tag, } if cmd.Flags().Changed("disable-cache") && cmd.Flags().Changed("enable-cache") { return errors.New("cannot set but disable-cache and enable-cache") @@ -85,6 +87,7 @@ func pkgCmd() *cobra.Command { cmd.PersistentFlags().StringVar(&argOrg, "org", piBase.Org, "Override the hub org") cmd.PersistentFlags().StringVar(&buildYML, "build-yml", "build.yml", "Override the name of the yml file") cmd.PersistentFlags().StringVar(&hash, "hash", "", "Override the image hash (default is to query git for the package's tree-sh)") + cmd.PersistentFlags().StringVar(&tag, "tag", "{{.Hash}}", "Override the tag using fixed strings and/or text templates. Acceptable are .Hash for the hash") cmd.PersistentFlags().StringVar(&hashCommit, "hash-commit", "HEAD", "Override the git commit to use for the hash") cmd.PersistentFlags().StringVar(&hashPath, "hash-path", "", "Override the directory to use for the image hash, must be a parent of the package dir (default is to use the package dir)") cmd.PersistentFlags().BoolVar(&dirty, "force-dirty", false, "Force the pkg(s) to be considered dirty") diff --git a/src/cmd/linuxkit/pkglib/build_test.go b/src/cmd/linuxkit/pkglib/build_test.go index b95ef0ad8..c558f0c89 100644 --- a/src/cmd/linuxkit/pkglib/build_test.go +++ b/src/cmd/linuxkit/pkglib/build_test.go @@ -534,6 +534,7 @@ func TestBuild(t *testing.T) { } opts = append(opts, WithBuildPlatforms(targets...)) } + tt.p.dockerfile = "testdata/Dockerfile" err := tt.p.Build(opts...) switch { case (tt.err == "" && err != nil) || (tt.err != "" && err == nil) || (tt.err != "" && err != nil && !strings.HasPrefix(err.Error(), tt.err)): diff --git a/src/cmd/linuxkit/pkglib/pkglib.go b/src/cmd/linuxkit/pkglib/pkglib.go index e8809fb35..3e675ac53 100644 --- a/src/cmd/linuxkit/pkglib/pkglib.go +++ b/src/cmd/linuxkit/pkglib/pkglib.go @@ -1,12 +1,14 @@ package pkglib import ( + "bytes" "crypto/sha1" "fmt" "os" "path" "path/filepath" "strings" + "text/template" "gopkg.in/yaml.v2" @@ -50,6 +52,7 @@ type PkglibConfig struct { HashPath string Dirty bool Dev bool + Tag string // Tag is a text/template string, defaults to {{.Hash}} } // NewPkInfo returns a new pkgInfo with default values @@ -89,6 +92,7 @@ type Pkg struct { path string dockerfile string hash string + tag string dirty bool commitHash string git *git @@ -254,6 +258,16 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) { } } + // calculate the tag to use based on the template and the pkgHash + tmpl, err := template.New("tag").Parse(cfg.Tag) + if err != nil { + return nil, fmt.Errorf("invalid tag template: %v", err) + } + var buf bytes.Buffer + if err := tmpl.Execute(&buf, map[string]string{"Hash": pkgHash}); err != nil { + return nil, fmt.Errorf("failed to execute tag template: %v", err) + } + tag := buf.String() pkgs = append(pkgs, Pkg{ image: pi.Image, org: pi.Org, @@ -271,6 +285,7 @@ func NewFromConfig(cfg PkglibConfig, args ...string) ([]Pkg, error) { path: pkgPath, dockerfile: pi.Dockerfile, git: git, + tag: tag, }) } return pkgs, nil @@ -295,7 +310,7 @@ func (p Pkg) ReleaseTag(release string) (string, error) { // Tag returns the tag to use for the package func (p Pkg) Tag() string { - t := p.hash + t := p.tag if t == "" { t = "latest" } diff --git a/src/cmd/linuxkit/pkglib/testdata/Dockerfile b/src/cmd/linuxkit/pkglib/testdata/Dockerfile new file mode 100644 index 000000000..e69de29bb