From 77a82ba0f9d3ec421728affd767dfa8e64fc3868 Mon Sep 17 00:00:00 2001 From: Leonardo Milleri Date: Mon, 21 Nov 2022 12:17:40 +0000 Subject: [PATCH] vDPA webhook validation Signed-off-by: Leonardo Milleri --- pkg/webhook/validate.go | 9 ++++++- pkg/webhook/validate_test.go | 49 +++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pkg/webhook/validate.go b/pkg/webhook/validate.go index bb053c42f1..5c66752ffa 100644 --- a/pkg/webhook/validate.go +++ b/pkg/webhook/validate.go @@ -150,12 +150,19 @@ func staticValidateSriovNetworkNodePolicy(cr *sriovnetworkv1.SriovNetworkNodePol // To configure RoCE on baremetal or virtual machine: // BM: DeviceType = netdevice && isRdma = true // VM: DeviceType = vfio-pci && isRdma = false - if cr.Spec.DeviceType == "vfio-pci" && cr.Spec.IsRdma { + if cr.Spec.DeviceType == constants.DeviceTypeVfioPci && cr.Spec.IsRdma { return false, fmt.Errorf("'deviceType: vfio-pci' conflicts with 'isRdma: true'; Set 'deviceType' to (string)'netdevice' Or Set 'isRdma' to (bool)'false'") } if strings.EqualFold(cr.Spec.LinkType, constants.LinkTypeIB) && !cr.Spec.IsRdma { return false, fmt.Errorf("'linkType: ib or IB' requires 'isRdma: true'; Set 'isRdma' to (bool)'true'") } + // vdpa specific validation + if cr.Spec.VdpaType == constants.VdpaTypeVHost { + return false, fmt.Errorf("'vdpaType: vhost' is not supported yet") + } + if cr.Spec.DeviceType == constants.DeviceTypeVfioPci && cr.Spec.VdpaType == constants.VdpaTypeVirtio { + return false, fmt.Errorf("'deviceType: vfio-pci' conflicts with 'vdpaType: virtio'; Set 'deviceType' to (string)'netdevice' Or Remove 'vdpaType'") + } return true, nil } diff --git a/pkg/webhook/validate_test.go b/pkg/webhook/validate_test.go index e6a6690f55..4e41357206 100644 --- a/pkg/webhook/validate_test.go +++ b/pkg/webhook/validate_test.go @@ -8,6 +8,7 @@ import ( corev1 "k8s.io/api/core/v1" . "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" + constants "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" . "github.com/onsi/gomega" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" @@ -407,7 +408,7 @@ func TestStaticValidateSriovNetworkNodePolicyWithInvalidVendorDevice(t *testing. func TestStaticValidateSriovNetworkNodePolicyWithConflictIsRdmaAndDeviceType(t *testing.T) { policy := &SriovNetworkNodePolicy{ Spec: SriovNetworkNodePolicySpec{ - DeviceType: "vfio-pci", + DeviceType: constants.DeviceTypeVfioPci, NicSelector: SriovNetworkNicSelector{ Vendor: "8086", DeviceID: "158b", @@ -427,6 +428,52 @@ func TestStaticValidateSriovNetworkNodePolicyWithConflictIsRdmaAndDeviceType(t * g.Expect(ok).To(Equal(false)) } +func TestStaticValidateSriovNetworkNodePolicyWithConflictDeviceTypeAndVdpaType(t *testing.T) { + policy := &SriovNetworkNodePolicy{ + Spec: SriovNetworkNodePolicySpec{ + DeviceType: constants.DeviceTypeVfioPci, + NicSelector: SriovNetworkNicSelector{ + Vendor: "8086", + DeviceID: "158b", + }, + NodeSelector: map[string]string{ + "feature.node.kubernetes.io/network-sriov.capable": "true", + }, + NumVfs: 1, + Priority: 99, + ResourceName: "p0", + VdpaType: constants.VdpaTypeVirtio, + }, + } + g := NewGomegaWithT(t) + ok, err := staticValidateSriovNetworkNodePolicy(policy) + g.Expect(err).To(MatchError(ContainSubstring("'deviceType: vfio-pci' conflicts with 'vdpaType: virtio'"))) + g.Expect(ok).To(Equal(false)) +} + +func TestStaticValidateSriovNetworkNodePolicyVdpaTypeVHostNotSupported(t *testing.T) { + policy := &SriovNetworkNodePolicy{ + Spec: SriovNetworkNodePolicySpec{ + DeviceType: "netdevice", + NicSelector: SriovNetworkNicSelector{ + Vendor: "8086", + DeviceID: "158b", + }, + NodeSelector: map[string]string{ + "feature.node.kubernetes.io/network-sriov.capable": "true", + }, + NumVfs: 1, + Priority: 99, + ResourceName: "p0", + VdpaType: constants.VdpaTypeVHost, + }, + } + g := NewGomegaWithT(t) + ok, err := staticValidateSriovNetworkNodePolicy(policy) + g.Expect(err).To(MatchError(ContainSubstring("'vdpaType: vhost' is not supported yet"))) + g.Expect(ok).To(Equal(false)) +} + func TestValidatePolicyForNodeStateWithInvalidDevice(t *testing.T) { state := newNodeState() policy := &SriovNetworkNodePolicy{