Skip to content

Commit

Permalink
vDPA webhook validation
Browse files Browse the repository at this point in the history
Signed-off-by: Leonardo Milleri <[email protected]>
  • Loading branch information
lmilleri committed Jan 24, 2023
1 parent 110adc2 commit 77a82ba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/webhook/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
49 changes: 48 additions & 1 deletion pkg/webhook/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand All @@ -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{
Expand Down

0 comments on commit 77a82ba

Please sign in to comment.