From c850c3e14d7748837ea075caf91a5f1fdf87e884 Mon Sep 17 00:00:00 2001 From: hfuss Date: Fri, 22 Mar 2024 00:55:35 -0400 Subject: [PATCH 1/2] Exposing ExtraPodLabels and Tolerations on Deployments Signed-off-by: hfuss --- pkg/app/context.go | 2 +- pkg/processor/deployment/deployment.go | 8 ++++++-- pkg/processor/pod/pod.go | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/pkg/app/context.go b/pkg/app/context.go index 2c4e52eb..5aef94c5 100644 --- a/pkg/app/context.go +++ b/pkg/app/context.go @@ -75,7 +75,7 @@ func (c *appContext) CreateHelm(stop <-chan struct{}) error { default: } } - return c.output.Create(c.config.ChartDir, c.config.ChartName, c.config.Crd, c.config.CertManagerAsSubchart, c.config.CertManagerVersion , templates, filenames) + return c.output.Create(c.config.ChartDir, c.config.ChartName, c.config.Crd, c.config.CertManagerAsSubchart, c.config.CertManagerVersion, templates, filenames) } func (c *appContext) process(obj *unstructured.Unstructured) (helmify.Template, error) { diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index b96f7b0d..7d713e6a 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -98,11 +98,16 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr selector = strings.Trim(selector, " \n") selector = string(yamlformat.Indent([]byte(selector), 4)) + nameCamel := strcase.ToLowerCamel(name) podLabels, err := yamlformat.Marshal(depl.Spec.Template.ObjectMeta.Labels, 8) if err != nil { return true, nil, err } - podLabels += fmt.Sprintf("\n {{- include \"%s.selectorLabels\" . | nindent 8 }}", appMeta.ChartName()) + podLabels += fmt.Sprintf("\n {{- include \"%s.selectorLabels\" . | nindent 8 }}\n {{- toYaml .Values.%s.extraPodLabels | nindent 8 }}", appMeta.ChartName(), nameCamel) + err = unstructured.SetNestedField(values, make(map[string]interface{}), nameCamel, "podLabels") + if err != nil { + return true, nil, err + } podAnnotations := "" if len(depl.Spec.Template.ObjectMeta.Annotations) != 0 { @@ -114,7 +119,6 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr podAnnotations = "\n" + podAnnotations } - nameCamel := strcase.ToLowerCamel(name) specMap, podValues, err := pod.ProcessSpec(nameCamel, appMeta, depl.Spec.Template.Spec) if err != nil { return true, nil, err diff --git a/pkg/processor/pod/pod.go b/pkg/processor/pod/pod.go index 09979abd..52bc1dcc 100644 --- a/pkg/processor/pod/pod.go +++ b/pkg/processor/pod/pod.go @@ -71,6 +71,28 @@ func ProcessSpec(objName string, appMeta helmify.AppMetadata, spec corev1.PodSpe if err != nil { return nil, nil, err } + + err = unstructured.SetNestedField(specMap, fmt.Sprintf(`{{- toYaml .Values.%s.tolerations | nindent 8 }}`, objName), "tolerations") + if err != nil { + return nil, nil, err + } + uTolerations := make([]interface{}, 0) + for i, toleration := range spec.Tolerations { + uTolerations[i] = map[string]interface{}{ + "value": toleration.Value, + "key": toleration.Key, + "operator": toleration.Operator, + "effect": toleration.Effect, + } + + if toleration.TolerationSeconds != nil { + uTolerations[i].(map[string]interface{})["tolerationSeconds"] = *toleration.TolerationSeconds + } + } + err = unstructured.SetNestedSlice(values, uTolerations, objName, "tolerations") + if err != nil { + return nil, nil, err + } } return specMap, values, nil From 92126fdfaf294a0230c633c0b4a46898b983a6f0 Mon Sep 17 00:00:00 2001 From: hfuss Date: Tue, 26 Mar 2024 23:40:13 -0400 Subject: [PATCH 2/2] fixing podLabels templating Signed-off-by: hfuss --- pkg/processor/deployment/deployment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index 7d713e6a..591c9a2c 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -103,7 +103,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr if err != nil { return true, nil, err } - podLabels += fmt.Sprintf("\n {{- include \"%s.selectorLabels\" . | nindent 8 }}\n {{- toYaml .Values.%s.extraPodLabels | nindent 8 }}", appMeta.ChartName(), nameCamel) + podLabels += fmt.Sprintf("\n {{- include \"%s.selectorLabels\" . | nindent 8 }}\n {{- if .Values.%s.podLabels }}\n {{- toYaml .Values.%s.podLabels | nindent 8 }}\n {{- end }}", appMeta.ChartName(), nameCamel, nameCamel) err = unstructured.SetNestedField(values, make(map[string]interface{}), nameCamel, "podLabels") if err != nil { return true, nil, err