Skip to content

Commit 9fa1285

Browse files
author
Mateus Oliveira
committed
fix: centralize plugin key declaration
Signed-off-by: Mateus Oliveira <[email protected]>
1 parent 077dea8 commit 9fa1285

File tree

9 files changed

+37
-38
lines changed

9 files changed

+37
-38
lines changed

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func init() {
3939
}
4040

4141
func main() {
42-
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v4 with kustomize v2
42+
// Bundle plugin which built the golang projects scaffold with base.go/v4 and kustomize/v2 plugins
4343
gov4Bundle, _ := plugin.NewBundleWithOptions(plugin.WithName(golang.DefaultNameQualifier),
4444
plugin.WithVersion(plugin.Version{Number: 4}),
4545
plugin.WithPlugins(kustomizecommonv2.Plugin{}, golangv4.Plugin{}),

pkg/cli/alpha/internal/generate.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,19 @@ import (
3131
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
3232
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
3333
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
34+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3435
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
3536
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
37+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
38+
hemlv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
3639
)
3740

3841
type Generate struct {
3942
InputDir string
4043
OutputDir string
4144
}
4245

43-
const (
44-
defaultOutputDir = "output-dir"
45-
grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"
46-
deployImagePluginKey = "deploy-image.go.kubebuilder.io/v1-alpha"
47-
helmPluginKey = "helm.kubebuilder.io/v1-alpha"
48-
)
46+
const defaultOutputDir = "output-dir"
4947

5048
// Generate handles the migration and scaffolding process.
5149
func (opts *Generate) Generate() error {
@@ -178,7 +176,7 @@ func kubebuilderCreate(store store.Store) error {
178176
// Migrates the Grafana plugin.
179177
func migrateGrafanaPlugin(store store.Store, src, des string) error {
180178
var grafanaPlugin struct{}
181-
err := store.Config().DecodePluginConfig(grafanaPluginKey, grafanaPlugin)
179+
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha.Plugin{}), grafanaPlugin)
182180
if errors.As(err, &config.PluginKeyNotFoundError{}) {
183181
log.Info("Grafana plugin not found, skipping migration")
184182
return nil
@@ -200,7 +198,7 @@ func migrateGrafanaPlugin(store store.Store, src, des string) error {
200198
// Migrates the Deploy Image plugin.
201199
func migrateDeployImagePlugin(store store.Store) error {
202200
var deployImagePlugin v1alpha1.PluginConfig
203-
err := store.Config().DecodePluginConfig(deployImagePluginKey, &deployImagePlugin)
201+
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &deployImagePlugin)
204202
if errors.As(err, &config.PluginKeyNotFoundError{}) {
205203
log.Info("Deploy-image plugin not found, skipping migration")
206204
return nil
@@ -308,7 +306,7 @@ func getDeployImageOptions(resource v1alpha1.ResourceData) []string {
308306
if resource.Options.RunAsUser != "" {
309307
args = append(args, fmt.Sprintf("--run-as-user=%s", resource.Options.RunAsUser))
310308
}
311-
args = append(args, fmt.Sprintf("--plugins=%s", "deploy-image/v1-alpha"))
309+
args = append(args, fmt.Sprintf("--plugins=%s", plugin.KeyFor(v1alpha1.Plugin{})))
312310
return args
313311
}
314312

@@ -393,7 +391,7 @@ func grafanaConfigMigrate(src, des string) error {
393391

394392
// Edits the project to include the Grafana plugin.
395393
func kubebuilderGrafanaEdit() error {
396-
args := []string{"edit", "--plugins", grafanaPluginKey}
394+
args := []string{"edit", "--plugins", plugin.KeyFor(v1alpha.Plugin{})}
397395
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
398396
return fmt.Errorf("failed to run edit subcommand for Grafana plugin: %w", err)
399397
}
@@ -402,7 +400,7 @@ func kubebuilderGrafanaEdit() error {
402400

403401
// Edits the project to include the Helm plugin.
404402
func kubebuilderHelmEdit() error {
405-
args := []string{"edit", "--plugins", helmPluginKey}
403+
args := []string{"edit", "--plugins", plugin.KeyFor(hemlv1alpha.Plugin{})}
406404
if err := util.RunCmd("kubebuilder edit", "kubebuilder", args...); err != nil {
407405
return fmt.Errorf("failed to run edit subcommand for Helm plugin: %w", err)
408406
}
@@ -414,7 +412,7 @@ func hasHelmPlugin(cfg store.Store) bool {
414412
var pluginConfig map[string]interface{}
415413

416414
// Decode the Helm plugin configuration to check if it's present
417-
err := cfg.Config().DecodePluginConfig(helmPluginKey, &pluginConfig)
415+
err := cfg.Config().DecodePluginConfig(plugin.KeyFor(hemlv1alpha.Plugin{}), &pluginConfig)
418416
if err != nil {
419417
// If the Helm plugin is not found, return false
420418
if errors.As(err, &config.PluginKeyNotFoundError{}) {

pkg/plugins/common/kustomize/v2/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
4949
NOTE: This plugin requires kustomize version v5 and kubectl >= 1.22.
5050
`
5151
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
52-
%[1]s init --plugins common/v3 --domain example.org
52+
%[1]s init --plugins %[2]s --domain example.org
5353
5454
# Initialize a common project defining a specific project version
55-
%[1]s init --plugins common/v3 --project-version 3
56-
`, cliMeta.CommandName)
55+
%[1]s init --plugins %[2]s --project-version 3
56+
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
5757
}
5858

5959
func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {

pkg/plugins/golang/deploy-image/v1alpha1/api.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,31 @@ type createAPISubcommand struct {
6565
func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
6666
// nolint: lll
6767
subcmdMeta.Description = `Scaffold the code implementation to deploy and manage your Operand which is represented by the API informed and will be reconciled by its controller. This plugin will generate the code implementation to help you out.
68-
68+
6969
Note: In general, it’s recommended to have one controller responsible for managing each API created for the project to properly follow the design goals set by Controller Runtime(https://github.com/kubernetes-sigs/controller-runtime).
7070
7171
This plugin will work as the common behaviour of the flag --force and will scaffold the API and controller always. Use core types or external APIs is not officially support by default with.
7272
`
7373
// nolint: lll
74-
subcmdMeta.Examples = fmt.Sprintf(` # Create a frigates API with Group: ship, Version: v1beta1, Kind: Frigate to represent the
74+
subcmdMeta.Examples = fmt.Sprintf(` # Create a frigates API with Group: ship, Version: v1beta1, Kind: Frigate to represent the
7575
Image: example.com/frigate:v0.0.1 and its controller with a code to deploy and manage this Operand.
76-
76+
7777
Note that in the following example we are also adding the optional options to let you inform the command which should be used to create the container and initialize itvia the flag --image-container-command as the Port that should be used
7878
7979
- By informing the command (--image-container-command="memcached,--memory-limit=64,-o,modern,-v") your deployment will be scaffold with, i.e.:
8080
8181
Command: []string{"memcached","--memory-limit=64","-o","modern","-v"},
8282
83-
- By informing the Port (--image-container-port) will deployment will be scaffold with, i.e:
83+
- By informing the Port (--image-container-port) will deployment will be scaffold with, i.e:
8484
8585
Ports: []corev1.ContainerPort{
8686
ContainerPort: Memcached.Spec.ContainerPort,
8787
Name: "Memcached",
8888
},
8989
90-
Therefore, the default values informed will be used to scaffold specs for the API.
90+
Therefore, the default values informed will be used to scaffold specs for the API.
9191
92-
%[1]s create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.6.15-alpine --image-container-command="memcached --memory-limit=64 modern -v" --image-container-port="11211" --plugins="deploy-image/v1-alpha" --make=false --namespaced=false
92+
%[1]s create api --group example.com --version v1alpha1 --kind Memcached --image=memcached:1.6.15-alpine --image-container-command="memcached --memory-limit=64 modern -v" --image-container-port="11211" --plugins="%[2]s" --make=false --namespaced=false
9393
9494
# Generate the manifests
9595
make manifests
@@ -99,7 +99,7 @@ func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdM
9999
100100
# Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config
101101
make run
102-
`, cliMeta.CommandName)
102+
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
103103
}
104104

105105
func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {

pkg/plugins/optional/grafana/v1alpha/edit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
3535
subcmdMeta.Description = MetaDataDescription
3636

3737
subcmdMeta.Examples = fmt.Sprintf(` # Edit a common project with this plugin
38-
%[1]s edit --plugins=grafana.kubebuilder.io/v1-alpha
39-
`, cliMeta.CommandName)
38+
%[1]s edit --plugins=%[2]s
39+
`, cliMeta.CommandName, pluginKey)
4040
}
4141

4242
func (p *editSubcommand) InjectConfig(c config.Config) error {

pkg/plugins/optional/grafana/v1alpha/init.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
3535
subcmdMeta.Description = MetaDataDescription
3636

3737
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with this plugin
38-
%[1]s init --plugins=grafana.kubebuilder.io/v1-alpha
39-
`, cliMeta.CommandName)
38+
%[1]s init --plugins=%[2]s
39+
`, cliMeta.CommandName, pluginKey)
4040
}
4141

4242
func (p *initSubcommand) InjectConfig(c config.Config) error {

pkg/plugins/optional/helm/v1alpha/edit.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ type editSubcommand struct {
3737
func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
3838
subcmdMeta.Description = `Initialize or update a Helm chart to distribute the project under the dist/ directory.
3939
40-
**NOTE** Before running the edit command, ensure you first execute 'make manifests' to regenerate
40+
**NOTE** Before running the edit command, ensure you first execute 'make manifests' to regenerate
4141
the latest Helm chart with your most recent changes.`
4242

4343
subcmdMeta.Examples = fmt.Sprintf(`# Initialize or update a Helm chart to distribute the project under the dist/ directory
44-
%[1]s edit --plugins=helm/v1-alpha
44+
%[1]s edit --plugins=%[2]s
4545
4646
# Update the Helm chart under the dist/ directory and overwrite all files
47-
%[1]s edit --plugins=helm/v1-alpha --force
47+
%[1]s edit --plugins=%[2]s --force
4848
4949
**IMPORTANT**: If the "--force" flag is not used, the following files will not be updated to preserve your customizations:
5050
dist/chart/
@@ -58,10 +58,10 @@ The following files are never updated after their initial creation:
5858
- chart/templates/_helpers.tpl
5959
- chart/.helmignore
6060
61-
All other files are updated without the usage of the '--force=true' flag
62-
when the edit option is used to ensure that the
61+
All other files are updated without the usage of the '--force=true' flag
62+
when the edit option is used to ensure that the
6363
manifests in the chart align with the latest changes.
64-
`, cliMeta.CommandName)
64+
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
6565
}
6666

6767
func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {

pkg/plugins/optional/helm/v1alpha/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
3535
subcmdMeta.Description = `Initialize a helm chart to distribute the project under dist/
3636
`
3737
subcmdMeta.Examples = fmt.Sprintf(`# Initialize a helm chart to distribute the project under dist/
38-
%[1]s init --plugins=helm/v1-alpha
38+
%[1]s init --plugins=%[2]s
3939
40-
**IMPORTANT** You must use %[1]s edit --plugins=helm/v1-alpha to update the chart when changes are made.
41-
`, cliMeta.CommandName)
40+
**IMPORTANT** You must use %[1]s edit --plugins=%[2]s to update the chart when changes are made.
41+
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
4242
}
4343

4444
func (p *initSubcommand) InjectConfig(c config.Config) error {

pkg/plugins/optional/helm/v1alpha/scaffolds/init.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ import (
2929

3030
"sigs.k8s.io/kubebuilder/v4/pkg/config"
3131
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
32+
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
3233
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
34+
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
3335
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
3436
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates"
3537
chart_templates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
@@ -132,8 +134,7 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
132134
} `json:"resources"`
133135
}{}
134136

135-
const deployImageKey = "deploy-image.go.kubebuilder.io/v1-alpha"
136-
err := s.config.DecodePluginConfig(deployImageKey, &pluginConfig)
137+
err := s.config.DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &pluginConfig)
137138
if err == nil {
138139
for _, res := range pluginConfig.Resources {
139140
image, ok := res.Options["image"]

0 commit comments

Comments
 (0)