From f4a9fbbb0216bb0298d8bf378346893a49e0b443 Mon Sep 17 00:00:00 2001 From: yyy1000 <992364620@qq.com> Date: Fri, 21 Jul 2023 20:11:03 +0800 Subject: [PATCH] feat: enable grafana plugin migration fix: golang lint refactor: error handling --- go.mod | 1 + go.sum | 2 ++ pkg/rescaffold/migrate.go | 40 +++++++++++++++++++++++-- test/e2e/alphagenerate/generate_test.go | 13 ++++++++ 4 files changed, 53 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 33866c029e..79063c954c 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kr/text v0.2.0 // indirect github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect + golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 golang.org/x/mod v0.12.0 // indirect golang.org/x/net v0.12.0 // indirect golang.org/x/sys v0.10.0 // indirect diff --git a/go.sum b/go.sum index 79c5ca6cab..b43737423f 100644 --- a/go.sum +++ b/go.sum @@ -199,6 +199,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0 golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4= golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM= golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw= +golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc= golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js= golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= diff --git a/pkg/rescaffold/migrate.go b/pkg/rescaffold/migrate.go index bd329aa05c..7e443865c3 100644 --- a/pkg/rescaffold/migrate.go +++ b/pkg/rescaffold/migrate.go @@ -14,12 +14,17 @@ limitations under the License. package rescaffold import ( + "errors" "fmt" "log" "os" "os/exec" + "strings" + + "golang.org/x/exp/slices" "github.com/spf13/afero" + "sigs.k8s.io/kubebuilder/v3/pkg/config" "sigs.k8s.io/kubebuilder/v3/pkg/config/store" "sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml" "sigs.k8s.io/kubebuilder/v3/pkg/machinery" @@ -33,6 +38,7 @@ type MigrateOptions struct { } const DefaultOutputDir = "output-dir" +const grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha" func (opts *MigrateOptions) Rescaffold() error { config := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()}) @@ -56,10 +62,14 @@ func (opts *MigrateOptions) Rescaffold() error { if err := kubebuilderEdit(config); err != nil { log.Fatalf("Failed to run edit subcommand %v", err) } - // create APIs + // create APIs and Webhooks if err := kubebuilderCreate(config); err != nil { log.Fatalf("Failed to run create API subcommand %v", err) } + // plugin specific migration + if err := kubebuilderGrafanaPlugin(config); err != nil { + log.Fatalf("Failed to run plugin migration %v", err) + } return nil } @@ -142,12 +152,31 @@ func kubebuilderCreate(store store.Store) error { return nil } +func kubebuilderGrafanaPlugin(store store.Store) error { + // If the plugin is already in the plugin chain, we don't need call 'edit' method + // Because the plugin is already migrated in the previous step + plugins := store.Config().GetPluginChain() + if slices.Contains(plugins, grafanaPluginKey) { + return nil + } + // If the plugin is not in the plugin chain, we need to call 'edit' method to add the plugin + var grafanaPlugin struct{} + err := store.Config().DecodePluginConfig(grafanaPluginKey, grafanaPlugin) + // If the grafana plugin is not found, we don't need to migrate + if errors.As(err, &config.PluginKeyNotFoundError{}) { + return nil + } + if err != nil { + return fmt.Errorf("Failed to Decode Grafana Plugin: %s. %v", grafanaPluginKey, err) + } + return migrateGrafanaPlugin() +} + func getInitArgs(store store.Store) []string { var args []string plugins := store.Config().GetPluginChain() if len(plugins) > 0 { - args = append(args, "--plugins") - args = append(args, plugins...) + args = append(args, "--plugins", strings.Join(plugins, ",")) } domain := store.Config().GetDomain() if domain != "" { @@ -228,3 +257,8 @@ func getWebhookResourceFlags(resource resource.Resource) []string { } return args } + +func migrateGrafanaPlugin() error { + args := []string{"edit", "--plugins", grafanaPluginKey} + return util.RunCmd("kubebuilder edit", "kubebuilder", args...) +} diff --git a/test/e2e/alphagenerate/generate_test.go b/test/e2e/alphagenerate/generate_test.go index 30d9f478f7..6153f41eec 100644 --- a/test/e2e/alphagenerate/generate_test.go +++ b/test/e2e/alphagenerate/generate_test.go @@ -121,6 +121,12 @@ func ReGenerateProject(kbc *utils.TestContext) { ) ExpectWithOffset(1, err).NotTo(HaveOccurred()) + By("Enable grafana plugin to an existing project") + err = kbc.Edit( + "--plugins", "grafana.kubebuilder.io/v1-alpha", + ) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + By("regenerating the project at another output directory") err = kbc.Regenerate( "--input-dir", kbc.Dir, @@ -178,4 +184,11 @@ func ReGenerateProject(kbc *utils.TestContext) { filepath.Join(kbc.Dir, "testdir2", "PROJECT"), webhook) ExpectWithOffset(1, err).NotTo(HaveOccurred()) ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) + + By("checking if the project file was generated with the expected controller") + var grafanaPlugin = "grafana.kubebuilder.io/v1-alpha" + fileContainsExpr, err = pluginutil.HasFileContentWith( + filepath.Join(kbc.Dir, "testdir2", "PROJECT"), grafanaPlugin) + ExpectWithOffset(1, err).NotTo(HaveOccurred()) + ExpectWithOffset(1, fileContainsExpr).To(BeTrue()) }