diff --git a/docs/reference/ko_apply.md b/docs/reference/ko_apply.md index b492120985..af3ea29ba1 100644 --- a/docs/reference/ko_apply.md +++ b/docs/reference/ko_apply.md @@ -47,6 +47,7 @@ ko apply -f FILENAME [flags] ``` --bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags). -B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags). + --binary string Set to override binary path in image. --disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container. -f, --filename strings Filename, directory, or URL to files to use to create the resource -h, --help help for apply diff --git a/docs/reference/ko_build.md b/docs/reference/ko_build.md index 8c4d97d6e1..f6553a811d 100644 --- a/docs/reference/ko_build.md +++ b/docs/reference/ko_build.md @@ -44,6 +44,7 @@ ko build IMPORTPATH... [flags] ``` --bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags). -B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags). + --binary string Set to override binary path in image. --disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container. -h, --help help for build --image-label strings Which labels (key=value) to add to the image. diff --git a/docs/reference/ko_create.md b/docs/reference/ko_create.md index 700f94340c..a91dbb521e 100644 --- a/docs/reference/ko_create.md +++ b/docs/reference/ko_create.md @@ -47,6 +47,7 @@ ko create -f FILENAME [flags] ``` --bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags). -B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags). + --binary string Set to override binary path in image. --disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container. -f, --filename strings Filename, directory, or URL to files to use to create the resource -h, --help help for create diff --git a/docs/reference/ko_resolve.md b/docs/reference/ko_resolve.md index 9466bbb308..bda9030bc2 100644 --- a/docs/reference/ko_resolve.md +++ b/docs/reference/ko_resolve.md @@ -40,6 +40,7 @@ ko resolve -f FILENAME [flags] ``` --bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags). -B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags). + --binary string Set to override binary path in image. --disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container. -f, --filename strings Filename, directory, or URL to files to use to create the resource -h, --help help for resolve diff --git a/docs/reference/ko_run.md b/docs/reference/ko_run.md index 5e2c88b045..254432f3ca 100644 --- a/docs/reference/ko_run.md +++ b/docs/reference/ko_run.md @@ -32,6 +32,7 @@ ko run IMPORTPATH [flags] ``` --bare Whether to just use KO_DOCKER_REPO without additional context (may not work properly with --tags). -B, --base-import-paths Whether to use the base path without MD5 hash after KO_DOCKER_REPO (may not work properly with --tags). + --binary string Set to override binary path in image. --disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container. -h, --help help for run --image-label strings Which labels (key=value) to add to the image. diff --git a/pkg/build/gobuild.go b/pkg/build/gobuild.go index b207763dc2..655fa3f2fe 100644 --- a/pkg/build/gobuild.go +++ b/pkg/build/gobuild.go @@ -77,6 +77,7 @@ type gobuild struct { build builder sbom sbomber sbomDir string + binaryPath string disableOptimizations bool trimpath bool buildConfigs map[string]Config @@ -99,6 +100,7 @@ type gobuildOpener struct { build builder sbom sbomber sbomDir string + binaryPath string disableOptimizations bool trimpath bool buildConfigs map[string]Config @@ -127,6 +129,7 @@ func (gbo *gobuildOpener) Open() (Interface, error) { build: gbo.build, sbom: gbo.sbom, sbomDir: gbo.sbomDir, + binaryPath: gbo.binaryPath, disableOptimizations: gbo.disableOptimizations, trimpath: gbo.trimpath, buildConfigs: gbo.buildConfigs, @@ -795,6 +798,10 @@ func (g *gobuild) configForImportPath(ip string) Config { config.Flags = append(config.Flags, "-gcflags", "all=-N -l") } + if g.binaryPath != "" { + config.Binary = g.binaryPath + } + if config.ID != "" { log.Printf("Using build config %s for %s", config.ID, ip) } diff --git a/pkg/build/gobuild_test.go b/pkg/build/gobuild_test.go index 3551d0f048..3321747e6d 100644 --- a/pkg/build/gobuild_test.go +++ b/pkg/build/gobuild_test.go @@ -369,6 +369,16 @@ func TestBuildConfig(t *testing.T) { Flags: FlagArray{"-gcflags", "all=-N -l"}, }, }, + { + description: "override binary path", + options: []Option{ + WithBaseImages(nilGetBase), + WithBinaryPath("/mydir/myprogram"), + }, + expectConfig: Config{ + Binary: "/mydir/myprogram", + }, + }, } for _, test := range tests { t.Run(test.description, func(t *testing.T) { diff --git a/pkg/build/options.go b/pkg/build/options.go index 4cedf4d0fb..f81501e158 100644 --- a/pkg/build/options.go +++ b/pkg/build/options.go @@ -177,3 +177,12 @@ func WithSBOMDir(dir string) Option { return nil } } + +// WithBinaryPath is a functional option for overriding the path +// to the executable in the output image. +func WithBinaryPath(binaryPath string) Option { + return func(gbo *gobuildOpener) error { + gbo.binaryPath = binaryPath + return nil + } +} diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index a16c1033ad..100ee450d2 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -67,6 +67,9 @@ type BuildOptions struct { // `AddBuildOptions()` defaults this field to `true`. Trimpath bool + // BinaryPath overrides the default path for the binary in the output image. + BinaryPath string + // BuildConfigs stores the per-image build config from `.ko.yaml`. BuildConfigs map[string]build.Config } @@ -82,6 +85,8 @@ func AddBuildOptions(cmd *cobra.Command, bo *BuildOptions) { "Path to file where the SBOM will be written.") cmd.Flags().StringSliceVar(&bo.Platforms, "platform", []string{}, "Which platform to use when pulling a multi-platform base. Format: all | [/[/]][,platform]*") + cmd.Flags().StringVar(&bo.BinaryPath, "binary", "", + "Set to override binary path in image.") cmd.Flags().StringSliceVar(&bo.Labels, "image-label", []string{}, "Which labels (key=value) to add to the image.") bo.Trimpath = true diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index 8a348f0e24..199f0d45b0 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -125,6 +125,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) { opts = append(opts, build.WithSBOMDir(bo.SBOMDir)) } + if bo.BinaryPath != "" { + opts = append(opts, build.WithBinaryPath(bo.BinaryPath)) + } + return opts, nil }