Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions pkg/processor/webhook/mutating.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/arttor/helmify/pkg/helmify"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
v1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -47,15 +48,30 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
return false, nil, nil
}
name := appMeta.TrimName(obj.GetName())
nameCamel := strcase.ToLowerCamel(name)

whConf := v1.MutatingWebhookConfiguration{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &whConf)
if err != nil {
return true, nil, errors.Wrap(err, "unable to cast to MutatingWebhookConfiguration")
}

values := helmify.Values{}
err = unstructured.SetNestedMap(values, make(map[string]interface{}), nameCamel)
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("can not set webhook parameter map for %s", name))
}

for i, whc := range whConf.Webhooks {
whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
whcField := strcase.ToLowerCamel(whc.Name)
whcValues, err := ProcessMWH(appMeta, &whc, fmt.Sprintf(".Values.%s.%s", nameCamel, whcField))
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("unable to Process WebHook config: %s / %s", name, whc.Name))
}
err = unstructured.SetNestedField(values, whcValues, nameCamel, whcField)
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("can not set webhook parameters for %s / %s", name, whc.Name))
}
whConf.Webhooks[i] = whc
}
webhooks, _ := yaml.Marshal(whConf.Webhooks)
Expand All @@ -68,22 +84,36 @@ func (w mwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
certName = appMeta.TrimName(certName)
res := fmt.Sprintf(mwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
return true, &mwhResult{
name: name,
data: []byte(res),
values: values,
name: name,
data: []byte(res),
}, nil
}

func ProcessMWH(appMeta helmify.AppMetadata, whc *v1.MutatingWebhook, valuesRoot string) (map[string]interface{}, error) {
values := make(map[string]interface{})
failurePolicyField := "failurePolicy"
failurePolicy := v1.FailurePolicyType(fmt.Sprintf("{{ %s.%s }}", valuesRoot, failurePolicyField))
values[failurePolicyField] = "Fail"

whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
whc.FailurePolicy = &failurePolicy
return values, 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
42 changes: 37 additions & 5 deletions pkg/processor/webhook/validating.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/arttor/helmify/pkg/helmify"
"github.com/iancoleman/strcase"
"github.com/pkg/errors"
v1 "k8s.io/api/admissionregistration/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -47,13 +48,30 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
return false, nil, nil
}
name := appMeta.TrimName(obj.GetName())
nameCamel := strcase.ToLowerCamel(name)

whConf := v1.ValidatingWebhookConfiguration{}
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &whConf)
if err != nil {
return true, nil, errors.Wrap(err, "unable to cast to ValidatingWebhookConfiguration")
}

values := helmify.Values{}
err = unstructured.SetNestedMap(values, make(map[string]interface{}), nameCamel)
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("can not set webhook parameter map for %s", name))
}

for i, whc := range whConf.Webhooks {
whcField := strcase.ToLowerCamel(whc.Name)
whcValues, err := ProcessVWH(appMeta, &whc, fmt.Sprintf(".Values.%s.%s", nameCamel, whcField))
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("unable to Process WebHook config: %s / %s", name, whc.Name))
}
err = unstructured.SetNestedField(values, whcValues, nameCamel, whcField)
if err != nil {
return true, nil, errors.Wrap(err, fmt.Sprintf("can not set webhook parameters for %s / %s", name, whc.Name))
}
whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
whConf.Webhooks[i] = whc
Expand All @@ -68,22 +86,36 @@ func (w vwh) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured
certName = appMeta.TrimName(certName)
res := fmt.Sprintf(vwhTempl, appMeta.ChartName(), name, certName, string(webhooks))
return true, &vwhResult{
name: name,
data: []byte(res),
values: values,
name: name,
data: []byte(res),
}, nil
}

func ProcessVWH(appMeta helmify.AppMetadata, whc *v1.ValidatingWebhook, valuesRoot string) (map[string]interface{}, error) {
values := make(map[string]interface{})
failurePolicyField := "failurePolicy"
failurePolicy := v1.FailurePolicyType(fmt.Sprintf("{{ %s.%s }}", valuesRoot, failurePolicyField))
values[failurePolicyField] = "Fail"

whc.ClientConfig.Service.Name = appMeta.TemplatedName(whc.ClientConfig.Service.Name)
whc.ClientConfig.Service.Namespace = strings.ReplaceAll(whc.ClientConfig.Service.Namespace, appMeta.Namespace(), `{{ .Release.Namespace }}`)
whc.FailurePolicy = &failurePolicy
return values, 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