Skip to content

Commit

Permalink
Refactor global values to be defaults
Browse files Browse the repository at this point in the history
There was recent work to add global values for `env`, `flags`, and `ldflags`. The global values would be merged with per-build values to generate the value used for the builds.

There are a couple issues with this:

- It's inconsistent with the existing code, which only has `default` values declared globally (there is no merging today).
- The name of the `flag` variable, caused a conflict with knative's `KO_FLAGS` environment variable (see #1317)

This PR does the following:

- Refactors the logic to use `defaultEnv`, `defaultFlags`, and `defaultLdflags`. This resolves both issues described above.
- Updates documentation
- Removes a few missed references to `vendor` in scripts.

Fixes #1317
  • Loading branch information
nmittler committed May 18, 2024
1 parent bb99ecc commit 9500660
Show file tree
Hide file tree
Showing 13 changed files with 160 additions and 133 deletions.
1 change: 0 additions & 1 deletion .github/workflows/style.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ jobs:
fail_on_error: true
locale: "US"
exclude: |
./vendor/*
./.golangci.yaml
- uses: get-woke/woke-action-reviewdog@d71fd0115146a01c3181439ce714e21a69d75e31 # v0
Expand Down
2 changes: 0 additions & 2 deletions .wokeignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
vendor/**

# Uses some Cobra methods
pkg/commands/*
25 changes: 12 additions & 13 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,52 +125,51 @@ KO_DEFAULTPLATFORMS=linux/arm64,linux/amd64
### Setting build environment variables

By default, `ko` builds use the ambient environment from the system (i.e. `os.Environ()`).
These values can be overridden globally or per-build (or both).
These values can be overridden for your build.

```yaml
env:
defaultEnv:
- FOO=foo
builds:
- id: foo
dir: .
main: ./foobar/foo
env:
- FOO=bar # Overrides the global value.
- id: bar
- FOO=bar
- id: bar # Will use defaultEnv.
dir: ./bar
main: .
```

For a given build, the environment variables are merged in the following order:

- System `os.Environ` (lowest precedence)
- Global `env`
- Build `env` (highest precedence)
- Build variables: `build.env` if specified, otherwise `defaultEnv` (highest precedence)

### Setting build flags and ldflags

You can specify both `flags` and `ldflags` globally as well as per-build.

```yaml
flags:
defaultFlags:
- -v
ldflags:
defaultLdflags:
- -s
builds:
- id: foo
dir: .
main: ./foobar/foo
flags:
- -trimpath # Build will use: -v -trimpath
- -trimpath
ldflags:
- -w # Build will use: -s -w
- id: bar
- -w
- id: bar # Will use defaultFlags and defaultLdflags.
dir: ./bar
main: .
```

The values for each `build` will be appended to the global values when creating each build.
Both global and per-build values may use [template parameters](#templating-support).
The values for a `build` will be used if specified, otherwise their respective defaults will be used.
Both default and per-build values may use [template parameters](#templating-support).

### Environment Variables (advanced)

Expand Down
2 changes: 1 addition & 1 deletion hack/presubmit.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pushd "${PROJECT_ROOT}"
trap popd EXIT

# Verify that all source files are correctly formatted.
find . -name "*.go" | grep -v vendor/ | xargs gofmt -d -e -l
find . -name "*.go" | xargs gofmt -d -e -l

# Verify that generated Markdown docs are up-to-date.
tmpdir=$(mktemp -d)
Expand Down
1 change: 0 additions & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ pushd ${PROJECT_ROOT}
trap popd EXIT

go mod tidy
go mod vendor

go run $PROJECT_ROOT/cmd/help/main.go --dir=$PROJECT_ROOT/docs/reference/
6 changes: 0 additions & 6 deletions hack/update-deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ pushd ${PROJECT_ROOT}
trap popd EXIT

go mod tidy
go mod vendor

# Delete all vendored broken symlinks.
# From https://stackoverflow.com/questions/22097130/delete-all-broken-symbolic-links-with-a-line
find vendor/ -type l -exec sh -c 'for x; do [ -e "$x" ] || rm "$x"; done' _ {} +

94 changes: 51 additions & 43 deletions pkg/build/gobuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ type GetBase func(context.Context, string) (name.Reference, Result, error)

// buildContext provides parameters for a builder function.
type buildContext struct {
creationTime v1.Time
ip string
dir string
mergedEnv []string
mergedFlags []string
mergedLdflags []string
platform v1.Platform
creationTime v1.Time
ip string
dir string
env []string
flags []string
ldflags []string
platform v1.Platform
}

type builder func(context.Context, buildContext) (string, error)
Expand All @@ -95,9 +95,9 @@ type gobuild struct {
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
env []string
flags []string
ldflags []string
defaultEnv []string
defaultFlags []string
defaultLdflags []string
platformMatcher *platformMatcher
dir string
labels map[string]string
Expand All @@ -120,9 +120,9 @@ type gobuildOpener struct {
disableOptimizations bool
trimpath bool
buildConfigs map[string]Config
env []string
flags []string
ldflags []string
defaultEnv []string
defaultFlags []string
defaultLdflags []string
platforms []string
labels map[string]string
dir string
Expand Down Expand Up @@ -151,9 +151,9 @@ func (gbo *gobuildOpener) Open() (Interface, error) {
disableOptimizations: gbo.disableOptimizations,
trimpath: gbo.trimpath,
buildConfigs: gbo.buildConfigs,
env: gbo.env,
flags: gbo.flags,
ldflags: gbo.ldflags,
defaultEnv: gbo.defaultEnv,
defaultFlags: gbo.defaultFlags,
defaultLdflags: gbo.defaultLdflags,
labels: gbo.labels,
dir: gbo.dir,
platformMatcher: matcher,
Expand Down Expand Up @@ -320,7 +320,7 @@ func build(ctx context.Context, buildCtx buildContext) (string, error) {
gobin := getGoBinary()
cmd := exec.CommandContext(ctx, gobin, args...)
cmd.Dir = buildCtx.dir
cmd.Env = buildCtx.mergedEnv
cmd.Env = buildCtx.env

var output bytes.Buffer
cmd.Stderr = &output
Expand Down Expand Up @@ -493,9 +493,9 @@ func cycloneDX() sbomber {
}

// buildEnv creates the environment variables used by the `go build` command.
// From `os/exec.Cmd`: If Env contains duplicate environment keys, only the last
// From `os/exec.Cmd`: If there are duplicate environment keys, only the last
// value in the slice for each duplicate key is used.
func buildEnv(platform v1.Platform, osEnv, globalEnv, configEnv []string) ([]string, error) {
func buildEnv(platform v1.Platform, osEnv, buildEnv []string) ([]string, error) {
// Default env
env := []string{
"CGO_ENABLED=0",
Expand All @@ -520,8 +520,7 @@ func buildEnv(platform v1.Platform, osEnv, globalEnv, configEnv []string) ([]str
}

env = append(env, osEnv...)
env = append(env, globalEnv...)
env = append(env, configEnv...)
env = append(env, buildEnv...)
return env, nil
}

Expand Down Expand Up @@ -768,7 +767,7 @@ func createTemplateData(ctx context.Context, buildCtx buildContext) (map[string]
envVars := map[string]string{
"LDFLAGS": "",
}
for _, entry := range buildCtx.mergedEnv {
for _, entry := range buildCtx.env {
kv := strings.SplitN(entry, "=", 2)
if len(kv) != 2 {
return nil, fmt.Errorf("invalid environment variable entry: %q", entry)
Expand Down Expand Up @@ -837,17 +836,17 @@ func createBuildArgs(ctx context.Context, buildCtx buildContext) ([]string, erro
return nil, err
}

if len(buildCtx.mergedFlags) > 0 {
flags, err := applyTemplating(buildCtx.mergedFlags, data)
if len(buildCtx.flags) > 0 {
flags, err := applyTemplating(buildCtx.flags, data)
if err != nil {
return nil, err
}

args = append(args, flags...)
}

if len(buildCtx.mergedLdflags) > 0 {
ldflags, err := applyTemplating(buildCtx.mergedLdflags, data)
if len(buildCtx.ldflags) > 0 {
ldflags, err := applyTemplating(buildCtx.ldflags, data)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -928,31 +927,40 @@ func (g *gobuild) buildOne(ctx context.Context, refStr string, base v1.Image, pl

config := g.configForImportPath(ref.Path())

// Merge the system, global, and build config environment variables.
mergedEnv, err := buildEnv(*platform, os.Environ(), g.env, config.Env)
// Merge the system and build environment variables.
env := config.Env
if len(env) == 0 {
// Use the default, if any.
env = g.defaultEnv
}
env, err = buildEnv(*platform, os.Environ(), env)
if err != nil {
return nil, fmt.Errorf("could not create env for %s: %w", ref.Path(), err)
}

// Merge global and build config flags.
var mergedFlags []string
mergedFlags = append(mergedFlags, g.flags...)
mergedFlags = append(mergedFlags, config.Flags...)
// Get the build flags.
flags := config.Flags
if len(flags) == 0 {
// Use the default, if any.
flags = g.defaultFlags
}

// Merge global and build config ldflags.
var mergedLdflags []string
mergedLdflags = append(mergedLdflags, g.ldflags...)
mergedLdflags = append(mergedLdflags, config.Ldflags...)
// Get the build ldflags.
ldflags := config.Ldflags
if len(ldflags) == 0 {
// Use the default, if any
ldflags = g.defaultLdflags
}

// Do the build into a temporary file.
file, err := g.build(ctx, buildContext{
creationTime: g.creationTime,
ip: ref.Path(),
dir: g.dir,
mergedEnv: mergedEnv,
mergedFlags: mergedFlags,
mergedLdflags: mergedLdflags,
platform: *platform,
creationTime: g.creationTime,
ip: ref.Path(),
dir: g.dir,
env: env,
flags: flags,
ldflags: ldflags,
platform: *platform,
})
if err != nil {
return nil, fmt.Errorf("build: %w", err)
Expand Down
Loading

0 comments on commit 9500660

Please sign in to comment.