Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ 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: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,6 @@ 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
61 changes: 42 additions & 19 deletions pkg/rescaffold/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"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"
Expand All @@ -42,7 +40,7 @@ const grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"

func (opts *MigrateOptions) Rescaffold() error {
config := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
if err := config.LoadFrom(opts.InputDir); err != nil {
if err := config.LoadFrom(fmt.Sprintf("%s/%s", opts.InputDir, yaml.DefaultPath)); err != nil {
log.Fatalf("Failed to load PROJECT file %v", err)
}
// create output directory
Expand All @@ -67,7 +65,7 @@ func (opts *MigrateOptions) Rescaffold() error {
log.Fatalf("Failed to run create API subcommand %v", err)
}
// plugin specific migration
if err := kubebuilderGrafanaPlugin(config); err != nil {
if err := migrateGrafanaPlugin(config, opts.InputDir, opts.OutputDir); err != nil {
log.Fatalf("Failed to run plugin migration %v", err)
}
return nil
Expand Down Expand Up @@ -102,7 +100,7 @@ func getInputPath(currentWorkingDirectory string, inputPath string) (string, err
if _, err := os.Stat(projectPath); os.IsNotExist(err) {
return "", fmt.Errorf("PROJECT path: %s does not exist. %v", projectPath, err)
}
return projectPath, nil
return inputPath, nil
}

func getOutputPath(currentWorkingDirectory, outputPath string) (string, error) {
Expand Down Expand Up @@ -152,24 +150,26 @@ 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
func migrateGrafanaPlugin(store store.Store, src, des string) error {
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 {
if errors.As(err, &config.PluginKeyNotFoundError{}) {
log.Printf("Grafana plugin is not found, skip the migration")
return nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us add an warning here, instead of skipping the migration silently.

}
return fmt.Errorf("failed to decode grafana plugin config %v", err)
}
err = kubebuilderGrafanaEdit()
if err != nil {
return fmt.Errorf("Failed to Decode Grafana Plugin: %s. %v", grafanaPluginKey, err)
return err
}
err = grafanaConfigMigrate(src, des)
if err != nil {
return err
}
return migrateGrafanaPlugin()
return kubebuilderGrafanaEdit()
}

func getInitArgs(store store.Store) []string {
Expand Down Expand Up @@ -258,7 +258,30 @@ func getWebhookResourceFlags(resource resource.Resource) []string {
return args
}

func migrateGrafanaPlugin() error {
func copyFile(src, des string) error {
// nolint:gosec
bytesRead, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf("Source file path: %s does not exist. %v", src, err)
}
//Copy all the contents to the desitination file
// nolint:gosec
return os.WriteFile(des, bytesRead, 0755)
}

func grafanaConfigMigrate(src, des string) error {
grafanaConfig := fmt.Sprintf("%s/%s", src, "grafana/custom-metrics/config.yaml")
if _, err := os.Stat(grafanaConfig); os.IsNotExist(err) {
return fmt.Errorf("Grafana Config path: %s does not exist. %v", grafanaConfig, err)
}
return copyFile(grafanaConfig, fmt.Sprintf("%s/%s", des, "grafana/custom-metrics/config.yaml"))
}

func kubebuilderGrafanaEdit() error {
args := []string{"edit", "--plugins", grafanaPluginKey}
return util.RunCmd("kubebuilder edit", "kubebuilder", args...)
err := util.RunCmd("kubebuilder edit", "kubebuilder", args...)
if err != nil {
return fmt.Errorf("Failed to run edit subcommand for Grafana Plugin %v", err)
}
return nil
}
23 changes: 23 additions & 0 deletions test/e2e/alphagenerate/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package alphagenerate

import (
"fmt"
"io"
"os"
"path/filepath"

pluginutil "sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
Expand Down Expand Up @@ -127,6 +129,16 @@ func ReGenerateProject(kbc *utils.TestContext) {
)
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("Edit the grafana config file")
grafanaConfig, err := os.OpenFile(filepath.Join(kbc.Dir, "grafana/custom-metrics/config.yaml"),
os.O_APPEND|os.O_WRONLY, 0644)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
newLine := "test_new_line"
_, err = io.WriteString(grafanaConfig, newLine)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
err = grafanaConfig.Close()
ExpectWithOffset(1, err).NotTo(HaveOccurred())

By("regenerating the project at another output directory")
err = kbc.Regenerate(
"--input-dir", kbc.Dir,
Expand Down Expand Up @@ -191,4 +203,15 @@ func ReGenerateProject(kbc *utils.TestContext) {
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), grafanaPlugin)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())

By("checking if the generated grafana config file has the same content as the old one")
grafanaConfigPath := filepath.Join(kbc.Dir, "grafana/custom-metrics/config.yaml")
generatedGrafanaConfigPath := filepath.Join(kbc.Dir, "testdir2", "grafana/custom-metrics/config.yaml")
Expect(grafanaConfigPath).Should(BeARegularFile())
Expect(generatedGrafanaConfigPath).Should(BeARegularFile())
bytesBefore, err := os.ReadFile(grafanaConfigPath)
Expect(err).NotTo(HaveOccurred())
bytesAfter, err := os.ReadFile(generatedGrafanaConfigPath)
Expect(err).NotTo(HaveOccurred())
Expect(bytesBefore).Should(Equal(bytesAfter))
}