Skip to content

Commit

Permalink
Introduce --binary flag to override path in image
Browse files Browse the repository at this point in the history
The default path is /ko-app/<binaryname>, but this allows that to be
changed from the command line.
  • Loading branch information
justinsb committed Sep 20, 2023
1 parent 6b6134b commit a11384e
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/reference/ko_apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 @@ -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.
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 @@ -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
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 @@ -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
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 @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type gobuild struct {
build builder
sbom sbomber
sbomDir string
binaryPath string
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
Expand All @@ -99,6 +100,7 @@ type gobuildOpener struct {
build builder
sbom sbomber
sbomDir string
binaryPath string
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/build/gobuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions pkg/build/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
5 changes: 5 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 | <os>[/<arch>[/<variant>]][,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
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit a11384e

Please sign in to comment.