Skip to content

Commit c175998

Browse files
authored
Generate correct node affinity if no values are passed (#1557)
* Generate correct node affinity if no values are passed Signed-off-by: Marvin Beckers <marvin@kubermatic.com> * Add test for affinity without values Signed-off-by: Marvin Beckers <marvin@kubermatic.com> * Fix yamllint complaint Signed-off-by: Marvin Beckers <marvin@kubermatic.com> --------- Signed-off-by: Marvin Beckers <marvin@kubermatic.com>
1 parent 86f2a37 commit c175998

File tree

3 files changed

+108
-17
lines changed

3 files changed

+108
-17
lines changed

pkg/cloudprovider/provider/kubevirt/provider.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,19 @@ func getDataVolumeTemplates(config *Config, dataVolumeName string) []kubevirtv1.
852852
func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
853853
affinity := &corev1.Affinity{}
854854

855+
expressions := []corev1.NodeSelectorRequirement{
856+
{
857+
Key: config.NodeAffinityPreset.Key,
858+
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpExists),
859+
},
860+
}
861+
862+
// change the operator if any values were passed for node affinity matching
863+
if len(config.NodeAffinityPreset.Values) > 0 {
864+
expressions[0].Operator = corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn)
865+
expressions[0].Values = config.NodeAffinityPreset.Values
866+
}
867+
855868
// NodeAffinity
856869
switch config.NodeAffinityPreset.Type {
857870
case softAffinityType:
@@ -860,13 +873,7 @@ func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
860873
{
861874
Weight: 1,
862875
Preference: corev1.NodeSelectorTerm{
863-
MatchExpressions: []corev1.NodeSelectorRequirement{
864-
{
865-
Key: config.NodeAffinityPreset.Key,
866-
Values: config.NodeAffinityPreset.Values,
867-
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn),
868-
},
869-
},
876+
MatchExpressions: expressions,
870877
},
871878
},
872879
},
@@ -876,13 +883,7 @@ func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
876883
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
877884
NodeSelectorTerms: []corev1.NodeSelectorTerm{
878885
{
879-
MatchExpressions: []corev1.NodeSelectorRequirement{
880-
{
881-
Key: config.NodeAffinityPreset.Key,
882-
Values: config.NodeAffinityPreset.Values,
883-
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn),
884-
},
885-
},
886+
MatchExpressions: expressions,
886887
},
887888
},
888889
},

pkg/cloudprovider/provider/kubevirt/provider_test.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type kubevirtProviderSpecConf struct {
6060
OperatingSystem string
6161
TopologySpreadConstraint bool
6262
Affinity bool
63+
AffinityValues bool
6364
SecondaryDisks bool
6465
OsImageSource imageSource
6566
}
@@ -86,9 +87,11 @@ func (k kubevirtProviderSpecConf) rawProviderSpec(t *testing.T) []byte {
8687
"affinity": {
8788
"nodeAffinityPreset": {
8889
"type": "hard",
89-
"key": "key1",
90-
"values": [
90+
"key": "key1"
91+
{{- if .AffinityValues }}
92+
, "values": [
9193
"foo1", "foo2" ]
94+
{{- end }}
9295
}
9396
},
9497
{{- end }}
@@ -194,7 +197,11 @@ func TestNewVirtualMachine(t *testing.T) {
194197
},
195198
{
196199
name: "affinity",
197-
specConf: kubevirtProviderSpecConf{Affinity: true},
200+
specConf: kubevirtProviderSpecConf{Affinity: true, AffinityValues: true},
201+
},
202+
{
203+
name: "affinity-no-values",
204+
specConf: kubevirtProviderSpecConf{Affinity: true, AffinityValues: false},
198205
},
199206
{
200207
name: "secondary-disks",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
apiVersion: kubevirt.io/v1
2+
kind: VirtualMachine
3+
metadata:
4+
annotations:
5+
labels:
6+
cluster.x-k8s.io/cluster-name: cluster-name
7+
cluster.x-k8s.io/role: worker
8+
kubevirt.io/vm: affinity-no-values
9+
md: md-name
10+
name: affinity-no-values
11+
namespace: test-namespace
12+
spec:
13+
dataVolumeTemplates:
14+
- metadata:
15+
creationTimestamp: null
16+
name: affinity-no-values
17+
spec:
18+
pvc:
19+
accessModes:
20+
- ReadWriteOnce
21+
resources:
22+
requests:
23+
storage: 10Gi
24+
storageClassName: longhorn
25+
source:
26+
http:
27+
url: http://x.y.z.t/ubuntu.img
28+
running: true
29+
template:
30+
metadata:
31+
labels:
32+
cluster.x-k8s.io/cluster-name: cluster-name
33+
cluster.x-k8s.io/role: worker
34+
kubevirt.io/vm: affinity-no-values
35+
md: md-name
36+
spec:
37+
affinity:
38+
nodeAffinity: # Section present if nodeAffinityPreset.type != ""
39+
requiredDuringSchedulingIgnoredDuringExecution:
40+
nodeSelectorTerms:
41+
- matchExpressions:
42+
- key: key1
43+
operator: Exists
44+
domain:
45+
devices:
46+
disks:
47+
- disk:
48+
bus: virtio
49+
name: datavolumedisk
50+
- disk:
51+
bus: virtio
52+
name: cloudinitdisk
53+
interfaces:
54+
- macAddress: b6:f5:b4:fe:45:1d
55+
name: default
56+
bridge: {}
57+
resources:
58+
limits:
59+
cpu: "2"
60+
memory: 2Gi
61+
requests:
62+
cpu: "2"
63+
memory: 2Gi
64+
networks:
65+
- name: default
66+
pod: {}
67+
terminationGracePeriodSeconds: 30
68+
topologyspreadconstraints:
69+
- maxskew: 1
70+
topologykey: kubernetes.io/hostname
71+
whenunsatisfiable: ScheduleAnyway
72+
labelselector:
73+
matchlabels:
74+
md: md-name
75+
volumes:
76+
- dataVolume:
77+
name: affinity-no-values
78+
name: datavolumedisk
79+
- cloudInitNoCloud:
80+
secretRef:
81+
name: udsn
82+
name: cloudinitdisk
83+
evictionStrategy: External

0 commit comments

Comments
 (0)