Skip to content

Commit

Permalink
Merge pull request #42 from pwittrock/master
Browse files Browse the repository at this point in the history
Minor improvements to example creation and config creation commands
  • Loading branch information
Phillip Wittrock authored Apr 2, 2018
2 parents 999ae5a + 8a85d7c commit f951b50
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 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
8 changes: 6 additions & 2 deletions cmd/kubebuilder/create/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import (
"os"
"path/filepath"

"strings"

"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
"github.com/spf13/cobra"
"strings"
)

// configCmd represents the config command
Expand All @@ -34,14 +35,17 @@ var configCmd = &cobra.Command{
Long: `Create the docs example scaffoling for an API.
Example is written to docs/reference/examples/<lower kind>/<lower kind>.yaml
`,
Example: `# Create a new documentation example under docs/reference/examples/mykind/mykind.yaml
kubebuilder create example --kind MyKind --version v1beta1 --group mygroup.my.domain
`,
Run: func(cmd *cobra.Command, args []string) {
if kind == "" {
fmt.Printf("Must specify --kind\n")
return
}
if version == "" {
fmt.Printf("Must specify --name\n")
fmt.Printf("Must specify --version\n")
return
}
if group == "" {
Expand Down

0 comments on commit f951b50

Please sign in to comment.