@@ -21,8 +21,6 @@ import (
21
21
"os/exec"
22
22
"strings"
23
23
24
- "golang.org/x/exp/slices"
25
-
26
24
"github.com/spf13/afero"
27
25
"sigs.k8s.io/kubebuilder/v3/pkg/config"
28
26
"sigs.k8s.io/kubebuilder/v3/pkg/config/store"
@@ -42,7 +40,7 @@ const grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"
42
40
43
41
func (opts * MigrateOptions ) Rescaffold () error {
44
42
config := yaml .New (machinery.Filesystem {FS : afero .NewOsFs ()})
45
- if err := config .LoadFrom (opts .InputDir ); err != nil {
43
+ if err := config .LoadFrom (fmt . Sprintf ( "%s/%s" , opts .InputDir , yaml . DefaultPath ) ); err != nil {
46
44
log .Fatalf ("Failed to load PROJECT file %v" , err )
47
45
}
48
46
// create output directory
@@ -67,7 +65,7 @@ func (opts *MigrateOptions) Rescaffold() error {
67
65
log .Fatalf ("Failed to run create API subcommand %v" , err )
68
66
}
69
67
// plugin specific migration
70
- if err := kubebuilderGrafanaPlugin (config ); err != nil {
68
+ if err := migrateGrafanaPlugin (config , opts . InputDir , opts . OutputDir ); err != nil {
71
69
log .Fatalf ("Failed to run plugin migration %v" , err )
72
70
}
73
71
return nil
@@ -102,7 +100,7 @@ func getInputPath(currentWorkingDirectory string, inputPath string) (string, err
102
100
if _ , err := os .Stat (projectPath ); os .IsNotExist (err ) {
103
101
return "" , fmt .Errorf ("PROJECT path: %s does not exist. %v" , projectPath , err )
104
102
}
105
- return projectPath , nil
103
+ return inputPath , nil
106
104
}
107
105
108
106
func getOutputPath (currentWorkingDirectory , outputPath string ) (string , error ) {
@@ -152,24 +150,26 @@ func kubebuilderCreate(store store.Store) error {
152
150
return nil
153
151
}
154
152
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
153
+ func migrateGrafanaPlugin (store store.Store , src , des string ) error {
163
154
var grafanaPlugin struct {}
164
155
err := store .Config ().DecodePluginConfig (grafanaPluginKey , grafanaPlugin )
165
156
// If the grafana plugin is not found, we don't need to migrate
166
- if errors .As (err , & config.PluginKeyNotFoundError {}) {
167
- return nil
157
+ if err != nil {
158
+ if errors .As (err , & config.PluginKeyNotFoundError {}) {
159
+ log .Printf ("Grafana plugin is not found, skip the migration" )
160
+ return nil
161
+ }
162
+ return fmt .Errorf ("failed to decode grafana plugin config %v" , err )
168
163
}
164
+ err = kubebuilderGrafanaEdit ()
169
165
if err != nil {
170
- return fmt .Errorf ("Failed to Decode Grafana Plugin: %s. %v" , grafanaPluginKey , err )
166
+ return err
167
+ }
168
+ err = grafanaConfigMigrate (src , des )
169
+ if err != nil {
170
+ return err
171
171
}
172
- return migrateGrafanaPlugin ()
172
+ return kubebuilderGrafanaEdit ()
173
173
}
174
174
175
175
func getInitArgs (store store.Store ) []string {
@@ -258,7 +258,30 @@ func getWebhookResourceFlags(resource resource.Resource) []string {
258
258
return args
259
259
}
260
260
261
- func migrateGrafanaPlugin () error {
261
+ func copyFile (src , des string ) error {
262
+ // nolint:gosec
263
+ bytesRead , err := os .ReadFile (src )
264
+ if err != nil {
265
+ return fmt .Errorf ("Source file path: %s does not exist. %v" , src , err )
266
+ }
267
+ //Copy all the contents to the desitination file
268
+ // nolint:gosec
269
+ return os .WriteFile (des , bytesRead , 0755 )
270
+ }
271
+
272
+ func grafanaConfigMigrate (src , des string ) error {
273
+ grafanaConfig := fmt .Sprintf ("%s/%s" , src , "grafana/custom-metrics/config.yaml" )
274
+ if _ , err := os .Stat (grafanaConfig ); os .IsNotExist (err ) {
275
+ return fmt .Errorf ("Grafana Config path: %s does not exist. %v" , grafanaConfig , err )
276
+ }
277
+ return copyFile (grafanaConfig , fmt .Sprintf ("%s/%s" , des , "grafana/custom-metrics/config.yaml" ))
278
+ }
279
+
280
+ func kubebuilderGrafanaEdit () error {
262
281
args := []string {"edit" , "--plugins" , grafanaPluginKey }
263
- return util .RunCmd ("kubebuilder edit" , "kubebuilder" , args ... )
282
+ err := util .RunCmd ("kubebuilder edit" , "kubebuilder" , args ... )
283
+ if err != nil {
284
+ return fmt .Errorf ("Failed to run edit subcommand for Grafana Plugin %v" , err )
285
+ }
286
+ return nil
264
287
}
0 commit comments