From e4c09163ae2a857eb3e0f379e4ffa0638eba6683 Mon Sep 17 00:00:00 2001 From: Artem Torubarov Date: Wed, 20 Oct 2021 16:58:17 +0300 Subject: [PATCH] handle empty replicas --- pkg/processor/deployment/deployment.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/pkg/processor/deployment/deployment.go b/pkg/processor/deployment/deployment.go index 06aa841..a01419b 100644 --- a/pkg/processor/deployment/deployment.go +++ b/pkg/processor/deployment/deployment.go @@ -28,7 +28,9 @@ var deploymentGVC = schema.GroupVersionKind{ var deploymentTempl, _ = template.New("deployment").Parse( `{{- .Meta }} spec: - replicas: {{ .Replicas }} +{{- if .Replicas }} +{{ .Replicas }} +{{- end }} selector: {{ .Selector }} template: @@ -68,7 +70,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr values := helmify.Values{} name := appMeta.TrimName(obj.GetName()) - replicas, err := values.Add(int64(*depl.Spec.Replicas), name, "replicas") + replicas, err := processReplicas(name, &depl, &values) if err != nil { return true, nil, err } @@ -164,6 +166,22 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr }, nil } +func processReplicas(name string, deployment *appsv1.Deployment, values *helmify.Values) (string, error) { + if deployment.Spec.Replicas == nil { + return "", nil + } + replicasTpl, err := values.Add(int64(*deployment.Spec.Replicas), name, "replicas") + if err != nil { + return "", err + } + replicas, err := yamlformat.Marshal(map[string]interface{}{"replicas": replicasTpl}, 2) + if err != nil { + return "", err + } + replicas = strings.ReplaceAll(replicas, "'", "") + return replicas, nil +} + func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpec) (helmify.Values, error) { values := helmify.Values{} for i, c := range pod.Containers {