Skip to content

Commit 4ae4f03

Browse files
committed
Add an option for enable/disable webhook for a standard operator generated by kubebuilder
Signed-off-by: dashanji <[email protected]>
1 parent 7701f30 commit 4ae4f03

File tree

10 files changed

+105
-20
lines changed

10 files changed

+105
-20
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ Usage:
8787
| -crd-dir | Place crds in their own folder per Helm 3 [docs](https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#method-1-let-helm-do-it-for-you). Caveat: CRDs templating is not supported by Helm. | `helmify -crd-dir`|
8888
| -image-pull-secrets| Allows the user to use existing secrets as imagePullSecrets | `helmify -image-pull-secrets`|
8989
| -cert-manager-as-subchart | Allows the user to install cert-manager as a subchart | `helmify -cert-manager-as-subchart`|
90+
| -add-webhook-option | Adds an option to enable/disable webhook installation | `helmify -add-webhook-option`|
91+
9092
## Status
9193
Supported k8s resources:
9294
- Deployment, DaemonSet, StatefulSet

cmd/helmify/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ func ReadFlags() config.Config {
3939
flag.BoolVar(&result.ImagePullSecrets, "image-pull-secrets", false, "Allows the user to use existing secrets as imagePullSecrets in values.yaml")
4040
flag.BoolVar(&result.GenerateDefaults, "generate-defaults", false, "Allows the user to add empty placeholders for tipical customization options in values.yaml. Currently covers: topology constraints, node selectors, tolerances")
4141
flag.BoolVar(&result.CertManagerAsSubchart, "cert-manager-as-subchart", false, "Allows the user to add cert-manager as a subchart")
42+
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml")
4243

4344
flag.Parse()
4445
if h || help {

pkg/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ type Config struct {
2828
GenerateDefaults bool
2929
// CertManagerAsSubchart enables the generation of a subchart for cert-manager
3030
CertManagerAsSubchart bool
31+
// AddWebhookOption enables the generation of a webhook option in values.yamlß
32+
AddWebhookOption bool
3133
}
3234

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

pkg/processor/deployment/deployment.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package deployment
22

33
import (
44
"fmt"
5-
"github.com/arttor/helmify/pkg/processor/pod"
65
"io"
76
"strings"
87
"text/template"
98

9+
"github.com/arttor/helmify/pkg/processor/pod"
10+
1011
"github.com/arttor/helmify/pkg/helmify"
1112
"github.com/arttor/helmify/pkg/processor"
1213
yamlformat "github.com/arttor/helmify/pkg/yaml"
@@ -119,9 +120,11 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
119120
if err != nil {
120121
return true, nil, err
121122
}
123+
if appMeta.Config().AddWebhookOption {
124+
spec = addWebhookOption(spec)
125+
}
122126

123127
spec = strings.ReplaceAll(spec, "'", "")
124-
125128
return true, &result{
126129
values: values,
127130
data: struct {
@@ -142,6 +145,28 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
142145
}, nil
143146
}
144147

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

pkg/processor/pod/pod.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package pod
22

33
import (
44
"fmt"
5+
"strings"
6+
57
"github.com/arttor/helmify/pkg/cluster"
68
"github.com/arttor/helmify/pkg/helmify"
79
securityContext "github.com/arttor/helmify/pkg/processor/security-context"
810
"github.com/iancoleman/strcase"
911
corev1 "k8s.io/api/core/v1"
1012
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1113
"k8s.io/apimachinery/pkg/runtime"
12-
"strings"
1314
)
1415

1516
const imagePullPolicyTemplate = "{{ .Values.%[1]s.%[2]s.imagePullPolicy }}"

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: 21 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
@@ -57,6 +59,10 @@ func (c cert) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructure
5759
}
5860
name := appMeta.TrimName(obj.GetName())
5961

62+
// Add webhook.enabled value to values.yaml
63+
values := helmify.Values{}
64+
values.Add(true, "webhook", "enabled")
65+
6066
dnsNames, _, err := unstructured.NestedSlice(obj.Object, "spec", "dnsNames")
6167
if err != nil {
6268
return true, nil, errors.Wrap(err, "unable get cert dnsNames")
@@ -93,24 +99,33 @@ func (c cert) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructure
9399
} else {
94100
tmpl = certTempl
95101
}
102+
if appMeta.Config().AddWebhookOption {
103+
// Add webhook.enabled value to values.yaml
104+
values := helmify.Values{}
105+
values.Add(true, "webhook", "enabled")
106+
107+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
108+
}
96109
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
97110
return true, &certResult{
98-
name: name,
99-
data: []byte(res),
111+
name: name,
112+
data: []byte(res),
113+
values: values,
100114
}, nil
101115
}
102116

103117
type certResult struct {
104-
name string
105-
data []byte
118+
name string
119+
data []byte
120+
values helmify.Values
106121
}
107122

108123
func (r *certResult) Filename() string {
109124
return r.name + ".yaml"
110125
}
111126

112127
func (r *certResult) Values() helmify.Values {
113-
return helmify.Values{}
128+
return r.values
114129
}
115130

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

pkg/processor/webhook/issuer.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
5353
return false, nil, nil
5454
}
5555
name := appMeta.TrimName(obj.GetName())
56+
57+
// Add webhook.enabled to values
58+
values := helmify.Values{}
59+
values.Add(true, "webhook", "enabled")
60+
5661
spec, _ := yaml.Marshal(obj.Object["spec"])
5762
spec = yamlformat.Indent(spec, 2)
5863
spec = bytes.TrimRight(spec, "\n ")
@@ -62,6 +67,13 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
6267
} else {
6368
tmpl = issuerTempl
6469
}
70+
if appMeta.Config().AddWebhookOption {
71+
// Add webhook.enabled value to values.yaml
72+
values := helmify.Values{}
73+
values.Add(true, "webhook", "enabled")
74+
75+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
76+
}
6577
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
6678
return true, &issResult{
6779
name: name,
@@ -70,16 +82,17 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
7082
}
7183

