Skip to content

Commit

Permalink
Merge branch 'default-plugins' into plugin-resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
Adirio committed Mar 1, 2021
2 parents 5a877ef + 2b0ee4b commit 2173c50
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 68 deletions.
4 changes: 1 addition & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"log"

"sigs.k8s.io/kubebuilder/v3/pkg/cli"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/declarative/v1"
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
Expand All @@ -37,8 +36,7 @@ func main() {
&pluginv3.Plugin{},
&declarativev1.Plugin{},
),
cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}),
cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}),
cli.WithDefaultPlugins(&pluginv3.Plugin{}),
cli.WithCompletion(),
)
if err != nil {
Expand Down
21 changes: 16 additions & 5 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (

"sigs.k8s.io/kubebuilder/v3/pkg/config"
yamlstore "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
cfgv2 "sigs.k8s.io/kubebuilder/v3/pkg/config/v2"
cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3"
"sigs.k8s.io/kubebuilder/v3/pkg/plugin"
pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2"
)

const (
Expand Down Expand Up @@ -76,7 +78,7 @@ type cli struct { //nolint:maligned
// Default project version in case none is provided and a config file can't be found.
defaultProjectVersion config.Version
// Default plugins in case none is provided and a config file can't be found.
defaultPlugins map[config.Version][]string
defaultPlugins []string
// Plugins registered in the cli.
plugins map[string]plugin.Plugin
// Commands injected by options.
Expand Down Expand Up @@ -135,7 +137,7 @@ func newCLI(opts ...Option) (*cli, error) {
c := &cli{
commandName: "kubebuilder",
defaultProjectVersion: cfgv3.Version,
defaultPlugins: make(map[config.Version][]string),
defaultPlugins: make([]string, 0),
plugins: make(map[string]plugin.Plugin),
fs: afero.NewOsFs(),
}
Expand Down Expand Up @@ -191,6 +193,12 @@ func (c *cli) getInfoFromFlags() (string, []string, error) {
plugins[i] = strings.TrimSpace(key)
}

// For backwards compatibility reasons, defining the project version flag and not defining
// the plugins flag should be interpreted as if the plugins flag was set to go/v2.
if projectVersion == "2" && len(plugins) == 0 {
plugins = append(plugins, plugin.KeyFor(pluginv2.Plugin{}))
}

return projectVersion, plugins, nil
}

Expand All @@ -213,6 +221,11 @@ func (c cli) getInfoFromConfigFile() (config.Version, []string, error) {
// getInfoFromConfig obtains the project version and plugin keys from the project config.
// It is extracted from getInfoFromConfigFile for testing purposes.
func getInfoFromConfig(projectConfig config.Config) (config.Version, []string, error) {
// Project v2 did not store the layout field, so we set it to the only available plugin
if projectConfig.GetVersion().Compare(cfgv2.Version) == 0 {
return cfgv2.Version, []string{plugin.KeyFor(pluginv2.Plugin{})}, nil
}

// Split the comma-separated plugins
var pluginSet []string
if projectConfig.GetLayout() != "" {
Expand Down Expand Up @@ -268,9 +281,7 @@ func (c cli) resolveFlagsAndConfigFileConflicts(
switch {
// If they are both empty, use the default
case isFlagPluginsEmpty && isCfgPluginsEmpty:
if defaults, hasDefaults := c.defaultPlugins[projectVersion]; hasDefaults {
plugins = defaults
}
plugins = c.defaultPlugins
// If any is empty, choose the other
case isCfgPluginsEmpty:
plugins = flagPlugins
Expand Down
35 changes: 11 additions & 24 deletions pkg/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var _ = Describe("CLI", func() {
projectVersion, plugins, err = c.getInfoFromFlags()
Expect(err).NotTo(HaveOccurred())
Expect(projectVersion).To(Equal("2"))
Expect(len(plugins)).To(Equal(0))
Expect(plugins).To(Equal([]string{"go.kubebuilder.io/v2"}))
})
})

Expand Down Expand Up @@ -198,14 +198,14 @@ var _ = Describe("CLI", func() {
projectVersion, plugins, err = getInfoFromConfig(projectConfig)
Expect(err).NotTo(HaveOccurred())
Expect(projectVersion.Compare(projectConfig.GetVersion())).To(Equal(0))
Expect(len(plugins)).To(Equal(0))
Expect(plugins).To(Equal([]string{"go.kubebuilder.io/v2"}))
})
})

When("having layout field", func() {
It("should succeed", func() {
projectConfig = cfgv3.New()
Expect(projectConfig.SetLayout("go.kubebuilder.io/v2")).To(Succeed())
Expect(projectConfig.SetLayout("go.kubebuilder.io/v3")).To(Succeed())
projectVersion, plugins, err = getInfoFromConfig(projectConfig)
Expect(err).NotTo(HaveOccurred())
Expect(projectVersion.Compare(projectConfig.GetVersion())).To(Equal(0))
Expand Down Expand Up @@ -412,10 +412,7 @@ var _ = Describe("CLI", func() {
It("should succeed", func() {
c = &cli{
defaultProjectVersion: projectVersion1,
defaultPlugins: map[config.Version][]string{
projectVersion1: {pluginKey1},
projectVersion2: {pluginKey2},
},
defaultPlugins: []string{pluginKey1},
}
_, plugins, err = c.resolveFlagsAndConfigFileConflicts(
"",
Expand Down Expand Up @@ -464,9 +461,7 @@ var _ = Describe("CLI", func() {
When("having default plugin keys set and from flags", func() {
It("should succeed", func() {
c = &cli{
defaultPlugins: map[config.Version][]string{
{}: {pluginKey1},
},
defaultPlugins: []string{pluginKey1},
}
_, plugins, err = c.resolveFlagsAndConfigFileConflicts(
"",
Expand All @@ -483,9 +478,7 @@ var _ = Describe("CLI", func() {
When("having default plugin keys set and from config file", func() {
It("should succeed", func() {
c = &cli{
defaultPlugins: map[config.Version][]string{
{}: {pluginKey1},
},
defaultPlugins: []string{pluginKey1},
}
_, plugins, err = c.resolveFlagsAndConfigFileConflicts(
"",
Expand Down Expand Up @@ -529,9 +522,7 @@ var _ = Describe("CLI", func() {
When("having three plugin keys sources", func() {
It("should succeed if plugin keys from flags and config file are the same", func() {
c = &cli{
defaultPlugins: map[config.Version][]string{
{}: {pluginKey1},
},
defaultPlugins: []string{pluginKey1},
}
_, plugins, err = c.resolveFlagsAndConfigFileConflicts(
"",
Expand All @@ -546,9 +537,7 @@ var _ = Describe("CLI", func() {

It("should fail if plugin keys from flags and config file are different", func() {
c = &cli{
defaultPlugins: map[config.Version][]string{
{}: {pluginKey1},
},
defaultPlugins: []string{pluginKey1},
}
_, _, err = c.resolveFlagsAndConfigFileConflicts(
"",
Expand Down Expand Up @@ -583,10 +572,8 @@ var _ = Describe("CLI", func() {
pluginKeys := []string{"go.kubebuilder.io/v2"}
c := &cli{
defaultProjectVersion: projectVersion,
defaultPlugins: map[config.Version][]string{
projectVersion: pluginKeys,
},
fs: afero.NewMemMapFs(),
defaultPlugins: pluginKeys,
fs: afero.NewMemMapFs(),
}
c.cmd = c.newRootCmd()
Expect(c.getInfo()).To(Succeed())
Expand Down Expand Up @@ -747,7 +734,7 @@ var _ = Describe("CLI", func() {

c, err = New(
WithDefaultProjectVersion(projectVersion),
WithDefaultPlugins(projectVersion, deprecatedPlugin),
WithDefaultPlugins(deprecatedPlugin),
WithPlugins(deprecatedPlugin),
)
_ = w.Close()
Expand Down
12 changes: 3 additions & 9 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,16 @@ func WithDefaultProjectVersion(version config.Version) Option {
}

// WithDefaultPlugins is an Option that sets the cli's default plugins.
func WithDefaultPlugins(projectVersion config.Version, plugins ...plugin.Plugin) Option {
func WithDefaultPlugins(plugins ...plugin.Plugin) Option {
return func(c *cli) error {
if err := projectVersion.Validate(); err != nil {
return fmt.Errorf("broken pre-set project version %q for default plugins: %v", projectVersion, err)
}
if len(plugins) == 0 {
return fmt.Errorf("empty set of plugins provided for project version %q", projectVersion)
return fmt.Errorf("empty set of plugins provided")
}
for _, p := range plugins {
if err := plugin.Validate(p); err != nil {
return fmt.Errorf("broken pre-set default plugin %q: %v", plugin.KeyFor(p), err)
}
if !plugin.SupportsVersion(p, projectVersion) {
return fmt.Errorf("default plugin %q doesn't support version %q", plugin.KeyFor(p), projectVersion)
}
c.defaultPlugins[projectVersion] = append(c.defaultPlugins[projectVersion], plugin.KeyFor(p))
c.defaultPlugins = append(c.defaultPlugins, plugin.KeyFor(p))
}
return nil
}
Expand Down
28 changes: 7 additions & 21 deletions pkg/cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,57 +99,43 @@ var _ = Describe("CLI options", func() {

Context("WithDefaultPlugins", func() {
It("should return a valid CLI", func() {
c, err = newCLI(WithDefaultPlugins(projectVersion, p))
c, err = newCLI(WithDefaultPlugins(p))
Expect(err).NotTo(HaveOccurred())
Expect(c).NotTo(BeNil())
Expect(c.defaultPlugins).To(Equal(map[config.Version][]string{projectVersion: {plugin.KeyFor(p)}}))
})

When("providing an invalid project version", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(config.Version{}, p))
Expect(err).To(HaveOccurred())
})
Expect(c.defaultPlugins).To(Equal([]string{plugin.KeyFor(p)}))
})

When("providing an empty set of plugins", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(projectVersion))
_, err = newCLI(WithDefaultPlugins())
Expect(err).To(HaveOccurred())
})
})

When("providing a plugin with an invalid name", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(projectVersion, np1))
_, err = newCLI(WithDefaultPlugins(np1))
Expect(err).To(HaveOccurred())
})
})

When("providing a plugin with an invalid version", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(projectVersion, np2))
_, err = newCLI(WithDefaultPlugins(np2))
Expect(err).To(HaveOccurred())
})
})

When("providing a plugin with an empty list of supported versions", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(projectVersion, np3))
_, err = newCLI(WithDefaultPlugins(np3))
Expect(err).To(HaveOccurred())
})
})

When("providing a plugin with an invalid list of supported versions", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(projectVersion, np4))
Expect(err).To(HaveOccurred())
})
})

When("providing a default plugin for an unsupported project version", func() {
It("should return an error", func() {
_, err = newCLI(WithDefaultPlugins(config.Version{Number: 2}, p))
_, err = newCLI(WithDefaultPlugins(np4))
Expect(err).To(HaveOccurred())
})
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ configuration please run:

str += fmt.Sprintf("\nDefault project version: %s\n", c.defaultProjectVersion)

if defaultPlugins, hasDefaultPlugins := c.defaultPlugins[c.defaultProjectVersion]; hasDefaultPlugins {
str += fmt.Sprintf("Default plugin keys: %q\n", strings.Join(defaultPlugins, ","))
if len(c.defaultPlugins) != 0 {
str += fmt.Sprintf("Default plugin keys: %q\n", strings.Join(c.defaultPlugins, ","))
}

str += fmt.Sprintf(`
Expand Down
1 change: 1 addition & 0 deletions test/e2e/v2/plugin_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ var _ = Describe("kubebuilder", func() {
By("init v2 project")
err := kbc.Init(
"--project-version", "2",
"--plugins", "go/v2",
"--domain", kbc.Domain,
"--fetch-deps=false")
Expect(err).Should(Succeed())
Expand Down
8 changes: 4 additions & 4 deletions test/testdata/generate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ function scaffold_test_project {

build_kb

# Project version 2 uses plugin go/v2 (default).
scaffold_test_project project-v2 --project-version=2
scaffold_test_project project-v2-multigroup --project-version=2
scaffold_test_project project-v2-addon --project-version=3 --plugins="go/v2,declarative"
# Project version 2 uses plugin go/v2.
scaffold_test_project project-v2 --plugins=go/v2 --project-version=2
scaffold_test_project project-v2-multigroup --plugins=go/v2 --project-version=2
scaffold_test_project project-v2-addon --plugins="go/v2,declarative" --project-version=3
# Project version 3 (default) uses plugin go/v3 (default).
scaffold_test_project project-v3
scaffold_test_project project-v3-multigroup
Expand Down

0 comments on commit 2173c50

Please sign in to comment.