|
| 1 | +// Copyright 2018 The Operator-SDK Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package generate |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "os" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + cmdError "github.com/operator-framework/operator-sdk/commands/operator-sdk/error" |
| 25 | + "github.com/operator-framework/operator-sdk/pkg/generator" |
| 26 | + |
| 27 | + "github.com/spf13/cobra" |
| 28 | +) |
| 29 | + |
| 30 | +var ( |
| 31 | + apiVersion string |
| 32 | + kind string |
| 33 | +) |
| 34 | + |
| 35 | +const ( |
| 36 | + goDir = "GOPATH" |
| 37 | + deployCrdDir = "deploy" |
| 38 | +) |
| 39 | + |
| 40 | +func NewGenerateCrdCmd() *cobra.Command { |
| 41 | + crdCmd := &cobra.Command{ |
| 42 | + Use: "crd", |
| 43 | + Short: "Generates a custom resource definition (CRD) and the custom resource (CR) files", |
| 44 | + 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. |
| 45 | +
|
| 46 | +Generated CRD filename: <project-name>/deploy/<group>_<version>_<kind>_crd.yaml |
| 47 | +Generated CR filename: <project-name>/deploy/<group>_<version>_<kind>_cr.yaml |
| 48 | +
|
| 49 | + <project-name>/deploy path must already exist |
| 50 | + --api-version and --kind are required flags to generate the new operator application. |
| 51 | +`, |
| 52 | + Run: crdFunc, |
| 53 | + } |
| 54 | + crdCmd.Flags().StringVar(&apiVersion, "api-version", "", "Kubernetes apiVersion and has a format of $GROUP_NAME/$VERSION (e.g app.example.com/v1alpha1)") |
| 55 | + crdCmd.MarkFlagRequired("api-version") |
| 56 | + crdCmd.Flags().StringVar(&kind, "kind", "", "Kubernetes CustomResourceDefintion kind. (e.g AppService)") |
| 57 | + crdCmd.MarkFlagRequired("kind") |
| 58 | + return crdCmd |
| 59 | +} |
| 60 | + |
| 61 | +func crdFunc(cmd *cobra.Command, args []string) { |
| 62 | + if len(args) != 0 { |
| 63 | + cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("crd command doesn't accept any arguments.")) |
| 64 | + } |
| 65 | + verifyCrdFlags() |
| 66 | + verifyCrdDeployPath() |
| 67 | + |
| 68 | + fmt.Fprintln(os.Stdout, "Generating custom resource definition (CRD) file") |
| 69 | + |
| 70 | + // generate CR/CRD file |
| 71 | + wd, err := os.Getwd() |
| 72 | + if err != nil { |
| 73 | + cmdError.ExitWithError(cmdError.ExitError, err) |
| 74 | + } |
| 75 | + if err := generator.RenderDeployCrdFiles(filepath.Join(wd, deployCrdDir), apiVersion, kind); err != nil { |
| 76 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("failed to generate CRD and CR files: (%v)", err)) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func verifyCrdFlags() { |
| 81 | + if len(apiVersion) == 0 { |
| 82 | + cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--api-version must not have empty value")) |
| 83 | + } |
| 84 | + if len(kind) == 0 { |
| 85 | + cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must not have empty value")) |
| 86 | + } |
| 87 | + kindFirstLetter := string(kind[0]) |
| 88 | + if kindFirstLetter != strings.ToUpper(kindFirstLetter) { |
| 89 | + cmdError.ExitWithError(cmdError.ExitBadArgs, errors.New("--kind must start with an uppercase letter")) |
| 90 | + } |
| 91 | + if strings.Count(apiVersion, "/") != 1 { |
| 92 | + 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)) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// verifyCrdDeployPath checks if the path <project-name>/deploy sub-directory is exists, and that is rooted under $GOPATH |
| 97 | +func verifyCrdDeployPath() { |
| 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 the deploy sub-directory exist |
| 103 | + _, err = os.Stat(filepath.Join(wd, deployCrdDir)) |
| 104 | + if err != nil { |
| 105 | + cmdError.ExitWithError(cmdError.ExitError, fmt.Errorf("the path (./%v) does not exist. run this command in your project directory", deployCrdDir)) |
| 106 | + } |
| 107 | +} |
0 commit comments