Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow specifying binary path in image #1128

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
4 changes: 3 additions & 1 deletion pkg/build/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,16 @@ type Config struct {
// Env allows setting environment variables for `go build`
Env []string `yaml:",omitempty"`

// Binary allows overriding the output binary name (in the image)
Binary string `yaml:",omitempty"`

// Other GoReleaser fields that are not supported or do not make sense
// in the context of ko, for reference or for future use:
// Goos []string `yaml:",omitempty"`
// Goarch []string `yaml:",omitempty"`
// Goarm []string `yaml:",omitempty"`
// Gomips []string `yaml:",omitempty"`
// Targets []string `yaml:",omitempty"`
// Binary string `yaml:",omitempty"`
// Lang string `yaml:",omitempty"`
// Asmflags StringArray `yaml:",omitempty"`
// Gcflags StringArray `yaml:",omitempty"`
Expand Down
73 changes: 52 additions & 21 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 @@ -587,25 +590,39 @@ func tarBinary(name, binary string, platform *v1.Platform, opts *layerOptions) (
// For Windows, the layer must contain a Hives/ directory, and the root
// of the actual filesystem goes in a Files/ directory.
// For Linux, the binary goes into /ko-app/
dirs := []string{"ko-app"}
appDir := filepath.Dir(name)
dirs := []string{appDir}
if platform.OS == "windows" {
dirs = []string{
"Hives",
"Files",
"Files/ko-app",
"Files/" + appDir,
}
name = "Files" + name
}
for _, dir := range dirs {
if err := tw.WriteHeader(&tar.Header{
Name: dir,
Typeflag: tar.TypeDir,
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
// under which it was created. Additionally, windows can only set 0222,
// 0444, or 0666, none of which are executable.
Mode: 0555,
}); err != nil {
return nil, fmt.Errorf("writing dir %q to tar: %w", dir, err)
// Create all parent directories also
var parents []string
current := dir
for {
parents = append(parents, current)
current = filepath.Dir(current)
if current == "/" {
break
}
}

for i := len(parents) - 1; i >= 0; i-- {
parent := parents[i]
if err := tw.WriteHeader(&tar.Header{
Name: parent,
Typeflag: tar.TypeDir,
// Use a fixed Mode, so that this isn't sensitive to the directory and umask
// under which it was created. Additionally, windows can only set 0222,
// 0444, or 0666, none of which are executable.
Mode: 0555,
}); err != nil {
return nil, fmt.Errorf("writing dir %q to tar: %w", parent, err)
}
}
}

Expand Down Expand Up @@ -916,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 All @@ -927,6 +948,14 @@ func (g gobuild) useDebugging(platform v1.Platform) bool {
return g.debug && doesPlatformSupportDebugging(platform)
}

// pathToWindows converts a unix-style path to a windows-style path.
// For example, /apps/foo => C:\apps\foo
func pathToWindows(s string) string {
pathComponents := []string{"C:"}
pathComponents = append(pathComponents, strings.Split(s, "/")...)
return strings.Join(pathComponents, `\`)
}

func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, platform *v1.Platform) (oci.SignedImage, error) {
if err := g.semaphore.Acquire(ctx, 1); err != nil {
return nil, err
Expand Down Expand Up @@ -1043,9 +1072,12 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
},
})

appDir := "/ko-app"
appFileName := appFilename(ref.Path())
appPath := path.Join(appDir, appFileName)
appPath := config.Binary
if appPath == "" {
appPath = path.Join("/ko-app", appFilename(ref.Path()))
}
appDir := path.Dir(appPath)
appFileName := path.Base(appPath)

var lo layerOptions
lo.linuxCapabilities, err = caps.NewFileCaps(config.LinuxCapabilities...)
Expand Down Expand Up @@ -1136,16 +1168,15 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl
cfg.Config.Entrypoint = []string{appPath}
cfg.Config.Cmd = nil
if platform.OS == "windows" {
appPath := `C:\ko-app\` + appFileName
if g.debug {
cfg.Config.Entrypoint = append([]string{"C:\\" + delvePath}, delveArgs...)
cfg.Config.Entrypoint = append(cfg.Config.Entrypoint, appPath)
cfg.Config.Entrypoint = append([]string{pathToWindows(delvePath)}, delveArgs...)
cfg.Config.Entrypoint = append(cfg.Config.Entrypoint, pathToWindows(appPath))
} else {
cfg.Config.Entrypoint = []string{appPath}
cfg.Config.Entrypoint = []string{pathToWindows(appPath)}
}

updatePath(cfg, `C:\ko-app`)
cfg.Config.Env = append(cfg.Config.Env, `KO_DATA_PATH=C:\var\run\ko`)
updatePath(cfg, pathToWindows(filepath.Dir(appPath)))
cfg.Config.Env = append(cfg.Config.Env, `KO_DATA_PATH=`+pathToWindows(kodataRoot))
} else {
if g.useDebugging(*platform) {
cfg.Config.Entrypoint = append([]string{delvePath}, delveArgs...)
Expand Down
Loading
Loading