Skip to content

Commit d6f44f3

Browse files
committed
* 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 e6af99e commit d6f44f3

File tree

9 files changed

+91
-18
lines changed

9 files changed

+91
-18
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ Usage:
109109
| -image-pull-secrets | Allows the user to use existing secrets as imagePullSecrets | `helmify -image-pull-secrets` |
110110
| -cert-manager-as-subchart | Allows the user to install cert-manager as a subchart | `helmify -cert-manager-as-subchart` |
111111
| -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-as-subchart` |
112+
| -add-webhook-option | Adds an option to enable/disable webhook installation | `helmify -add-webhook-option`|
112113
## Status
113114
Supported k8s resources:
114115
- Deployment, DaemonSet, StatefulSet

cmd/helmify/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ func ReadFlags() config.Config {
6767
flag.StringVar(&result.CertManagerVersion, "cert-manager-version", "v1.12.2", "Allows the user to specify cert-manager subchart version. Only useful with cert-manager-as-subchart.")
6868
flag.BoolVar(&result.FilesRecursively, "r", false, "Scan dirs from -f option recursively")
6969
flag.Var(&files, "f", "File or directory containing k8s manifests")
70+
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml")
7071

7172
flag.Parse()
7273
if h || help {

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type Config struct {
3434
Files []string
3535
// FilesRecursively read Files recursively
3636
FilesRecursively bool
37+
// AddWebhookOption enables the generation of a webhook option in values.yamlß
38+
AddWebhookOption bool
3739
}
3840

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

pkg/processor/deployment/deployment.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package deployment
33
import (
44
"fmt"
55
"io"
6+
"regexp"
67
"strings"
78
"text/template"
89

@@ -120,9 +121,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
120121
if err != nil {
121122
return true, nil, err
122123
}
124+
if appMeta.Config().AddWebhookOption {
125+
spec = addWebhookOption(spec)
126+
}
123127

124128
spec = strings.ReplaceAll(spec, "'", "")
125-
126129
return true, &result{
127130
values: values,
128131
data: struct {
@@ -143,6 +146,30 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
143146
}, nil
144147
}
145148

149+
func addWebhookOption(manifest string) string {
150+
webhookOptionHeader := " {{- if .Values.webhook.enabled }}"
151+
webhookOptionFooter := " {{- end }}"
152+
volumes := ` - name: cert
153+
secret:
154+
defaultMode: 420
155+
secretName: webhook-server-cert`
156+
volumeMounts := ` - mountPath: /tmp/k8s-webhook-server/serving-certs
157+
name: cert
158+
readOnly: true`
159+
manifest = strings.ReplaceAll(manifest, volumes, fmt.Sprintf("%s\n%s\n%s",
160+
webhookOptionHeader, volumes, webhookOptionFooter))
161+
manifest = strings.ReplaceAll(manifest, volumeMounts, fmt.Sprintf("%s\n%s\n%s",
162+
webhookOptionHeader, volumeMounts, webhookOptionFooter))
163+
164+
re := regexp.MustCompile(` - containerPort: \d+
165+
name: webhook-server
166+
protocol: TCP`)
167+
168+
manifest = re.ReplaceAllString(manifest, fmt.Sprintf("%s\n%s\n%s", webhookOptionHeader,
169+
re.FindString(manifest), webhookOptionFooter))
170+
return manifest
171+
}
172+
146173
func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
147174
if deployment.Spec.Replicas == nil {
148175
return "", nil

pkg/processor/service/service.go

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

pkg/processor/webhook/cert.go

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

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

103113
type certResult struct {
104-
name string
105-
data []byte
114+
name string
115+
data []byte
116+
values helmify.Values
106117
}
107118

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

112123
func (r *certResult) Values() helmify.Values {
113-
return helmify.Values{}
124+
return r.values
114125
}
115126

116127
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
@@ -66,24 +66,33 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
6666
}
6767
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
6868
certName = appMeta.TrimName(certName)
69-
res := fmt.Sprintf(mwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
69+
tmpl := mwhTempl
70+
values := helmify.Values{}
71+
if appMeta.Config().AddWebhookOption {
72+
// Add webhook.enabled value to values.yaml
73+
_, _ = values.Add(true, "webhook", "enabled")
74+
75+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
76+
}
77+
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
7078
return true, &mwhResult{
7179
name: name,
7280
data: []byte(res),
7381
}, nil
7482
}
7583

7684
type mwhResult struct {
77-
name string
78-
data []byte
85+
name string
86+
data []byte
87+
values helmify.Values
7988
}
8089

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

8594
func (r *mwhResult) Values() helmify.Values {
86-
return helmify.Values{}
95+
return r.values
8796
}
8897

8998
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
@@ -66,24 +66,33 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
6666
}
6767
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
6868
certName = appMeta.TrimName(certName)
69-
res := fmt.Sprintf(vwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
69+
tmpl := vwhTempl
70+
values := helmify.Values{}
71+
if appMeta.Config().AddWebhookOption {
72+
// Add webhook.enabled value to values.yaml
73+
_, _ = values.Add(true, "webhook", "enabled")
74+
75+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
76+
}
77+
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
7078
return true, &vwhResult{
7179
name: name,
7280
data: []byte(res),
7381
}, nil
7482
}
7583

7684
type vwhResult struct {
77-
name string
78-
data []byte
85+
name string
86+
data []byte
87+
values helmify.Values
7988
}
8089

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

8594
func (r *vwhResult) Values() helmify.Values {
86-
return helmify.Values{}
95+
return r.values
8796
}
8897

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

0 commit comments

Comments
 (0)