Skip to content

Commit 5286eb3

Browse files
John KimJohn Kim
authored andcommitted
verify deploy sub-directory existance and also generate cr.yaml
1 parent ae90297 commit 5286eb3

File tree

3 files changed

+61
-7
lines changed

3 files changed

+61
-7
lines changed

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

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818
"errors"
1919
"fmt"
2020
"os"
21+
"path/filepath"
22+
"strings"
2123

2224
cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error"
2325
"github.com/operator-framework/operator-sdk/pkg/generator"
@@ -29,11 +31,22 @@ var (
2931
kind string
3032
)
3133

34+
const (
35+
goDir = "GOPATH"
36+
deployCrdDir = "deploy"
37+
)
38+
3239
func NewGenerateCrdCmd() *cobra.Command {
3340
crdCmd := &cobra.Command{
3441
Use: "crd",
35-
Short: "Generates a custom resource definition",
36-
Long: `generates a custom resource definition (CRD) file for the .
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.
44+
45+
Generated CRD filename: <project-name>/deploy/<kind>_crd.yaml
46+
Generated CR filename: <project-name>/deploy/<kind>_cr.yaml
47+
48+
<project-name>/deploy path must already exist
49+
--api-version and --kind are required flags to generate the new operator application.
3750
`,
3851
Run: crdFunc,
3952
}
@@ -49,12 +62,14 @@ func crdFunc(cmd *cobra.Command, args []string) {
4962
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("crd command doesn't accept any arguments."))
5063
}
5164
verifyCrdFlags()
65+
verifyCrdDeployPath()
5266

5367
fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file")
5468

55-
// Generate CRD file
56-
if err := generator.RenderDeployCrdFile(apiVersion, kind); err != nil {
57-
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD file: (%v)", err))
69+
// generate CRD file
70+
wd, _ := os.Getwd()
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))
5873
}
5974
}
6075

@@ -65,4 +80,33 @@ func verifyCrdFlags() {
6580
if len(kind) == 0 {
6681
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must not have empty value"))
6782
}
83+
kindFirstLetter := string(kind[0])
84+
if kindFirstLetter != strings.ToUpper(kindFirstLetter) {
85+
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must start with an uppercase letter"))
86+
}
87+
if strings.Count(apiVersion, "/") != 1 {
88+
cmdError.ExitWithError(cmdError.ExitBadArgs, fmt.Errorf("api-version has wrong format (%v); format must be $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)", apiVersion))
89+
}
90+
}
91+
92+
// verifyCrdDeployPath checks if the path <project-name>/deploy sub-directory is exists, and that is rooted under $GOPATH
93+
func verifyCrdDeployPath() {
94+
// check if $GOPATH env exists
95+
gp := os.Getenv(goDir)
96+
if len(gp) == 0 {
97+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("$GOPATH env not set"))
98+
}
99+
wd, err := os.Getwd()
100+
if err != nil {
101+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
102+
}
103+
// check if this project's repository path is rooted under $GOPATH
104+
if !strings.HasPrefix(wd, gp) {
105+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp))
106+
}
107+
// check if the deploy sub-directory exist
108+
_, err = os.Stat(filepath.Join(wd, deployCrdDir))
109+
if err != nil {
110+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir))
111+
}
68112
}

pkg/generator/generator.go

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

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

pkg/generator/generator_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ spec:
232232
labels:
233233
name: app-operator
234234
spec:
235+
serviceAccountName: app-operator
235236
containers:
236237
- name: app-operator
237238
image: quay.io/example-inc/app-operator:0.0.1

0 commit comments

Comments
 (0)