Skip to content

Commit

Permalink
Generate correct node affinity if no values are passed (#1557)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
embik committed Feb 8, 2023
1 parent 86f2a37 commit c175998
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 17 deletions.
29 changes: 15 additions & 14 deletions pkg/cloudprovider/provider/kubevirt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,19 @@ func getDataVolumeTemplates(config *Config, dataVolumeName string) []kubevirtv1.
func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
affinity := &corev1.Affinity{}

expressions := []corev1.NodeSelectorRequirement{
{
Key: config.NodeAffinityPreset.Key,
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpExists),
},
}

// change the operator if any values were passed for node affinity matching
if len(config.NodeAffinityPreset.Values) > 0 {
expressions[0].Operator = corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn)
expressions[0].Values = config.NodeAffinityPreset.Values
}

// NodeAffinity
switch config.NodeAffinityPreset.Type {
case softAffinityType:
Expand All @@ -860,13 +873,7 @@ func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
{
Weight: 1,
Preference: corev1.NodeSelectorTerm{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: config.NodeAffinityPreset.Key,
Values: config.NodeAffinityPreset.Values,
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn),
},
},
MatchExpressions: expressions,
},
},
},
Expand All @@ -876,13 +883,7 @@ func getAffinity(config *Config, matchKey, matchValue string) *corev1.Affinity {
RequiredDuringSchedulingIgnoredDuringExecution: &corev1.NodeSelector{
NodeSelectorTerms: []corev1.NodeSelectorTerm{
{
MatchExpressions: []corev1.NodeSelectorRequirement{
{
Key: config.NodeAffinityPreset.Key,
Values: config.NodeAffinityPreset.Values,
Operator: corev1.NodeSelectorOperator(metav1.LabelSelectorOpIn),
},
},
MatchExpressions: expressions,
},
},
},
Expand Down
13 changes: 10 additions & 3 deletions pkg/cloudprovider/provider/kubevirt/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type kubevirtProviderSpecConf struct {
OperatingSystem string
TopologySpreadConstraint bool
Affinity bool
AffinityValues bool
SecondaryDisks bool
OsImageSource imageSource
}
Expand All @@ -86,9 +87,11 @@ func (k kubevirtProviderSpecConf) rawProviderSpec(t *testing.T) []byte {
"affinity": {
"nodeAffinityPreset": {
"type": "hard",
"key": "key1",
"values": [
"key": "key1"
{{- if .AffinityValues }}
, "values": [
"foo1", "foo2" ]
{{- end }}
}
},
{{- end }}
Expand Down Expand Up @@ -194,7 +197,11 @@ func TestNewVirtualMachine(t *testing.T) {
},
{
name: "affinity",
specConf: kubevirtProviderSpecConf{Affinity: true},
specConf: kubevirtProviderSpecConf{Affinity: true, AffinityValues: true},
},
{
name: "affinity-no-values",
specConf: kubevirtProviderSpecConf{Affinity: true, AffinityValues: false},
},
{
name: "secondary-disks",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
annotations:
labels:
cluster.x-k8s.io/cluster-name: cluster-name
cluster.x-k8s.io/role: worker
kubevirt.io/vm: affinity-no-values
md: md-name
name: affinity-no-values
namespace: test-namespace
spec:
dataVolumeTemplates:
- metadata:
creationTimestamp: null
name: affinity-no-values
spec:
pvc:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
storageClassName: longhorn
source:
http:
url: http://x.y.z.t/ubuntu.img
running: true
template:
metadata:
labels:
cluster.x-k8s.io/cluster-name: cluster-name
cluster.x-k8s.io/role: worker
kubevirt.io/vm: affinity-no-values
md: md-name
spec:
affinity:
nodeAffinity: # Section present if nodeAffinityPreset.type != ""
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: key1
operator: Exists
domain:
devices:
disks:
- disk:
bus: virtio
name: datavolumedisk
- disk:
bus: virtio
name: cloudinitdisk
interfaces:
- macAddress: b6:f5:b4:fe:45:1d
name: default
bridge: {}
resources:
limits:
cpu: "2"
memory: 2Gi
requests:
cpu: "2"
memory: 2Gi
networks:
- name: default
pod: {}
terminationGracePeriodSeconds: 30
topologyspreadconstraints:
- maxskew: 1
topologykey: kubernetes.io/hostname
whenunsatisfiable: ScheduleAnyway
labelselector:
matchlabels:
md: md-name
volumes:
- dataVolume:
name: affinity-no-values
name: datavolumedisk
- cloudInitNoCloud:
secretRef:
name: udsn
name: cloudinitdisk
evictionStrategy: External

0 comments on commit c175998

Please sign in to comment.