Skip to content

Commit

Permalink
Fixed the rest of the linting that went a little crazy
Browse files Browse the repository at this point in the history
  • Loading branch information
kaleho authored and arttor committed Apr 30, 2023
1 parent 024f1e0 commit 03f8524
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
48 changes: 24 additions & 24 deletions pkg/processor/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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"
Expand All @@ -21,46 +21,46 @@ func ProcessSpec(objName string, appMeta helmify.AppMetadata, spec corev1.PodSpe
if err != nil {
return nil, nil, err
}

// replace PVC to templated name
for i := 0; i < len(spec.Volumes); i++ {
vol := spec.Volumes[i]
if vol.PersistentVolumeClaim == nil {
continue
}
tempPVCName := appMeta.TemplatedName(vol.PersistentVolumeClaim.ClaimName)

spec.Volumes[i].PersistentVolumeClaim.ClaimName = tempPVCName
}

// replace container resources with template to values.
specMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&spec)
if err != nil {
return nil, nil, fmt.Errorf("%w: unable to convert podSpec to map", err)
}

specMap, values, err = processNestedContainers(specMap, objName, values, "containers")
if err != nil {
return nil, nil, err
}

specMap, values, err = processNestedContainers(specMap, objName, values, "initContainers")
if err != nil {
return nil, nil, err
}

if appMeta.Config().ImagePullSecrets {
if _, defined := specMap["imagePullSecrets"]; !defined {
specMap["imagePullSecrets"] = "{{ .Values.imagePullSecrets | default list | toJson }}"
values["imagePullSecrets"] = []string{}
}
}

err = securityContext.ProcessContainerSecurityContext(objName, specMap, &values)
if err != nil {
return nil, nil, err
}

// process nodeSelector if presented:
if spec.NodeSelector != nil {
err = unstructured.SetNestedField(specMap, fmt.Sprintf(`{{- toYaml .Values.%s.nodeSelector | nindent 8 }}`, objName), "nodeSelector")
Expand All @@ -72,7 +72,7 @@ func ProcessSpec(objName string, appMeta helmify.AppMetadata, spec corev1.PodSpe
return nil, nil, err
}
}

return specMap, values, nil
}

Expand All @@ -81,19 +81,19 @@ func processNestedContainers(specMap map[string]interface{}, objName string, val
if err != nil {
return nil, nil, err
}

if len(containers) > 0 {
containers, values, err = processContainers(objName, values, containerKey, containers)
if err != nil {
return nil, nil, err
}

err = unstructured.SetNestedSlice(specMap, containers, containerKey)
if err != nil {
return nil, nil, err
}
}

return specMap, values, nil
}

Expand All @@ -110,7 +110,7 @@ func processContainers(objName string, values helmify.Values, containerType stri
return nil, nil, err
}
}

args, exists, err := unstructured.NestedStringSlice(containers[i].(map[string]interface{}), "args")
if err != nil {
return nil, nil, err
Expand All @@ -120,7 +120,7 @@ func processContainers(objName string, values helmify.Values, containerType stri
if err != nil {
return nil, nil, err
}

err = unstructured.SetNestedStringSlice(values, args, objName, containerName, "args")
if err != nil {
return nil, nil, fmt.Errorf("%w: unable to set deployment value field", err)
Expand All @@ -139,15 +139,15 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
}
pod.Containers[i] = processed
}

for i, c := range pod.InitContainers {
processed, err := processPodContainer(name, appMeta, c, &values)
if err != nil {
return nil, err
}
pod.InitContainers[i] = processed
}

for _, v := range pod.Volumes {
if v.ConfigMap != nil {
v.ConfigMap.Name = appMeta.TemplatedName(v.ConfigMap.Name)
Expand All @@ -157,11 +157,11 @@ func processPodSpec(name string, appMeta helmify.AppMetadata, pod *corev1.PodSpe
}
}
pod.ServiceAccountName = appMeta.TemplatedName(pod.ServiceAccountName)

for i, s := range pod.ImagePullSecrets {
pod.ImagePullSecrets[i].Name = appMeta.TemplatedName(s.Name)
}

return values, nil
}

Expand All @@ -173,7 +173,7 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
repo, tag := c.Image[:index], c.Image[index+1:]
containerName := strcase.ToLowerCamel(c.Name)
c.Image = fmt.Sprintf("{{ .Values.%[1]s.%[2]s.image.repository }}:{{ .Values.%[1]s.%[2]s.image.tag | default .Chart.AppVersion }}", name, containerName)

err := unstructured.SetNestedField(*values, repo, name, containerName, "image", "repository")
if err != nil {
return c, fmt.Errorf("%w: unable to set deployment value field", err)
Expand All @@ -182,12 +182,12 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
if err != nil {
return c, fmt.Errorf("%w: unable to set deployment value field", err)
}

c, err = processEnv(name, appMeta, c, values)
if err != nil {
return c, err
}

for _, e := range c.EnvFrom {
if e.SecretRef != nil {
e.SecretRef.Name = appMeta.TemplatedName(e.SecretRef.Name)
Expand All @@ -212,7 +212,7 @@ func processPodContainer(name string, appMeta helmify.AppMetadata, c corev1.Cont
return c, fmt.Errorf("%w: unable to set container resources value", err)
}
}

if c.ImagePullPolicy != "" {
err = unstructured.SetNestedField(*values, string(c.ImagePullPolicy), name, containerName, "imagePullPolicy")
if err != nil {
Expand All @@ -237,7 +237,7 @@ func processEnv(name string, appMeta helmify.AppMetadata, c corev1.Container, va
}
continue
}

err := unstructured.SetNestedField(*values, c.Env[i].Value, name, containerName, "env", strcase.ToLowerCamel(strings.ToLower(c.Env[i].Name)))
if err != nil {
return c, fmt.Errorf("%w: unable to set deployment value field", err)
Expand Down
10 changes: 5 additions & 5 deletions pkg/processor/security-context/container_security_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package security_context

import (
"fmt"

"github.com/arttor/helmify/pkg/helmify"
"github.com/iancoleman/strcase"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand All @@ -20,12 +20,12 @@ func ProcessContainerSecurityContext(nameCamel string, specMap map[string]interf
if err != nil {
return err
}

err = processSecurityContext(nameCamel, "initContainers", specMap, values)
if err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -55,9 +55,9 @@ func setSecContextValue(resourceName string, containerName string, castedContain
if err != nil {
return err
}

valueString := fmt.Sprintf(helmTemplate, resourceName, containerName)

err = unstructured.SetNestedField(castedContainer, valueString, sc)
if err != nil {
return err
Expand Down

0 comments on commit 03f8524

Please sign in to comment.