Skip to content

Commit 3f645a2

Browse files
committed
fix: specifying platforms in ko builder
1 parent 9de79cd commit 3f645a2

File tree

7 files changed

+30
-27
lines changed

7 files changed

+30
-27
lines changed

docs/content/en/schemas/v2beta28.json

+1-11
Original file line numberDiff line numberDiff line change
@@ -2714,15 +2714,6 @@
27142714
"type": "string",
27152715
"description": "location of the main package. It is the pattern passed to `go build`. If main is specified as a relative path, it is relative to the `context` directory. If main is empty, the ko builder uses a default value of `.`. If main is a pattern with wildcards, such as `./...`, the expansion must contain only one main package, otherwise ko fails. Main is ignored if the `ImageName` starts with `ko://`. Example: `./cmd/foo`.",
27162716
"x-intellij-html-description": "location of the main package. It is the pattern passed to <code>go build</code>. If main is specified as a relative path, it is relative to the <code>context</code> directory. If main is empty, the ko builder uses a default value of <code>.</code>. If main is a pattern with wildcards, such as <code>./...</code>, the expansion must contain only one main package, otherwise ko fails. Main is ignored if the <code>ImageName</code> starts with <code>ko://</code>. Example: <code>./cmd/foo</code>."
2717-
},
2718-
"platforms": {
2719-
"items": {
2720-
"type": "string"
2721-
},
2722-
"type": "array",
2723-
"description": "list of platforms to build images for. Each platform is of the format `os[/arch[/variant]]`, e.g., `linux/amd64`. Use `[\"all\"]` to build for all platforms supported by the base image. If empty, the builder uses the ko default (`[\"linux/amd64\"]`). Example: `[\"linux/amd64\", \"linux/arm64\"]`.",
2724-
"x-intellij-html-description": "list of platforms to build images for. Each platform is of the format <code>os[/arch[/variant]]</code>, e.g., <code>linux/amd64</code>. Use <code>[&quot;all&quot;]</code> to build for all platforms supported by the base image. If empty, the builder uses the ko default (<code>[&quot;linux/amd64&quot;]</code>). Example: <code>[&quot;linux/amd64&quot;, &quot;linux/arm64&quot;]</code>.",
2725-
"default": "[]"
27262717
}
27272718
},
27282719
"preferredOrder": [
@@ -2733,8 +2724,7 @@
27332724
"flags",
27342725
"labels",
27352726
"ldflags",
2736-
"main",
2737-
"platforms"
2727+
"main"
27382728
],
27392729
"additionalProperties": false,
27402730
"type": "object",

