-
Notifications
You must be signed in to change notification settings - Fork 139
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
feat: add constraints #77
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5067160
feat: introduce topologyConstraints, node selectors and tolerations
RealAnna c071c56
feat: added constraints
RealAnna 12d38e7
fix: tolerations and other constraint under deployment name
RealAnna e4d3d6e
fix: tolerations and other constraint under deployment name
RealAnna 764a7de
hide autofill behind CLI flag
RealAnna 44599c8
fix test
RealAnna 0dee285
fix test
RealAnna c283169
fix according to review
RealAnna 652f30c
fix according to review
RealAnna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,67 @@ | ||
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(name string, specMap map[string]interface{}, values *helmify.Values, defaultEmpty bool) string { | ||
|
||
spec, err := yamlformat.Marshal(specMap, 6) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
if defaultEmpty { | ||
mapConstraintWithEmpty(name, specMap, topology, []interface{}{}, values) | ||
mapConstraintWithEmpty(name, specMap, tolerations, []interface{}{}, values) | ||
mapConstraintWithEmpty(name, specMap, nodeSelector, map[string]string{}, values) | ||
return spec + topologyExpression + nodeSelectorExpression + tolerationsExpression | ||
} | ||
|
||
if specMap[topology] != nil { | ||
(*values)[name].(map[string]interface{})[topology] = specMap[topology].(interface{}) | ||
delete(specMap, topology) | ||
spec += topologyExpression | ||
} | ||
|
||
if specMap[tolerations] != nil { | ||
(*values)[name].(map[string]interface{})[tolerations] = specMap[tolerations].(interface{}) | ||
delete(specMap, tolerations) | ||
spec += tolerationsExpression | ||
} | ||
if specMap[nodeSelector] != nil { | ||
(*values)[name].(map[string]interface{})[nodeSelector] = specMap[nodeSelector].(interface{}) | ||
delete(specMap, nodeSelector) | ||
spec += nodeSelectorExpression | ||
} | ||
|
||
return spec | ||
} | ||
|
||
func mapConstraintWithEmpty(name string, specMap map[string]interface{}, constraint string, override interface{}, values *helmify.Values) { | ||
if specMap[constraint] != nil { | ||
(*values)[name].(map[string]interface{})[constraint] = specMap[constraint].(interface{}) | ||
} else { | ||
(*values)[name].(map[string]interface{})[constraint] = override | ||
arttor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
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,83 @@ | ||
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{ | ||
"mydep": map[string]interface{}{}, | ||
}, | ||
want: templatedResult, | ||
wantValues: &helmify.Values{ | ||
"mydep": map[string]interface{}{ | ||
"nodeSelector": map[string]string{}, | ||
"tolerations": []interface{}{}, | ||
"topologySpreadConstraints": []interface{}{}, | ||
}, | ||
}, | ||
}, | ||
{name: "predefined resource are added to values, template is the same", | ||
values: &helmify.Values{ | ||
"mydep": map[string]interface{}{}, | ||
}, | ||
specMap: map[string]interface{}{ | ||
|
||
"topologySpreadConstraints": []v1.TopologySpreadConstraint{ | ||
{ | ||
MaxSkew: 0, | ||
TopologyKey: "trtr", | ||
WhenUnsatisfiable: "test", | ||
LabelSelector: nil, | ||
}, | ||
}, | ||
}, | ||
want: templatedResult, | ||
wantValues: &helmify.Values{ | ||
"mydep": map[string]interface{}{ | ||
"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("mydep", tt.specMap, tt.values, true) | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are 2
topologySpreadConstraints
. Here and on line 107There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry @RealAnna i had to revert constraints feature in this commit because of duplicates in specs. Commit also contains a template for
nodeSelector
which can be used as an example for other specs.