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 Jun 16, 2024
1 parent 7437c4d commit f22ca24
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.
--debug Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.
--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
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.
--debug Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.
--disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container.
-h, --help help for build
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.
--debug Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.
--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
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.
--debug Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.
--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
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.
--debug Include Delve debugger into image and wrap around ko-app. This debugger will listen to port 40000.
--disable-optimizations Disable optimizations when building Go code. Useful when you want to interactively debug the created container.
-h, --help help for run
Expand Down
7 changes: 7 additions & 0 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ type gobuild struct {
build builder
sbom sbomber
sbomDir string
binaryPath string
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
Expand All @@ -118,6 +119,7 @@ type gobuildOpener struct {
build builder
sbom sbomber
sbomDir string
binaryPath string
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
Expand Down Expand Up @@ -150,6 +152,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 @@ -930,6 +933,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 @@ -496,6 +496,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 @@ -191,3 +191,12 @@ func WithDebugger() 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 @@ -78,6 +78,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 @@ -93,6 +96,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.")
cmd.Flags().BoolVar(&bo.Debug, "debug", bo.Debug,
Expand Down
4 changes: 4 additions & 0 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,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 f22ca24

Please sign in to comment.