GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat: add platform support to pack builder (#3603) · knative/func@2eabfc8

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • cmd
    • build.go
    • errors.go
  • docs/reference
    • func_build.md
  • pkg/buildpacks
    • builder.go
    • builder_test.go
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎cmd/build.go‎

Copy file name to clipboardExpand all lines: cmd/build.go
+9-10Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ EXAMPLES
123123
cmd.Flags().BoolP("push", "u", false,
124124
"Attempt to push the function image to the configured registry after being successfully built")
125125
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")
127127
cmd.Flags().StringP("username", "", "",
128128
"Username to use when pushing to the registry. ($FUNC_USERNAME)")
129129
cmd.Flags().StringP("password", "", "",
@@ -234,7 +234,7 @@ type buildConfig struct {
234234
// working directory of the process.
235235
Path string
236236

237-
// Platform ofr resultant image (s2i builder only)
237+
// Platform for resultant image (s2i and pack builders)
238238
Platform string
239239

240240
// Push the resulting image to the registry after building.
@@ -414,8 +414,8 @@ func (c buildConfig) Validate(cmd *cobra.Command) (err error) {
414414
return fn.ErrConflictingImageAndRegistry
415415
}
416416

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 {
419419
err = fn.ErrPlatformNotSupported
420420
return
421421
}
@@ -433,11 +433,10 @@ func (c buildConfig) Validate(cmd *cobra.Command) (err error) {
433433
// builder and pusher are the default implementations and the Pack and S2I
434434
// constructors simplified.
435435
//
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.
441440
//
442441
// TODO: As a further optimization, it might be ideal to only build the
443442
// 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) {
496495
//
497496
// TODO: upgrade --platform to a multi-value field. The individual builder
498497
// 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
500499
// supports multi.
501500
if c.Platform != "" {
502501
parts := strings.Split(c.Platform, "/")
Collapse file

‎cmd/errors.go‎

Copy file name to clipboardExpand all lines: cmd/errors.go
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,16 @@ func NewErrPlatformNotSupported(err error, cmd string) error {
8383
func (e *ErrPlatformNotSupported) Error() string {
8484
return fmt.Sprintf(`%v
8585
86-
The --platform flag is only supported with the S2I builder.
86+
The --platform flag is only supported with the S2I and Pack builders.
8787
8888
Try this:
8989
func %s --registry <registry> --builder=s2i --platform linux/amd64
90+
func %s --registry <registry> --builder=pack --platform linux/amd64
9091
9192
Or remove the --platform flag:
9293
func %s --registry <registry>
9394
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)
9596
}
9697

9798
func (e *ErrPlatformNotSupported) Unwrap() error {
Collapse file

‎docs/reference/func_build.md‎

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 numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func build
6666
-i, --image string Full image name in the form [registry]/[namespace]/[name]:[tag] (optional). This option takes precedence over --registry ($FUNC_IMAGE)
6767
--password string Password to use when pushing to the registry. ($FUNC_PASSWORD)
6868
-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
7070
-u, --push Attempt to push the function image to the configured registry after being successfully built
7171
-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)
7272
--registry-authfile string Path to a authentication file containing registry credentials ($FUNC_REGISTRY_AUTHFILE)
Collapse file

‎pkg/buildpacks/builder.go‎

Copy file name to clipboardExpand all lines: pkg/buildpacks/builder.go
+13-4Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,22 @@ var DefaultLifecycleImage = "docker.io/buildpacksio/lifecycle:553c041"
121121

122122
// Build the Function at path.
123123
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-
128124
// Builder image from the function if defined, default otherwise.
129125
image, err := BuilderImage(f, b.name)
130126
if err != nil {
131127
return
132128
}
133129

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+
134140
buildpacks := f.Build.Buildpacks
135141
if len(buildpacks) == 0 {
136142
buildpacks = defaultBuildpacks[f.Runtime]
@@ -172,6 +178,9 @@ func (b *Builder) Build(ctx context.Context, f fn.Function, platforms []fn.Platf
172178
Volumes []string
173179
}{Network: "", Volumes: nil},
174180
}
181+
if len(platforms) == 1 {
182+
opts.Platform = strings.ToLower(platforms[0].OS + "/" + platforms[0].Architecture)
183+
}
175184
if b.withTimestamp {
176185
now := time.Now()
177186
opts.CreationTime = &now
Collapse file

‎pkg/buildpacks/builder_test.go‎

Copy file name to clipboardExpand all lines: pkg/buildpacks/builder_test.go
+72Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,78 @@ func TestBuild_Errors(t *testing.T) {
304304
}
305305
}
306306

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+
307379
type mockImpl struct {
308380
BuildFn func(context.Context, pack.BuildOptions) error
309381
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.