Skip to content

Commit

Permalink
Set build config via BuildOptions
Browse files Browse the repository at this point in the history
Enables programmatically overriding build configs when ko is
embedded in another tool.

Related: ko-build#340, ko-build#419
  • Loading branch information
halvards committed Aug 19, 2021
1 parent 780c281 commit 9bae8e0
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 36 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package options

import (
"github.com/google/ko/pkg/build"
"github.com/spf13/cobra"
)

Expand All @@ -39,6 +40,9 @@ type BuildOptions struct {
UserAgent string

InsecureRegistry bool

// BuildConfigs enables programmatic overriding of build config set in `.ko.yaml`.
BuildConfigs map[string]build.Config
}

func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
Expand Down
5 changes: 4 additions & 1 deletion pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
opts = append(opts, build.WithLabel(parts[0], parts[1]))
}

if len(buildConfigs) > 0 {
// prefer buildConfigs from BuildOptions
if bo.BuildConfigs != nil {
opts = append(opts, build.WithConfig(bo.BuildConfigs))
} else if len(buildConfigs) > 0 {
opts = append(opts, build.WithConfig(buildConfigs))
}

Expand Down
111 changes: 76 additions & 35 deletions pkg/commands/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,64 @@ kind: Bar
}
}

func TestNewBuilderCanBuild(t *testing.T) {
ctx := context.Background()
bo := &options.BuildOptions{
ConcurrentBuilds: 1,
}
builder, err := NewBuilder(ctx, bo)
if err != nil {
t.Fatalf("NewBuilder(): %v", err)
}
res, err := builder.Build(ctx, "ko://github.com/google/ko/test")
if err != nil {
t.Fatalf("builder.Build(): %v", err)
// TODO This test accesses the network and is slow. Implement a dry-run mode?
func TestNewBuilder(t *testing.T) {
tests := []struct {
description string
importpath string
bo *options.BuildOptions
wantQualifiedImportpath string
shouldBuildError bool
}{
{
description: "test app with already qualified import path",
importpath: "ko://github.com/google/ko/test",
bo: &options.BuildOptions{
ConcurrentBuilds: 1,
},
wantQualifiedImportpath: "ko://github.com/google/ko/test",
shouldBuildError: false,
},
{
description: "programmatic build config",
importpath: "./test",
bo: &options.BuildOptions{
ConcurrentBuilds: 1,
BuildConfigs: map[string]build.Config{
"github.com/google/ko/test": {
ID: "id-can-be-anything",
Flags: []string{"-invalid-flag-should-cause-error"},
},
},
WorkingDirectory: "../..",
},
wantQualifiedImportpath: "ko://github.com/google/ko/test",
shouldBuildError: true,
},
}
gotDigest, err := res.Digest()
if err != nil {
t.Fatalf("res.Digest(): %v", err)
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ctx := context.Background()
builder, err := NewBuilder(ctx, test.bo)
if err != nil {
t.Fatalf("NewBuilder(): %v", err)
}
qualifiedImportpath, err := builder.QualifyImport(test.importpath)
if err != nil {
t.Fatalf("builder.QualifyImport(%s): %v", test.importpath, err)
}
if qualifiedImportpath != test.wantQualifiedImportpath {
t.Fatalf("incorrect qualified import path, got %s, wanted %s", qualifiedImportpath, test.wantQualifiedImportpath)
}
_, err = builder.Build(ctx, qualifiedImportpath)
if err != nil && !test.shouldBuildError {
t.Fatalf("builder.Build(): %v", err)
}
if err == nil && test.shouldBuildError {
t.Fatalf("expected error got nil")
}
})
}
fmt.Println(gotDigest.String())
}

func TestNewPublisherCanPublish(t *testing.T) {
Expand Down Expand Up @@ -224,26 +264,27 @@ func TestNewPublisherCanPublish(t *testing.T) {
},
}
for _, test := range tests {
publisher, err := NewPublisher(test.po)
if err != nil {
t.Fatalf("%s: NewPublisher(): %v", test.description, err)
}
defer publisher.Close()
ref, err := publisher.Publish(context.Background(), empty.Image, build.StrictScheme+importpath)
if test.shouldError {
if err == nil || !strings.HasSuffix(err.Error(), test.wantError.Error()) {
t.Errorf("%s: got error %v, wanted %v", test.description, err, test.wantError)
t.Run(test.description, func(t *testing.T) {
publisher, err := NewPublisher(test.po)
if err != nil {
t.Fatalf("NewPublisher(): %v", err)
}
continue
}
if err != nil {
t.Fatalf("%s: publisher.Publish(): %v", test.description, err)
}
fmt.Printf("ref: %+v\n", ref)
gotImageName := ref.Context().Name()
if gotImageName != test.wantImageName {
t.Errorf("%s: got %s, wanted %s", test.description, gotImageName, test.wantImageName)
}
defer publisher.Close()
ref, err := publisher.Publish(context.Background(), empty.Image, build.StrictScheme+importpath)
if test.shouldError {
if err == nil || !strings.HasSuffix(err.Error(), test.wantError.Error()) {
t.Errorf("%s: got error %v, wanted %v", test.description, err, test.wantError)
}
return
}
if err != nil {
t.Fatalf("publisher.Publish(): %v", err)
}
gotImageName := ref.Context().Name()
if gotImageName != test.wantImageName {
t.Errorf("got %s, wanted %s", gotImageName, test.wantImageName)
}
})
}
}

Expand Down

0 comments on commit 9bae8e0

Please sign in to comment.