@@ -14,12 +14,17 @@ limitations under the License.
14
14
package rescaffold
15
15
16
16
import (
17
+ "errors"
17
18
"fmt"
18
19
"log"
19
20
"os"
20
21
"os/exec"
22
+ "strings"
23
+
24
+ "golang.org/x/exp/slices"
21
25
22
26
"github.com/spf13/afero"
27
+ "sigs.k8s.io/kubebuilder/v3/pkg/config"
23
28
"sigs.k8s.io/kubebuilder/v3/pkg/config/store"
24
29
"sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
25
30
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
@@ -33,6 +38,7 @@ type MigrateOptions struct {
33
38
}
34
39
35
40
const DefaultOutputDir = "output-dir"
41
+ const grafanaPluginKey = "grafana.kubebuilder.io/v1-alpha"
36
42
37
43
func (opts * MigrateOptions ) Rescaffold () error {
38
44
config := yaml .New (machinery.Filesystem {FS : afero .NewOsFs ()})
@@ -56,10 +62,14 @@ func (opts *MigrateOptions) Rescaffold() error {
56
62
if err := kubebuilderEdit (config ); err != nil {
57
63
log .Fatalf ("Failed to run edit subcommand %v" , err )
58
64
}
59
- // create APIs
65
+ // create APIs and Webhooks
60
66
if err := kubebuilderCreate (config ); err != nil {
61
67
log .Fatalf ("Failed to run create API subcommand %v" , err )
62
68
}
69
+ // plugin specific migration
70
+ if err := kubebuilderGrafanaPlugin (config ); err != nil {
71
+ log .Fatalf ("Failed to run plugin migration %v" , err )
72
+ }
63
73
return nil
64
74
}
65
75
@@ -142,12 +152,31 @@ func kubebuilderCreate(store store.Store) error {
142
152
return nil
143
153
}
144
154
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
+
145
175
func getInitArgs (store store.Store ) []string {
146
176
var args []string
147
177
plugins := store .Config ().GetPluginChain ()
148
178
if len (plugins ) > 0 {
149
- args = append (args , "--plugins" )
150
- args = append (args , plugins ... )
179
+ args = append (args , "--plugins" , strings .Join (plugins , "," ))
151
180
}
152
181
domain := store .Config ().GetDomain ()
153
182
if domain != "" {
@@ -228,3 +257,8 @@ func getWebhookResourceFlags(resource resource.Resource) []string {
228
257
}
229
258
return args
230
259
}
260
+
261
+ func migrateGrafanaPlugin () error {
262
+ args := []string {"edit" , "--plugins" , grafanaPluginKey }
263
+ return util .RunCmd ("kubebuilder edit" , "kubebuilder" , args ... )
264
+ }
0 commit comments