Skip to content

Commit fe73ff7

Browse files
committed
Add kubebuilder alpha config-gen subcommand
- Add the alpha subcommand - Add config-gen as an alpha subcommand config-gen includes controller-gen as a library, and generates configuration for kubebuilder projects.
1 parent 0675cd5 commit fe73ff7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+5300
-13
lines changed

doc.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
//go:generate go run github.com/markbates/pkger/cmd/pkger
18+
19+
// Package kubebuilder contains pkged files compiled into the
20+
// go binaries.
21+
package kubebuilder

go.mod

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ module sigs.k8s.io/kubebuilder/v3
33
go 1.15
44

55
require (
6+
github.com/cloudflare/cfssl v1.5.0 // for `kubebuilder alpha config-gen`
67
github.com/gobuffalo/flect v0.2.2
8+
// TODO: remove this in favor of embed once using 1.16
9+
github.com/markbates/pkger v0.17.1 // for `kubebuilder alpha config-gen`
710
github.com/onsi/ginkgo v1.15.0
811
github.com/onsi/gomega v1.10.5
912
github.com/spf13/afero v1.2.2
10-
github.com/spf13/cobra v0.0.7
13+
github.com/spf13/cobra v1.1.1
1114
github.com/spf13/pflag v1.0.5
1215
golang.org/x/tools v0.0.0-20201224043029-2b0845dc783e
16+
// for `kubebuilder alpha config-gen`
17+
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
18+
k8s.io/apimachinery v0.20.2 // for `kubebuilder alpha config-gen`
19+
k8s.io/utils v0.0.0-20210111153108-fddb29f9d009 // indirect
20+
sigs.k8s.io/controller-tools v0.3.0 // for `kubebuilder alpha config-gen`
21+
sigs.k8s.io/kustomize/kyaml v0.10.10 // for `kubebuilder alpha config-gen`
1322
sigs.k8s.io/yaml v1.2.0
1423
)

go.sum

+552-9
Large diffs are not rendered by default.

pkg/cli/alpha.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cli
18+
19+
import (
20+
"strings"
21+
22+
"github.com/spf13/cobra"
23+
configgen "sigs.k8s.io/kubebuilder/v3/pkg/cli/alpha/config-gen"
24+
)
25+
26+
var alphaCommands = []*cobra.Command{
27+
configgen.NewCommand(),
28+
}
29+
30+
func (c *CLI) newAlphaCmd() *cobra.Command {
31+
alpha := &cobra.Command{
32+
Use: "alpha",
33+
SuggestFor: []string{"experimental"},
34+
Short: "Alpha kubebuilder subcommands",
35+
Long: strings.TrimSpace(`
36+
Alpha kubebuilder commands are for unstable features.
37+
38+
- Alpha commands are exploratory and may be removed without warning.
39+
- No backwards compatibility is provided for any alpha commands.
40+
`),
41+
}
42+
for i := range alphaCommands {
43+
alpha.AddCommand(alphaCommands[i])
44+
}
45+
return alpha
46+
}
47+
48+
func (c *CLI) addAlphaCmd() {
49+
if len(alphaCommands) > 0 {
50+
c.cmd.AddCommand(c.newAlphaCmd())
51+
}
52+
}

