Skip to content

Commit

Permalink
feat: added constraints
Browse files Browse the repository at this point in the history
Signed-off-by: RealAnna <anna.reale@dynatrace.com>
  • Loading branch information
RealAnna committed Feb 20, 2023
1 parent 547164e commit 4915534
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
34 changes: 16 additions & 18 deletions pkg/processor/constraints/constraint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package constraints

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

const tolerations = "tolerations"
Expand All @@ -23,26 +23,24 @@ const tolerationsExpression = "{{- if .Values.tolerations }}\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 {
func ProcessSpecMap(specMap map[string]interface{}, values *helmify.Values) string {

(*values)[topology] = podspec.TopologySpreadConstraints
(*values)[nodeSelector] = podspec.NodeSelector
(*values)[tolerations] = podspec.Tolerations
mapConstraint(specMap, topology, []interface{}{}, values)
mapConstraint(specMap, tolerations, []interface{}{}, values)
mapConstraint(specMap, nodeSelector, map[string]string{}, values)

tp := (*values)[topology].([]corev1.TopologySpreadConstraint)
if len(tp) == 0 {
(*values)[topology] = []interface{}{}
spec, err := yamlformat.Marshal(specMap, 6)
if err != nil {
return ""
}
ns := (*values)[nodeSelector].(map[string]string)
if len(ns) == 0 {
(*values)[nodeSelector] = map[string]string{}
}

tl := (*values)[tolerations].([]corev1.Toleration)
return spec + topologyExpression + nodeSelectorExpression + tolerationsExpression
}

if len(tl) == 0 {
(*values)[tolerations] = []interface{}{}
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
}

return specMap + topologyExpression + nodeSelectorExpression + tolerationsExpression
delete(specMap, constraint)
}
5 changes: 1 addition & 4 deletions pkg/processor/deployment/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,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

0 comments on commit 4915534

Please sign in to comment.