Skip to content

Commit fc50728

Browse files
authored
Merge pull request #3489 from yyy1000/create-api
✨ Add API fields in alpha generate subcommand
2 parents c2fbb3e + 38b2e39 commit fc50728

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

pkg/rescaffold/migrate.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sigs.k8s.io/kubebuilder/v3/pkg/config/store"
2424
"sigs.k8s.io/kubebuilder/v3/pkg/config/store/yaml"
2525
"sigs.k8s.io/kubebuilder/v3/pkg/machinery"
26+
"sigs.k8s.io/kubebuilder/v3/pkg/model/resource"
2627
"sigs.k8s.io/kubebuilder/v3/pkg/plugin/util"
2728
)
2829

@@ -55,6 +56,10 @@ func (opts *MigrateOptions) Rescaffold() error {
5556
if err := kubebuilderEdit(config); err != nil {
5657
log.Fatalf("Failed to run edit subcommand %v", err)
5758
}
59+
// create APIs
60+
if err := kubebuilderCreate(config); err != nil {
61+
log.Fatalf("Failed to run create API subcommand %v", err)
62+
}
5863
return nil
5964
}
6065

@@ -119,6 +124,21 @@ func kubebuilderEdit(store store.Store) error {
119124
return nil
120125
}
121126

127+
func kubebuilderCreate(store store.Store) error {
128+
resources, err := store.Config().GetResources()
129+
if err != nil {
130+
return err
131+
}
132+
133+
for _, r := range resources {
134+
if err = createAPI(r); err != nil {
135+
return err
136+
}
137+
}
138+
139+
return nil
140+
}
141+
122142
func getInitArgs(store store.Store) []string {
123143
var args []string
124144
plugins := store.Config().GetPluginChain()
@@ -132,3 +152,47 @@ func getInitArgs(store store.Store) []string {
132152
}
133153
return args
134154
}
155+
156+
func createAPI(resource resource.Resource) error {
157+
var args []string
158+
args = append(args, "create")
159+
args = append(args, "api")
160+
args = append(args, getAPIGVKFlags(resource)...)
161+
args = append(args, getAPIResourceFlags(resource)...)
162+
return util.RunCmd("kubebuilder create api", "kubebuilder", args...)
163+
}
164+
165+
func getAPIGVKFlags(resource resource.Resource) []string {
166+
var args []string
167+
168+
if len(resource.Group) > 0 {
169+
args = append(args, "--group", resource.Group)
170+
}
171+
if len(resource.Version) > 0 {
172+
args = append(args, "--version", resource.Version)
173+
}
174+
if len(resource.Kind) > 0 {
175+
args = append(args, "--kind", resource.Kind)
176+
}
177+
return args
178+
}
179+
180+
func getAPIResourceFlags(resource resource.Resource) []string {
181+
var args []string
182+
if resource.API == nil || resource.API.IsEmpty() {
183+
// create API without creating resource
184+
args = append(args, "--resource=false")
185+
} else {
186+
args = append(args, "--resource")
187+
if resource.API.Namespaced {
188+
args = append(args, "--namespaced")
189+
}
190+
}
191+
192+
if resource.Controller {
193+
args = append(args, "--controller")
194+
} else {
195+
args = append(args, "--controller=false")
196+
}
197+
return args
198+
}

test/e2e/alphagenerate/generate_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ func ReGenerateProject(kbc *utils.TestContext) {
100100
)
101101
ExpectWithOffset(1, err).NotTo(HaveOccurred())
102102

103+
By("create APIs with resource and controller")
104+
err = kbc.CreateAPI(
105+
"--group", "crew",
106+
"--version", "v1",
107+
"--kind", "Captain",
108+
"--namespaced",
109+
"--resource",
110+
"--controller",
111+
)
112+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
113+
103114
By("regenerating the project at another output directory")
104115
err = kbc.Regenerate(
105116
"--input-dir", kbc.Dir,
@@ -113,4 +124,39 @@ func ReGenerateProject(kbc *utils.TestContext) {
113124
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), multiGroup)
114125
ExpectWithOffset(1, err).NotTo(HaveOccurred())
115126
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
127+
128+
By("checking if the project file was generated with the expected group")
129+
var APIGroup = "group: crew"
130+
fileContainsExpr, err = pluginutil.HasFileContentWith(
131+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIGroup)
132+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
133+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
134+
135+
By("checking if the project file was generated with the expected kind")
136+
var APIKind = "kind: Captain"
137+
fileContainsExpr, err = pluginutil.HasFileContentWith(
138+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIKind)
139+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
140+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
141+
142+
By("checking if the project file was generated with the expected version")
143+
var APIVersion = "version: v1"
144+
fileContainsExpr, err = pluginutil.HasFileContentWith(
145+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), APIVersion)
146+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
147+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
148+
149+
By("checking if the project file was generated with the expected namespaced")
150+
var namespaced = "namespaced: true"
151+
fileContainsExpr, err = pluginutil.HasFileContentWith(
152+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), namespaced)
153+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
154+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
155+
156+
By("checking if the project file was generated with the expected controller")
157+
var controller = "controller: true"
158+
fileContainsExpr, err = pluginutil.HasFileContentWith(
159+
filepath.Join(kbc.Dir, "testdir2", "PROJECT"), controller)
160+
ExpectWithOffset(1, err).NotTo(HaveOccurred())
161+
ExpectWithOffset(1, fileContainsExpr).To(BeTrue())
116162
}

0 commit comments

Comments
 (0)