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

Refactor global values to be defaults #1318

Merged
merged 2 commits into from
May 21, 2024
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
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
10 changes: 9 additions & 1 deletion integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ else
echo "Test PASSED"
fi

echo "2. Linux capabilities."
echo "2. Test knative 'KO_FLAGS' variable is ignored."
RESULT="$(KO_FLAGS="--platform=badvalue" ./ko build --local --platform="linux/$GOARCH" "$GOPATH/src/github.com/google/ko/test" | grep "$FILTER" | xargs -I% docker run %)"
if [[ "$RESULT" != *"Hello there"* ]]; then
echo "Test FAILED. Saw $RESULT" && exit 1
else
echo "Test PASSED"
fi

echo "3. Linux capabilities."
pushd test/build-configs || exit 1
# run as non-root user with net_bind_service cap granted
docker_run_opts="--user 1 --cap-add=net_bind_service"
Expand Down
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
Loading