Skip to content

Commit

Permalink
feat: enable grafana plugin migration
Browse files Browse the repository at this point in the history
fix: golang lint

refactor: error handling
  • Loading branch information
yyy1000 committed Jul 23, 2023
1 parent 5356022 commit f4a9fbb
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
40 changes: 37 additions & 3 deletions pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()})
Expand All @@ -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
}

Expand Down Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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...)
}
13 changes: 13 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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())
}

0 comments on commit f4a9fbb

Please sign in to comment.