Skip to content

Commit f951b50

Browse files
author
Phillip Wittrock
authored
Merge pull request kubernetes-sigs#42 from pwittrock/master
Minor improvements to example creation and config creation commands
2 parents 999ae5a + 8a85d7c commit f951b50

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

cmd/kubebuilder/create/config/config.go

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,63 @@ package config
1616

1717
import (
1818
"fmt"
19-
"github.com/spf13/cobra"
2019
"log"
2120
"path/filepath"
21+
22+
"github.com/spf13/cobra"
2223
)
2324

2425
// configCmd represents the config command
2526
var configCmd = &cobra.Command{
2627
Use: "config",
2728
Short: "Generate installation config for the API",
28-
Long: `Generate installation config for the API. Includes:
29-
- Namespace
30-
- RBAC ClusterRole
31-
- RBAC ClusterRoleBinding
32-
- CRDs
33-
- Controller Deployment
29+
Long: `Generate installation config for the API.
30+
31+
May create config for just CRDs or for controller-manager and CRDs.
32+
`,
33+
Example: `# Generate config to install controller-manager and CRDs into a cluster
34+
kubebuilder create config --controller-image myimage:v1 --name myextensionname
35+
36+
# Generate config to install only CRDs
37+
kubebuilder create config --crds
38+
39+
# Generate config to install controller-manager and CRDs using a Deployment
40+
kubebuilder create config --controller-image myimage:v1 --name myextensionname --controller-type deployment
41+
42+
# Generate config file at a specific location
43+
kubebuilder create config --crds --output myextensionname.yaml
44+
3445
`,
3546
Run: func(cmd *cobra.Command, args []string) {
3647
if controllerType != "statefulset" && controllerType != "deployment" {
3748
fmt.Printf(
38-
"Invalid value %s for --controller-type, must be statefulset or deployment\n", controllerType)
49+
"Invalid value %s for --controller-type, must be statefulset or deployment.\n", controllerType)
3950
return
4051
}
41-
if controllerImage == "" {
42-
fmt.Printf("Must specify --controller-image\n")
52+
if controllerImage == "" && !crds {
53+
fmt.Printf("Must either specify --controller-image or set --crds.\n")
4354
return
4455
}
45-
if name == "" {
46-
fmt.Printf("Must specify --name\n")
56+
if name == "" && !crds {
57+
fmt.Printf("Must either specify the name of the extension with --name or set --crds.\n")
4758
return
4859
}
4960
CodeGenerator{}.Execute()
50-
log.Printf("Config written to hack/install.yaml")
61+
log.Printf("Config written to %s", output)
5162
},
5263
}
5364

5465
var (
55-
controllerType, controllerImage, name, output string
66+
controllerType, controllerImage, name, output, crdNamespace string
67+
crds bool
5668
)
5769

5870
func AddCreateConfig(cmd *cobra.Command) {
5971
cmd.AddCommand(configCmd)
6072
configCmd.Flags().StringVar(&controllerType, "controller-type", "statefulset", "either statefulset or deployment.")
73+
configCmd.Flags().BoolVar(&crds, "crds", false, "if set to true, only generate crd definitions")
74+
configCmd.Flags().StringVar(&crdNamespace, "crd-namespace", "", "if set, install CRDs to this namespace.")
6175
configCmd.Flags().StringVar(&controllerImage, "controller-image", "", "name of the controller container to run.")
62-
configCmd.Flags().StringVar(&name, "name", "", "name of the installation.")
76+
configCmd.Flags().StringVar(&name, "name", "", "name of the installation. used to generate the namespace and resource names.")
6377
configCmd.Flags().StringVar(&output, "output", filepath.Join("hack", "install.yaml"), "location to write yaml to")
6478
}

cmd/kubebuilder/create/config/gen.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ func (g CodeGenerator) Execute() error {
6666
}
6767

6868
p := parse.NewAPIs(c, arguments)
69+
if crds {
70+
util.WriteString(output, strings.Join(getCrds(p), "---\n"))
71+
return nil
72+
}
73+
6974
result := append([]string{},
7075
getNamespace(p),
7176
getClusterRole(p),
@@ -274,6 +279,9 @@ func getCrds(p *parse.APIs) []string {
274279
for _, v := range g.Versions {
275280
for _, r := range v.Resources {
276281
crd := r.CRD
282+
if len(crdNamespace) > 0 {
283+
crd.Namespace = crdNamespace
284+
}
277285
crd.Labels = addLabels(map[string]string{})
278286
s, err := yaml.Marshal(crd)
279287
if err != nil {

cmd/kubebuilder/create/example/example.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"os"
2323
"path/filepath"
2424

25+
"strings"
26+
2527
"github.com/kubernetes-sigs/kubebuilder/cmd/kubebuilder/util"
2628
"github.com/spf13/cobra"
27-
"strings"
2829
)
2930

3031
// configCmd represents the config command
@@ -34,14 +35,17 @@ var configCmd = &cobra.Command{
3435
Long: `Create the docs example scaffoling for an API.
3536
3637
Example is written to docs/reference/examples/<lower kind>/<lower kind>.yaml
38+
`,
39+
Example: `# Create a new documentation example under docs/reference/examples/mykind/mykind.yaml
40+
kubebuilder create example --kind MyKind --version v1beta1 --group mygroup.my.domain
3741
`,
3842
Run: func(cmd *cobra.Command, args []string) {
3943
if kind == "" {
4044
fmt.Printf("Must specify --kind\n")
4145
return
4246
}
4347
if version == "" {
44-
fmt.Printf("Must specify --name\n")
48+
fmt.Printf("Must specify --version\n")
4549
return
4650
}
4751
if group == "" {

0 commit comments

Comments
 (0)