Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tests #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions cmd/helmify/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func ReadFlags() config.Config {
flag.BoolVar(&result.VeryVerbose, "vv", false, "Enable very verbose output. Same as verbose but with DEBUG. Example: helmify -vv")
flag.BoolVar(&crd, "crd-dir", false, "Enable crd install into 'crds' directory.\nWarning: CRDs placed in 'crds' directory will not be templated by Helm.\nSee https://helm.sh/docs/chart_best_practices/custom_resource_definitions/#some-caveats-and-explanations\nExample: helmify -crd-dir")
flag.BoolVar(&result.ImagePullSecrets, "image-pull-secrets", false, "Allows the user to use existing secrets as imagePullSecrets in values.yaml")
flag.BoolVar(&result.Probes, "probes", true, "Allows the user to customize liveness and readiness probes")

flag.Parse()
if h || help {
Expand Down
13 changes: 13 additions & 0 deletions examples/operator/templates/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Renders a value that contains template.
Usage:
{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $) }}
*/}}
{{- define "tplvalues.render" -}}
{{- if typeIs "string" .value }}
{{- tpl .value .context }}
{{- else }}
{{- tpl (.value | toYaml) .context }}
{{- end }}
{{- end -}}
11 changes: 10 additions & 1 deletion examples/operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,13 @@ spec:
name: manager-config
- name: secret-volume
secret:
secretName: {{ include "operator.fullname" . }}-secret-ca
secretName: {{ include "operator.fullname" . }}-secret-ca
{{- if .Values.topologySpreadConstraints }}
topologySpreadConstraints: {{- include "tplvalues.render" (dict "value" .Values.topologySpreadConstraints "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.nodeSelector }}
nodeSelector: {{- include "tplvalues.render" ( dict "value" .Values.nodeSelector "context" $) | nindent 8 }}
{{- end }}
{{- if .Values.tolerations }}
tolerations: {{- include "tplvalues.render" (dict "value" .Values.tolerations "context" .) | nindent 8 }}
{{- end }}
18 changes: 18 additions & 0 deletions examples/operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ controllerManager:
image:
repository: controller
tag: latest
livenessProbe:
httpGet:
path: /healthz
port: 8081
initialDelaySeconds: 15
periodSeconds: 20
readinessProbe:
httpGet:
path: /readyz
port: 8081
initialDelaySeconds: 5
periodSeconds: 10
resources:
limits:
cpu: 100m
Expand Down Expand Up @@ -40,6 +52,7 @@ metricsService:
port: 8443
targetPort: https
type: ClusterIP
nodeSelector: {}
pvc:
pvcLim:
storageClass: cust1-mypool-lim
Expand All @@ -51,6 +64,11 @@ secretRegistryCredentials:
secretVars:
var1: ""
var2: ""
tolerations: []
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
webhookService:
ports:
- port: 443
Expand Down
2 changes: 2 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Config struct {
Crd bool
// ImagePullSecrets flag
ImagePullSecrets bool
// Probes flag if true the probes will be parametrised
Probes bool
}

func (c *Config) Validate() error {
Expand Down
14 changes: 14 additions & 0 deletions pkg/helm/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ Create the name of the service account to use
{{- default "default" .Values.serviceAccount.name }}
{{- end }}
{{- end }}

{{/*
Renders a value that contains template.
Usage:
{{ include "tplvalues.render" ( dict "value" .Values.path.to.the.Value "context" $) }}
*/}}
{{- define "tplvalues.render" -}}
{{- if typeIs "string" .value }}
{{- tpl .value .context }}
{{- else }}
{{- tpl (.value | toYaml) .context }}
{{- end }}
{{- end -}}

`

const defaultChartfile = `apiVersion: v2
Expand Down
4 changes: 3 additions & 1 deletion pkg/helmify/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package helmify

import (
"github.com/arttor/helmify/pkg/config"
"io"

"github.com/arttor/helmify/pkg/config"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

Expand Down Expand Up @@ -43,6 +44,7 @@ type AppMetadata interface {
TemplatedName(objName string) string
// TemplatedString converts a string to templated string with chart name.
TemplatedString(str string) string
TemplatedValue(container string, str string) string
// TrimName trims common prefix from object name if exists.
// We trim common prefix because helm already using release for this purpose.
TrimName(objName string) string
Expand Down
10 changes: 9 additions & 1 deletion pkg/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ package metadata

import (
"fmt"
"github.com/arttor/helmify/pkg/config"
"strings"

"github.com/arttor/helmify/pkg/config"

"github.com/arttor/helmify/pkg/helmify"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)

const valuesTempl = `{{- include "tplvalues.render" (dict "value" .Values.%s.%s.%s "context" $)}}`

const nameTeml = `{{ include "%s.fullname" . }}-%s`

var nsGVK = schema.GroupVersionKind{
Expand Down Expand Up @@ -96,6 +99,11 @@ func (a *Service) TemplatedString(str string) string {
return fmt.Sprintf(nameTeml, a.conf.ChartName, name)
}

func (a *Service) TemplatedValue(container string, str string) string {
name := a.TrimName(str)
return fmt.Sprintf(valuesTempl, a.conf.ChartName, container, name)
}

func extractAppNamespace(obj *unstructured.Unstructured) string {
if obj.GroupVersionKind() == nsGVK {
return obj.GetName()
Expand Down
48 changes: 48 additions & 0 deletions pkg/processor/constraints/constraint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package constraints

import (
"github.com/arttor/helmify/pkg/helmify"
corev1 "k8s.io/api/core/v1"
)

const tolerations = "tolerations"
const topology = "topologySpreadConstraints"
const nodeSelector = "nodeSelector"

const topologyExpression = "\n{{- if .Values.topologySpreadConstraints }}\n" +
" topologySpreadConstraints: {{- include \"tplvalues.render\" (dict \"value\" .Values.topologySpreadConstraints \"context\" $) | nindent 8 }}\n" +
"{{- end }}\n"

const nodeSelectorExpression = "{{- if .Values.nodeSelector }}\n" +
" nodeSelector: {{- include \"tplvalues.render\" ( dict \"value\" .Values.nodeSelector \"context\" $) | nindent 8 }}\n" +
"{{- end }}\n"

const tolerationsExpression = "{{- if .Values.tolerations }}\n" +
" tolerations: {{- include \"tplvalues.render\" (dict \"value\" .Values.tolerations \"context\" .) | nindent 8 }}\n" +
"{{- end }}\n"

// ProcessSpecMap adds 'topologyConstraints' to the podSpec in specMap, if it doesn't
// already has one defined.
func ProcessSpecMap(specMap string, values *helmify.Values, podspec corev1.PodSpec) string {

(*values)[topology] = podspec.TopologySpreadConstraints
(*values)[nodeSelector] = podspec.NodeSelector
(*values)[tolerations] = podspec.Tolerations

tp := (*values)[topology].([]corev1.TopologySpreadConstraint)
if len(tp) == 0 {
(*values)[topology] = []interface{}{}
}
ns := (*values)[nodeSelector].(map[string]string)
if len(ns) == 0 {
(*values)[nodeSelector] = map[string]string{}
}

tl := (*values)[tolerations].([]corev1.Toleration)

if len(tl) == 0 {
(*values)[tolerations] = []interface{}{}
}

return specMap + topologyExpression + nodeSelectorExpression + tolerationsExpression
}
76 changes: 76 additions & 0 deletions pkg/processor/constraints/constraint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package constraints

import (
"testing"

"github.com/arttor/helmify/pkg/helmify"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
)

const templatedResult = "{{- if .Values.topologySpreadConstraints }}\n" +
" topologySpreadConstraints: {{- include \"tplvalues.render\" (dict \"value\" .Values.topologySpreadConstraints \"context\" $) | nindent 8 }}\n" +
"{{- end }}\n" +
"{{- if .Values.nodeSelector }}\n" +
" nodeSelector: {{- include \"tplvalues.render\" ( dict \"value\" .Values.nodeSelector \"context\" $) | nindent 8 }}\n" +
"{{- end }}\n" +
"{{- if .Values.tolerations }}\n" +
" tolerations: {{- include \"tplvalues.render\" (dict \"value\" .Values.tolerations \"context\" .) | nindent 8 }}\n" +
"{{- end }}\n"

func TestProcessSpecMap(t *testing.T) {

tests := []struct {
name string
specMap string
values *helmify.Values
podspec v1.PodSpec
want string
wantValues *helmify.Values
}{
{name: "no predefined resource returns still a template and to fill in values",
specMap: "",
values: &helmify.Values{},
want: templatedResult,
wantValues: &helmify.Values{
"nodeSelector": map[string]string{},
"tolerations": []interface{}{},
"topologySpreadConstraints": []interface{}{},
},
},
{name: "predefined resource are added to values, template is the same",
specMap: "",
values: &helmify.Values{},
podspec: v1.PodSpec{
TopologySpreadConstraints: []v1.TopologySpreadConstraint{
{
MaxSkew: 0,
TopologyKey: "trtr",
WhenUnsatisfiable: "test",
LabelSelector: nil,
},
},
},
want: templatedResult,
wantValues: &helmify.Values{
"nodeSelector": map[string]string{},
"tolerations": []interface{}{},
"topologySpreadConstraints": []v1.TopologySpreadConstraint{
{
MaxSkew: 0,
TopologyKey: "trtr",
WhenUnsatisfiable: "test",
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ProcessSpecMap(tt.specMap, tt.values, tt.podspec)
require.Contains(t, got, tt.want)

require.Equal(t, tt.wantValues, tt.values)
})
}
}
Loading