Skip to content

Commit

Permalink
use bundle options struct for creating new bundles
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
  • Loading branch information
NikhilSharmaWe committed May 1, 2023
1 parent 7aa151a commit 0ceec77
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 61 deletions.
15 changes: 5 additions & 10 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,21 @@ 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+." +
"It is recommended to upgrade your project to the latest versions available (go/v4)." +
"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{}),
)
bpgov3 := plugin.NewBundlePlugin()
gov3Bundle, _ := bpgov3.WithName(golang.DefaultNameQualifier).WithVersion(plugin.Version{Number: 3}).WithDeprecationMessage(deprecateMessageGoV3Bundle).WithPlugins(kustomizecommonv1.Plugin{}, golangv3.Plugin{}).NewBundleWithOptions()

// 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{}),
)
bpgov4 := plugin.NewBundlePlugin()
gov4Bundle, _ := bpgov4.WithName(golang.DefaultNameQualifier).WithVersion(plugin.Version{Number: 4}).WithPlugins(kustomizecommonv2alpha.Plugin{}, golangv4.Plugin{}).NewBundleWithOptions()

fs := machinery.Filesystem{
FS: afero.NewOsFs(),
Expand Down
61 changes: 31 additions & 30 deletions pkg/plugin/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,38 @@ type bundle struct {
deprecateWarning string
}

type BundleOption func(*bundle)

func WithName(name string) BundleOption {
return func(opts *bundle) {
opts.name = name
type BundlePlugin struct {
Options struct {
name string
version Version
plugins []Plugin
supportedProjectVersions []config.Version
deprecateWarning string
}
}

func WithVersion(version Version) BundleOption {
return func(opts *bundle) {
opts.version = version
}
func NewBundlePlugin() BundlePlugin {
return BundlePlugin{}
}

func WithPlugins(plugins ...Plugin) BundleOption {
return func(opts *bundle) {
opts.plugins = plugins
}
func (bp *BundlePlugin) WithName(name string) *BundlePlugin {
bp.Options.name = name
return bp
}

func WithDeprecationMessage(msg string) BundleOption {
return func(opts *bundle) {
opts.deprecateWarning = msg
}
func (bp *BundlePlugin) WithVersion(version Version) *BundlePlugin {
bp.Options.version = version
return bp
}

func (bp *BundlePlugin) WithPlugins(plugins ...Plugin) *BundlePlugin {
bp.Options.plugins = plugins
return bp
}

func (bp *BundlePlugin) WithDeprecationMessage(msg string) *BundlePlugin {
bp.Options.deprecateWarning = msg
return bp
}

// NewBundle creates a new Bundle with the provided name and version, and that wraps the provided plugins.
Expand Down Expand Up @@ -96,23 +103,17 @@ 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 *BundlePlugin) NewBundleWithOptions() (Bundle, error) {
supportedProjectVersions := CommonSupportedProjectVersions(bp.Options.plugins...)
if len(supportedProjectVersions) == 0 {
return nil, fmt.Errorf("in order to bundle plugins, they must all support at least one common project version")
}

// 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.Options.plugins))
for _, plugin := range bp.Options.plugins {
if pluginBundle, isBundle := plugin.(Bundle); isBundle {
allPlugins = append(allPlugins, pluginBundle.Plugins()...)
} else {
Expand All @@ -121,11 +122,11 @@ func NewBundleWithOptions(opts ...BundleOption) (Bundle, error) {
}

return bundle{
name: bundleOpts.name,
version: bundleOpts.version,
name: bp.Options.name,
version: bp.Options.version,
plugins: allPlugins,
supportedProjectVersions: supportedProjectVersions,
deprecateWarning: bundleOpts.deprecateWarning,
deprecateWarning: bp.Options.deprecateWarning,
}, nil
}

Expand Down
31 changes: 10 additions & 21 deletions pkg/plugin/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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 := NewBundlePlugin()
b, err := bp.WithName(name).WithVersion(version).WithDeprecationMessage("").WithPlugins(plugins...).NewBundleWithOptions()
Expect(err).NotTo(HaveOccurred())
Expect(b.Name()).To(Equal(name))
Expect(b.Version().Compare(version)).To(Equal(0))
Expand All @@ -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 := NewBundlePlugin()
a, err = bpa.WithName("a").WithVersion(version).WithDeprecationMessage("").WithPlugins(p1, p2).NewBundleWithOptions()

Expect(err).NotTo(HaveOccurred())
b, err = NewBundleWithOptions(WithName("b"),
WithVersion(version),
WithDeprecationMessage(""),
WithPlugins(a, p3),
)
bpb := NewBundlePlugin()
b, err = bpb.WithName("b").WithVersion(version).WithDeprecationMessage("").WithPlugins(a, p3).NewBundleWithOptions()
Expect(err).NotTo(HaveOccurred())
versions := b.SupportedProjectVersions()
sort.Slice(versions, func(i int, j int) bool {
Expand All @@ -189,11 +181,8 @@ var _ = Describe("Bundle", func() {

{p1, p2, p3, p4},
} {
_, err := NewBundleWithOptions(WithName(name),
WithVersion(version),
WithDeprecationMessage(""),
WithPlugins(plugins...),
)
bp := NewBundlePlugin()
_, err := bp.WithName(name).WithVersion(version).WithDeprecationMessage("").WithPlugins(plugins...).NewBundleWithOptions()
Expect(err).To(HaveOccurred())
}
})
Expand Down

0 comments on commit 0ceec77

Please sign in to comment.