Skip to content
Draft
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
128 changes: 128 additions & 0 deletions api/v1alpha/networkinterface_schema_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// SPDX-License-Identifier: AGPL-3.0-only

package v1alpha_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

networkingv1alpha "go.datum.net/network-services-operator/api/v1alpha"
)

func physicalInterfaceSpec() networkingv1alpha.NetworkInterfaceSpec {
return networkingv1alpha.NetworkInterfaceSpec{
Network: networkingv1alpha.LocalNetworkRef{Name: "app"},
AttachmentMode: networkingv1alpha.NetworkInterfaceAttachmentModePhysical,
AttachmentPoint: &networkingv1alpha.NetworkInterfaceAttachmentPoint{
MACAddress: "00:00:5e:00:53:01",
Switch: "rack-a1-tor-1",
Port: "Ethernet1/12",
},
}
}

func TestPhysicalInterfaceAdmission(t *testing.T) {
k8sClient := startSchemaEnv(t, "networkinterfaces")
ctx := context.Background()

tests := []struct {
name string
mutate func(*networkingv1alpha.NetworkInterfaceSpec)
valid bool
}{
{name: "physical with an attachment point", valid: true},
{name: "boot purpose and private isolation", valid: true, mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot
spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate
}},
{name: "physical without an attachment point", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.AttachmentPoint = nil
}},
{name: "attachment point on the default mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.AttachmentMode = ""
}},
{name: "purpose on another mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeNetns
spec.AttachmentPoint = nil
spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot
}},
{name: "isolation on another mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeHypervisor
spec.AttachmentPoint = nil
spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationShared
}},
}

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iface := &networkingv1alpha.NetworkInterface{
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("create-%d", i), Namespace: "default"},
Spec: physicalInterfaceSpec(),
}
if tt.mutate != nil {
tt.mutate(&iface.Spec)
}
err := k8sClient.Create(ctx, iface)
if tt.valid {
require.NoError(t, err)
return
}
require.Error(t, err)
})
}
}

func TestPhysicalInterfaceFieldsAreImmutable(t *testing.T) {
k8sClient := startSchemaEnv(t, "networkinterfaces")
ctx := context.Background()

tests := []struct {
name string
mutate func(*networkingv1alpha.NetworkInterfaceSpec)
}{
{name: "change the port", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.AttachmentPoint.Port = "Ethernet1/13"
}},
{name: "set the purpose", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot
}},
{name: "set the isolation", mutate: func(spec *networkingv1alpha.NetworkInterfaceSpec) {
spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate
}},
}

for i, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
iface := &networkingv1alpha.NetworkInterface{
ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("update-%d", i), Namespace: "default"},
Spec: physicalInterfaceSpec(),
}
require.NoError(t, k8sClient.Create(ctx, iface))

tt.mutate(&iface.Spec)
require.Error(t, k8sClient.Update(ctx, iface))
})
}
}

