Skip to content

Commit e9d0da3

Browse files
John KimJohn Kim
authored andcommitted
verify deploy sub-directory existance
1 parent d3d0bed commit e9d0da3

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

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

Lines changed: 46 additions & 3 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,21 @@ 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",
3542
Short: "Generates a custom resource definition",
36-
Long: `generates a custom resource definition (CRD) file for the .
43+
Long: `The operator-sdk generate command will create a custom resource definition (CRD) file for the specified api-version and kind.
44+
45+
Generated CRD filename: <project-name>/deploy/<kind>_crd.yaml
46+
47+
<project-name>/deploy path must already exist
48+
--api-version and --kind are required flags to generate the new operator application.
3749
`,
3850
Run: crdFunc,
3951
}
@@ -49,11 +61,13 @@ func crdFunc(cmd *cobra.Command, args []string) {
4961
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("crd command doesn't accept any arguments."))
5062
}
5163
verifyCrdFlags()
64+
verifyCrdDeployPath()
5265

5366
fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file")
5467

55-
// Generate CRD file
56-
if err := generator.RenderDeployCrdFile(apiVersion, kind); err != nil {
68+
// generate CRD file
69+
wd, _ := os.Getwd()
70+
if err := generator.RenderDeployCrdFile(filepath.Join(wd, deployCrdDir), apiVersion, kind); err != nil {
5771
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD file: (%v)", err))
5872
}
5973
}
@@ -65,4 +79,33 @@ func verifyCrdFlags() {
6579
if len(kind) == 0 {
6680
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must not have empty value"))
6781
}
82+
kindFirstLetter := string(kind[0])
83+
if kindFirstLetter != strings.ToUpper(kindFirstLetter) {
84+
cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must start with an uppercase letter"))
85+
}
86+
if strings.Count(apiVersion, "/") != 1 {
87+
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))
88+
}
89+
}
90+
91+
// verifyCrdDeployPath checks if the path <project-name>/deploy sub-directory is exists, and that is rooted under $GOPATH
92+
func verifyCrdDeployPath() {
93+
// check if $GOPATH env exists
94+
gp := os.Getenv(goDir)
95+
if len(gp) == 0 {
96+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("$GOPATH env not set"))
97+
}
98+
wd, err := os.Getwd()
99+
if err != nil {
100+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to determine the full path of the current directory: %v", err))
101+
}
102+
// check if this project's repository path is rooted under $GOPATH
103+
if !strings.HasPrefix(wd, gp) {
104+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("project's repository path (%v) is not rooted under GOPATH (%v)", wd, gp))
105+
}
106+
// check if the deploy sub-directory exist
107+
_, err = os.Stat(filepath.Join(wd, deployCrdDir))
108+
if err != nil {
109+
cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir))
110+
}
68111
}

pkg/generator/generator.go

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

336-
func RenderDeployCrdFile(apiVersion, kind string) error {
336+
func RenderDeployCrdFile(crdDeployPath, apiVersion, kind string) error {
337337
crdTd := tmplData{
338338
Kind: kind,
339339
KindSingular: strings.ToLower(kind),
340340
KindPlural: toPlural(strings.ToLower(kind)),
341341
GroupName: groupName(apiVersion),
342342
Version: version(apiVersion),
343343
}
344-
crdFilePath := filepath.Join(deployDir, strings.ToLower(kind)+"_crd.yaml")
344+
crdFilePath := filepath.Join(crdDeployPath, strings.ToLower(kind)+"_crd.yaml")
345345
return renderWriteFile(crdFilePath, crdFilePath, crdYamlTmpl, crdTd)
346346
}
347347

0 commit comments

Comments
 (0)