Skip to content

Commit

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

Signed-off-by: dashanji <[email protected]>
  • Loading branch information
dashanji committed Apr 3, 2023
1 parent 7701f30 commit 826e4c4
Show file tree
Hide file tree
Showing 10 changed files with 93 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ Usage:
| -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`|
| -image-pull-secrets| Allows the user to use existing secrets as imagePullSecrets | `helmify -image-pull-secrets`|
| -cert-manager-as-subchart | Allows the user to install cert-manager as a subchart | `helmify -cert-manager-as-subchart`|
| -add-webhook-option | Adds an option to enable/disable webhook installation | `helmify -add-webhook-option`|

## Status
Supported k8s resources:
- Deployment, DaemonSet, StatefulSet
Expand Down
1 change: 1 addition & 0 deletions cmd/helmify/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func ReadFlags() config.Config {
flag.BoolVar(&result.ImagePullSecrets, "image-pull-secrets", false, "Allows the user to use existing secrets as imagePullSecrets in values.yaml")
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")
flag.BoolVar(&result.CertManagerAsSubchart, "cert-manager-as-subchart", false, "Allows the user to add cert-manager as a subchart")
flag.BoolVar(&result.AddWebhookOption, "add-webhook-option", false, "Allows the user to add webhook option in values.yaml")

flag.Parse()
if h || help {
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ type Config struct {
GenerateDefaults bool
// CertManagerAsSubchart enables the generation of a subchart for cert-manager
CertManagerAsSubchart bool
// AddWebhookOption enables the generation of a webhook option in values.yamlß
AddWebhookOption bool
}

func (c *Config) Validate() error {
Expand Down
29 changes: 27 additions & 2 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package deployment

import (
"fmt"
"github.com/arttor/helmify/pkg/processor/pod"
"io"
"strings"
"text/template"

"github.com/arttor/helmify/pkg/processor/pod"

"github.com/arttor/helmify/pkg/helmify"
"github.com/arttor/helmify/pkg/processor"
yamlformat "github.com/arttor/helmify/pkg/yaml"
Expand Down Expand Up @@ -119,9 +120,11 @@ 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 = strings.ReplaceAll(spec, "'", "")

return true, &result{
values: values,
data: struct {
Expand All @@ -142,6 +145,28 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
}, nil
}

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`
webhookPort := ` - containerPort: 9443
name: webhook-server
protocol: TCP`
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))
manifest = strings.ReplaceAll(manifest, webhookPort, fmt.Sprintf("%s\n%s\n%s",
webhookOptionHeader, webhookPort, webhookOptionFooter))
return manifest
}

func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) {
if deployment.Spec.Replicas == nil {
return "", nil
Expand Down
3 changes: 2 additions & 1 deletion pkg/processor/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package pod

import (
"fmt"
"strings"

"github.com/arttor/helmify/pkg/cluster"
"github.com/arttor/helmify/pkg/helmify"
securityContext "github.com/arttor/helmify/pkg/processor/security-context"
"github.com/iancoleman/strcase"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"strings"
)

const imagePullPolicyTemplate = "{{ .Values.%[1]s.%[2]s.imagePullPolicy }}"
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 @@ -95,8 +95,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 @@ -16,7 +16,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 @@ -93,24 +95,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 @@ -66,24 +66,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 @@ -66,24 +66,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 826e4c4

Please sign in to comment.