Skip to content

Commit 826e4c4

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 826e4c4

File tree

10 files changed

+93
-20
lines changed

10 files changed

+93
-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: 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)