Skip to content

Commit

Permalink
create test image with go test -c
Browse files Browse the repository at this point in the history
  • Loading branch information
y-tajiri committed Jul 28, 2023
1 parent 621f7a4 commit 9f719ba
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/reference/ko_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/reference/ko_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/reference/ko_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/reference/ko_resolve.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/reference/ko_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 22 additions & 2 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ type gobuild struct {
dir string
labels map[string]string
semaphore *semaphore.Weighted
createTestBinary bool

cache *layerCache
}
Expand All @@ -108,6 +109,7 @@ type gobuildOpener struct {
labels map[string]string
dir string
jobs int
createTestBinary bool
}

func (gbo *gobuildOpener) Open() (Interface, error) {
Expand All @@ -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{},
Expand All @@ -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()
}

Expand Down Expand Up @@ -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`")
}
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions pkg/build/gobuilds.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 8 additions & 0 deletions pkg/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
6 changes: 6 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -84,6 +87,9 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) {
"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.")
cmd.Flags().BoolVar(&bo.CreateTestBinary, "test", bo.CreateTestBinary,
"Use go test -c instead of go build when building Go code.")

bo.Trimpath = true
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 9f719ba

Please sign in to comment.