Skip to content

Commit

Permalink
feat: introduce topologyConstraints, node selectors and tolerations
Browse files Browse the repository at this point in the history
Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat:  added  constraints

Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat: introduce topologyConstraints, node selectors and tolerations

Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat: added node selector and tolerations

Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat: added node selector and tolerations

Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat: added topologyConstraint

Signed-off-by: RealAnna <anna.reale@dynatrace.com>

feat: introduce topologyConstraints

Signed-off-by: RealAnna <anna.reale@dynatrace.com>
  • Loading branch information
RealAnna committed Feb 20, 2023
1 parent 2351ee0 commit 5a604fe
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 5 deletions.
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 @@ -109,4 +109,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 }}
6 changes: 6 additions & 0 deletions examples/operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ metricsService:
port: 8443
targetPort: https
type: ClusterIP
nodeSelector: {}
pvc:
pvcLim:
storageClass: cust1-mypool-lim
Expand All @@ -53,6 +54,11 @@ secretRegistryCredentials:
secretVars:
var1: ""
var2: ""
tolerations: []
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
webhookService:
ports:
- port: 443
Expand Down
46 changes: 46 additions & 0 deletions pkg/processor/constraints/constraint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package constraints

import (
"github.com/arttor/helmify/pkg/helmify"
yamlformat "github.com/arttor/helmify/pkg/yaml"
)

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 map[string]interface{}, values *helmify.Values) string {

mapConstraint(specMap, topology, []interface{}{}, values)
mapConstraint(specMap, tolerations, []interface{}{}, values)
mapConstraint(specMap, nodeSelector, map[string]string{}, values)

spec, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return ""
}
return spec + topologyExpression + nodeSelectorExpression + tolerationsExpression
}

func mapConstraint(specMap map[string]interface{}, constraint string, override interface{}, values *helmify.Values) {
if specMap[constraint] != nil {
(*values)[constraint] = specMap[constraint].(interface{})
} else {
(*values)[constraint] = override
}
delete(specMap, constraint)
}
74 changes: 74 additions & 0 deletions pkg/processor/constraints/constraint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 map[string]interface{}
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: make(map[string]interface{}, 4),
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",
values: &helmify.Values{},
specMap: map[string]interface{}{
"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)
require.Contains(t, got, tt.want)
require.Equal(t, tt.wantValues, tt.values)
})
}
}
6 changes: 2 additions & 4 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/arttor/helmify/pkg/cluster"
"github.com/arttor/helmify/pkg/processor"
"github.com/arttor/helmify/pkg/processor/constraints"
"github.com/arttor/helmify/pkg/processor/imagePullSecrets"

"github.com/arttor/helmify/pkg/helmify"
Expand Down Expand Up @@ -162,10 +163,7 @@ func (d deployment) Process(appMeta helmify.AppMetadata, obj *unstructured.Unstr
imagePullSecrets.ProcessSpecMap(specMap, &values)
}

spec, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return true, nil, err
}
spec := constraints.ProcessSpecMap(specMap, &values)
spec = strings.ReplaceAll(spec, "'", "")

return true, &result{
Expand Down
7 changes: 7 additions & 0 deletions test_data/k8s-operator-kustomize.output
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,13 @@ spec:
labels:
control-plane: controller-manager
spec:
topologySpreadConstraints:
- maxSkew: 1
topologyKey: kubernetes.io/hostname
whenUnsatisfiable: DoNotSchedule
matchLabelKeys:
- app
- pod-template-hash
imagePullSecrets:
- name: my-operator-secret-registry-credentials
containers:
Expand Down

0 comments on commit 5a604fe

Please sign in to comment.