Skip to content

Minor improvements to example creation and config creation commands #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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