Skip to content
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 examples/app/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ spec:
app: myapp
{{- include "app.selectorLabels" . | nindent 8 }}
spec:
affinity: {{- toYaml .Values.myapp.affinity | nindent 8 }}
containers:
- args: {{- toYaml .Values.myapp.app.args | nindent 8 }}
command:
Expand Down
7 changes: 7 additions & 0 deletions examples/app/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ mySecretVars:
var1: ""
var2: ""
myapp:
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists
app:
args:
- --health-probe-bind-address=:8081
Expand Down
16 changes: 16 additions & 0 deletions pkg/processor/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ func ProcessSpec(objName string, appMeta helmify.AppMetadata, spec corev1.PodSpe
}
}

// process affinity if presented:
if spec.Affinity != nil {
err = unstructured.SetNestedField(specMap, fmt.Sprintf(`{{- toYaml .Values.%s.affinity | nindent %d }}`, objName, nindent), "affinity")
if err != nil {
return nil, nil, err
}
affinityMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(spec.Affinity)
if err != nil {
return nil, nil, err
}
err = unstructured.SetNestedField(values, affinityMap, objName, "affinity")
if err != nil {
return nil, nil, err
}
}

// process tolerations if presented:
err = unstructured.SetNestedField(specMap, fmt.Sprintf(`{{- toYaml .Values.%s.tolerations | nindent %d }}`, objName, nindent), "tolerations")
if err != nil {
Expand Down
85 changes: 85 additions & 0 deletions pkg/processor/pod/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,35 @@ spec:
runAsNonRoot: true
runAsUser: 65532

`
strDeploymentWithAffinity = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: localhost:6001/my_project:latest
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists

`
)

Expand Down Expand Up @@ -376,5 +405,61 @@ func Test_pod_Process(t *testing.T) {
},
}, tmpl)
})
t.Run("deployment with affinity", func(t *testing.T) {
var deploy appsv1.Deployment
obj := internal.GenerateObj(strDeploymentWithAffinity)
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &deploy)
specMap, tmpl, err := ProcessSpec("nginx", &metadata.Service{}, deploy.Spec.Template.Spec, 0)
assert.NoError(t, err)
assert.Equal(t, map[string]interface{}{
"containers": []interface{}{
map[string]interface{}{
"env": []interface{}{
map[string]interface{}{
"name": "KUBERNETES_CLUSTER_DOMAIN",
"value": "{{ quote .Values.kubernetesClusterDomain }}",
},
},
"image": "{{ .Values.nginx.nginx.image.repository }}:{{ .Values.nginx.nginx.image.tag | default .Chart.AppVersion }}",
"name": "nginx",
"resources": map[string]interface{}{},
},
},
"nodeSelector": "{{- toYaml .Values.nginx.nodeSelector | nindent 8 }}",
"serviceAccountName": `{{ include ".serviceAccountName" . }}`,
"tolerations": "{{- toYaml .Values.nginx.tolerations | nindent 8 }}",
"topologySpreadConstraints": "{{- toYaml .Values.nginx.topologySpreadConstraints | nindent 8 }}",
"affinity": "{{- toYaml .Values.nginx.affinity | nindent 8 }}",
}, specMap)

assert.Equal(t, helmify.Values{
"nginx": map[string]interface{}{
"affinity": map[string]interface{}{
"nodeAffinity": map[string]interface{}{
"requiredDuringSchedulingIgnoredDuringExecution": map[string]interface{}{
"nodeSelectorTerms": []interface{}{
map[string]interface{}{
"matchExpressions": []interface{}{
map[string]interface{}{
"key": "node-role.kubernetes.io/control-plane",
"operator": "Exists",
},
},
},
},
},
},
},
"nginx": map[string]interface{}{
"image": map[string]interface{}{
"repository": "localhost:6001/my_project",
"tag": "latest",
},
},
"nodeSelector": map[string]interface{}{},
"tolerations": []interface{}{},
"topologySpreadConstraints": []interface{}{},
},
}, tmpl)
})
}
7 changes: 7 additions & 0 deletions test_data/sample-app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ spec:
nodeSelector:
region: east
type: user-node
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: node-role.kubernetes.io/control-plane
operator: Exists
terminationGracePeriodSeconds: 10
volumes:
- configMap:
Expand Down