From 583baaddf9e2faa9e6d01a91e61bd0e21b93330b Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Thu, 25 Mar 2021 12:35:28 +0000 Subject: [PATCH] add common base plugin to allow consumers use that --- cmd/main.go | 24 ++-- pkg/cli/cli.go | 2 +- pkg/plugins/common/v1/init.go | 111 ++++++++++++++++++ pkg/plugins/common/v1/plugin.go | 50 ++++++++ pkg/plugins/common/v1/scaffolds/init.go | 84 +++++++++++++ .../config/kdefault/kustomization.go | 0 .../kdefault/manager_auth_proxy_patch.go | 0 .../config/kdefault/manager_config_patch.go | 0 .../templates/config/manager/config.go | 0 .../manager/controller_manager_config.go | 0 .../templates/config/manager/kustomization.go | 0 .../config/prometheus/kustomization.go | 0 .../templates/config/prometheus/monitor.go | 0 .../config/rbac/auth_proxy_client_role.go | 0 .../templates/config/rbac/auth_proxy_role.go | 0 .../config/rbac/auth_proxy_role_binding.go | 0 .../config/rbac/auth_proxy_service.go | 0 .../templates/config/rbac/kustomization.go | 0 .../config/rbac/leader_election_role.go | 0 .../rbac/leader_election_role_binding.go | 0 .../templates/config/rbac/role_binding.go | 0 .../templates/config/rbac/service_account.go | 0 pkg/plugins/golang/domain.go | 22 ++++ pkg/plugins/golang/v2/plugin.go | 4 +- pkg/plugins/golang/v3/init.go | 39 +----- pkg/plugins/golang/v3/plugin.go | 4 +- pkg/plugins/golang/v3/scaffolds/init.go | 25 ---- .../config/certmanager/certificate.go | 3 + .../config/certmanager/kustomization.go | 3 + .../config/certmanager/kustomizeconfig.go | 3 + pkg/plugins/golang/v3/scaffolds/webhook.go | 4 + .../config/certmanager/certificate.yaml | 25 ---- .../config/certmanager/kustomization.yaml | 5 - .../config/certmanager/kustomizeconfig.yaml | 16 --- .../bases/crew.testproject.org_admirals.yaml | 2 - .../bases/crew.testproject.org_captains.yaml | 2 - .../crew.testproject.org_firstmates.yaml | 2 - testdata/project-v3-addon/go.mod | 2 +- .../crew.testproject.org_firstmates.yaml | 2 - testdata/project-v3-config/go.mod | 2 +- .../crew.testproject.org_firstmates.yaml | 2 - testdata/project-v3/go.mod | 2 +- 42 files changed, 308 insertions(+), 132 deletions(-) create mode 100644 pkg/plugins/common/v1/init.go create mode 100644 pkg/plugins/common/v1/plugin.go create mode 100644 pkg/plugins/common/v1/scaffolds/init.go rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/kdefault/kustomization.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/kdefault/manager_auth_proxy_patch.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/kdefault/manager_config_patch.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/manager/config.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/manager/controller_manager_config.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/manager/kustomization.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/prometheus/kustomization.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/prometheus/monitor.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/auth_proxy_client_role.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/auth_proxy_role.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/auth_proxy_service.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/kustomization.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/leader_election_role.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/role_binding.go (100%) rename pkg/plugins/{golang/v3 => common/v1}/scaffolds/internal/templates/config/rbac/service_account.go (100%) create mode 100644 pkg/plugins/golang/domain.go delete mode 100644 testdata/project-v3-addon/config/certmanager/certificate.yaml delete mode 100644 testdata/project-v3-addon/config/certmanager/kustomization.yaml delete mode 100644 testdata/project-v3-addon/config/certmanager/kustomizeconfig.yaml diff --git a/cmd/main.go b/cmd/main.go index 6cb55af7014..cf41389cfb5 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -18,26 +18,36 @@ package main 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" + "sigs.k8s.io/kubebuilder/v3/pkg/plugin" + commonv1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1" declarativev1 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/declarative/v1" - pluginv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2" - pluginv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang" + golangv2 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v2" + golangv3 "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3" ) func main() { + + // Bundle plugin which built the golang projects scaffold by Kubebuilder + gov3Bundle, _ := plugin.NewBundle(golang.DefaultGoNameQualifier, plugin.Version{Number: 3}, + commonv1.Plugin{}, + golangv3.Plugin{}, + ) + c, err := cli.New( cli.WithCommandName("kubebuilder"), cli.WithVersion(versionString()), cli.WithPlugins( - &pluginv2.Plugin{}, - &pluginv3.Plugin{}, + &golangv2.Plugin{}, + gov3Bundle, + &commonv1.Plugin{}, &declarativev1.Plugin{}, ), - cli.WithDefaultPlugins(cfgv2.Version, &pluginv2.Plugin{}), - cli.WithDefaultPlugins(cfgv3.Version, &pluginv3.Plugin{}), + cli.WithDefaultPlugins(cfgv2.Version, &golangv2.Plugin{}), + cli.WithDefaultPlugins(cfgv3.Version, gov3Bundle), cli.WithDefaultProjectVersion(cfgv3.Version), cli.WithCompletion(), ) diff --git a/pkg/cli/cli.go b/pkg/cli/cli.go index 3396c1a85b1..64f870c0396 100644 --- a/pkg/cli/cli.go +++ b/pkg/cli/cli.go @@ -215,7 +215,7 @@ func (c *CLI) getInfoFromConfig(projectConfig config.Config) error { // getInfoFromFlags obtains the project version and plugin keys from flags. func (c *CLI) getInfoFromFlags(hasConfigFile bool) error { // Partially parse the command line arguments - fs := pflag.NewFlagSet("base", pflag.ContinueOnError) + fs := pflag.NewFlagSet("common", pflag.ContinueOnError) // Load the base command global flags fs.AddFlagSet(c.cmd.PersistentFlags()) diff --git a/pkg/plugins/common/v1/init.go b/pkg/plugins/common/v1/init.go new file mode 100644 index 00000000000..cf62c32923a --- /dev/null +++ b/pkg/plugins/common/v1/init.go @@ -0,0 +1,111 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/spf13/pflag" + + "sigs.k8s.io/kubebuilder/v3/pkg/config" + "sigs.k8s.io/kubebuilder/v3/pkg/internal/validation" + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" + "sigs.k8s.io/kubebuilder/v3/pkg/plugin" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds" +) + +var _ plugin.InitSubcommand = &initSubcommand{} + +type initSubcommand struct { + config config.Config + // For help text. + commandName string + + // config options + domain string + name string + componentConfig bool +} + +func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) { + p.commandName = cliMeta.CommandName + + subcmdMeta.Description = `Initialize a common project including the following files: + - a "PROJECT" file that stores project configuration + - several YAML files for project deployment under the "config" directory +` + subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright + %[1]s init --plugins go/v3 --domain example.org + + # Initialize a common project defining an specific project version + %[1]s init --plugins go/v3 --project-version 3 +`, cliMeta.CommandName) +} + +func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) { + // project args + fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups") + fs.StringVar(&p.name, "project-name", "", "name of this project") + fs.BoolVar(&p.componentConfig, "component-config", false, + "create a versioned ComponentConfig file, may be 'true' or 'false'") +} + +func (p *initSubcommand) InjectConfig(c config.Config) error { + p.config = c + + if err := p.config.SetDomain(p.domain); err != nil { + return err + } + + // Assign a default project name + if p.name == "" { + dir, err := os.Getwd() + if err != nil { + return fmt.Errorf("error getting current directory: %v", err) + } + p.name = strings.ToLower(filepath.Base(dir)) + } + // Check if the project name is a valid k8s namespace (DNS 1123 label). + if err := validation.IsDNS1123Label(p.name); err != nil { + return fmt.Errorf("project name (%s) is invalid: %v", p.name, err) + } + if err := p.config.SetProjectName(p.name); err != nil { + return err + } + + if p.componentConfig { + if err := p.config.SetComponentConfig(); err != nil { + return err + } + } + + return nil +} + +func (p *initSubcommand) Scaffold(fs machinery.Filesystem) error { + scaffolder := scaffolds.NewInitScaffolder(p.config) + scaffolder.InjectFS(fs) + err := scaffolder.Scaffold() + if err != nil { + return err + } + + return nil +} diff --git a/pkg/plugins/common/v1/plugin.go b/pkg/plugins/common/v1/plugin.go new file mode 100644 index 00000000000..0a5ffd8b50b --- /dev/null +++ b/pkg/plugins/common/v1/plugin.go @@ -0,0 +1,50 @@ +/* +Copyright 2021 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +import ( + "sigs.k8s.io/kubebuilder/v3/pkg/config" + cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3" + "sigs.k8s.io/kubebuilder/v3/pkg/plugin" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins" +) + +const pluginName = "common" + plugins.DefaultNameQualifier + +var ( + pluginVersion = plugin.Version{Number: 1} + supportedProjectVersions = []config.Version{cfgv3.Version} +) + +var _ plugin.Init = Plugin{} + +// Plugin implements the plugin.Full interface +type Plugin struct { + initSubcommand +} + +// Name returns the name of the plugin +func (Plugin) Name() string { return pluginName } + +// Version returns the version of the plugin +func (Plugin) Version() plugin.Version { return pluginVersion } + +// SupportedProjectVersions returns an array with all project versions supported by the plugin +func (Plugin) SupportedProjectVersions() []config.Version { return supportedProjectVersions } + +// GetInitSubcommand will return the subcommand which is responsible for scaffolding init project +func (p Plugin) GetInitSubcommand() plugin.InitSubcommand { return &p.initSubcommand } diff --git a/pkg/plugins/common/v1/scaffolds/init.go b/pkg/plugins/common/v1/scaffolds/init.go new file mode 100644 index 00000000000..bea091cce90 --- /dev/null +++ b/pkg/plugins/common/v1/scaffolds/init.go @@ -0,0 +1,84 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package scaffolds + +import ( + "fmt" + + "sigs.k8s.io/kubebuilder/v3/pkg/config" + "sigs.k8s.io/kubebuilder/v3/pkg/machinery" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/manager" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac" +) + +const ( + imageName = "controller:latest" +) + +var _ plugins.Scaffolder = &initScaffolder{} + +type initScaffolder struct { + config config.Config + + // fs is the filesystem that will be used by the scaffolder + fs machinery.Filesystem +} + +// NewInitScaffolder returns a new Scaffolder for project initialization operations +func NewInitScaffolder(config config.Config) plugins.Scaffolder { + return &initScaffolder{ + config: config, + } +} + +// InjectFS implements cmdutil.Scaffolder +func (s *initScaffolder) InjectFS(fs machinery.Filesystem) { + s.fs = fs +} + +// Scaffold implements cmdutil.Scaffolder +func (s *initScaffolder) Scaffold() error { + fmt.Println("Writing scaffold for you to edit...") + + // Initialize the machinery.Scaffold that will write the files to disk + scaffold := machinery.NewScaffold(s.fs, + machinery.WithConfig(s.config), + ) + + return scaffold.Execute( + &rbac.Kustomization{}, + &rbac.AuthProxyRole{}, + &rbac.AuthProxyRoleBinding{}, + &rbac.AuthProxyService{}, + &rbac.AuthProxyClientRole{}, + &rbac.RoleBinding{}, + &rbac.LeaderElectionRole{}, + &rbac.LeaderElectionRoleBinding{}, + &rbac.ServiceAccount{}, + &manager.Kustomization{}, + &manager.Config{Image: imageName}, + &manager.ControllerManagerConfig{}, + &kdefault.Kustomization{}, + &kdefault.ManagerAuthProxyPatch{}, + &kdefault.ManagerConfigPatch{}, + &prometheus.Kustomization{}, + &prometheus.Monitor{}, + ) +} diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/kustomization.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/kustomization.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/kustomization.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/kustomization.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/manager_auth_proxy_patch.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/manager_auth_proxy_patch.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/manager_auth_proxy_patch.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/manager_auth_proxy_patch.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/manager_config_patch.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/manager_config_patch.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault/manager_config_patch.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/kdefault/manager_config_patch.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/config.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/config.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/config.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/config.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/controller_manager_config.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/controller_manager_config.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/controller_manager_config.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/controller_manager_config.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/kustomization.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/kustomization.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager/kustomization.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/manager/kustomization.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/prometheus/kustomization.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus/kustomization.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/prometheus/kustomization.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus/kustomization.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/prometheus/monitor.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus/monitor.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/prometheus/monitor.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/prometheus/monitor.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_client_role.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_client_role.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_client_role.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_client_role.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_role.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_role.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_role.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_role.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_role_binding.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_service.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_service.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/auth_proxy_service.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/auth_proxy_service.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/kustomization.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/kustomization.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/kustomization.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/kustomization.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/leader_election_role.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/leader_election_role.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/leader_election_role.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/leader_election_role.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/leader_election_role_binding.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/role_binding.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/role_binding.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/role_binding.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/role_binding.go diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/service_account.go b/pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/service_account.go similarity index 100% rename from pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac/service_account.go rename to pkg/plugins/common/v1/scaffolds/internal/templates/config/rbac/service_account.go diff --git a/pkg/plugins/golang/domain.go b/pkg/plugins/golang/domain.go new file mode 100644 index 00000000000..acfe169c5c3 --- /dev/null +++ b/pkg/plugins/golang/domain.go @@ -0,0 +1,22 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package golang + +import "sigs.k8s.io/kubebuilder/v3/pkg/plugins" + +// DefaultGoNameQualifier is the suffix appended to all kubebuilder plugin names. +const DefaultGoNameQualifier = "go" + plugins.DefaultNameQualifier diff --git a/pkg/plugins/golang/v2/plugin.go b/pkg/plugins/golang/v2/plugin.go index dd38b95ddda..66803066f76 100644 --- a/pkg/plugins/golang/v2/plugin.go +++ b/pkg/plugins/golang/v2/plugin.go @@ -21,10 +21,10 @@ import ( 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" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang" ) -const pluginName = "go" + plugins.DefaultNameQualifier +const pluginName = golang.DefaultGoNameQualifier var ( pluginVersion = plugin.Version{Number: 2} diff --git a/pkg/plugins/golang/v3/init.go b/pkg/plugins/golang/v3/init.go index 3c9883e7254..d762bda4301 100644 --- a/pkg/plugins/golang/v3/init.go +++ b/pkg/plugins/golang/v3/init.go @@ -25,7 +25,6 @@ import ( "github.com/spf13/pflag" "sigs.k8s.io/kubebuilder/v3/pkg/config" - "sigs.k8s.io/kubebuilder/v3/pkg/internal/validation" "sigs.k8s.io/kubebuilder/v3/pkg/machinery" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util" @@ -44,11 +43,8 @@ type initSubcommand struct { license string owner string - // config options - domain string - repo string - name string - componentConfig bool + // go config options + repo string // flags fetchDeps bool @@ -86,21 +82,14 @@ func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) { fs.StringVar(&p.owner, "owner", "", "owner to add to the copyright") // project args - fs.StringVar(&p.domain, "domain", "my.domain", "domain for groups") fs.StringVar(&p.repo, "repo", "", "name to use for go module (e.g., github.com/user/repo), "+ "defaults to the go package of the current working directory.") - fs.StringVar(&p.name, "project-name", "", "name of this project") - fs.BoolVar(&p.componentConfig, "component-config", false, - "create a versioned ComponentConfig file, may be 'true' or 'false'") + } func (p *initSubcommand) InjectConfig(c config.Config) error { p.config = c - if err := p.config.SetDomain(p.domain); err != nil { - return err - } - // Try to guess repository if flag is not set. if p.repo == "" { repoPath, err := golang.FindCurrentRepo() @@ -113,28 +102,6 @@ func (p *initSubcommand) InjectConfig(c config.Config) error { return err } - // Assign a default project name - if p.name == "" { - dir, err := os.Getwd() - if err != nil { - return fmt.Errorf("error getting current directory: %v", err) - } - p.name = strings.ToLower(filepath.Base(dir)) - } - // Check if the project name is a valid k8s namespace (DNS 1123 label). - if err := validation.IsDNS1123Label(p.name); err != nil { - return fmt.Errorf("project name (%s) is invalid: %v", p.name, err) - } - if err := p.config.SetProjectName(p.name); err != nil { - return err - } - - if p.componentConfig { - if err := p.config.SetComponentConfig(); err != nil { - return err - } - } - return nil } diff --git a/pkg/plugins/golang/v3/plugin.go b/pkg/plugins/golang/v3/plugin.go index 1d6a2c381cb..7aa26236e9e 100644 --- a/pkg/plugins/golang/v3/plugin.go +++ b/pkg/plugins/golang/v3/plugin.go @@ -20,10 +20,10 @@ import ( "sigs.k8s.io/kubebuilder/v3/pkg/config" cfgv3 "sigs.k8s.io/kubebuilder/v3/pkg/config/v3" "sigs.k8s.io/kubebuilder/v3/pkg/plugin" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang" ) -const pluginName = "go" + plugins.DefaultNameQualifier +const pluginName = "base" + golang.DefaultGoNameQualifier var ( pluginVersion = plugin.Version{Number: 3} diff --git a/pkg/plugins/golang/v3/scaffolds/init.go b/pkg/plugins/golang/v3/scaffolds/init.go index 1b7b6ece584..14d3c84317c 100644 --- a/pkg/plugins/golang/v3/scaffolds/init.go +++ b/pkg/plugins/golang/v3/scaffolds/init.go @@ -25,11 +25,6 @@ import ( "sigs.k8s.io/kubebuilder/v3/pkg/machinery" "sigs.k8s.io/kubebuilder/v3/pkg/plugins" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/manager" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/prometheus" - "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/rbac" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/hack" ) @@ -103,18 +98,6 @@ func (s *initScaffolder) Scaffold() error { ) return scaffold.Execute( - &rbac.Kustomization{}, - &rbac.AuthProxyRole{}, - &rbac.AuthProxyRoleBinding{}, - &rbac.AuthProxyService{}, - &rbac.AuthProxyClientRole{}, - &rbac.RoleBinding{}, - &rbac.LeaderElectionRole{}, - &rbac.LeaderElectionRoleBinding{}, - &rbac.ServiceAccount{}, - &manager.Kustomization{}, - &manager.Config{Image: imageName}, - &manager.ControllerManagerConfig{}, &templates.Main{}, &templates.GoMod{ControllerRuntimeVersion: ControllerRuntimeVersion}, &templates.GitIgnore{}, @@ -127,13 +110,5 @@ func (s *initScaffolder) Scaffold() error { }, &templates.Dockerfile{}, &templates.DockerIgnore{}, - &kdefault.Kustomization{}, - &kdefault.ManagerAuthProxyPatch{}, - &kdefault.ManagerConfigPatch{}, - &prometheus.Kustomization{}, - &prometheus.Monitor{}, - &certmanager.Certificate{}, - &certmanager.Kustomization{}, - &certmanager.KustomizeConfig{}, ) } diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/certificate.go b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/certificate.go index 76c2b186f65..fc449472db2 100644 --- a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/certificate.go +++ b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/certificate.go @@ -37,6 +37,9 @@ func (f *Certificate) SetTemplateDefaults() error { f.TemplateBody = certManagerTemplate + // If file exists (ex. because a webhook was already created), skip creation. + f.IfExistsAction = machinery.SkipFile + return nil } diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomization.go b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomization.go index 522bdd9b67f..2e31a62d4ae 100644 --- a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomization.go +++ b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomization.go @@ -37,6 +37,9 @@ func (f *Kustomization) SetTemplateDefaults() error { f.TemplateBody = kustomizationTemplate + // If file exists (ex. because a webhook was already created), skip creation. + f.IfExistsAction = machinery.SkipFile + return nil } diff --git a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomizeconfig.go b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomizeconfig.go index 573a7e8f602..02bda5e7341 100644 --- a/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomizeconfig.go +++ b/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager/kustomizeconfig.go @@ -37,6 +37,9 @@ func (f *KustomizeConfig) SetTemplateDefaults() error { f.TemplateBody = kustomizeConfigTemplate + // If file exists (ex. because a webhook was already created), skip creation. + f.IfExistsAction = machinery.SkipFile + return nil } diff --git a/pkg/plugins/golang/v3/scaffolds/webhook.go b/pkg/plugins/golang/v3/scaffolds/webhook.go index 51a020e1495..7035f50029c 100644 --- a/pkg/plugins/golang/v3/scaffolds/webhook.go +++ b/pkg/plugins/golang/v3/scaffolds/webhook.go @@ -27,6 +27,7 @@ import ( "sigs.k8s.io/kubebuilder/v3/pkg/plugins" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/api" + "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/certmanager" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/kdefault" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/config/webhook" "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/v3/scaffolds/internal/templates/hack" @@ -93,6 +94,9 @@ func (s *webhookScaffolder) Scaffold() error { &webhook.Kustomization{Force: s.force}, &webhook.KustomizeConfig{}, &webhook.Service{}, + &certmanager.Certificate{}, + &certmanager.Kustomization{}, + &certmanager.KustomizeConfig{}, ); err != nil { return err } diff --git a/testdata/project-v3-addon/config/certmanager/certificate.yaml b/testdata/project-v3-addon/config/certmanager/certificate.yaml deleted file mode 100644 index 52d866183c7..00000000000 --- a/testdata/project-v3-addon/config/certmanager/certificate.yaml +++ /dev/null @@ -1,25 +0,0 @@ -# The following manifests contain a self-signed issuer CR and a certificate CR. -# More document can be found at https://docs.cert-manager.io -# WARNING: Targets CertManager v1.0. Check https://cert-manager.io/docs/installation/upgrading/ for breaking changes. -apiVersion: cert-manager.io/v1 -kind: Issuer -metadata: - name: selfsigned-issuer - namespace: system -spec: - selfSigned: {} ---- -apiVersion: cert-manager.io/v1 -kind: Certificate -metadata: - name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml - namespace: system -spec: - # $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize - dnsNames: - - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc - - $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local - issuerRef: - kind: Issuer - name: selfsigned-issuer - secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize diff --git a/testdata/project-v3-addon/config/certmanager/kustomization.yaml b/testdata/project-v3-addon/config/certmanager/kustomization.yaml deleted file mode 100644 index bebea5a595e..00000000000 --- a/testdata/project-v3-addon/config/certmanager/kustomization.yaml +++ /dev/null @@ -1,5 +0,0 @@ -resources: -- certificate.yaml - -configurations: -- kustomizeconfig.yaml diff --git a/testdata/project-v3-addon/config/certmanager/kustomizeconfig.yaml b/testdata/project-v3-addon/config/certmanager/kustomizeconfig.yaml deleted file mode 100644 index 90d7c313ca1..00000000000 --- a/testdata/project-v3-addon/config/certmanager/kustomizeconfig.yaml +++ /dev/null @@ -1,16 +0,0 @@ -# This configuration is for teaching kustomize how to update name ref and var substitution -nameReference: -- kind: Issuer - group: cert-manager.io - fieldSpecs: - - kind: Certificate - group: cert-manager.io - path: spec/issuerRef/name - -varReference: -- kind: Certificate - group: cert-manager.io - path: spec/commonName -- kind: Certificate - group: cert-manager.io - path: spec/dnsNames diff --git a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_admirals.yaml b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_admirals.yaml index 57fd324668b..8c7a721b771 100644 --- a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_admirals.yaml +++ b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_admirals.yaml @@ -58,8 +58,6 @@ spec: type: array healthy: type: boolean - phase: - type: string required: - healthy type: object diff --git a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_captains.yaml b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_captains.yaml index fdc98d0233d..96e657c2571 100644 --- a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_captains.yaml +++ b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_captains.yaml @@ -58,8 +58,6 @@ spec: type: array healthy: type: boolean - phase: - type: string required: - healthy type: object diff --git a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_firstmates.yaml b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_firstmates.yaml index dac655c8885..6d0102e0af3 100644 --- a/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_firstmates.yaml +++ b/testdata/project-v3-addon/config/crd/bases/crew.testproject.org_firstmates.yaml @@ -58,8 +58,6 @@ spec: type: array healthy: type: boolean - phase: - type: string required: - healthy type: object diff --git a/testdata/project-v3-addon/go.mod b/testdata/project-v3-addon/go.mod index 79cebd20f2f..fa0e1f82c98 100644 --- a/testdata/project-v3-addon/go.mod +++ b/testdata/project-v3-addon/go.mod @@ -9,5 +9,5 @@ require ( k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.2 - sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20210113160450-b84d99da0217 + sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20200522144838-848d48e5b073 ) diff --git a/testdata/project-v3-config/config/crd/bases/crew.testproject.org_firstmates.yaml b/testdata/project-v3-config/config/crd/bases/crew.testproject.org_firstmates.yaml index dac655c8885..6d0102e0af3 100644 --- a/testdata/project-v3-config/config/crd/bases/crew.testproject.org_firstmates.yaml +++ b/testdata/project-v3-config/config/crd/bases/crew.testproject.org_firstmates.yaml @@ -58,8 +58,6 @@ spec: type: array healthy: type: boolean - phase: - type: string required: - healthy type: object diff --git a/testdata/project-v3-config/go.mod b/testdata/project-v3-config/go.mod index 073e09a9498..e60bfc2f58d 100644 --- a/testdata/project-v3-config/go.mod +++ b/testdata/project-v3-config/go.mod @@ -10,5 +10,5 @@ require ( k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.2 - sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20210113160450-b84d99da0217 + sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20200522144838-848d48e5b073 ) diff --git a/testdata/project-v3/config/crd/bases/crew.testproject.org_firstmates.yaml b/testdata/project-v3/config/crd/bases/crew.testproject.org_firstmates.yaml index dac655c8885..6d0102e0af3 100644 --- a/testdata/project-v3/config/crd/bases/crew.testproject.org_firstmates.yaml +++ b/testdata/project-v3/config/crd/bases/crew.testproject.org_firstmates.yaml @@ -58,8 +58,6 @@ spec: type: array healthy: type: boolean - phase: - type: string required: - healthy type: object diff --git a/testdata/project-v3/go.mod b/testdata/project-v3/go.mod index 705bc66c458..ef9c2635859 100644 --- a/testdata/project-v3/go.mod +++ b/testdata/project-v3/go.mod @@ -10,5 +10,5 @@ require ( k8s.io/apimachinery v0.19.2 k8s.io/client-go v0.19.2 sigs.k8s.io/controller-runtime v0.7.2 - sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20210113160450-b84d99da0217 + sigs.k8s.io/kubebuilder-declarative-pattern v0.0.0-20200522144838-848d48e5b073 )