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

Add option to configure default platforms #897

Merged
merged 1 commit into from
Dec 12, 2022
Merged
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
24 changes: 24 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,30 @@ 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:

**As a parameter**
See [Multi-Platform Images](./features/multi-platform.md).

**In .ko.yaml**
Add this to your `.ko.yaml` file:

```yaml
defaultPlatforms:
- linux/arm64
- linux/amd64
```

You can also use the `KO_DEFAULTPLATFORMS` environment variable to set the default platforms, which overrides the YAML configuration:

```shell
KO_DEFAULTPLATFORMS=linux/arm64,linux/amd64
```


## Naming Images

`ko` provides a few different strategies for naming the image it pushes, to
Expand Down
8 changes: 8 additions & 0 deletions pkg/commands/options/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ type BuildOptions struct {
// BaseImageOverrides stores base image overrides for import paths.
BaseImageOverrides map[string]string

// DefaultPlatforms defines the default platforms when Platforms is not explicitly defined
DefaultPlatforms []string

// WorkingDirectory allows for setting the working directory for invocations of the `go` tool.
// Empty string means the current working directory.
WorkingDirectory string
Expand Down Expand Up @@ -129,6 +132,11 @@ func (bo *BuildOptions) LoadConfig() error {
}
}

dp := v.GetStringSlice("defaultPlatforms")
if len(dp) > 0 {
bo.DefaultPlatforms = dp
}

if bo.BaseImage == "" {
ref := v.GetString("defaultBaseImage")
if _, err := name.ParseReference(ref); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions pkg/commands/options/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package options

import (
"os"
"reflect"
"strings"
"testing"

Expand All @@ -38,6 +39,34 @@ func TestDefaultBaseImage(t *testing.T) {
}
}

func TestDefaultPlatformsAll(t *testing.T) {
allBo := &BuildOptions{
WorkingDirectory: "testdata/config",
}
err := allBo.LoadConfig()
if err != nil {
t.Fatal(err)
}

wantDefaultPlatformsAll := []string{"all"} // matches value in ./testdata/config/.ko.yaml
if !reflect.DeepEqual(allBo.DefaultPlatforms, wantDefaultPlatformsAll) {
t.Fatalf("wanted DefaultPlatforms %s, got %s", wantDefaultPlatformsAll, allBo.DefaultPlatforms)
}

multipleBo := &BuildOptions{
WorkingDirectory: "testdata/multiple-platforms",
}
err = multipleBo.LoadConfig()
if err != nil {
t.Fatal(err)
}

wantDefaultPlatformsMultiple := []string{"linux/arm64", "linux/amd64"} // matches value in ./testdata/multiple-platforms/.ko.yaml
if !reflect.DeepEqual(multipleBo.DefaultPlatforms, wantDefaultPlatformsMultiple) {
t.Fatalf("wanted DefaultPlatforms %s, got %s", wantDefaultPlatformsMultiple, multipleBo.DefaultPlatforms)
}
}

func TestBuildConfigWithWorkingDirectoryAndDirAndMain(t *testing.T) {
bo := &BuildOptions{
WorkingDirectory: "testdata/paths",
Expand Down
1 change: 1 addition & 0 deletions pkg/commands/options/testdata/config/.ko.yaml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
defaultBaseImage: alpine
defaultPlatforms: all
4 changes: 4 additions & 0 deletions pkg/commands/options/testdata/multiple-platforms/.ko.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defaultBaseImage: alpine
defaultPlatforms:
- linux/arm64
- linux/amd64
ReToCode marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
return nil, err
}

if len(bo.Platforms) == 0 && len(bo.DefaultPlatforms) > 0 {
bo.Platforms = bo.DefaultPlatforms
}

if len(bo.Platforms) == 0 {
envPlatform := "linux/amd64"

Expand All @@ -78,7 +82,7 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
// Make sure these are all unset
for _, env := range []string{"GOOS", "GOARCH", "GOARM"} {
if s, ok := os.LookupEnv(env); ok {
return nil, fmt.Errorf("cannot use --platform with %s=%q", env, s)
return nil, fmt.Errorf("cannot use --platform or defaultPlatforms in .ko.yaml or env KO_DEFAULTPLATFORMS combined with %s=%q", env, s)
}
}
}
Expand Down