Skip to content

Commit 8a85d7c

Browse files
committed
Support creating config only for CRDs and for adding namespaces to CRDs
1 parent 5126718 commit 8a85d7c

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
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 {

0 commit comments

Comments
 (0)