From ab8e3c14b42ff102d48ee9a56139741424f4734e Mon Sep 17 00:00:00 2001 From: ReToCode Date: Thu, 1 Dec 2022 13:49:55 +0100 Subject: [PATCH] Add option to configure default platforms --- docs/configuration.md | 24 +++++++++++++++ pkg/commands/options/build.go | 8 +++++ pkg/commands/options/build_test.go | 29 +++++++++++++++++++ pkg/commands/options/testdata/config/.ko.yaml | 1 + .../testdata/multiple-platforms/.ko.yaml | 4 +++ pkg/commands/resolver.go | 6 +++- 6 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 pkg/commands/options/testdata/multiple-platforms/.ko.yaml diff --git a/docs/configuration.md b/docs/configuration.md index 48586cb414..31850d8ff7 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -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 diff --git a/pkg/commands/options/build.go b/pkg/commands/options/build.go index 99e422a9f0..be7d734834 100644 --- a/pkg/commands/options/build.go +++ b/pkg/commands/options/build.go @@ -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 @@ -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 { diff --git a/pkg/commands/options/build_test.go b/pkg/commands/options/build_test.go index 60d4ac46a1..55043cd334 100644 --- a/pkg/commands/options/build_test.go +++ b/pkg/commands/options/build_test.go @@ -16,6 +16,7 @@ package options import ( "os" + "reflect" "strings" "testing" @@ -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", diff --git a/pkg/commands/options/testdata/config/.ko.yaml b/pkg/commands/options/testdata/config/.ko.yaml index b65d481c4a..c502a0b562 100644 --- a/pkg/commands/options/testdata/config/.ko.yaml +++ b/pkg/commands/options/testdata/config/.ko.yaml @@ -1 +1,2 @@ defaultBaseImage: alpine +defaultPlatforms: all diff --git a/pkg/commands/options/testdata/multiple-platforms/.ko.yaml b/pkg/commands/options/testdata/multiple-platforms/.ko.yaml new file mode 100644 index 0000000000..07dacab2c2 --- /dev/null +++ b/pkg/commands/options/testdata/multiple-platforms/.ko.yaml @@ -0,0 +1,4 @@ +defaultBaseImage: alpine +defaultPlatforms: +- linux/arm64 +- linux/amd64 diff --git a/pkg/commands/resolver.go b/pkg/commands/resolver.go index e2b3d16c99..ddf6e5bc01 100644 --- a/pkg/commands/resolver.go +++ b/pkg/commands/resolver.go @@ -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" @@ -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) } } }