Skip to content

Commit

Permalink
Add --image-label to add labels to built images (ko-build#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
imjasonh authored Mar 3, 2021
1 parent c14c08e commit f7df810
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type gobuild struct {
mod *modules
buildContext buildContext
platformMatcher *platformMatcher
labels map[string]string
}

// Option is a functional option for NewGo.
Expand All @@ -96,6 +97,7 @@ type gobuildOpener struct {
mod *modules
buildContext buildContext
platform string
labels map[string]string
}

func (gbo *gobuildOpener) Open() (Interface, error) {
Expand All @@ -113,6 +115,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
disableOptimizations: gbo.disableOptimizations,
mod: gbo.mod,
buildContext: gbo.buildContext,
labels: gbo.labels,
platformMatcher: matcher,
}, nil
}
Expand Down Expand Up @@ -606,6 +609,13 @@ func (g *gobuild) buildOne(ctx context.Context, s string, base v1.Image, platfor
cfg.Config.Env = append(cfg.Config.Env, "KO_DATA_PATH="+kodataRoot)
cfg.Author = "github.com/google/ko"

if cfg.Config.Labels == nil {
cfg.Config.Labels = map[string]string{}
}
for k, v := range g.labels {
cfg.Config.Labels[k] = v
}

image, err := mutate.ConfigFile(withApp, cfg)
if err != nil {
return nil, err
Expand Down
18 changes: 18 additions & 0 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"testing"
"time"

"github.com/google/go-cmp/cmp"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/empty"
"github.com/google/go-containerregistry/pkg/v1/mutate"
Expand Down Expand Up @@ -404,6 +405,8 @@ func TestGoBuild(t *testing.T) {
WithCreationTime(creationTime),
WithBaseImages(func(context.Context, string) (Result, error) { return base, nil }),
withBuilder(writeTempFile),
WithLabel("foo", "bar"),
WithLabel("hello", "world"),
)
if err != nil {
t.Fatalf("NewGo() = %v", err)
Expand Down Expand Up @@ -442,6 +445,21 @@ func TestGoBuild(t *testing.T) {
}
})

t.Run("check labels", func(t *testing.T) {
cfg, err := img.ConfigFile()
if err != nil {
t.Fatalf("ConfigFile() = %v", err)
}

want := map[string]string{
"foo": "bar",
"hello": "world",
}
got := cfg.Config.Labels
if d := cmp.Diff(got, want); d != "" {
t.Fatalf("Labels diff (-got,+want): %s", d)
}
})
}

func TestGoBuildIndex(t *testing.T) {
Expand Down
11 changes: 11 additions & 0 deletions pkg/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func WithPlatforms(platforms string) Option {
}
}

// WithLabel is a functional option for adding labels to built images.
func WithLabel(k, v string) Option {
return func(gbo *gobuildOpener) error {
if gbo.labels == nil {
gbo.labels = map[string]string{}
}
gbo.labels[k] = v
return nil
}
}

// withBuilder is a functional option for overriding the way go binaries
// are built.
func withBuilder(b builder) Option {
Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type BuildOptions struct {
ConcurrentBuilds int
DisableOptimizations bool
Platform string
Labels []string
}

func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
Expand All @@ -34,4 +35,6 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
"Disable optimizations when building Go code. Useful when you want to interactively debug the created container.")
cmd.Flags().StringVar(&bo.Platform, "platform", "",
"Which platform to use when pulling a multi-platform base. Format: all | <os>[/<arch>[/<variant>]][,platform]*")
cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{},
"Which labels (key=value) to add to the image.")
}
7 changes: 7 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
if bo.DisableOptimizations {
opts = append(opts, build.WithDisabledOptimizations())
}
for _, lf := range bo.Labels {
parts := strings.SplitN(lf, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid label flag: %s", lf)
}
opts = append(opts, build.WithLabel(parts[0], parts[1]))
}
return opts, nil
}

Expand Down

0 comments on commit f7df810

Please sign in to comment.