pkg/skaffold/build/ko/build.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (b *Builder) Build(ctx context.Context, out io.Writer, a *latestV1.Artifact
4141
if b.pushImages && strings.HasPrefix(ref, build.StrictScheme) {
4242
return "", fmt.Errorf("default repo must be set when using the 'ko://' prefix and pushing to a registry: %s, see https://skaffold.dev/docs/environment/image-registries/", a.ImageName)
4343
}
44-
koBuilder, err := b.newKoBuilder(ctx, a)
44+
koBuilder, err := b.newKoBuilder(ctx, a, platforms)
4545
if err != nil {
4646
return "", fmt.Errorf("error creating ko builder: %w", err)
4747
}

pkg/skaffold/build/ko/builder.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,21 @@ import (
2727
"github.com/google/ko/pkg/commands/options"
2828

2929
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
30+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform"
3031
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
3132
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
3233
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
3334
)
3435

35-
func (b *Builder) newKoBuilder(ctx context.Context, a *latestV1.Artifact) (build.Interface, error) {
36-
bo, err := buildOptions(a, b.runMode)
36+
func (b *Builder) newKoBuilder(ctx context.Context, a *latestV1.Artifact, platforms platform.Matcher) (build.Interface, error) {
37+
bo, err := buildOptions(a, b.runMode, platforms)
3738
if err != nil {
3839
return nil, fmt.Errorf("could not construct ko build options: %v", err)
3940
}
4041
return commands.NewBuilder(ctx, bo)
4142
}
4243

43-
func buildOptions(a *latestV1.Artifact, runMode config.RunMode) (*options.BuildOptions, error) {
44+
func buildOptions(a *latestV1.Artifact, runMode config.RunMode, platforms platform.Matcher) (*options.BuildOptions, error) {
4445
buildconfig, err := buildConfig(a)
4546
if err != nil {
4647
return nil, fmt.Errorf("could not create ko build config: %v", err)
@@ -55,7 +56,7 @@ func buildOptions(a *latestV1.Artifact, runMode config.RunMode) (*options.BuildO
5556
ConcurrentBuilds: 1, // we could plug in Skaffold's max builds here, but it'd be incorrect if users build more than one artifact
5657
DisableOptimizations: runMode == config.RunModes.Debug,
5758
Labels: imageLabels,
58-
Platform: strings.Join(a.KoArtifact.Platforms, ","),
59+
Platform: platforms.String(),
5960
Trimpath: runMode != config.RunModes.Debug,
6061
UserAgent: version.UserAgentWithClient(),
6162
WorkingDirectory: filepath.Join(a.Workspace, a.KoArtifact.Dir),

pkg/skaffold/build/ko/builder_test.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ import (
2525
"github.com/google/go-cmp/cmp/cmpopts"
2626
"github.com/google/ko/pkg/build"
2727
"github.com/google/ko/pkg/commands/options"
28+
specs "github.com/opencontainers/image-spec/specs-go/v1"
2829

2930
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
31+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/platform"
3032
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
3133
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
3234
"github.com/GoogleContainerTools/skaffold/testutil"
@@ -40,6 +42,7 @@ func TestBuildOptions(t *testing.T) {
4042
tests := []struct {
4143
description string
4244
artifact latestV1.Artifact
45+
platforms platform.Matcher
4346
envVarValue string
4447
runMode config.RunMode
4548
wantBo options.BuildOptions
@@ -82,13 +85,14 @@ func TestBuildOptions(t *testing.T) {
8285
fmt.Sprintf("-ldflag-{{.%s}}", testKoBuildOptionsEnvVar),
8386
fmt.Sprintf("-ldflag2-{{.Env.%s}}", testKoBuildOptionsEnvVar),
8487
},
85-
Main: "cmd/app",
86-
Platforms: []string{"linux/amd64", "linux/arm64"},
88+
Main: "cmd/app",
8789
},
8890
},
8991
ImageName: "ko://example.com/foo",
9092
Workspace: "workdir",
9193
},
94+
platforms: platform.Matcher{Platforms: []specs.Platform{{OS: "linux", Architecture: "amd64"}, {OS: "linux", Architecture: "arm64"}}},
95+
9296
envVarValue: "baz",
9397
runMode: config.RunModes.Debug,
9498
wantBo: options.BuildOptions{
@@ -148,7 +152,7 @@ func TestBuildOptions(t *testing.T) {
148152
for _, test := range tests {
149153
testutil.Run(t, test.description, func(t *testutil.T) {
150154
os.Setenv(testKoBuildOptionsEnvVar, test.envVarValue)
151-
gotBo, err := buildOptions(&test.artifact, test.runMode)
155+
gotBo, err := buildOptions(&test.artifact, test.runMode, test.platforms)
152156
defer os.Unsetenv(testKoBuildOptionsEnvVar)
153157
t.CheckErrorAndFailNow(false, err)
154158
t.CheckDeepEqual(test.wantBo, *gotBo,

pkg/skaffold/schema/latest/v1/config.go

-7
Original file line numberDiff line numberDiff line change
@@ -1403,13 +1403,6 @@ type KoArtifact struct {
14031403
// Main is ignored if the `ImageName` starts with `ko://`.
14041404
// Example: `./cmd/foo`.
14051405
Main string `yaml:"main,omitempty"`
1406-
1407-
// Platforms is the list of platforms to build images for.
1408-
// Each platform is of the format `os[/arch[/variant]]`, e.g., `linux/amd64`.
1409-
// Use `["all"]` to build for all platforms supported by the base image.
1410-
// If empty, the builder uses the ko default (`["linux/amd64"]`).
1411-
// Example: `["linux/amd64", "linux/arm64"]`.
1412-
Platforms []string `yaml:"platforms,omitempty"`
14131406
}
14141407

14151408
// KoDependencies is used to specify dependencies for an artifact built by ko.

pkg/skaffold/schema/v2beta27/upgrade.go

+13
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,18 @@ func (c *SkaffoldConfig) Upgrade() (util.VersionedConfig, error) {
3434
}
3535

3636
func upgradeOnePipeline(oldPipeline, newPipeline interface{}) error {
37+
oldBuild := &oldPipeline.(*Pipeline).Build
38+
newBuild := &newPipeline.(*next.Pipeline).Build
39+
40+
// move: artifact.ko.Platforms
41+
// to: artifact.Platforms
42+
for i, newArtifact := range newBuild.Artifacts {
43+
oldArtifact := oldBuild.Artifacts[i]
44+
if oldArtifact.KoArtifact == nil || len(oldArtifact.KoArtifact.Platforms) == 0 {
45+
continue
46+
}
47+
newArtifact.Platforms = oldArtifact.KoArtifact.Platforms
48+
}
49+
3750
return nil
3851
}

pkg/skaffold/schema/v2beta27/upgrade_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ build:
5151
sync:
5252
auto: true
5353
- image: ko://github.com/GoogleContainerTools/skaffold/cmd/skaffold
54-
ko: {}
54+
ko:
55+
platforms: ['linux/arm64', 'linux/amd64']
5556
googleCloudBuild:
5657
projectId: test-project
5758
test:
@@ -133,6 +134,7 @@ build:
133134
auto: true
134135
- image: ko://github.com/GoogleContainerTools/skaffold/cmd/skaffold
135136
ko: {}
137+
platforms: ['linux/arm64', 'linux/amd64']
136138
googleCloudBuild:
137139
projectId: test-project
138140
test:

0 commit comments

Comments
 (0)