Skip to content

Commit

Permalink
Merge pull request #2112 from Adirio/bundle-recursion
Browse files Browse the repository at this point in the history
✨ Allow bundles to be used as input to other bundles
  • Loading branch information
k8s-ci-robot committed Mar 27, 2021
2 parents f65ddb2 + 39ec8f8 commit 84f357b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
14 changes: 13 additions & 1 deletion pkg/plugin/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,22 @@ func NewBundle(name string, version Version, plugins ...Plugin) (Bundle, error)
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 {
if pluginBundle, isBundle := plugin.(Bundle); isBundle {
allPlugins = append(allPlugins, pluginBundle.Plugins()...)
} else {
allPlugins = append(allPlugins, plugin)
}
}

return bundle{
name: name,
version: version,
plugins: plugins,
plugins: allPlugins,
supportedProjectVersions: supportedProjectVersions,
}, nil
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/plugin/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ var _ = Describe("Bundle", func() {
}
})

It("should accept bundles as input", func() {
var a, b Bundle
var err error
plugins := []Plugin{p1, p2, p3}
a, err = NewBundle("a", version, p1, p2)
Expect(err).NotTo(HaveOccurred())
b, err = NewBundle("b", version, a, p3)
Expect(err).NotTo(HaveOccurred())
versions := b.SupportedProjectVersions()
sort.Slice(versions, func(i int, j int) bool {
return versions[i].Compare(versions[j]) == -1
})
expectedVersions := CommonSupportedProjectVersions(plugins...)
sort.Slice(expectedVersions, func(i int, j int) bool {
return expectedVersions[i].Compare(expectedVersions[j]) == -1
})
Expect(versions).To(Equal(expectedVersions))
Expect(b.Plugins()).To(Equal(plugins))
})

It("should fail for plugins with no common supported project version", func() {
for _, plugins := range [][]Plugin{
{p2, p4},
Expand Down
17 changes: 9 additions & 8 deletions pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"sigs.k8s.io/kubebuilder/v3/pkg/config"
)

// Plugin is an interface that defines the common base for all plugins
// Plugin is an interface that defines the common base for all plugins.
type Plugin interface {
// Name returns a DNS1123 label string identifying the plugin uniquely. This name should be fully-qualified,
// i.e. have a short prefix describing the plugin type (like a language) followed by a domain.
Expand All @@ -41,45 +41,46 @@ type Deprecated interface {
DeprecationWarning() string
}

// Init is an interface for plugins that provide an `init` subcommand
// Init is an interface for plugins that provide an `init` subcommand.
type Init interface {
Plugin
// GetInitSubcommand returns the underlying InitSubcommand interface.
GetInitSubcommand() InitSubcommand
}

// CreateAPI is an interface for plugins that provide a `create api` subcommand
// CreateAPI is an interface for plugins that provide a `create api` subcommand.
type CreateAPI interface {
Plugin
// GetCreateAPISubcommand returns the underlying CreateAPISubcommand interface.
GetCreateAPISubcommand() CreateAPISubcommand
}

// CreateWebhook is an interface for plugins that provide a `create webhook` subcommand
// CreateWebhook is an interface for plugins that provide a `create webhook` subcommand.
type CreateWebhook interface {
Plugin
// GetCreateWebhookSubcommand returns the underlying CreateWebhookSubcommand interface.
GetCreateWebhookSubcommand() CreateWebhookSubcommand
}

// Edit is an interface for plugins that provide a `edit` subcommand
// Edit is an interface for plugins that provide a `edit` subcommand.
type Edit interface {
Plugin
// GetEditSubcommand returns the underlying EditSubcommand interface.
GetEditSubcommand() EditSubcommand
}

// Full is an interface for plugins that provide `init`, `create api`, `create webhook` and `edit` subcommands
// Full is an interface for plugins that provide `init`, `create api`, `create webhook` and `edit` subcommands.
type Full interface {
Init
CreateAPI
CreateWebhook
Edit
}

// Bundle allows to group plugins under a single key
// Bundle allows to group plugins under a single key.
type Bundle interface {
Plugin
// Plugins returns a list of the bundled plugins
// Plugins returns a list of the bundled plugins.
// The returned list should be flattened, i.e., no plugin bundles should be part of this list.
Plugins() []Plugin
}

0 comments on commit 84f357b

Please sign in to comment.