7284
type issResult struct {
73-
name string
74-
data []byte
85+
name string
86+
data []byte
87+
values helmify.Values
7588
}
7689

7790
func (r *issResult) Filename() string {
7891
return r.name + ".yaml"
7992
}
8093

8194
func (r *issResult) Values() helmify.Values {
82-
return helmify.Values{}
95+
return r.values
8396
}
8497

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

pkg/processor/webhook/mutating.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
4848
}
4949
name := appMeta.TrimName(obj.GetName())
5050

51+
// Add webhook.enabled value to values.yaml
52+
values := helmify.Values{}
53+
values.Add(true, "webhook", "enabled")
54+
5155
whConf := v1.MutatingWebhookConfiguration{}
5256
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &whConf)
5357
if err != nil {
@@ -66,24 +70,33 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
6670
}
6771
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
6872
certName = appMeta.TrimName(certName)
69-
res := fmt.Sprintf(mwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
73+
tmpl := mwhTempl
74+
if appMeta.Config().AddWebhookOption {
75+
// Add webhook.enabled value to values.yaml
76+
values := helmify.Values{}
77+
values.Add(true, "webhook", "enabled")
78+
79+
tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
80+
}
81+
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
7082
return true, &mwhResult{
7183
name: name,
7284
data: []byte(res),
7385
}, nil
7486
}
7587

7688
type mwhResult struct {
77-
name string
78-
data []byte
89+
name string
90+
data []byte
91+
values helmify.Values
7992
}
8093

8194
func (r *mwhResult) Filename() string {
8295
return r.name + ".yaml"
8396
}
8497

8598
func (r *mwhResult) Values() helmify.Values {
86-
return helmify.Values{}
99+
return r.values
87100
}
88101

89102
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+
if appMeta.Config().AddWebhookOption {
71+
// Add webhook.enabled value to values.yaml
72+
values := helmify.Values{}
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)