Skip to content

Commit

Permalink
Support creating config only for CRDs and for adding namespaces to CRDs
Browse files Browse the repository at this point in the history
  • Loading branch information
pwittrock committed Apr 2, 2018
1 parent 5126718 commit 8a85d7c
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 15 deletions.
44 changes: 29 additions & 15 deletions cmd/kubebuilder/create/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,49 +16,63 @@ package config

import (
"fmt"
"github.com/spf13/cobra"
"log"
"path/filepath"

"github.com/spf13/cobra"
)

// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Generate installation config for the API",
Long: `Generate installation config for the API. Includes:
- Namespace
- RBAC ClusterRole
- RBAC ClusterRoleBinding
- CRDs
- Controller Deployment
Long: `Generate installation config for the API.
May create config for just CRDs or for controller-manager and CRDs.
`,
Example: `# Generate config to install controller-manager and CRDs into a cluster
kubebuilder create config --controller-image myimage:v1 --name myextensionname
# Generate config to install only CRDs
kubebuilder create config --crds
# Generate config to install controller-manager and CRDs using a Deployment
kubebuilder create config --controller-image myimage:v1 --name myextensionname --controller-type deployment
# Generate config file at a specific location
kubebuilder create config --crds --output myextensionname.yaml
`,
Run: func(cmd *cobra.Command, args []string) {
if controllerType != "statefulset" && controllerType != "deployment" {
fmt.Printf(
"Invalid value %s for --controller-type, must be statefulset or deployment\n", controllerType)
"Invalid value %s for --controller-type, must be statefulset or deployment.\n", controllerType)
return
}
if controllerImage == "" {
fmt.Printf("Must specify --controller-image\n")
if controllerImage == "" && !crds {
fmt.Printf("Must either specify --controller-image or set --crds.\n")
return
}
if name == "" {
fmt.Printf("Must specify --name\n")
if name == "" && !crds {
fmt.Printf("Must either specify the name of the extension with --name or set --crds.\n")
return
}
CodeGenerator{}.Execute()
log.Printf("Config written to hack/install.yaml")
log.Printf("Config written to %s", output)
},
}

var (
controllerType, controllerImage, name, output string
controllerType, controllerImage, name, output, crdNamespace string
crds bool
)

func AddCreateConfig(cmd *cobra.Command) {
cmd.AddCommand(configCmd)
configCmd.Flags().StringVar(&controllerType, "controller-type", "statefulset", "either statefulset or deployment.")
configCmd.Flags().BoolVar(&crds, "crds", false, "if set to true, only generate crd definitions")
configCmd.Flags().StringVar(&crdNamespace, "crd-namespace", "", "if set, install CRDs to this namespace.")
configCmd.Flags().StringVar(&controllerImage, "controller-image", "", "name of the controller container to run.")
configCmd.Flags().StringVar(&name, "name", "", "name of the installation.")
configCmd.Flags().StringVar(&name, "name", "", "name of the installation. used to generate the namespace and resource names.")
configCmd.Flags().StringVar(&output, "output", filepath.Join("hack", "install.yaml"), "location to write yaml to")
}
8 changes: 8 additions & 0 deletions cmd/kubebuilder/create/config/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (g CodeGenerator) Execute() error {
}

p := parse.NewAPIs(c, arguments)
if crds {
util.WriteString(output, strings.Join(getCrds(p), "---\n"))
return nil
}

result := append([]string{},
getNamespace(p),
getClusterRole(p),
Expand Down Expand Up @@ -274,6 +279,9 @@ func getCrds(p *parse.APIs) []string {
for _, v := range g.Versions {
for _, r := range v.Resources {
crd := r.CRD
if len(crdNamespace) > 0 {
crd.Namespace = crdNamespace
}
crd.Labels = addLabels(map[string]string{})
s, err := yaml.Marshal(crd)
if err != nil {
Expand Down

0 comments on commit 8a85d7c

Please sign in to comment.