Skip to content

Commit

Permalink
Merge pull request #4390 from mateusoliveira43/fix/centralize-plugin-key
Browse files Browse the repository at this point in the history
🌱 fix: centralize plugin key declaration
  • Loading branch information
k8s-ci-robot authored Nov 27, 2024
2 parents c32f971 + 9fa1285 commit cbc6e38
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 38 deletions.
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func init() {
}

func main() {
// Bundle plugin which built the golang projects scaffold by Kubebuilder go/v4 with kustomize v2
// Bundle plugin which built the golang projects scaffold with base.go/v4 and kustomize/v2 plugins
gov4Bundle, _ := plugin.NewBundleWithOptions(plugin.WithName(golang.DefaultNameQualifier),
plugin.WithVersion(plugin.Version{Number: 4}),
plugin.WithPlugins(kustomizecommonv2.Plugin{}, golangv4.Plugin{}),
Expand Down
22 changes: 10 additions & 12 deletions pkg/cli/alpha/internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,19 @@ import (
"sigs.k8s.io/kubebuilder/v4/pkg/config/store/yaml"
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
"sigs.k8s.io/kubebuilder/v4/pkg/model/resource"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
hemlv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
)

type Generate struct {
InputDir string
OutputDir string
}

const (
defaultOutputDir = "output-dir"
grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"
deployImagePluginKey = "deploy-image.go.kubebuilder.io/v1-alpha"
helmPluginKey = "helm.kubebuilder.io/v1-alpha"
)
const defaultOutputDir = "output-dir"

// Generate handles the migration and scaffolding process.
func (opts *Generate) Generate() error {
Expand Down Expand Up @@ -178,7 +176,7 @@ func kubebuilderCreate(store store.Store) error {
// Migrates the Grafana plugin.
func migrateGrafanaPlugin(store store.Store, src, des string) error {
var grafanaPlugin struct{}
err := store.Config().DecodePluginConfig(grafanaPluginKey, grafanaPlugin)
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha.Plugin{}), grafanaPlugin)
if errors.As(err, &config.PluginKeyNotFoundError{}) {
log.Info("Grafana plugin not found, skipping migration")
return nil
Expand All @@ -200,7 +198,7 @@ func migrateGrafanaPlugin(store store.Store, src, des string) error {
// Migrates the Deploy Image plugin.
func migrateDeployImagePlugin(store store.Store) error {
var deployImagePlugin v1alpha1.PluginConfig
err := store.Config().DecodePluginConfig(deployImagePluginKey, &deployImagePlugin)
err := store.Config().DecodePluginConfig(plugin.KeyFor(v1alpha1.Plugin{}), &deployImagePlugin)
if errors.As(err, &config.PluginKeyNotFoundError{}) {
log.Info("Deploy-image plugin not found, skipping migration")
return nil
Expand Down Expand Up @@ -308,7 +306,7 @@ func getDeployImageOptions(resource v1alpha1.ResourceData) []string {
if resource.Options.RunAsUser != "" {
args = append(args, fmt.Sprintf("--run-as-user=%s", resource.Options.RunAsUser))
}
args = append(args, fmt.Sprintf("--plugins=%s", "deploy-image/v1-alpha"))
args = append(args, fmt.Sprintf("--plugins=%s", plugin.KeyFor(v1alpha1.Plugin{})))
return args
}

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

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

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

// Decode the Helm plugin configuration to check if it's present
err := cfg.Config().DecodePluginConfig(helmPluginKey, &pluginConfig)
err := cfg.Config().DecodePluginConfig(plugin.KeyFor(hemlv1alpha.Plugin{}), &pluginConfig)
if err != nil {
// If the Helm plugin is not found, return false
if errors.As(err, &config.PluginKeyNotFoundError{}) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/common/kustomize/v2/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
NOTE: This plugin requires kustomize version v5 and kubectl >= 1.22.
`
subcmdMeta.Examples = fmt.Sprintf(` # Initialize a common project with your domain and name in copyright
%[1]s init --plugins common/v3 --domain example.org
%[1]s init --plugins %[2]s --domain example.org
# Initialize a common project defining a specific project version
%[1]s init --plugins common/v3 --project-version 3
`, cliMeta.CommandName)
%[1]s init --plugins %[2]s --project-version 3
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
}

func (p *initSubcommand) BindFlags(fs *pflag.FlagSet) {
Expand Down
14 changes: 7 additions & 7 deletions pkg/plugins/golang/deploy-image/v1alpha1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,31 @@ type createAPISubcommand struct {
func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
// nolint: lll
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.
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).
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.
`
// nolint: lll
subcmdMeta.Examples = fmt.Sprintf(` # Create a frigates API with Group: ship, Version: v1beta1, Kind: Frigate to represent the
subcmdMeta.Examples = fmt.Sprintf(` # Create a frigates API with Group: ship, Version: v1beta1, Kind: Frigate to represent the
Image: example.com/frigate:v0.0.1 and its controller with a code to deploy and manage this Operand.
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
- By informing the command (--image-container-command="memcached,--memory-limit=64,-o,modern,-v") your deployment will be scaffold with, i.e.:
Command: []string{"memcached","--memory-limit=64","-o","modern","-v"},
- By informing the Port (--image-container-port) will deployment will be scaffold with, i.e:
- By informing the Port (--image-container-port) will deployment will be scaffold with, i.e:
Ports: []corev1.ContainerPort{
ContainerPort: Memcached.Spec.ContainerPort,
Name: "Memcached",
},
Therefore, the default values informed will be used to scaffold specs for the API.
Therefore, the default values informed will be used to scaffold specs for the API.
%[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
%[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
# Generate the manifests
make manifests
Expand All @@ -99,7 +99,7 @@ func (p *createAPISubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdM
# Regenerate code and run against the Kubernetes cluster configured by ~/.kube/config
make run
`, cliMeta.CommandName)
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
}

func (p *createAPISubcommand) BindFlags(fs *pflag.FlagSet) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/optional/grafana/v1alpha/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
subcmdMeta.Description = MetaDataDescription

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

func (p *editSubcommand) InjectConfig(c config.Config) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/plugins/optional/grafana/v1alpha/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
subcmdMeta.Description = MetaDataDescription

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

func (p *initSubcommand) InjectConfig(c config.Config) error {
Expand Down
12 changes: 6 additions & 6 deletions pkg/plugins/optional/helm/v1alpha/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ type editSubcommand struct {
func (p *editSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *plugin.SubcommandMetadata) {
subcmdMeta.Description = `Initialize or update a Helm chart to distribute the project under the dist/ directory.
**NOTE** Before running the edit command, ensure you first execute 'make manifests' to regenerate
**NOTE** Before running the edit command, ensure you first execute 'make manifests' to regenerate
the latest Helm chart with your most recent changes.`

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

func (p *editSubcommand) BindFlags(fs *pflag.FlagSet) {
Expand Down
6 changes: 3 additions & 3 deletions pkg/plugins/optional/helm/v1alpha/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (p *initSubcommand) UpdateMetadata(cliMeta plugin.CLIMetadata, subcmdMeta *
subcmdMeta.Description = `Initialize a helm chart to distribute the project under dist/
`
subcmdMeta.Examples = fmt.Sprintf(`# Initialize a helm chart to distribute the project under dist/
%[1]s init --plugins=helm/v1-alpha
%[1]s init --plugins=%[2]s
**IMPORTANT** You must use %[1]s edit --plugins=helm/v1-alpha to update the chart when changes are made.
`, cliMeta.CommandName)
**IMPORTANT** You must use %[1]s edit --plugins=%[2]s to update the chart when changes are made.
`, cliMeta.CommandName, plugin.KeyFor(Plugin{}))
}

func (p *initSubcommand) InjectConfig(c config.Config) error {
Expand Down
5 changes: 3 additions & 2 deletions pkg/plugins/optional/helm/v1alpha/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ import (

"sigs.k8s.io/kubebuilder/v4/pkg/config"
"sigs.k8s.io/kubebuilder/v4/pkg/machinery"
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/golang/deploy-image/v1alpha1"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm"
"sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates"
chart_templates "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha/scaffolds/internal/templates/chart-templates"
Expand Down Expand Up @@ -132,8 +134,7 @@ func (s *initScaffolder) getDeployImagesEnvVars() map[string]string {
} `json:"resources"`
}{}

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

0 comments on commit cbc6e38

Please sign in to comment.