// The operator changes an existing interface's MTU, claim and mode, so the new
// rules must leave interfaces that carry none of the physical fields alone.
func TestExistingInterfacesStillUpdate(t *testing.T) {
k8sClient := startSchemaEnv(t, "networkinterfaces")
ctx := context.Background()

iface := &networkingv1alpha.NetworkInterface{
ObjectMeta: metav1.ObjectMeta{Name: "existing", Namespace: "default"},
Spec: networkingv1alpha.NetworkInterfaceSpec{Network: networkingv1alpha.LocalNetworkRef{Name: "app"}},
}
require.NoError(t, k8sClient.Create(ctx, iface))

iface.Spec.MTU = 1400
iface.Spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeHypervisor
iface.Spec.ClaimRef = &networkingv1alpha.NetworkInterfaceClaimRef{Name: "web-0-eth0"}
require.NoError(t, k8sClient.Update(ctx, iface))
}
91 changes: 90 additions & 1 deletion api/v1alpha/networkinterface_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const (
// by the consumer, carried by the operator, and acted on by whoever realizes
// the interface.
//
// +kubebuilder:validation:Enum=Netns;Hypervisor;HypervisorDeclared
// +kubebuilder:validation:Enum=Netns;Hypervisor;HypervisorDeclared;Physical
type NetworkInterfaceAttachmentMode string

const (
Expand All @@ -58,6 +58,69 @@ const (
// hypervisor directly. A guest whose hypervisor reads no node state needs
// this mode.
NetworkInterfaceAttachmentModeHypervisorDeclared NetworkInterfaceAttachmentMode = "HypervisorDeclared"

// NetworkInterfaceAttachmentModePhysical means the interface is a physical
// NIC that reaches its network through the switch port it is cabled to.
// There is no node, container or hypervisor, and the claim names the port
// in its attachment point.
NetworkInterfaceAttachmentModePhysical NetworkInterfaceAttachmentMode = "Physical"
)

// NetworkInterfaceAttachmentPoint names the physical NIC and the switch port it
// is cabled to. The provider that writes the claim reads it from its own
// inventory, and the operator copies it without checking it.
type NetworkInterfaceAttachmentPoint struct {
// macAddress is the NIC's MAC address, as six colon-separated hexadecimal
// pairs.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:Pattern=`^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$`
MACAddress string `json:"macAddress"`

// switch is the switch the NIC is cabled to, in the name network operations
// uses for it.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Switch string `json:"switch"`

// port is the switch port the NIC is cabled to, in the name network
// operations uses for it. It may hold characters a Kubernetes name cannot,
// such as the slash in Ethernet1/12.
//
// +kubebuilder:validation:Required
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=253
Port string `json:"port"`
}

// NetworkInterfacePurpose sets what a physical claim is for.
//
// +kubebuilder:validation:Enum=Boot
type NetworkInterfacePurpose string

const (
// NetworkInterfacePurposeBoot marks a claim that attaches its port to a
// boot network for as long as the claim exists, ahead of any other claim on
// the port.
NetworkInterfacePurposeBoot NetworkInterfacePurpose = "Boot"
)

// NetworkInterfaceIsolation sets whether a physical port shares its network's
// segment with the network's other members.
//
// +kubebuilder:validation:Enum=Private;Shared
type NetworkInterfaceIsolation string

const (
// NetworkInterfaceIsolationPrivate puts the port in a segment of its own on
// the network, reaching the network's gateway and no other member.
NetworkInterfaceIsolationPrivate NetworkInterfaceIsolation = "Private"

// NetworkInterfaceIsolationShared puts the port in the network's shared
// segment, reaching every other member.
NetworkInterfaceIsolationShared NetworkInterfaceIsolation = "Shared"
)

// NetworkInterfacePhase reports whether an interface is held by a claim.
Expand Down Expand Up @@ -303,6 +366,13 @@ type NetworkInterfaceAttachmentRef struct {
// NetworkInterfaceSpec defines the desired state of NetworkInterface. It is
// written by the operator when a claim is fulfilled, and it carries everything
// a provider needs to configure a NIC without reading any other resource.
//
// +kubebuilder:validation:XValidation:message="attachmentPoint is immutable and cannot be set, changed, or cleared after creation",rule="has(self.attachmentPoint) == has(oldSelf.attachmentPoint) && (!has(self.attachmentPoint) || self.attachmentPoint == oldSelf.attachmentPoint)"
// +kubebuilder:validation:XValidation:message="purpose is immutable and cannot be set, changed, or cleared after creation",rule="has(self.purpose) == has(oldSelf.purpose) && (!has(self.purpose) || self.purpose == oldSelf.purpose)"
// +kubebuilder:validation:XValidation:message="isolation is immutable and cannot be set, changed, or cleared after creation",rule="has(self.isolation) == has(oldSelf.isolation) && (!has(self.isolation) || self.isolation == oldSelf.isolation)"
// +kubebuilder:validation:XValidation:message="attachmentPoint is required for attachmentMode Physical and not allowed for any other mode",rule="has(self.attachmentPoint) == (has(self.attachmentMode) && self.attachmentMode == 'Physical')"
// +kubebuilder:validation:XValidation:message="purpose is only allowed for attachmentMode Physical",rule="!has(self.purpose) || (has(self.attachmentMode) && self.attachmentMode == 'Physical')"
// +kubebuilder:validation:XValidation:message="isolation is only allowed for attachmentMode Physical",rule="!has(self.isolation) || (has(self.attachmentMode) && self.attachmentMode == 'Physical')"
type NetworkInterfaceSpec struct {
// network is the network this interface belongs to, in the same namespace as
// the interface. It comes from the claim and does not change.
Expand Down Expand Up @@ -339,6 +409,25 @@ type NetworkInterfaceSpec struct {
// +kubebuilder:default="Netns"
AttachmentMode NetworkInterfaceAttachmentMode `json:"attachmentMode,omitempty"`

// attachmentPoint is the physical NIC and switch port a Physical interface
// reaches its network through. It comes from the claim and is absent for
// every other mode.
//
// +kubebuilder:validation:Optional
AttachmentPoint *NetworkInterfaceAttachmentPoint `json:"attachmentPoint,omitempty"`

// purpose is what a Physical interface is for. It comes from the claim.
//
// +kubebuilder:validation:Optional
Purpose NetworkInterfacePurpose `json:"purpose,omitempty"`

// isolation is whether a Physical interface's port shares its network's
// segment. It comes from the claim. Left unset, a boot interface is private
// and any other is shared.
//
// +kubebuilder:validation:Optional
Isolation NetworkInterfaceIsolation `json:"isolation,omitempty"`

// mtu is the MTU, in bytes, the interface must be configured with. It is
// resolved from the network, so a provider never has to read the network to
// configure the NIC.
Expand Down
Loading
Loading