@@ -34,8 +34,8 @@ import (
34
34
"sigs.k8s.io/kubebuilder/v4/pkg/plugin"
35
35
"sigs.k8s.io/kubebuilder/v4/pkg/plugin/util"
36
36
"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"
37
+ grafanav1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/grafana/v1alpha"
38
+ helmv1alpha "sigs.k8s.io/kubebuilder/v4/pkg/plugins/optional/helm/v1alpha"
39
39
)
40
40
41
41
// Generate store the required info for the command
@@ -110,8 +110,8 @@ func (opts *Generate) Generate() error {
110
110
}
111
111
112
112
if hasHelmPlugin (projectConfig ) {
113
- if err = kubebuilderHelmEdit ( ); err != nil {
114
- return fmt .Errorf ("error editing Helm plugin: %w" , err )
113
+ if err = migrateHelmPlugin ( projectConfig ); err != nil {
114
+ return fmt .Errorf ("error migrating Helm plugin: %w" , err )
115
115
}
116
116
}
117
117
@@ -226,7 +226,7 @@ func kubebuilderCreate(s store.Store) error {
226
226
// Migrates the Grafana plugin.
227
227
func migrateGrafanaPlugin (s store.Store , src , des string ) error {
228
228
var grafanaPlugin struct {}
229
- err := s .Config ().DecodePluginConfig (plugin .KeyFor (v1alpha .Plugin {}), grafanaPlugin )
229
+ err := s .Config ().DecodePluginConfig (plugin .KeyFor (grafanav1alpha .Plugin {}), grafanaPlugin )
230
230
if errors .As (err , & config.PluginKeyNotFoundError {}) {
231
231
log .Info ("Grafana plugin not found, skipping migration" )
232
232
return nil
@@ -481,28 +481,58 @@ func grafanaConfigMigrate(src, des string) error {
481
481
482
482
// Edits the project to include the Grafana plugin.
483
483
func kubebuilderGrafanaEdit () error {
484
- args := []string {"edit" , "--plugins" , plugin .KeyFor (v1alpha .Plugin {})}
484
+ args := []string {"edit" , "--plugins" , plugin .KeyFor (grafanav1alpha .Plugin {})}
485
485
if err := util .RunCmd ("kubebuilder edit" , "kubebuilder" , args ... ); err != nil {
486
486
return fmt .Errorf ("failed to run edit subcommand for Grafana plugin: %w" , err )
487
487
}
488
488
return nil
489
489
}
490
490
491
+ // Migrates the Helm plugin.
492
+ func migrateHelmPlugin (s store.Store ) error {
493
+ var helmPlugin helmv1alpha.PluginConfig
494
+
495
+ err := s .Config ().DecodePluginConfig (plugin .KeyFor (helmv1alpha.Plugin {}), & helmPlugin )
496
+ if errors .As (err , & config.PluginKeyNotFoundError {}) {
497
+ log .Info ("Helm plugin not found, skipping migration" )
498
+ return nil
499
+ } else if err != nil {
500
+ return fmt .Errorf ("failed to decode Helm plugin config: %w" , err )
501
+ }
502
+
503
+ return kubebuilderHelmEdit (helmPlugin )
504
+ }
505
+
491
506
// Edits the project to include the Helm plugin.
492
- func kubebuilderHelmEdit () error {
493
- args := []string {"edit" , "--plugins" , plugin .KeyFor (hemlv1alpha.Plugin {})}
507
+ func kubebuilderHelmEdit (resourceData helmv1alpha.PluginConfig ) error {
508
+ args := []string {"edit" , "--plugins" , plugin .KeyFor (helmv1alpha.Plugin {})}
509
+ args = append (args , getHelmOptions (resourceData )... )
494
510
if err := util .RunCmd ("kubebuilder edit" , "kubebuilder" , args ... ); err != nil {
495
511
return fmt .Errorf ("failed to run edit subcommand for Helm plugin: %w" , err )
496
512
}
497
513
return nil
498
514
}
499
515
516
+ // Gets the options for Helm resource.
517
+ // If the directory is not the default, it sets the directory option.
518
+ // otherwise, it returns an empty slice which then use the default value from the edit/init subcommand.
519
+ func getHelmOptions (resourceData helmv1alpha.PluginConfig ) []string {
520
+ var args []string
521
+
522
+ if resourceData .Options .Directory != helmv1alpha .HelmDefaultTargetDirectory {
523
+ log .Info ("setting directory for Helm chart" )
524
+ args = append (args , fmt .Sprintf ("--directory=%s" , resourceData .Options .Directory ))
525
+ }
526
+
527
+ return args
528
+ }
529
+
500
530
// hasHelmPlugin checks if the Helm plugin is present by inspecting the plugin chain or configuration.
501
531
func hasHelmPlugin (cfg store.Store ) bool {
502
532
var pluginConfig map [string ]interface {}
503
533
504
534
// Decode the Helm plugin configuration to check if it's present
505
- err := cfg .Config ().DecodePluginConfig (plugin .KeyFor (hemlv1alpha .Plugin {}), & pluginConfig )
535
+ err := cfg .Config ().DecodePluginConfig (plugin .KeyFor (helmv1alpha .Plugin {}), & pluginConfig )
506
536
if err != nil {
507
537
// If the Helm plugin is not found, return false
508
538
if errors .As (err , & config.PluginKeyNotFoundError {}) {
0 commit comments