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

feat: Add KO_GO_PATH env var #930

Merged
merged 2 commits into from
Jan 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

## Basic Configuration

Aside from `KO_DOCKER_REPO`, you can configure `ko`'s behavior using a
`.ko.yaml` file. The location of this file can be overridden with
`KO_CONFIG_PATH`.
Aside from certain environment variables (see [below](#environment-variables-advanced)) like `KO_DOCKER_REPO`, you can
configure `ko`'s behavior using a `.ko.yaml` file. The location of this file can be overridden with `KO_CONFIG_PATH`.

### Overriding Base Images

Expand Down Expand Up @@ -83,7 +82,6 @@ The `ldflags` default value is `[]`.
only the `env`, `flags` and `ldflags` fields are currently supported. Also, the
templating support is currently limited to using environment variables only.


### Setting default platforms

By default, `ko` builds images based on the platform it runs on. If your target platform differs from your build platform you can specify the build platform:
Expand All @@ -106,6 +104,18 @@ You can also use the `KO_DEFAULTPLATFORMS` environment variable to set the defau
KO_DEFAULTPLATFORMS=linux/arm64,linux/amd64
```

### Environment Variables (advanced)
imjasonh marked this conversation as resolved.
Show resolved Hide resolved

For ease of use, backward compatibility and advanced use cases, `ko` supports the following environment variables to
influence the build process.

| Variable | Default Value | Description |
|------------------|--------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `KO_DOCKER_REPO` | (not set) | Container repository where to push images built with `ko` (required) |
| `KO_DATA_PATH` | `/var/run/ko` (`C:\var\run\ko` on Windows) | By convention, any contents of a directory named `<importpath>/kodata/` will be bundled into the image, and the path where it's available in the image will be identified by the environment variable `KO_DATA_PATH` (optional). |
imjasonh marked this conversation as resolved.
Show resolved Hide resolved
| `KO_GO_PATH` | `go` | `go` binary to use for builds, relative or absolute path, otherwise looked up via $PATH (optional) |
| `KO_CONFIG_PATH` | `./ko.yaml` | Path to `ko` configuration file (optional) |
| `KOCACHE` | (not set) | This tells `ko` to store a local mapping between the `go build` inputs to the image layer that they produce, so `go build` can be skipped entirely if the layer is already present in the image registry (optional). |

## Naming Images

Expand Down
4 changes: 3 additions & 1 deletion pkg/build/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ func (c *layerCache) readBuildToDiff(file string) (buildIDToDiffID, error) {
}

func getBuildID(ctx context.Context, file string) (string, error) {
cmd := exec.CommandContext(ctx, "go", "tool", "buildid", file)
gobin := getGoBinary()

cmd := exec.CommandContext(ctx, gobin, "tool", "buildid", file)
var output bytes.Buffer
cmd.Stderr = &output
cmd.Stdout = &output
Expand Down
18 changes: 16 additions & 2 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ import (

const (
defaultAppFilename = "ko-app"

imjasonh marked this conversation as resolved.
Show resolved Hide resolved
defaultGoBin = "go" // defaults to first go binary found in PATH
goBinPathEnv = "KO_GO_PATH" // env lookup for optional relative or full go binary path
)

// GetBase takes an importpath and returns a base image reference and base image (or index).
Expand Down Expand Up @@ -244,6 +247,13 @@ func getGoarm(platform v1.Platform) (string, error) {
return "", nil
}

func getGoBinary() string {
if env := os.Getenv(goBinPathEnv); env != "" {
return env
}
return defaultGoBin
}

func build(ctx context.Context, ip string, dir string, platform v1.Platform, config Config) (string, error) {
buildArgs, err := createBuildArgs(config)
if err != nil {
Expand Down Expand Up @@ -285,7 +295,9 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con

args = append(args, "-o", file)
args = append(args, ip)
cmd := exec.CommandContext(ctx, "go", args...)

gobin := getGoBinary()
cmd := exec.CommandContext(ctx, gobin, args...)
cmd.Dir = dir
cmd.Env = env

Expand All @@ -305,10 +317,12 @@ func build(ctx context.Context, ip string, dir string, platform v1.Platform, con
}

func goversionm(ctx context.Context, file string, appPath string, appFileName string, se oci.SignedEntity, dir string) ([]byte, types.MediaType, error) {
gobin := getGoBinary()

switch se.(type) {
case oci.SignedImage:
sbom := bytes.NewBuffer(nil)
cmd := exec.CommandContext(ctx, "go", "version", "-m", file)
cmd := exec.CommandContext(ctx, gobin, "version", "-m", file)
cmd.Stdout = sbom
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
Expand Down