Skip to content

Commit f4a9fbb

Browse files
committed
feat: enable grafana plugin migration
fix: golang lint refactor: error handling
1 parent 5356022 commit f4a9fbb

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2424
github.com/kr/text v0.2.0 // indirect
2525
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
26+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1
2627
golang.org/x/mod v0.12.0 // indirect
2728
golang.org/x/net v0.12.0 // indirect
2829
golang.org/x/sys v0.10.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
199199
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
200200
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
201201
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
202+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1 h1:MGwJjxBy0HJshjDNfLsYO8xppfqWlA5ZT9OhtUUhTNw=
203+
golang.org/x/exp v0.0.0-20230713183714-613f0c0eb8a1/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
202204
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
203205
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
204206
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=

pkg/rescaffold/migrate.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ limitations under the License.
1414
package rescaffold
1515

1616
import (
17+
"errors"
1718
"fmt"
1819
"log"
1920
"os"
2021
"os/exec"
22+
"strings"
23+
24+
"golang.org/x/exp/slices"
2125

2226
"github.com/spf13/afero"
27+
"sigs.k8s.io/kubebuilder/v3/pkg/config"
2328
"sigs.k8s.io/kubebuilder/v3/pkg/config/store"
2429
"sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
2530
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
@@ -33,6 +38,7 @@ type MigrateOptions struct {
3338
}
3439

3540
const DefaultOutputDir = "output-dir"
41+
const grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"
3642

3743
func (opts *MigrateOptions) Rescaffold() error {
3844
config := yaml.New(machinery.Filesystem{FS: afero.NewOsFs()})
@@ -56,10 +62,14 @@ func (opts *MigrateOptions) Rescaffold() error {
5662
if err := kubebuilderEdit(config); err != nil {
5763
log.Fatalf("Failed to run edit subcommand %v", err)
5864
}
59-
// create APIs
65+
// create APIs and Webhooks
6066
if err := kubebuilderCreate(config); err != nil {
6167
log.Fatalf("Failed to run create API subcommand %v", err)
6268
}
69+
// plugin specific migration
70+
if err := kubebuilderGrafanaPlugin(config); err != nil {
71+
log.Fatalf("Failed to run plugin migration %v", err)
72+
}
6373
return nil
6474
}
6575

@@ -142,12 +152,31 @@ func kubebuilderCreate(store store.Store) error {
142152
return nil
143153
}
144154

155+
func kubebuilderGrafanaPlugin(store store.Store) error {
156+
// If the plugin is already in the plugin chain, we don't need call 'edit' method
157+
// Because the plugin is already migrated in the previous step
158+
plugins := store.Config().GetPluginChain()
159+
if slices.Contains(plugins, grafanaPluginKey) {
160+
return nil
161+
}
162+
// If the plugin is not in the plugin chain, we need to call 'edit' method to add the plugin
163+
var grafanaPlugin struct{}
164+
err := store.Config().DecodePluginConfig(grafanaPluginKey, grafanaPlugin)
165+
// If the grafana plugin is not found, we don't need to migrate
166+
if errors.As(err, &config.PluginKeyNotFoundError{}) {
167+
return nil
168+
}
169+
if err != nil {
170+
return fmt.Errorf("Failed to Decode Grafana Plugin: %s. %v", grafanaPluginKey, err)
171+
}
172+
return migrateGrafanaPlugin()
173+
}
174+
145175
func getInitArgs(store store.Store) []string {
146176
var args []string
147177
plugins := store.Config().GetPluginChain()
148178
if len(plugins) > 0 {
149-
args = append(args, "--plugins")
150-
args = append(args, plugins...)
179+
args = append(args, "--plugins", strings.Join(plugins, ","))
151180
}
152181
domain := store.Config().GetDomain()
153182
if domain != "" {
@@ -228,3 +257,8 @@ func getWebhookResourceFlags(resource resource.Resource) []string {
228257
}
229258
return args
230259
}
260+
261+
func migrateGrafanaPlugin() error {
262+
args := []string{"edit", "--plugins", grafanaPluginKey}
263+
return util.RunCmd("kubebuilder edit", "kubebuilder", args...)
264+
}

test/e2e/alphagenerate/generate_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,12 @@ func ReGenerateProject(kbc *utils.TestContext) {
121121
)
122122
ExpectWithOffset(1, err).NotTo(HaveOccurred())
123123

124+
By("Enable grafana plugin to an existing project")
125+
err = kbc.Edit(
126+
"--plugins", "grafana.kubebuilder.io/v1-alpha",
127+
)
128+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
129+
124130
By("regenerating the project at another output directory")
125131
err = kbc.Regenerate(
126132
"--input-dir", kbc.Dir,
@@ -178,4 +184,11 @@ func ReGenerateProject(kbc *utils.TestContext) {
178184
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), webhook)
179185
ExpectWithOffset(1, err).NotTo(HaveOccurred())
180186
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
187+
188+
By("checking if the project file was generated with the expected controller")
189+
var grafanaPlugin = "grafana.kubebuilder.io/v1-alpha"
190+
fileContainsExpr, err = pluginutil.HasFileContentWith(
191+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), grafanaPlugin)
192+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
193+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
181194
}

0 commit comments

Comments
 (0)