pkg/cli/alpha/config-gen/README.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Config-gen
2+
3+
`kubebuilder alpha config-gen` is a subcommand that generates configuration for kubebuilder projects as a configuration function.
4+
5+
Supports:
6+
7+
- Generating CRDs and RBAC from code
8+
- Generating webhook certificates for development
9+
- Selectively enabling / disabling components such as prometheus and webhooks
10+
- See [types.go](apis/v1alpha1/types.go) for a list of components
11+
12+
## Usage
13+
14+
`config-gen` may be run as a standalone command or from kustomize as a transformer plugin.
15+
16+
### Standalone command
17+
18+
config-gen may be run as a standalone program on the commandline.
19+
20+
See [examples/standalone](examples/standalone/README.md)
21+
22+
### From kustomize
23+
24+
config-gen may be run as a Kustomize plugin using kustomize.
25+
26+
See [examples/kustomize](examples/kustomize/README.md)
27+
28+
### Extending `config-gen`
29+
30+
config-gen may be extended by composing additional functions on top of it.
31+
32+
See examples of layering additional functions on:
33+
34+
- [examples/basicextension](examples/basicextension/README.md)
35+
- [examples/advancedextension](examples/advancedextension/README.md)
36+
37+
## `KubebuilderConfigGen`
38+
39+
See [types.go](apis/v1alpha1/types.go) for KubebuilderConfigGen schema.
40+
41+
See [testdata](apis/v1alpha1/testdata) for examples of configuration options.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package configgen
18+
19+
import (
20+
"encoding/base64"
21+
"fmt"
22+
23+
"github.com/cloudflare/cfssl/cli/genkey"
24+
"github.com/cloudflare/cfssl/config"
25+
"github.com/cloudflare/cfssl/csr"
26+
"github.com/cloudflare/cfssl/helpers"
27+
"github.com/cloudflare/cfssl/selfsign"
28+
"sigs.k8s.io/kustomize/kyaml/fn/framework"
29+
"sigs.k8s.io/kustomize/kyaml/kio"
30+
"sigs.k8s.io/kustomize/kyaml/yaml"
31+
)
32+
33+
var _ kio.Filter = &CertFilter{}
34+
35+
// CertFilter generates and injects certificates into webhook
36+
type CertFilter struct {
37+
*KubebuilderConfigGen
38+
}
39+
40+
// Filter implements kio.Filter
41+
func (c CertFilter) Filter(input []*yaml.RNode) ([]*yaml.RNode, error) {
42+
43+
if c.Spec.Webhooks.CertificateSource.Type != "dev" {
44+
return input, nil
45+
}
46+
if err := c.generateCert(); err != nil {
47+
return nil, err
48+
}
49+
50+
s := &framework.Selector{
51+
Kinds: []string{
52+
"ValidatingWebhookConfiguration",
53+
"MutatingWebhookConfiguration",
54+
},
55+
}
56+
matches, err := s.GetMatches(&framework.ResourceList{Items: input})
57+
if err != nil {
58+
return nil, err
59+
}
60+
for i := range matches {
61+
wh := matches[i].Field("webhooks")
62+
if wh.IsNilOrEmpty() {
63+
continue
64+
}
65+
err := wh.Value.VisitElements(func(node *yaml.RNode) error {
66+
err := node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "clientConfig", "caBundle"),
67+
yaml.FieldSetter{StringValue: c.Status.CertCA})
68+
if err != nil {
69+
return err
70+
}
71+
err = node.PipeE(yaml.LookupCreate(yaml.ScalarNode, "clientConfig", "service", "namespace"),
72+
yaml.FieldSetter{StringValue: c.Namespace})
73+
if err != nil {
74+
return err
75+
}
76+
77+
return nil
78+
})
79+
if err != nil {
80+
return nil, err
81+
}
82+
}
83+
84+
s = &framework.Selector{
85+
Filter: func(n *yaml.RNode) bool {
86+
// Allow-list conversion webhooks
87+
m, _ := n.GetMeta()
88+
if m.Kind != "CustomResourceDefinition" {
89+
return true
90+
}
91+
return c.Spec.Webhooks.Conversions[m.Name]
92+
},
93+
}
94+
matches, err = s.GetMatches(&framework.ResourceList{Items: input})
95+
if err != nil {
96+
return nil, err
97+
}
98+
for i := range matches {
99+
err := matches[i].PipeE(yaml.LookupCreate(yaml.ScalarNode, "spec", "conversion", "strategy"),
100+
yaml.FieldSetter{StringValue: "Webhook"})
101+
if err != nil {
102+
return nil, err
103+
}
104+
err = matches[i].PipeE(yaml.LookupCreate(
105+
yaml.ScalarNode, "spec", "conversion", "webhookClientConfig", "caBundle"),
106+
yaml.FieldSetter{StringValue: c.Status.CertCA})
107+
if err != nil {
108+
return nil, err
109+
}
110+
err = matches[i].PipeE(yaml.LookupCreate(
111+
yaml.ScalarNode, "spec", "conversion", "webhookClientConfig", "service", "name"),
112+
yaml.FieldSetter{StringValue: "webhook-service"})
113+
if err != nil {
114+
return nil, err
115+
}
116+
err = matches[i].PipeE(yaml.LookupCreate(
117+
yaml.ScalarNode, "spec", "conversion", "webhookClientConfig", "service", "namespace"),
118+
yaml.FieldSetter{StringValue: c.Namespace})
119+
if err != nil {
120+
return nil, err
121+
}
122+
123+
err = matches[i].PipeE(yaml.LookupCreate(
124+
yaml.ScalarNode, "spec", "conversion", "webhookClientConfig", "service", "path"),
125+
yaml.FieldSetter{StringValue: "/convert"})
126+
if err != nil {
127+
return nil, err
128+
}
129+
}
130+
131+
return input, nil
132+
}
133+
134+
func (c CertFilter) generateCert() error {
135+
var err error
136+
var req = csr.New()
137+
req.Hosts = []string{
138+
fmt.Sprintf("webhook-service.%s.svc", c.Namespace),
139+
fmt.Sprintf("webhook-service.%s.svc.cluster.local", c.Namespace),
140+
}
141+
req.CN = "kb-dev-controller-manager"
142+
143+
var key, csrPEM []byte
144+
g := &csr.Generator{Validator: genkey.Validator}
145+
csrPEM, key, err = g.ProcessRequest(req)
146+
if err != nil {
147+
return err
148+
}
149+
priv, err := helpers.ParsePrivateKeyPEM(key)
150+
if err != nil {
151+
return err
152+
}
153+
154+
profile := config.DefaultConfig()
155+
profile.Expiry = c.Spec.Webhooks.CertificateSource.DevCertificate.CertDuration
156+
cert, err := selfsign.Sign(priv, csrPEM, profile)
157+
if err != nil {
158+
return err
159+
}
160+
161+
c.Status.CertCA = base64.StdEncoding.EncodeToString(cert)
162+
c.Status.CertKey = base64.StdEncoding.EncodeToString(key)
163+
return nil
164+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package configgen
18+
19+
import (
20+
"github.com/markbates/pkger"
21+
"sigs.k8s.io/kustomize/kyaml/fn/framework"
22+
)
23+
24+
// CertManagerPatchTemplate returns the PatchTemplate for cert-manager
25+
func CertManagerPatchTemplate(_ *KubebuilderConfigGen) framework.PT {
26+
return framework.PT{
27+
// keep casting -- required by pkger to find the directory
28+
Dir: pkger.Dir("/pkg/cli/alpha/config-gen/templates/patches/cert-manager"),
29+
Selector: func() *framework.Selector {
30+
return &framework.Selector{
31+
Kinds: []string{
32+
"CustomResourceDefinition",
33+
"ValidatingWebhookConfiguration",
34+
"MutatingWebhookConfiguration",
35+
},
36+
}
37+
},
38+
}
39+
}

0 commit comments

Comments
 (0)