Skip to content

Commit ad4eef7

Browse files
dashanjiarttor
authored andcommitted
* Add an option for enable/disable webhook for a standard operator generated by kubebuilder.
* Use regular expressions to match the containerPort of the webhook. Signed-off-by: Ye Cao <[email protected]>
1 parent 1e2ec17 commit ad4eef7

File tree

9 files changed

+92
-17
lines changed

9 files changed

+92
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Usage:
112112
| -cert-manager-version | Allows the user to specify cert-manager subchart version. Only useful with cert-manager-as-subchart. (default "v1.12.2") | `helmify -cert-manager-version=v1.12.2` |
113113
| -cert-manager-install-crd | Allows the user to install cert-manager CRD as part of the cert-manager subchart.(default "true") | `helmify -cert-manager-install-crd` |
114114
| -preserve-ns | Allows users to use the object's original namespace instead of adding all the resources to a common namespace. (default "false") | `helmify -preserve-ns` |
115+
| -add-webhook-option | Adds an option to enable/disable webhook installation | `helmify -add-webhook-option`|
115116
## Status
116117
Supported k8s resources:
117118
- Deployment, DaemonSet, StatefulSet

cmd/helmify/flags.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func ReadFlags() config.Config {
7070
flag.BoolVar(&result.OriginalName, "original-name", false, "Use the object's original name instead of adding the chart's release name as the common prefix.")
7171
flag.Var(&files, "f", "File or directory containing k8s manifests")
7272
flag.BoolVar(&preservens, "preserve-ns", false, "Use the object's original namespace instead of adding all the resources to a common namespace")
73+
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml")
74+
7375
flag.Parse()
7476
if h || help {
7577
fmt.Print(helpText)

pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package config
22

33
import (
44
"fmt"
5+
56
"github.com/sirupsen/logrus"
67
"k8s.io/apimachinery/pkg/util/validation"
78
)
@@ -40,6 +41,8 @@ type Config struct {
4041
OriginalName bool
4142
// PreserveNs retains the namespaces on the Kubernetes manifests
4243
PreserveNs bool
44+
// AddWebhookOption enables the generation of a webhook option in values.yamlß
45+
AddWebhookOption bool
4346
}
4447

4548
func (c *Config) Validate() error {

pkg/processor/deployment/deployment.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
129129
if err != nil {
130130
return true, nil, err
131131
}
132+
if appMeta.Config().AddWebhookOption {
133+
spec = addWebhookOption(spec)
134+
}
132135

133136
spec = replaceSingleQuotes(spec)
134137

@@ -159,6 +162,30 @@ func replaceSingleQuotes(s string) string {
159162
return r.ReplaceAllString(s, "${1}")
160163
}
161164

165+
func addWebhookOption(manifest string) string {
166+
webhookOptionHeader := " {{- if .Values.webhook.enabled }}"
167+
webhookOptionFooter := " {{- end }}"
168+
volumes := ` - name: cert
169+
secret:
170+
defaultMode: 420
171+
secretName: webhook-server-cert`
172+
volumeMounts := ` - mountPath: /tmp/k8s-webhook-server/serving-certs
173+
name: cert
174+
readOnly: true`
175+
manifest = strings.ReplaceAll(manifest, volumes, fmt.Sprintf("%s\n%s\n%s",
176+
webhookOptionHeader, volumes, webhookOptionFooter))
177+
manifest = strings.ReplaceAll(manifest, volumeMounts, fmt.Sprintf("%s\n%s\n%s",
178+
webhookOptionHeader, volumeMounts, webhookOptionFooter))
179+
180+
re := regexp.MustCompile(` - containerPort: \d+
181+
name: webhook-server
182+
protocol: TCP`)
183+
184+
manifest = re.ReplaceAllString(manifest, fmt.Sprintf("%s\n%s\n%s", webhookOptionHeader,
185+
re.FindString(manifest), webhookOptionFooter))
186+
return manifest
187+
}
188+
162189
func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
163190
if deployment.Spec.Replicas == nil {
164191
return "", nil

pkg/processor/service/service.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ func (r svc) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
9494
}
9595
ports[i] = pMap
9696
}
97+
9798
_ = unstructured.SetNestedSlice(values, ports, shortNameCamel, "ports")
9899
res := meta + fmt.Sprintf(svcTempSpec, shortNameCamel, selector, appMeta.ChartName())
100+
if shortNameCamel == "webhookService" && appMeta.Config().AddWebhookOption {
101+
res = fmt.Sprintf("{{- if .Values.webhook.enabled }}\n%s\n{{- end }}", res)
102+
}
99103
return true, &result{
100104
name: shortName,
101105
data: res,

pkg/processor/webhook/cert.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import (
1515
)
1616

1717
const (
18-
certTempl = `apiVersion: cert-manager.io/v1
18+
WebhookHeader = `{{- if .Values.webhook.enabled }}`
19+
WebhookFooter = `{{- end }}`
20+
certTempl = `apiVersion: cert-manager.io/v1
1921
kind: Certificate
2022
metadata:
2123
name: {{ include "%[1]s.fullname" . }}-%[2]s
@@ -92,24 +94,33 @@ func (c cert) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructure
9294
} else {
9395
tmpl = certTempl
9496
}
97+
values := helmify.Values{}
98+
if appMeta.Config().AddWebhookOption {
99+
// Add webhook.enabled value to values.yaml
100+
_, _ = values.Add(true, "webhook", "enabled")
101+
102+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
103+
}
95104
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
96105
return true, &certResult{
97-
name: name,
98-
data: []byte(res),
106+
name: name,
107+
data: []byte(res),
108+
values: values,
99109
}, nil
100110
}
101111

102112
type certResult struct {
103-
name string
104-
data []byte
113+
name string
114+
data []byte
115+
values helmify.Values
105116
}
106117

107118
func (r *certResult) Filename() string {
108119
return r.name + ".yaml"
109120
}
110121

111122
func (r *certResult) Values() helmify.Values {
112-
return helmify.Values{}
123+
return r.values
113124
}
114125

115126
func (r *certResult) Write(writer io.Writer) error {

pkg/processor/webhook/issuer.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
5353
return false, nil, nil
5454
}
5555
name := appMeta.TrimName(obj.GetName())
56+
5657
spec, _ := yaml.Marshal(obj.Object["spec"])
5758
spec = yamlformat.Indent(spec, 2)
5859
spec = bytes.TrimRight(spec, "\n ")
@@ -62,6 +63,13 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
6263
} else {
6364
tmpl = issuerTempl
6465
}
66+
values := helmify.Values{}
67+
if appMeta.Config().AddWebhookOption {
68+
// Add webhook.enabled value to values.yaml
69+
_, _ = values.Add(true, "webhook", "enabled")
70+
71+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
72+
}
6573
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
6674
return true, &issResult{
6775
name: name,
@@ -70,16 +78,17 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
7078
}
7179

7280
type issResult struct {
73-
name string
74-
data []byte
81+
name string
82+
data []byte
83+
values helmify.Values
7584
}
7685

7786
func (r *issResult) Filename() string {
7887
return r.name + ".yaml"
7988
}
8089

8190
func (r *issResult) Values() helmify.Values {
82-
return helmify.Values{}
91+
return r.values
8392
}
8493

8594
func (r *issResult) Write(writer io.Writer) error {

pkg/processor/webhook/mutating.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,33 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
6565
}
6666
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
6767
certName = appMeta.TrimName(certName)
68-
res := fmt.Sprintf(mwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
68+
tmpl := mwhTempl
69+
values := helmify.Values{}
70+
if appMeta.Config().AddWebhookOption {
71+
// Add webhook.enabled value to values.yaml
72+
_, _ = values.Add(true, "webhook", "enabled")
73+
74+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
75+
}
76+
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
6977
return true, &mwhResult{
7078
name: name,
7179
data: []byte(res),
7280
}, nil
7381
}
7482

7583
type mwhResult struct {
76-
name string
77-
data []byte
84+
name string
85+
data []byte
86+
values helmify.Values
7887
}
7988

8089
func (r *mwhResult) Filename() string {
8190
return r.name + ".yaml"
8291
}
8392

8493
func (r *mwhResult) Values() helmify.Values {
85-
return helmify.Values{}
94+
return r.values
8695
}
8796

8897
func (r *mwhResult) Write(writer io.Writer) error {

pkg/processor/webhook/validating.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,33 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
6565
}
6666
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
6767
certName = appMeta.TrimName(certName)
68-
res := fmt.Sprintf(vwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
68+
tmpl := vwhTempl
69+
values := helmify.Values{}
70+
if appMeta.Config().AddWebhookOption {
71+
// Add webhook.enabled value to values.yaml
72+
_, _ = values.Add(true, "webhook", "enabled")
73+
74+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
75+
}
76+
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
6977
return true, &vwhResult{
7078
name: name,
7179
data: []byte(res),
7280
}, nil
7381
}
7482

7583
type vwhResult struct {
76-
name string
77-
data []byte
84+
name string
85+
data []byte
86+
values helmify.Values
7887
}
7988

8089
func (r *vwhResult) Filename() string {
8190
return r.name + ".yaml"
8291
}
8392

8493
func (r *vwhResult) Values() helmify.Values {
85-
return helmify.Values{}
94+
return r.values
8695
}
8796

8897
func (r *vwhResult) Write(writer io.Writer) error {

0 commit comments

Comments
 (0)