Skip to content

Commit

Permalink
change NewBundle method to build bundles through working with options
Browse files Browse the repository at this point in the history
Signed-off-by: Nikhil Sharma <nikhilsharma230303@gmail.com>
  • Loading branch information
NikhilSharmaWe committed Mar 24, 2023
1 parent 7f5d552 commit 1dae315
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
15 changes: 7 additions & 8 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
47 changes: 40 additions & 7 deletions pkg/plugin/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,52 @@ 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")
}

// 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 {
Expand All @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions pkg/plugin/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 {
Expand All @@ -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())
}
})
Expand Down

0 comments on commit 1dae315

Please sign in to comment.