From 07b07199f15725968a17a33c39ebda1f95174a5c Mon Sep 17 00:00:00 2001 From: Etrik Patricella Date: Fri, 16 Feb 2024 13:39:56 -0500 Subject: [PATCH 1/2] fix: handle mutually exclusive PDB attributes --- pkg/processor/poddisruptionbudget/pdb.go | 57 +++++++++++++----------- 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/pkg/processor/poddisruptionbudget/pdb.go b/pkg/processor/poddisruptionbudget/pdb.go index 99635d14..22eef3b9 100644 --- a/pkg/processor/poddisruptionbudget/pdb.go +++ b/pkg/processor/poddisruptionbudget/pdb.go @@ -5,9 +5,8 @@ import ( "fmt" "io" - "github.com/arttor/helmify/pkg/processor" - "github.com/arttor/helmify/pkg/helmify" + "github.com/arttor/helmify/pkg/processor" yamlformat "github.com/arttor/helmify/pkg/yaml" "github.com/iancoleman/strcase" policyv1 "k8s.io/api/policy/v1" @@ -17,30 +16,18 @@ import ( "sigs.k8s.io/yaml" ) -const ( - pdbTempSpec = ` -spec: - minAvailable: {{ .Values.%[1]s.minAvailable }} - maxUnavailable: {{ .Values.%[1]s.maxUnavailable }} - selector: -%[2]s - {{- include "%[3]s.selectorLabels" . | nindent 6 }}` -) - var pdbGVC = schema.GroupVersionKind{ Group: "policy", Version: "v1", Kind: "PodDisruptionBudget", } -// New creates processor for k8s Service resource. func New() helmify.Processor { return &pdb{} } type pdb struct{} -// Process k8s Service object into template. Returns false if not capable of processing given resource type. func (r pdb) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured) (bool, helmify.Template, error) { if obj.GroupVersionKind() != pdbGVC { return false, nil, nil @@ -50,36 +37,54 @@ func (r pdb) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured if err != nil { return true, nil, fmt.Errorf("%w: unable to cast to pdb", err) } - spec := pdb.Spec - values := helmify.Values{} + + // Extract the name and namespace for use in the error message + name := pdb.GetName() + namespace := pdb.GetNamespace() + if namespace == "" { + namespace = "default" // Assuming 'default' if no namespace is specified + } + + // Check if both MinAvailable and MaxUnavailable are specified + if pdb.Spec.MinAvailable != nil && pdb.Spec.MaxUnavailable != nil { + return true, nil, fmt.Errorf("error in PodDisruptionBudget '%s' in namespace '%s': both MinAvailable and MaxUnavailable are specified, but only one is allowed", name, namespace) + } meta, err := processor.ProcessObjMeta(appMeta, obj) if err != nil { return true, nil, err } - name := appMeta.TrimName(obj.GetName()) nameCamel := strcase.ToLowerCamel(name) selector, _ := yaml.Marshal(pdb.Spec.Selector) - selector = yamlformat.Indent(selector, 4) - selector = bytes.TrimRight(selector, "\n ") + selectorIndented := yamlformat.Indent(selector, 4) + selectorIndented = bytes.TrimRight(selectorIndented, "\n ") - if spec.MaxUnavailable != nil { - _, err := values.Add(spec.MaxUnavailable.IntValue(), nameCamel, "maxUnavailable") + values := helmify.Values{} + specSection := "" + + if pdb.Spec.MaxUnavailable != nil { + specSection = "maxUnavailable: {{ .Values." + nameCamel + ".maxUnavailable }}" + _, err := values.Add(pdb.Spec.MaxUnavailable.IntValue(), nameCamel, "maxUnavailable") if err != nil { return true, nil, err } - } - - if spec.MinAvailable != nil { - _, err := values.Add(spec.MinAvailable.IntValue(), nameCamel, "minAvailable") + } else if pdb.Spec.MinAvailable != nil { + specSection = "minAvailable: {{ .Values." + nameCamel + ".minAvailable }}" + _, err := values.Add(pdb.Spec.MinAvailable.IntValue(), nameCamel, "minAvailable") if err != nil { return true, nil, err } } - res := meta + fmt.Sprintf(pdbTempSpec, nameCamel, selector, appMeta.ChartName()) + res := meta + fmt.Sprintf(` +spec: + %[1]s + selector: +%[2]s + {{- include "%[3]s.selectorLabels" . | nindent 6 }}`, specSection, selectorIndented, appMeta.ChartName()) + return true, &result{ name: name, data: res, From d154352e0ce8255d5b12680c80ab0dedfc0a279c Mon Sep 17 00:00:00 2001 From: Etrik Patricella Date: Fri, 16 Feb 2024 13:43:49 -0500 Subject: [PATCH 2/2] fix: use const for pdbTempSpec --- pkg/processor/poddisruptionbudget/pdb.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/processor/poddisruptionbudget/pdb.go b/pkg/processor/poddisruptionbudget/pdb.go index 22eef3b9..ecc4c20b 100644 --- a/pkg/processor/poddisruptionbudget/pdb.go +++ b/pkg/processor/poddisruptionbudget/pdb.go @@ -16,6 +16,15 @@ import ( "sigs.k8s.io/yaml" ) +const ( + pdbTempSpec = ` +spec: + %[1]s + selector: +%[2]s + {{- include "%[3]s.selectorLabels" . | nindent 6 }}` +) + var pdbGVC = schema.GroupVersionKind{ Group: "policy", Version: "v1", @@ -78,12 +87,7 @@ func (r pdb) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstructured } } - res := meta + fmt.Sprintf(` -spec: - %[1]s - selector: -%[2]s - {{- include "%[3]s.selectorLabels" . | nindent 6 }}`, specSection, selectorIndented, appMeta.ChartName()) + res := meta + fmt.Sprintf(pdbTempSpec, specSection, selectorIndented, appMeta.ChartName()) return true, &result{ name: name,