Skip to content

Commit bddb6f5

Browse files
authored
Merge pull request #3544 from yyy1000/deploy-image
✨ Add support to re-scaffolds projects with alpha generate subcommand to deployImage plugin
2 parents 98194e3 + 8f1b1f5 commit bddb6f5

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
219219
}
220220

221221
// Track the resources following a declarative approach
222-
cfg := pluginConfig{}
222+
cfg := PluginConfig{}
223223
if err := p.config.DecodePluginConfig(pluginKey, &cfg); errors.As(err, &config.UnsupportedFieldError{}) {
224224
// Config doesn't support per-plugin configuration, so we can't track them
225225
} else {
@@ -233,7 +233,7 @@ func (p *createAPISubcommand) Scaffold(fs machinery.Filesystem) error {
233233
ContainerPort: p.imageContainerPort,
234234
RunAsUser: p.runAsUser,
235235
}
236-
cfg.Resources = append(cfg.Resources, resourceData{
236+
cfg.Resources = append(cfg.Resources, ResourceData{
237237
Group: p.resource.GVK.Group,
238238
Domain: p.resource.GVK.Domain,
239239
Version: p.resource.GVK.Version,

pkg/plugins/golang/deploy-image/v1alpha1/plugin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ func (Plugin) SupportedProjectVersions() []config.Version { return supportedProj
5151
// GetCreateAPISubcommand will return the subcommand which is responsible for scaffolding apis
5252
func (p Plugin) GetCreateAPISubcommand() plugin.CreateAPISubcommand { return &p.createAPISubcommand }
5353

54-
type pluginConfig struct {
55-
Resources []resourceData `json:"resources,omitempty"`
54+
type PluginConfig struct {
55+
Resources []ResourceData `json:"resources,omitempty"`
5656
}
5757

58-
type resourceData struct {
58+
type ResourceData struct {
5959
Group string `json:"group,omitempty"`
6060
Domain string `json:"domain,omitempty"`
6161
Version string `json:"version"`

pkg/rescaffold/migrate.go

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
2929
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
3030
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
31+
"sigs.k8s.io/kubebuilder/v3/pkg/plugins/golang/deploy-image/v1alpha1"
3132
)
3233

3334
type MigrateOptions struct {
@@ -66,7 +67,10 @@ func (opts *MigrateOptions) Rescaffold() error {
6667
}
6768
// plugin specific migration
6869
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)
7074
}
7175
return nil
7276
}
@@ -172,6 +176,35 @@ func migrateGrafanaPlugin(store store.Store, src, des string) error {
172176
return kubebuilderGrafanaEdit()
173177
}
174178

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+
175208
func getInitArgs(store store.Store) []string {
176209
var args []string
177210
plugins := store.Config().GetPluginChain()
@@ -203,6 +236,38 @@ func getGVKFlags(resource resource.Resource) []string {
203236
return args
204237
}
205238

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+
206271
func createAPI(resource resource.Resource) error {
207272
var args []string
208273
args = append(args, "create")

test/e2e/alphagenerate/generate_test.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,19 @@ func ReGenerateProject(kbc *utils.TestContext) {
123123
)
124124
ExpectWithOffset(1, err).NotTo(HaveOccurred())
125125

126+
By("create APIs with deploy-image plugin")
127+
err = kbc.CreateAPI(
128+
"--group", "crew",
129+
"--version", "v1",
130+
"--kind", "Memcached",
131+
"--image=memcached:1.6.15-alpine",
132+
"--image-container-command=memcached,-m=64,modern,-v",
133+
"--image-container-port=11211",
134+
"--run-as-user=1001",
135+
"--plugins=\"deploy-image/v1-alpha\"",
136+
)
137+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
138+
126139
By("Enable grafana plugin to an existing project")
127140
err = kbc.Edit(
128141
"--plugins", "grafana.kubebuilder.io/v1-alpha",
@@ -197,7 +210,24 @@ func ReGenerateProject(kbc *utils.TestContext) {
197210
ExpectWithOffset(1, err).NotTo(HaveOccurred())
198211
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
199212

200-
By("checking if the project file was generated with the expected controller")
213+
By("checking if the project file was generated with the expected deploy-image plugin fields")
214+
var deployImagePlugin = "deploy-image.go.kubebuilder.io/v1-alpha"
215+
fileContainsExpr, err = pluginutil.HasFileContentWith(
216+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), deployImagePlugin)
217+
Expect(err).NotTo(HaveOccurred())
218+
Expect(fileContainsExpr).To(BeTrue())
219+
var deployImagePluginFields = `kind: Memcached
220+
options:
221+
containerCommand: memcached,-m=64,modern,-v
222+
containerPort: "11211"
223+
image: memcached:1.6.15-alpine
224+
runAsUser: "1001"`
225+
fileContainsExpr, err = pluginutil.HasFileContentWith(
226+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), deployImagePluginFields)
227+
Expect(err).NotTo(HaveOccurred())
228+
Expect(fileContainsExpr).To(BeTrue())
229+
230+
By("checking if the project file was generated with the expected grafana plugin fields")
201231
var grafanaPlugin = "grafana.kubebuilder.io/v1-alpha"
202232
fileContainsExpr, err = pluginutil.HasFileContentWith(
203233
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), grafanaPlugin)

0 commit comments

Comments
 (0)