From 0d0b3416378b8b9f34c42dd5d664447a0241569d Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Mon, 1 May 2023 20:35:23 +0530 Subject: [PATCH] use bundle options struct for creating new bundles Signed-off-by: Nikhil Sharma --- cmd/main.go | 15 ++++------ pkg/plugin/bundle.go | 58 +++++++++++++++++++-------------------- pkg/plugin/bundle_test.go | 31 +++++++-------------- 3 files changed, 43 insertions(+), 61 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7f59bb26161..0bdf16e76fa 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -37,8 +37,8 @@ import ( grafanav1alpha1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/optional/grafana/v1alpha" ) +// nolint:lll func main() { - const deprecateMessageGoV3Bundle = "This version is deprecated." + "The `go/v3` cannot scaffold projects using kustomize versions v4x+" + " and cannot fully support Kubernetes 1.25+." + @@ -46,17 +46,12 @@ 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.NewBundleWithOptions(plugin.WithName(golang.DefaultNameQualifier), - plugin.WithVersion(plugin.Version{Number: 3}), - plugin.WithDeprecationMessage(deprecateMessageGoV3Bundle), - plugin.WithPlugins(kustomizecommonv1.Plugin{}, golangv3.Plugin{}), - ) + gov3 := plugin.NewBundlePluginBuilder() + gov3Bundle, _ := gov3.WithName(golang.DefaultNameQualifier).WithVersion(plugin.Version{Number: 3}).WithDeprecationMessage(deprecateMessageGoV3Bundle).WithPlugins(kustomizecommonv1.Plugin{}, golangv3.Plugin{}).Build() // Bundle plugin which built the golang projects scaffold by Kubebuilder go/v4 with kustomize alpha-v2 - gov4Bundle, _ := plugin.NewBundleWithOptions(plugin.WithName(golang.DefaultNameQualifier), - plugin.WithVersion(plugin.Version{Number: 4}), - plugin.WithPlugins(kustomizecommonv2alpha.Plugin{}, golangv4.Plugin{}), - ) + gov4 := plugin.NewBundlePluginBuilder() + gov4Bundle, _ := gov4.WithName(golang.DefaultNameQualifier).WithVersion(plugin.Version{Number: 4}).WithPlugins(kustomizecommonv2alpha.Plugin{}, golangv4.Plugin{}).Build() fs := machinery.Filesystem{ FS: afero.NewOsFs(), diff --git a/pkg/plugin/bundle.go b/pkg/plugin/bundle.go index 47e4f879eaf..c4d58926c88 100644 --- a/pkg/plugin/bundle.go +++ b/pkg/plugin/bundle.go @@ -31,31 +31,35 @@ type bundle struct { deprecateWarning string } -type BundleOption func(*bundle) +type BundlePluginBuilder struct { + name string + version Version + plugins []Plugin + deprecateWarning string +} -func WithName(name string) BundleOption { - return func(opts *bundle) { - opts.name = name - } +func NewBundlePluginBuilder() BundlePluginBuilder { + return BundlePluginBuilder{} } -func WithVersion(version Version) BundleOption { - return func(opts *bundle) { - opts.version = version - } +func (bp *BundlePluginBuilder) WithName(name string) *BundlePluginBuilder { + bp.name = name + return bp } -func WithPlugins(plugins ...Plugin) BundleOption { - return func(opts *bundle) { - opts.plugins = plugins - } +func (bp *BundlePluginBuilder) WithVersion(version Version) *BundlePluginBuilder { + bp.version = version + return bp } -func WithDeprecationMessage(msg string) BundleOption { - return func(opts *bundle) { - opts.deprecateWarning = msg - } +func (bp *BundlePluginBuilder) WithPlugins(plugins ...Plugin) *BundlePluginBuilder { + bp.plugins = plugins + return bp +} +func (bp *BundlePluginBuilder) WithDeprecationMessage(msg string) *BundlePluginBuilder { + bp.deprecateWarning = msg + return bp } // NewBundle creates a new Bundle with the provided name and version, and that wraps the provided plugins. @@ -96,14 +100,8 @@ func NewBundle(name string, version Version, deprecateWarning string, plugins .. // NewBundleWithOptions creates a new Bundle with the provided BundleOptions. // The list of supported project versions is computed from the provided plugins in options. -func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) { - bundleOpts := bundle{} - - for _, opts := range opts { - opts(&bundleOpts) - } - - supportedProjectVersions := CommonSupportedProjectVersions(bundleOpts.plugins...) +func (bp *BundlePluginBuilder) Build() (Bundle, error) { + supportedProjectVersions := CommonSupportedProjectVersions(bp.plugins...) if len(supportedProjectVersions) == 0 { return nil, fmt.Errorf("in order to bundle plugins, they must all support at least one common project version") } @@ -111,8 +109,8 @@ func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) { // 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(bundleOpts.plugins)) - for _, plugin := range bundleOpts.plugins { + allPlugins := make([]Plugin, 0, len(bp.plugins)) + for _, plugin := range bp.plugins { if pluginBundle, isBundle := plugin.(Bundle); isBundle { allPlugins = append(allPlugins, pluginBundle.Plugins()...) } else { @@ -121,11 +119,11 @@ func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) { } return bundle{ - name: bundleOpts.name, - version: bundleOpts.version, + name: bp.name, + version: bp.version, plugins: allPlugins, supportedProjectVersions: supportedProjectVersions, - deprecateWarning: bundleOpts.deprecateWarning, + deprecateWarning: bp.deprecateWarning, }, nil } diff --git a/pkg/plugin/bundle_test.go b/pkg/plugin/bundle_test.go index ccb1a41d574..4317d889204 100644 --- a/pkg/plugin/bundle_test.go +++ b/pkg/plugin/bundle_test.go @@ -118,7 +118,7 @@ var _ = Describe("Bundle", func() { } }) }) - + // nolint:lll Context("NewBundleWithOptions", func() { It("should succeed for plugins with common supported project versions", func() { for _, plugins := range [][]Plugin{ @@ -131,11 +131,8 @@ var _ = Describe("Bundle", func() { {p1, p2, p3}, {p1, p3, p4}, } { - b, err := NewBundleWithOptions(WithName(name), - WithVersion(version), - WithDeprecationMessage(""), - WithPlugins(plugins...), - ) + bp := NewBundlePluginBuilder() + b, err := bp.WithName(name).WithVersion(version).WithDeprecationMessage("").WithPlugins(plugins...).Build() Expect(err).NotTo(HaveOccurred()) Expect(b.Name()).To(Equal(name)) Expect(b.Version().Compare(version)).To(Equal(0)) @@ -156,17 +153,12 @@ var _ = Describe("Bundle", func() { var a, b Bundle var err error plugins := []Plugin{p1, p2, p3} - a, err = NewBundleWithOptions(WithName("a"), - WithVersion(version), - WithDeprecationMessage(""), - WithPlugins(p1, p2), - ) + bpa := NewBundlePluginBuilder() + a, err = bpa.WithName("a").WithVersion(version).WithDeprecationMessage("").WithPlugins(p1, p2).Build() + Expect(err).NotTo(HaveOccurred()) - b, err = NewBundleWithOptions(WithName("b"), - WithVersion(version), - WithDeprecationMessage(""), - WithPlugins(a, p3), - ) + bpb := NewBundlePluginBuilder() + b, err = bpb.WithName("b").WithVersion(version).WithDeprecationMessage("").WithPlugins(a, p3).Build() Expect(err).NotTo(HaveOccurred()) versions := b.SupportedProjectVersions() sort.Slice(versions, func(i int, j int) bool { @@ -189,11 +181,8 @@ var _ = Describe("Bundle", func() { {p1, p2, p3, p4}, } { - _, err := NewBundleWithOptions(WithName(name), - WithVersion(version), - WithDeprecationMessage(""), - WithPlugins(plugins...), - ) + bp := NewBundlePluginBuilder() + _, err := bp.WithName(name).WithVersion(version).WithDeprecationMessage("").WithPlugins(plugins...).Build() Expect(err).To(HaveOccurred()) } })