GeezerVison is showing github.com — feat: add platform support to pack builder (#3603) · knative/func@2eabfc8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/build.go +9-10Lines changed: 9 additions & 10 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -123,7 +123,7 @@ EXAMPLES | 123 | 123 | cmd.Flags().BoolP("push", "u", false, | 124 | 124 | "Attempt to push the function image to the configured registry after being successfully built") | 125 | 125 | cmd.Flags().StringP("platform", "", "", | 126 | | - "Optionally specify a target platform, for example \"linux/amd64\" when using the s2i build strategy") | | 126 | + "Optionally specify a target platform, for example \"linux/amd64\" when using the s2i or pack build strategy") | 127 | 127 | cmd.Flags().StringP("username", "", "", | 128 | 128 | "Username to use when pushing to the registry. ($FUNC_USERNAME)") | 129 | 129 | cmd.Flags().StringP("password", "", "", | @@ -234,7 +234,7 @@ type buildConfig struct { | 234 | 234 | // working directory of the process. | 235 | 235 | Path string | 236 | 236 |
| 237 | | - // Platform ofr resultant image (s2i builder only) | | 237 | + // Platform for resultant image (s2i and pack builders) | 238 | 238 | Platform string | 239 | 239 |
| 240 | 240 | // Push the resulting image to the registry after building. | @@ -414,8 +414,8 @@ func (c buildConfig) Validate(cmd *cobra.Command) (err error) { | 414 | 414 | return fn.ErrConflictingImageAndRegistry | 415 | 415 | } | 416 | 416 |
| 417 | | - // Platform is only supported with the S2I builder at this time | 418 | | - if c.Platform != "" && c.Builder != builders.S2I { | | 417 | + // Platform is only supported with the S2I and Pack builders | | 418 | + if c.Platform != "" && c.Builder != builders.S2I && c.Builder != builders.Pack { | 419 | 419 | err = fn.ErrPlatformNotSupported | 420 | 420 | return | 421 | 421 | } | @@ -433,11 +433,10 @@ func (c buildConfig) Validate(cmd *cobra.Command) (err error) { | 433 | 433 | // builder and pusher are the default implementations and the Pack and S2I | 434 | 434 | // constructors simplified. | 435 | 435 | // | 436 | | -// TODO: Platform is currently only used by the S2I builder. This should be | 437 | | -// a multi-valued argument which passes through to the "host" builder (which | 438 | | -// supports multi-arch/platform images), and throw an error if either trying | 439 | | -// to specify a platform for buildpacks, or trying to specify more than one | 440 | | -// for S2I. | | 436 | +// TODO: Platform is currently only used by the S2I and Pack builders (single | | 437 | +// platform each). This should be a multi-valued argument which passes through | | 438 | +// to the "host" builder (which supports multi-arch/platform images), and throw | | 439 | +// an error if trying to specify more than one for S2I or Pack. | 441 | 440 | // | 442 | 441 | // TODO: As a further optimization, it might be ideal to only build the | 443 | 442 | // image necessary for the target cluster, since the end product of a function | @@ -496,7 +495,7 @@ func (c buildConfig) buildOptions() (oo []fn.BuildOption, err error) { | 496 | 495 | // | 497 | 496 | // TODO: upgrade --platform to a multi-value field. The individual builder | 498 | 497 | // implementations are responsible for bubbling an error if they do | 499 | | - // not support this. Pack supports none, S2I supports one, host builder | | 498 | + // not support this. Pack supports one, S2I supports one, host builder | 500 | 499 | // supports multi. | 501 | 500 | if c.Platform != "" { | 502 | 501 | parts := strings.Split(c.Platform, "/") | |
Collapse file Copy file name to clipboardExpand all lines: cmd/errors.go +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -83,15 +83,16 @@ func NewErrPlatformNotSupported(err error, cmd string) error { | 83 | 83 | func (e *ErrPlatformNotSupported) Error() string { | 84 | 84 | return fmt.Sprintf(`%v | 85 | 85 |
| 86 | | -The --platform flag is only supported with the S2I builder. | | 86 | +The --platform flag is only supported with the S2I and Pack builders. | 87 | 87 |
| 88 | 88 | Try this: | 89 | 89 | func %s --registry <registry> --builder=s2i --platform linux/amd64 | | 90 | + func %s --registry <registry> --builder=pack --platform linux/amd64 | 90 | 91 |
| 91 | 92 | Or remove the --platform flag: | 92 | 93 | func %s --registry <registry> | 93 | 94 |
| 94 | | -For more options, run 'func %s --help'`, e.Err, e.Cmd, e.Cmd, e.Cmd) | | 95 | +For more options, run 'func %s --help'`, e.Err, e.Cmd, e.Cmd, e.Cmd, e.Cmd) | 95 | 96 | } | 96 | 97 |
| 97 | 98 | func (e *ErrPlatformNotSupported) Unwrap() error { | |
Collapse file Copy file name to clipboardExpand all lines: docs/reference/func_build.md +1-1Lines changed: 1 addition & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -66,7 +66,7 @@ func build | 66 | 66 | -i, --image string Full image name in the form [registry]/[namespace]/[name]:[tag] (optional). This option takes precedence over --registry ($FUNC_IMAGE) | 67 | 67 | --password string Password to use when pushing to the registry. ($FUNC_PASSWORD) | 68 | 68 | -p, --path string Path to the function. Default is current directory ($FUNC_PATH) | 69 | | - --platform string Optionally specify a target platform, for example "linux/amd64" when using the s2i build strategy | | 69 | + --platform string Optionally specify a target platform, for example "linux/amd64" when using the s2i or pack build strategy | 70 | 70 | -u, --push Attempt to push the function image to the configured registry after being successfully built | 71 | 71 | -r, --registry string Container registry + registry namespace. (ex 'ghcr.io/myuser'). The full image name is automatically determined using this along with function name. ($FUNC_REGISTRY) | 72 | 72 | --registry-authfile string Path to a authentication file containing registry credentials ($FUNC_REGISTRY_AUTHFILE) | |
Collapse file Copy file name to clipboardExpand all lines: pkg/buildpacks/builder.go +13-4Lines changed: 13 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -121,16 +121,22 @@ var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:553c041" | 121 | 121 |
| 122 | 122 | // Build the Function at path. | 123 | 123 | func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platform) (err error) { | 124 | | - if len(platforms) != 0 { | 125 | | - return errors.New("the pack builder does not support specifying target platforms directly") | 126 | | - } | 127 | | - | 128 | 124 | // Builder image from the function if defined, default otherwise. | 129 | 125 | image, err := BuilderImage(f, b.name) | 130 | 126 | if err != nil { | 131 | 127 | return | 132 | 128 | } | 133 | 129 |
| | 130 | + // Validate Platforms | | 131 | + if len(platforms) == 1 { | | 132 | + platform := strings.ToLower(platforms[0].OS + "/" + platforms[0].Architecture) | | 133 | + if image, err = docker.GetPlatformImage(image, platform); err != nil { | | 134 | + return fmt.Errorf("cannot get platform image reference for %q: %w", platform, err) | | 135 | + } | | 136 | + } else if len(platforms) > 1 { | | 137 | + return errors.New("the pack builder currently only supports specifying a single target platform") | | 138 | + } | | 139 | + | 134 | 140 | buildpacks := f.Build.Buildpacks | 135 | 141 | if len(buildpacks) == 0 { | 136 | 142 | buildpacks = defaultBuildpacks[f.Runtime] | @@ -172,6 +178,9 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf | 172 | 178 | Volumes []string | 173 | 179 | }{Network: "", Volumes: nil}, | 174 | 180 | } | | 181 | + if len(platforms) == 1 { | | 182 | + opts.Platform = strings.ToLower(platforms[0].OS + "/" + platforms[0].Architecture) | | 183 | + } | 175 | 184 | if b.withTimestamp { | 176 | 185 | now := time.Now() | 177 | 186 | opts.CreationTime = &now | |
Collapse file Copy file name to clipboardExpand all lines: pkg/buildpacks/builder_test.go +72Lines changed: 72 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -304,6 +304,78 @@ func TestBuild_Errors(t *testing.T) { | 304 | 304 | } | 305 | 305 | } | 306 | 306 |
| | 307 | +// TestBuild_PlatformSingle ensures that when a single platform is specified, | | 308 | +// opts.Platform is set correctly on the pack BuildOptions. | | 309 | +func TestBuild_PlatformSingle(t *testing.T) { | | 310 | + var ( | | 311 | + i = &mockImpl{} | | 312 | + b = NewBuilder(WithImpl(i)) | | 313 | + f = fn.Function{Runtime: "node"} | | 314 | + ) | | 315 | + | | 316 | + i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error { | | 317 | + expected := "linux/amd64" | | 318 | + if opts.Platform != expected { | | 319 | + t.Fatalf("expected platform '%v', got '%v'", expected, opts.Platform) | | 320 | + } | | 321 | + return nil | | 322 | + } | | 323 | + | | 324 | + platforms := []fn.Platform{{OS: "linux", Architecture: "amd64"}} | | 325 | + if err := b.Build(t.Context(), f, platforms); err != nil { | | 326 | + t.Fatal(err) | | 327 | + } | | 328 | +} | | 329 | + | | 330 | +// TestBuild_PlatformMultiple ensures that specifying more than one platform | | 331 | +// returns an error. | | 332 | +func TestBuild_PlatformMultiple(t *testing.T) { | | 333 | + var ( | | 334 | + i = &mockImpl{} | | 335 | + b = NewBuilder(WithImpl(i)) | | 336 | + f = fn.Function{Runtime: "node"} | | 337 | + ) | | 338 | + | | 339 | + i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error { | | 340 | + t.Fatal("build should not have been invoked") | | 341 | + return nil | | 342 | + } | | 343 | + | | 344 | + platforms := []fn.Platform{ | | 345 | + {OS: "linux", Architecture: "amd64"}, | | 346 | + {OS: "linux", Architecture: "arm64"}, | | 347 | + } | | 348 | + err := b.Build(t.Context(), f, platforms) | | 349 | + if err == nil { | | 350 | + t.Fatal("expected an error but got nil") | | 351 | + } | | 352 | + expected := "the pack builder currently only supports specifying a single target platform" | | 353 | + if err.Error() != expected { | | 354 | + t.Fatalf("expected error %q, got %q", expected, err.Error()) | | 355 | + } | | 356 | +} | | 357 | + | | 358 | +// TestBuild_PlatformNone ensures that passing no platforms still works | | 359 | +// and opts.Platform is empty. | | 360 | +func TestBuild_PlatformNone(t *testing.T) { | | 361 | + var ( | | 362 | + i = &mockImpl{} | | 363 | + b = NewBuilder(WithImpl(i)) | | 364 | + f = fn.Function{Runtime: "node"} | | 365 | + ) | | 366 | + | | 367 | + i.BuildFn = func(ctx context.Context, opts pack.BuildOptions) error { | | 368 | + if opts.Platform != "" { | | 369 | + t.Fatalf("expected empty platform, got '%v'", opts.Platform) | | 370 | + } | | 371 | + return nil | | 372 | + } | | 373 | + | | 374 | + if err := b.Build(t.Context(), f, nil); err != nil { | | 375 | + t.Fatal(err) | | 376 | + } | | 377 | +} | | 378 | + | 307 | 379 | type mockImpl struct { | 308 | 380 | BuildFn func(context.Context, pack.BuildOptions) error | 309 | 381 | } | |
|