@@ -28,6 +28,7 @@ import (
28
28
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
29
29
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
30
30
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
31
+ "sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/deploy-image/v1alpha1"
31
32
)
32
33
33
34
type MigrateOptions struct {
@@ -66,7 +67,10 @@ func (opts *MigrateOptions) Rescaffold() error {
66
67
}
67
68
// plugin specific migration
68
69
if err := migrateGrafanaPlugin (config , opts .InputDir , opts .OutputDir ); err != nil {
69
- log .Fatalf ("Failed to run plugin migration %v" , err )
70
+ log .Fatalf ("Failed to run grafana plugin migration %v" , err )
71
+ }
72
+ if err := migrateDeployImagePlugin (config ); err != nil {
73
+ log .Fatalf ("Failed to run deploy-image plugin migration %v" , err )
70
74
}
71
75
return nil
72
76
}
@@ -172,6 +176,35 @@ func migrateGrafanaPlugin(store store.Store, src, des string) error {
172
176
return kubebuilderGrafanaEdit ()
173
177
}
174
178
179
+ func migrateDeployImagePlugin (store store.Store ) error {
180
+ deployImagePlugin := v1alpha1.PluginConfig {}
181
+ err := store .Config ().DecodePluginConfig ("deploy-image.go.kubebuilder.io/v1-alpha" , & deployImagePlugin )
182
+ // If the deploy-image plugin is not found, we don't need to migrate
183
+ if err != nil {
184
+ if errors .As (err , & config.PluginKeyNotFoundError {}) {
185
+ log .Printf ("deploy-image plugin is not found, skip the migration" )
186
+ return nil
187
+ }
188
+ return fmt .Errorf ("failed to decode deploy-image plugin config %v" , err )
189
+ }
190
+
191
+ for _ , r := range deployImagePlugin .Resources {
192
+ if err = createAPIWithDeployImage (r ); err != nil {
193
+ return err
194
+ }
195
+ }
196
+ return nil
197
+ }
198
+
199
+ func createAPIWithDeployImage (resource v1alpha1.ResourceData ) error {
200
+ var args []string
201
+ args = append (args , "create" )
202
+ args = append (args , "api" )
203
+ args = append (args , getGVKFlagsFromDeployImage (resource )... )
204
+ args = append (args , getDeployImageOptions (resource )... )
205
+ return util .RunCmd ("kubebuilder create api" , "kubebuilder" , args ... )
206
+ }
207
+
175
208
func getInitArgs (store store.Store ) []string {
176
209
var args []string
177
210
plugins := store .Config ().GetPluginChain ()
@@ -203,6 +236,38 @@ func getGVKFlags(resource resource.Resource) []string {
203
236
return args
204
237
}
205
238
239
+ func getGVKFlagsFromDeployImage (resource v1alpha1.ResourceData ) []string {
240
+ var args []string
241
+ if len (resource .Group ) > 0 {
242
+ args = append (args , "--group" , resource .Group )
243
+ }
244
+ if len (resource .Version ) > 0 {
245
+ args = append (args , "--version" , resource .Version )
246
+ }
247
+ if len (resource .Kind ) > 0 {
248
+ args = append (args , "--kind" , resource .Kind )
249
+ }
250
+ return args
251
+ }
252
+
253
+ func getDeployImageOptions (resource v1alpha1.ResourceData ) []string {
254
+ var args []string
255
+ if len (resource .Options .Image ) > 0 {
256
+ args = append (args , fmt .Sprintf ("--image=%s" , resource .Options .Image ))
257
+ }
258
+ if len (resource .Options .ContainerCommand ) > 0 {
259
+ args = append (args , fmt .Sprintf ("--image-container-command=%s" , resource .Options .ContainerCommand ))
260
+ }
261
+ if len (resource .Options .ContainerPort ) > 0 {
262
+ args = append (args , fmt .Sprintf ("--image-container-port=%s" , resource .Options .ContainerPort ))
263
+ }
264
+ if len (resource .Options .RunAsUser ) > 0 {
265
+ args = append (args , fmt .Sprintf ("--run-as-user=%s" , resource .Options .RunAsUser ))
266
+ }
267
+ args = append (args , fmt .Sprintf ("--plugins=\" %s\" " , "deploy-image/v1-alpha" ))
268
+ return args
269
+ }
270
+
206
271
func createAPI (resource resource.Resource ) error {
207
272
var args []string
208
273
args = append (args , "create" )
0 commit comments