From 9f719bae110772bec9dbb13abc03f8acde52dc59 Mon Sep 17 00:00:00 2001 From: Yasuhiro TAJIRI Date: Fri, 28 Jul 2023 10:56:26 +0900 Subject: [PATCH] create test image with go test -c --- docs/reference/ko_apply.md | 1 + docs/reference/ko_build.md | 1 + docs/reference/ko_create.md | 1 + docs/reference/ko_resolve.md | 1 + docs/reference/ko_run.md | 1 + pkg/build/gobuild.go | 24 ++++++++++++++++++++++-- pkg/build/gobuilds.go | 3 +++ pkg/build/options.go | 8 ++++++++ pkg/commands/options/build.go | 6 ++++++ pkg/commands/resolver.go | 3 +++ 10 files changed, 47 insertions(+), 2 deletions(-) diff --git a/docs/reference/ko_apply.md b/docs/reference/ko_apply.md index b492120985..5d70c92d75 100644 --- a/docs/reference/ko_apply.md +++ b/docs/reference/ko_apply.md @@ -66,6 +66,7 @@ ko apply -f FILENAME [flags] --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. -t, --tags strings Which tags to use for the produced image instead of the default 'latest' tag (may not work properly with --base-import-paths or --bare). (default [latest]) --tarball string File to save images tarballs + --test Use go test -c instead of go build when building Go code. ``` ### Options inherited from parent commands diff --git a/docs/reference/ko_build.md b/docs/reference/ko_build.md index 8c4d97d6e1..6474fd7a09 100644 --- a/docs/reference/ko_build.md +++ b/docs/reference/ko_build.md @@ -60,6 +60,7 @@ ko build IMPORTPATH... [flags] --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. -t, --tags strings Which tags to use for the produced image instead of the default 'latest' tag (may not work properly with --base-import-paths or --bare). (default [latest]) --tarball string File to save images tarballs + --test Use go test -c instead of go build when building Go code. ``` ### Options inherited from parent commands diff --git a/docs/reference/ko_create.md b/docs/reference/ko_create.md index 700f94340c..c7df22919f 100644 --- a/docs/reference/ko_create.md +++ b/docs/reference/ko_create.md @@ -66,6 +66,7 @@ ko create -f FILENAME [flags] --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. -t, --tags strings Which tags to use for the produced image instead of the default 'latest' tag (may not work properly with --base-import-paths or --bare). (default [latest]) --tarball string File to save images tarballs + --test Use go test -c instead of go build when building Go code. ``` ### Options inherited from parent commands diff --git a/docs/reference/ko_resolve.md b/docs/reference/ko_resolve.md index 9466bbb308..ecb92019fb 100644 --- a/docs/reference/ko_resolve.md +++ b/docs/reference/ko_resolve.md @@ -59,6 +59,7 @@ ko resolve -f FILENAME [flags] --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. -t, --tags strings Which tags to use for the produced image instead of the default 'latest' tag (may not work properly with --base-import-paths or --bare). (default [latest]) --tarball string File to save images tarballs + --test Use go test -c instead of go build when building Go code. ``` ### Options inherited from parent commands diff --git a/docs/reference/ko_run.md b/docs/reference/ko_run.md index 5e2c88b045..c8baa68e74 100644 --- a/docs/reference/ko_run.md +++ b/docs/reference/ko_run.md @@ -48,6 +48,7 @@ ko run IMPORTPATH [flags] --tag-only Include tags but not digests in resolved image references. Useful when digests are not preserved when images are repopulated. -t, --tags strings Which tags to use for the produced image instead of the default 'latest' tag (may not work properly with --base-import-paths or --bare). (default [latest]) --tarball string File to save images tarballs + --test Use go test -c instead of go build when building Go code. ``` ### Options inherited from parent commands diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index f289e6cf85..b83ad8760d 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -86,6 +86,7 @@ type gobuild struct { dir string labels map[string]string semaphore *semaphore.Weighted + createTestBinary bool cache *layerCache } @@ -108,6 +109,7 @@ type gobuildOpener struct { labels map[string]string dir string jobs int + createTestBinary bool } func (gbo *gobuildOpener) Open() (Interface, error) { @@ -134,6 +136,8 @@ func (gbo *gobuildOpener) Open() (Interface, error) { buildConfigs: gbo.buildConfigs, labels: gbo.labels, dir: gbo.dir, + createTestBinary: gbo.createTestBinary, + platformMatcher: matcher, cache: &layerCache{ buildToDiff: map[string]buildIDToDiffID{}, @@ -156,12 +160,14 @@ func NewGo(ctx context.Context, dir string, options ...Option) (Interface, error dir: dir, sbom: spdx("(none)"), } - for _, option := range options { if err := option(gbo); err != nil { return nil, err } } + if gbo.createTestBinary { + gbo.build = buildTest + } return gbo.Open() } @@ -219,6 +225,9 @@ func (g *gobuild) IsSupportedReference(s string) error { if len(pkgs) != 1 { return fmt.Errorf("found %d local packages, expected 1", len(pkgs)) } + if g.createTestBinary { + return nil + } if pkgs[0].Name != "main" { return errors.New("importpath is not `package main`") } @@ -251,15 +260,26 @@ func getGoBinary() string { } return defaultGoBin } +func buildTest(ctx context.Context, ip string, dir string, platform v1.Platform, config Config) (string, error) { + return buildCommon(ctx, ip, dir, platform, config, false) +} func build(ctx context.Context, ip string, dir string, platform v1.Platform, config Config) (string, error) { + return buildCommon(ctx, ip, dir, platform, config, true) +} + +func buildCommon(ctx context.Context, ip string, dir string, platform v1.Platform, config Config, createTestBinary bool) (string, error) { buildArgs, err := createBuildArgs(config) if err != nil { return "", err } args := make([]string, 0, 4+len(buildArgs)) - args = append(args, "build") + if createTestBinary { + args = append(args, "test", "-c") + } else { + args = append(args, "build") + } args = append(args, buildArgs...) env, err := buildEnv(platform, os.Environ(), config.Env) diff --git a/pkg/build/gobuilds.go b/pkg/build/gobuilds.go index 7a73bc5fd7..7631beb7a0 100644 --- a/pkg/build/gobuilds.go +++ b/pkg/build/gobuilds.go @@ -32,6 +32,9 @@ type gobuilds struct { // workingDirectory is typically ".", but it may be a different value if ko is embedded as a library. workingDirectory string + + // ignore supported reference + ignoreSupportedReference bool } // builderWithConfig is not an imaginative name. diff --git a/pkg/build/options.go b/pkg/build/options.go index 4cedf4d0fb..67da52a85a 100644 --- a/pkg/build/options.go +++ b/pkg/build/options.go @@ -177,3 +177,11 @@ func WithSBOMDir(dir string) Option { return nil } } + +// WithCreateTestBinary is a functional option for overriding the go option +func WithCreateTestBinary(createTestBinary bool) Option { + return func(gbo *gobuildOpener) error { + gbo.createTestBinary = createTestBinary + return nil + } +} diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index a16c1033ad..28d59f18d4 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -69,6 +69,9 @@ type BuildOptions struct { // BuildConfigs stores the per-image build config from `.ko.yaml`. BuildConfigs map[string]build.Config + + // compile the test binary + CreateTestBinary bool } func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) { @@ -84,6 +87,9 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) { "Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]*") cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{}, "Which labels (key=value) to add to the image.") + cmd.Flags().BoolVar(&bo.CreateTestBinary, "test", bo.CreateTestBinary, + "Use go test -c instead of go build when building Go code.") + bo.Trimpath = true } diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index b9dc43414d..71775dcfc9 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -126,6 +126,9 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) { opts = append(opts, build.WithSBOMDir(bo.SBOMDir)) } + if bo.CreateTestBinary { + opts = append(opts, build.WithCreateTestBinary(bo.CreateTestBinary)) + } return opts, nil }