From 1dae315970277b8eb4c32b056f80f3fdfff7ca4b Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Fri, 24 Mar 2023 16:17:44 +0530 Subject: [PATCH] change NewBundle method to build bundles through working with options Signed-off-by: Nikhil Sharma --- cmd/main.go | 15 ++++++------- pkg/plugin/bundle.go | 47 +++++++++++++++++++++++++++++++++------ pkg/plugin/bundle_test.go | 8 +++---- 3 files changed, 51 insertions(+), 19 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index fa60babab37..54b9c0a04b0 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -46,17 +46,16 @@ func main() { "Please, check the migration guide to learn how to upgrade your project" // Bundle plugin which built the golang projects scaffold by Kubebuilder go/v3 - gov3Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 3}, - deprecateMessageGoV3Bundle, - kustomizecommonv1.Plugin{}, - golangv3.Plugin{}, + gov3Bundle, _ := plugin.NewBundle(plugin.WithName(golang.DefaultNameQualifier), + plugin.WithVersion(plugin.Version{Number: 3}), + plugin.WithDeprecationMessage(deprecateMessageGoV3Bundle), + plugin.WithPlugins(kustomizecommonv1.Plugin{}, golangv3.Plugin{}), ) // Bundle plugin which built the golang projects scaffold by Kubebuilder go/v4 with kustomize alpha-v2 - gov4Bundle, _ := plugin.NewBundle(golang.DefaultNameQualifier, plugin.Version{Number: 4}, - "", - kustomizecommonv2alpha.Plugin{}, - golangv4.Plugin{}, + gov4Bundle, _ := plugin.NewBundle(plugin.WithName(golang.DefaultNameQualifier), + plugin.WithVersion(plugin.Version{Number: 4}), + plugin.WithPlugins(kustomizecommonv2alpha.Plugin{}, golangv4.Plugin{}), ) fs := machinery.Filesystem{ diff --git a/pkg/plugin/bundle.go b/pkg/plugin/bundle.go index 8a4a9e657ff..6ad74f13979 100644 --- a/pkg/plugin/bundle.go +++ b/pkg/plugin/bundle.go @@ -31,10 +31,43 @@ type bundle struct { deprecateWarning string } +type BundleOption func(*bundle) + +func WithName(name string) BundleOption { + return func(opts *bundle) { + opts.name = name + } +} + +func WithVersion(version Version) BundleOption { + return func(opts *bundle) { + opts.version = version + } +} + +func WithPlugins(plugins ...Plugin) BundleOption { + return func(opts *bundle) { + opts.plugins = plugins + } +} + +func WithDeprecationMessage(msg string) BundleOption { + return func(opts *bundle) { + opts.deprecateWarning = msg + } + +} + // NewBundle creates a new Bundle with the provided name and version, and that wraps the provided plugins. // The list of supported project versions is computed from the provided plugins. -func NewBundle(name string, version Version, deprecateWarning string, plugins ...Plugin) (Bundle, error) { - supportedProjectVersions := CommonSupportedProjectVersions(plugins...) +func NewBundle(opts ...BundleOption) (Bundle, error) { + bundleOpts := bundle{} + + for _, opts := range opts { + opts(&bundleOpts) + } + + supportedProjectVersions := CommonSupportedProjectVersions(bundleOpts.plugins...) if len(supportedProjectVersions) == 0 { return nil, fmt.Errorf("in order to bundle plugins, they must all support at least one common project version") } @@ -42,8 +75,8 @@ func NewBundle(name string, version Version, deprecateWarning string, plugins .. // Plugins may be bundles themselves, so unbundle here // NOTE(Adirio): unbundling here ensures that Bundle.Plugin always returns a flat list of Plugins instead of also // including Bundles, and therefore we don't have to use a recursive algorithm when resolving. - allPlugins := make([]Plugin, 0, len(plugins)) - for _, plugin := range plugins { + allPlugins := make([]Plugin, 0, len(bundleOpts.plugins)) + for _, plugin := range bundleOpts.plugins { if pluginBundle, isBundle := plugin.(Bundle); isBundle { allPlugins = append(allPlugins, pluginBundle.Plugins()...) } else { @@ -52,11 +85,11 @@ func NewBundle(name string, version Version, deprecateWarning string, plugins .. } return bundle{ - name: name, - version: version, + name: bundleOpts.name, + version: bundleOpts.version, plugins: allPlugins, supportedProjectVersions: supportedProjectVersions, - deprecateWarning: deprecateWarning, + deprecateWarning: bundleOpts.deprecateWarning, }, nil } diff --git a/pkg/plugin/bundle_test.go b/pkg/plugin/bundle_test.go index c437bfd7b8f..d83b056cecf 100644 --- a/pkg/plugin/bundle_test.go +++ b/pkg/plugin/bundle_test.go @@ -67,7 +67,7 @@ var _ = Describe("Bundle", func() { {p1, p2, p3}, {p1, p3, p4}, } { - b, err := NewBundle(name, version, "", plugins...) + b, err := NewBundle(WithName(name), WithVersion(version), WithDeprecationMessage(""), WithPlugins(plugins...)) Expect(err).NotTo(HaveOccurred()) Expect(b.Name()).To(Equal(name)) Expect(b.Version().Compare(version)).To(Equal(0)) @@ -88,9 +88,9 @@ var _ = Describe("Bundle", func() { var a, b Bundle var err error plugins := []Plugin{p1, p2, p3} - a, err = NewBundle("a", version, "", p1, p2) + a, err = NewBundle(WithName("a"), WithVersion(version), WithDeprecationMessage(""), WithPlugins(p1, p2)) Expect(err).NotTo(HaveOccurred()) - b, err = NewBundle("b", version, "", a, p3) + b, err = NewBundle(WithName("b"), WithVersion(version), WithDeprecationMessage(""), WithPlugins(a, p3)) Expect(err).NotTo(HaveOccurred()) versions := b.SupportedProjectVersions() sort.Slice(versions, func(i int, j int) bool { @@ -113,7 +113,7 @@ var _ = Describe("Bundle", func() { {p1, p2, p3, p4}, } { - _, err := NewBundle(name, version, "", plugins...) + _, err := NewBundle(WithName(name), WithVersion(version), WithDeprecationMessage(""), WithPlugins(plugins...)) Expect(err).To(HaveOccurred()) } })