Skip to content

Commit

Permalink
* Add an option for enable/disable webhook for a standard operator ge…
Browse files Browse the repository at this point in the history
…nerated by kubebuilder.

* Use regular expressions to match the containerPort of the webhook.

Signed-off-by: Ye Cao <[email protected]>
  • Loading branch information
dashanji authored and arttor committed Nov 17, 2024
1 parent 1e2ec17 commit ad4eef7
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Usage:
| -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` |
| -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` |
| -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` |
| -add-webhook-option | Adds an option to enable/disable webhook installation | `helmify -add-webhook-option`|
## Status
Supported k8s resources:
- Deployment, DaemonSet, StatefulSet
Expand Down
2 changes: 2 additions & 0 deletions cmd/helmify/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ func ReadFlags() config.Config {
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.")
flag.Var(&files, "f", "File or directory containing k8s manifests")
flag.BoolVar(&preservens, "preserve-ns", false, "Use the object's original namespace instead of adding all the resources to a common namespace")
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml")

flag.Parse()
if h || help {
fmt.Print(helpText)
Expand Down
3 changes: 3 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"fmt"

"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/validation"
)
Expand Down Expand Up @@ -40,6 +41,8 @@ type Config struct {
OriginalName bool
// PreserveNs retains the namespaces on the Kubernetes manifests
PreserveNs bool
// AddWebhookOption enables the generation of a webhook option in values.yamlß
AddWebhookOption bool
}

func (c *Config) Validate() error {
Expand Down
27 changes: 27 additions & 0 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
if err != nil {
return true, nil, err
}
if appMeta.Config().AddWebhookOption {
spec = addWebhookOption(spec)
}

spec = replaceSingleQuotes(spec)

Expand Down Expand Up @@ -159,6 +162,30 @@ func replaceSingleQuotes(s string) string {
return r.ReplaceAllString(s, "${1}")
}

func addWebhookOption(manifest string) string {
webhookOptionHeader := " {{- if .Values.webhook.enabled }}"
webhookOptionFooter := " {{- end }}"
volumes := ` - name: cert
secret:
defaultMode: 420
secretName: webhook-server-cert`
volumeMounts := ` - mountPath: /tmp/k8s-webhook-server/serving-certs
name: cert
readOnly: true`
manifest = strings.ReplaceAll(manifest, volumes, fmt.Sprintf("%s\n%s\n%s",
webhookOptionHeader, volumes, webhookOptionFooter))
manifest = strings.ReplaceAll(manifest, volumeMounts, fmt.Sprintf("%s\n%s\n%s",
webhookOptionHeader, volumeMounts, webhookOptionFooter))

re := regexp.MustCompile(` - containerPort: \d+
name: webhook-server
protocol: TCP`)

manifest = re.ReplaceAllString(manifest, fmt.Sprintf("%s\n%s\n%s", webhookOptionHeader,
re.FindString(manifest), webhookOptionFooter))
return manifest
}

func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
if deployment.Spec.Replicas == nil {
return "", nil
Expand Down
4 changes: 4 additions & 0 deletions pkg/processor/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,12 @@ func (r svc) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
}
ports[i] = pMap
}

_ = unstructured.SetNestedSlice(values, ports, shortNameCamel, "ports")
res := meta + fmt.Sprintf(svcTempSpec, shortNameCamel, selector, appMeta.ChartName())
if shortNameCamel == "webhookService" && appMeta.Config().AddWebhookOption {
res = fmt.Sprintf("{{- if .Values.webhook.enabled }}\n%s\n{{- end }}", res)
}
return true, &result{
name: shortName,
data: res,
Expand Down
23 changes: 17 additions & 6 deletions pkg/processor/webhook/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import (
)

const (
certTempl = `apiVersion: cert-manager.io/v1
WebhookHeader = `{{- if .Values.webhook.enabled }}`
WebhookFooter = `{{- end }}`
certTempl = `apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
name: {{ include "%[1]s.fullname" . }}-%[2]s
Expand Down Expand Up @@ -92,24 +94,33 @@ func (c cert) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructure
} else {
tmpl = certTempl
}
values := helmify.Values{}
if appMeta.Config().AddWebhookOption {
// Add webhook.enabled value to values.yaml
_, _ = values.Add(true, "webhook", "enabled")

tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
}
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
return true, &certResult{
name: name,
data: []byte(res),
name: name,
data: []byte(res),
values: values,
}, nil
}

type certResult struct {
name string
data []byte
name string
data []byte
values helmify.Values
}

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

func (r *certResult) Values() helmify.Values {
return helmify.Values{}
return r.values
}

func (r *certResult) Write(writer io.Writer) error {
Expand Down
15 changes: 12 additions & 3 deletions pkg/processor/webhook/issuer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
return false, nil, nil
}
name := appMeta.TrimName(obj.GetName())

spec, _ := yaml.Marshal(obj.Object["spec"])
spec = yamlformat.Indent(spec, 2)
spec = bytes.TrimRight(spec, "\n ")
Expand All @@ -62,6 +63,13 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
} else {
tmpl = issuerTempl
}
values := helmify.Values{}
if appMeta.Config().AddWebhookOption {
// Add webhook.enabled value to values.yaml
_, _ = values.Add(true, "webhook", "enabled")

tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, tmpl, WebhookFooter)
}
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, string(spec))
return true, &issResult{
name: name,
Expand All @@ -70,16 +78,17 @@ func (i issuer) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructu
}

type issResult struct {
name string
data []byte
name string
data []byte
values helmify.Values
}

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

func (r *issResult) Values() helmify.Values {
return helmify.Values{}
return r.values
}

func (r *issResult) Write(writer io.Writer) error {
Expand Down
17 changes: 13 additions & 4 deletions pkg/processor/webhook/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,33 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
}
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
certName = appMeta.TrimName(certName)
res := fmt.Sprintf(mwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
tmpl := mwhTempl
values := helmify.Values{}
if appMeta.Config().AddWebhookOption {
// Add webhook.enabled value to values.yaml
_, _ = values.Add(true, "webhook", "enabled")

tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
}
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
return true, &mwhResult{
name: name,
data: []byte(res),
}, nil
}

type mwhResult struct {
name string
data []byte
name string
data []byte
values helmify.Values
}

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

func (r *mwhResult) Values() helmify.Values {
return helmify.Values{}
return r.values
}

func (r *mwhResult) Write(writer io.Writer) error {
Expand Down
17 changes: 13 additions & 4 deletions pkg/processor/webhook/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,33 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
}
certName = strings.TrimPrefix(certName, appMeta.Namespace()+"/")
certName = appMeta.TrimName(certName)
res := fmt.Sprintf(vwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
tmpl := vwhTempl
values := helmify.Values{}
if appMeta.Config().AddWebhookOption {
// Add webhook.enabled value to values.yaml
_, _ = values.Add(true, "webhook", "enabled")

tmpl = fmt.Sprintf("%s\n%s\n%s", WebhookHeader, mwhTempl, WebhookFooter)
}
res := fmt.Sprintf(tmpl, appMeta.ChartName(), name, certName, string(webhooks))
return true, &vwhResult{
name: name,
data: []byte(res),
}, nil
}

type vwhResult struct {
name string
data []byte
name string
data []byte
values helmify.Values
}

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

func (r *vwhResult) Values() helmify.Values {
return helmify.Values{}
return r.values
}

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

0 comments on commit ad4eef7

Please sign in to comment.