From 1b68d1261eb8e8f01aade2737d5268e54f4a4604 Mon Sep 17 00:00:00 2001 From: Colin Casey Date: Tue, 30 Jan 2024 15:12:55 -0400 Subject: [PATCH] Separate suggested builders from default trusted builders Signed-off-by: Colin Casey --- ...{suggested_builder.go => known_builder.go} | 20 +++++++++++++++++-- internal/commands/builder_suggest_test.go | 6 +++--- internal/commands/config_trusted_builder.go | 6 ++++-- internal/commands/suggest_builders.go | 18 +++++++++++------ internal/commands/suggest_builders_test.go | 6 +++--- pkg/client/build.go | 8 ++++---- 6 files changed, 44 insertions(+), 20 deletions(-) rename internal/builder/{suggested_builder.go => known_builder.go} (76%) diff --git a/internal/builder/suggested_builder.go b/internal/builder/known_builder.go similarity index 76% rename from internal/builder/suggested_builder.go rename to internal/builder/known_builder.go index 9495ce0a5b..ebdaba3cd2 100644 --- a/internal/builder/suggested_builder.go +++ b/internal/builder/known_builder.go @@ -1,45 +1,61 @@ package builder -type SuggestedBuilder struct { +type KnownBuilder struct { Vendor string Image string DefaultDescription string + Suggested bool + Trusted bool } -var SuggestedBuilders = []SuggestedBuilder{ +var KnownBuilders = []KnownBuilder{ { Vendor: "Google", Image: "gcr.io/buildpacks/builder:v1", DefaultDescription: "GCP Builder for all runtimes", + Suggested: true, + Trusted: true, }, { Vendor: "Heroku", Image: "heroku/builder:22", DefaultDescription: "Heroku-22 (Ubuntu 22.04) base image with buildpacks for Go, Java, Node.js, PHP, Python, Ruby & Scala", + Suggested: true, + Trusted: true, }, { Vendor: "Heroku", Image: "heroku/builder:20", DefaultDescription: "Heroku-20 (Ubuntu 20.04) base image with buildpacks for Go, Java, Node.js, PHP, Python, Ruby & Scala", + Suggested: true, + Trusted: true, }, { Vendor: "Paketo Buildpacks", Image: "paketobuildpacks/builder-jammy-base", DefaultDescription: "Small base image with buildpacks for Java, Node.js, Golang, .NET Core, Python & Ruby", + Suggested: true, + Trusted: true, }, { Vendor: "Paketo Buildpacks", Image: "paketobuildpacks/builder-jammy-full", DefaultDescription: "Larger base image with buildpacks for Java, Node.js, Golang, .NET Core, Python, Ruby, & PHP", + Suggested: true, + Trusted: true, }, { Vendor: "Paketo Buildpacks", Image: "paketobuildpacks/builder-jammy-tiny", DefaultDescription: "Tiny base image (jammy build image, distroless run image) with buildpacks for Golang & Java", + Suggested: true, + Trusted: true, }, { Vendor: "Paketo Buildpacks", Image: "paketobuildpacks/builder-jammy-buildpackless-static", DefaultDescription: "Static base image (jammy build image, distroless run image) suitable for static binaries like Go or Rust", + Suggested: true, + Trusted: true, }, } diff --git a/internal/commands/builder_suggest_test.go b/internal/commands/builder_suggest_test.go index f12bb0b164..75a9db7aff 100644 --- a/internal/commands/builder_suggest_test.go +++ b/internal/commands/builder_suggest_test.go @@ -47,7 +47,7 @@ func testSuggestCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays descriptions from metadata", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", @@ -65,7 +65,7 @@ func testSuggestCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays default descriptions", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", @@ -81,7 +81,7 @@ func testSuggestCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays default descriptions", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", diff --git a/internal/commands/config_trusted_builder.go b/internal/commands/config_trusted_builder.go index fe50041e65..3de01d3824 100644 --- a/internal/commands/config_trusted_builder.go +++ b/internal/commands/config_trusted_builder.go @@ -102,8 +102,10 @@ func listTrustedBuilders(args []string, logger logging.Logger, cfg config.Config logger.Info("Trusted Builders:") var trustedBuilders []string - for _, builder := range bldr.SuggestedBuilders { - trustedBuilders = append(trustedBuilders, builder.Image) + for _, knownBuilder := range bldr.KnownBuilders { + if knownBuilder.Trusted { + trustedBuilders = append(trustedBuilders, knownBuilder.Image) + } } for _, builder := range cfg.TrustedBuilders { diff --git a/internal/commands/suggest_builders.go b/internal/commands/suggest_builders.go index 0d8772ed92..d5e8fb657d 100644 --- a/internal/commands/suggest_builders.go +++ b/internal/commands/suggest_builders.go @@ -39,10 +39,16 @@ func suggestSettingBuilder(logger logging.Logger, inspector BuilderInspector) { } func suggestBuilders(logger logging.Logger, client BuilderInspector) { - WriteSuggestedBuilder(logger, client, bldr.SuggestedBuilders) + suggestedBuilders := []bldr.KnownBuilder{} + for _, knownBuilder := range bldr.KnownBuilders { + if knownBuilder.Suggested { + suggestedBuilders = append(suggestedBuilders, knownBuilder) + } + } + WriteSuggestedBuilder(logger, client, suggestedBuilders) } -func WriteSuggestedBuilder(logger logging.Logger, inspector BuilderInspector, builders []bldr.SuggestedBuilder) { +func WriteSuggestedBuilder(logger logging.Logger, inspector BuilderInspector, builders []bldr.KnownBuilder) { sort.Slice(builders, func(i, j int) bool { if builders[i].Vendor == builders[j].Vendor { return builders[i].Image < builders[j].Image @@ -60,7 +66,7 @@ func WriteSuggestedBuilder(logger logging.Logger, inspector BuilderInspector, bu wg.Add(len(builders)) for i, builder := range builders { - go func(w *sync.WaitGroup, i int, builder bldr.SuggestedBuilder) { + go func(w *sync.WaitGroup, i int, builder bldr.KnownBuilder) { descriptions[i] = getBuilderDescription(builder, inspector) w.Done() }(&wg, i, builder) @@ -78,7 +84,7 @@ func WriteSuggestedBuilder(logger logging.Logger, inspector BuilderInspector, bu logger.Info("\tpack builder inspect ") } -func getBuilderDescription(builder bldr.SuggestedBuilder, inspector BuilderInspector) string { +func getBuilderDescription(builder bldr.KnownBuilder, inspector BuilderInspector) string { info, err := inspector.InspectBuilder(builder.Image, false) if err == nil && info != nil && info.Description != "" { return info.Description @@ -88,8 +94,8 @@ func getBuilderDescription(builder bldr.SuggestedBuilder, inspector BuilderInspe } func isSuggestedBuilder(builder string) bool { - for _, sugBuilder := range bldr.SuggestedBuilders { - if builder == sugBuilder.Image { + for _, knownBuilder := range bldr.KnownBuilders { + if builder == knownBuilder.Image && knownBuilder.Suggested { return true } } diff --git a/internal/commands/suggest_builders_test.go b/internal/commands/suggest_builders_test.go index 6759e21de1..12d38c16e2 100644 --- a/internal/commands/suggest_builders_test.go +++ b/internal/commands/suggest_builders_test.go @@ -47,7 +47,7 @@ func testSuggestBuildersCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays descriptions from metadata", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", @@ -65,7 +65,7 @@ func testSuggestBuildersCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays default descriptions", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", @@ -81,7 +81,7 @@ func testSuggestBuildersCommand(t *testing.T, when spec.G, it spec.S) { }) it("displays default descriptions", func() { - commands.WriteSuggestedBuilder(logger, mockClient, []bldr.SuggestedBuilder{{ + commands.WriteSuggestedBuilder(logger, mockClient, []bldr.KnownBuilder{{ Vendor: "Builder", Image: "gcr.io/some/builder:latest", DefaultDescription: "Default description", diff --git a/pkg/client/build.go b/pkg/client/build.go index 9b94706fb9..5d63317c15 100644 --- a/pkg/client/build.go +++ b/pkg/client/build.go @@ -274,9 +274,9 @@ type layoutPathConfig struct { targetRunImagePath string } -var IsSuggestedBuilderFunc = func(b string) bool { - for _, suggestedBuilder := range builder.SuggestedBuilders { - if b == suggestedBuilder.Image { +var IsTrustedBuilderFunc = func(b string) bool { + for _, knownBuilder := range builder.KnownBuilders { + if b == knownBuilder.Image && knownBuilder.Trusted { return true } } @@ -384,7 +384,7 @@ func (c *Client) Build(ctx context.Context, opts BuildOptions) error { // Default mode: if the TrustBuilder option is not set, trust the suggested builders. if opts.TrustBuilder == nil { - opts.TrustBuilder = IsSuggestedBuilderFunc + opts.TrustBuilder = IsTrustedBuilderFunc } // Ensure the builder's platform APIs are supported