-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce topologyConstraints, node selectors and tolerations
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
Showing
7 changed files
with
158 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters