Skip to content

Commit 41ce6d7

Browse files
John KimJohn Kim
authored andcommitted
generate cr.yaml file as well
1 parent e9d0da3 commit 41ce6d7

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

commands/operator-sdk/cmd/generate/crd.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ const (
3939
func NewGenerateCrdCmd() *cobra.Command {
4040
crdCmd := &cobra.Command{
4141
Use: "crd",
42-
Short: "Generates a custom resource definition",
43-
Long: `The operator-sdk generate command will create a custom resource definition (CRD) file for the specified api-version and kind.
42+
Short: "Generates a custom resource definition (CRD) and the custom resource (CR) files",
43+
Long: `The operator-sdk generate command will create a custom resource definition (CRD) and the custom resource (CR) files for the specified api-version and kind.
4444
4545
Generated CRD filename: <project-name>/deploy/<kind>_crd.yaml
46+
Generated CR filename: <project-name>/deploy/<kind>_cr.yaml
4647
4748
<project-name>/deploy path must already exist
4849
--api-version and --kind are required flags to generate the new operator application.
@@ -67,8 +68,8 @@ func crdFunc(cmd *cobra.Command, args []string) {
6768

6869
// generate CRD file
6970
wd, _ := os.Getwd()
70-
if err := generator.RenderDeployCrdFile(filepath.Join(wd, deployCrdDir), apiVersion, kind); err != nil {
71-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD file: (%v)", err))
71+
if err := generator.RenderDeployCrdFiles(filepath.Join(wd, deployCrdDir), apiVersion, kind); err != nil {
72+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD and CR files: (%v)", err))
7273
}
7374
}
7475

pkg/generator/generator.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,24 @@ func renderRBAC(deployDir, projectName, groupName string) error {
333333
return renderWriteFile(filepath.Join(deployDir, rbacYaml), rbacTmplName, rbacYamlTmpl, td)
334334
}
335335

336-
func RenderDeployCrdFile(crdDeployPath, apiVersion, kind string) error {
336+
func RenderDeployCrdFiles(deployPath, apiVersion, kind string) error {
337+
crTd := tmplData{
338+
APIVersion: apiVersion,
339+
Kind: kind,
340+
}
341+
crFilePath := filepath.Join(deployPath, strings.ToLower(kind)+"_cr.yaml")
342+
if err := renderWriteFile(crFilePath, crFilePath, crYamlTmpl, crTd); err != nil {
343+
return err
344+
}
345+
337346
crdTd := tmplData{
338347
Kind: kind,
339348
KindSingular: strings.ToLower(kind),
340349
KindPlural: toPlural(strings.ToLower(kind)),
341350
GroupName: groupName(apiVersion),
342351
Version: version(apiVersion),
343352
}
344-
crdFilePath := filepath.Join(crdDeployPath, strings.ToLower(kind)+"_crd.yaml")
353+
crdFilePath := filepath.Join(deployPath, strings.ToLower(kind)+"_crd.yaml")
345354
return renderWriteFile(crdFilePath, crdFilePath, crdYamlTmpl, crdTd)
346355
}
347356

0 commit comments

Comments
 (0)