diff --git a/api/v1alpha/networkinterface_schema_test.go b/api/v1alpha/networkinterface_schema_test.go new file mode 100644 index 00000000..6394a255 --- /dev/null +++ b/api/v1alpha/networkinterface_schema_test.go @@ -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)) +} diff --git a/api/v1alpha/networkinterface_types.go b/api/v1alpha/networkinterface_types.go index 0e7aa774..aec67817 100644 --- a/api/v1alpha/networkinterface_types.go +++ b/api/v1alpha/networkinterface_types.go @@ -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 ( @@ -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. @@ -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. @@ -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. diff --git a/api/v1alpha/networkinterfaceclaim_schema_test.go b/api/v1alpha/networkinterfaceclaim_schema_test.go new file mode 100644 index 00000000..cc788ae7 --- /dev/null +++ b/api/v1alpha/networkinterfaceclaim_schema_test.go @@ -0,0 +1,234 @@ +// SPDX-License-Identifier: AGPL-3.0-only + +package v1alpha_test + +import ( + "context" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/envtest" + "sigs.k8s.io/yaml" + + networkingv1alpha "go.datum.net/network-services-operator/api/v1alpha" +) + +func startSchemaEnv(t *testing.T, crdName string) client.Client { + t.Helper() + if os.Getenv("KUBEBUILDER_ASSETS") == "" { + t.Skip("envtest assets are required") + } + + data, err := os.ReadFile(filepath.Join("..", "..", "config", "crd", "bases", "networking.datumapis.com_"+crdName+".yaml")) + require.NoError(t, err) + var crd apiextensionsv1.CustomResourceDefinition + require.NoError(t, yaml.Unmarshal(data, &crd)) + + env := &envtest.Environment{CRDs: []*apiextensionsv1.CustomResourceDefinition{&crd}} + cfg, err := env.Start() + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, env.Stop()) }) + + scheme := runtime.NewScheme() + require.NoError(t, networkingv1alpha.AddToScheme(scheme)) + k8sClient, err := client.New(cfg, client.Options{Scheme: scheme}) + require.NoError(t, err) + return k8sClient +} + +func physicalClaimSpec() networkingv1alpha.NetworkInterfaceClaimSpec { + return networkingv1alpha.NetworkInterfaceClaimSpec{ + 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 TestPhysicalClaimAdmission(t *testing.T) { + k8sClient := startSchemaEnv(t, "networkinterfaceclaims") + ctx := context.Background() + + tests := []struct { + name string + mutate func(*networkingv1alpha.NetworkInterfaceClaimSpec) + valid bool + }{ + {name: "physical with an attachment point", valid: true}, + {name: "boot purpose and private isolation", valid: true, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate + }}, + {name: "shared isolation", valid: true, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationShared + }}, + {name: "upper-case MAC address", valid: true, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.MACAddress = "00:00:5E:00:53:0A" + }}, + {name: "physical without an attachment point", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint = nil + }}, + {name: "attachment point on another mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeHypervisor + }}, + {name: "attachment point on the default mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentMode = "" + }}, + {name: "purpose on another mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeNetns + spec.AttachmentPoint = nil + spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot + }}, + {name: "isolation on another mode", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentMode = networkingv1alpha.NetworkInterfaceAttachmentModeNetns + spec.AttachmentPoint = nil + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate + }}, + {name: "unknown purpose", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Purpose = "Install" + }}, + {name: "unknown isolation", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = "Isolated" + }}, + {name: "MAC address with five pairs", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.MACAddress = "00:00:5e:00:53" + }}, + {name: "MAC address with dashes", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.MACAddress = "00-00-5e-00-53-01" + }}, + {name: "empty switch", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.Switch = "" + }}, + {name: "empty port", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.Port = "" + }}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + claim := &networkingv1alpha.NetworkInterfaceClaim{ + ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("create-%d", i), Namespace: "default"}, + Spec: physicalClaimSpec(), + } + if tt.mutate != nil { + tt.mutate(&claim.Spec) + } + err := k8sClient.Create(ctx, claim) + if tt.valid { + require.NoError(t, err) + return + } + require.Error(t, err) + }) + } +} + +func TestPhysicalClaimFieldsAreImmutable(t *testing.T) { + k8sClient := startSchemaEnv(t, "networkinterfaceclaims") + ctx := context.Background() + + tests := []struct { + name string + start func(*networkingv1alpha.NetworkInterfaceClaimSpec) + mutate func(*networkingv1alpha.NetworkInterfaceClaimSpec) + }{ + {name: "change the port", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.Port = "Ethernet1/13" + }}, + {name: "change the MAC address", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.AttachmentPoint.MACAddress = "00:00:5e:00:53:02" + }}, + {name: "set the purpose", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot + }}, + {name: "clear the purpose", start: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Purpose = networkingv1alpha.NetworkInterfacePurposeBoot + }, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Purpose = "" + }}, + {name: "set the isolation", mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationShared + }}, + {name: "change the isolation", start: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate + }, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationShared + }}, + {name: "clear the isolation", start: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = networkingv1alpha.NetworkInterfaceIsolationPrivate + }, mutate: func(spec *networkingv1alpha.NetworkInterfaceClaimSpec) { + spec.Isolation = "" + }}, + } + + for i, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + claim := &networkingv1alpha.NetworkInterfaceClaim{ + ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("update-%d", i), Namespace: "default"}, + Spec: physicalClaimSpec(), + } + if tt.start != nil { + tt.start(&claim.Spec) + } + require.NoError(t, k8sClient.Create(ctx, claim)) + + tt.mutate(&claim.Spec) + require.Error(t, k8sClient.Update(ctx, claim)) + }) + } +} + +// A provider compares the claim it reads back with the claim it wrote, so a +// default written by the server would read as a conflict. +func TestPhysicalClaimFieldsHaveNoServerDefaults(t *testing.T) { + k8sClient := startSchemaEnv(t, "networkinterfaceclaims") + ctx := context.Background() + + for name, spec := range map[string]networkingv1alpha.NetworkInterfaceClaimSpec{ + "physical": physicalClaimSpec(), + "netns": {Network: networkingv1alpha.LocalNetworkRef{Name: "app"}}, + } { + t.Run(name, func(t *testing.T) { + claim := &networkingv1alpha.NetworkInterfaceClaim{ + ObjectMeta: metav1.ObjectMeta{Name: "defaults-" + name, Namespace: "default"}, + Spec: spec, + } + require.NoError(t, k8sClient.Create(ctx, claim)) + + var stored networkingv1alpha.NetworkInterfaceClaim + require.NoError(t, k8sClient.Get(ctx, client.ObjectKeyFromObject(claim), &stored)) + require.Empty(t, stored.Spec.Purpose) + require.Empty(t, stored.Spec.Isolation) + require.Equal(t, spec.AttachmentPoint, stored.Spec.AttachmentPoint) + }) + } +} + +// Claims stored before the physical fields existed carry none of them, and +// must still accept the updates the operator and compute make. +func TestExistingClaimsStillUpdate(t *testing.T) { + k8sClient := startSchemaEnv(t, "networkinterfaceclaims") + ctx := context.Background() + + claim := &networkingv1alpha.NetworkInterfaceClaim{ + ObjectMeta: metav1.ObjectMeta{Name: "existing", Namespace: "default"}, + Spec: networkingv1alpha.NetworkInterfaceClaimSpec{Network: networkingv1alpha.LocalNetworkRef{Name: "app"}}, + } + require.NoError(t, k8sClient.Create(ctx, claim)) + + claim.Labels = map[string]string{"example.com/touched": "true"} + claim.Spec.Egress = &networkingv1alpha.NetworkInterfaceClaimEgress{ + Internet: &networkingv1alpha.NetworkInterfaceClaimInternetEgress{Mode: networkingv1alpha.NetworkInterfaceClaimInternetEgressInherit}, + } + require.NoError(t, k8sClient.Update(ctx, claim)) +} diff --git a/api/v1alpha/networkinterfaceclaim_types.go b/api/v1alpha/networkinterfaceclaim_types.go index 954fd7d5..d0a9b48d 100644 --- a/api/v1alpha/networkinterfaceclaim_types.go +++ b/api/v1alpha/networkinterfaceclaim_types.go @@ -77,6 +77,11 @@ const ( // The platform provisions it with the project, so its absence says the // control plane is not bootstrapped rather than that it is slow to appear. NetworkInterfaceClaimReasonProjectNamespaceNotFound = "ProjectNamespaceNotFound" + + // NetworkInterfaceClaimReasonPhysicalAttachmentUnsupported means the claim + // asks for a physical attachment, which this operator does not bind yet. + // Nothing is allocated for it. + NetworkInterfaceClaimReasonPhysicalAttachmentUnsupported = "PhysicalAttachmentUnsupported" ) // NetworkInterfaceAddressRequest asks for one address beyond the ones the @@ -106,6 +111,12 @@ type NetworkInterfaceAddressRequest struct { // // +kubebuilder:validation:XValidation:message="networkInterfaceName is immutable and cannot be set, changed, or cleared after creation",rule="has(self.networkInterfaceName) == has(oldSelf.networkInterfaceName) && (!has(self.networkInterfaceName) || self.networkInterfaceName == oldSelf.networkInterfaceName)" // +kubebuilder:validation:XValidation:message="addresses is immutable and cannot be set, changed, or cleared after creation",rule="has(self.addresses) == has(oldSelf.addresses) && (!has(self.addresses) || self.addresses == oldSelf.addresses)" +// +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 NetworkInterfaceClaimSpec struct { // network is the network the interface attaches to. The network must already // exist in the same namespace as the claim. @@ -137,6 +148,8 @@ type NetworkInterfaceClaimSpec struct { // virtual machine or microVM guest needs. HypervisorDeclared also hands it // to a hypervisor, and additionally has the realizer state the device to // that hypervisor instead of letting it discover the device from the node. + // Physical is a physical NIC that reaches its network through the switch + // port named in attachmentPoint. // // It is copied to the bound interface and never interpreted here. Whoever // realizes the interface decides what each mode means on its data plane. @@ -148,6 +161,37 @@ type NetworkInterfaceClaimSpec struct { // +kubebuilder:validation:XValidation:message="attachmentMode is immutable and cannot be changed after creation",rule="self == oldSelf" AttachmentMode NetworkInterfaceAttachmentMode `json:"attachmentMode,omitempty"` + // attachmentPoint is the physical NIC and the switch port it is cabled to. + // It is required for attachmentMode Physical and not allowed for any other + // mode. It is copied to the bound interface and never checked against any + // inventory, so only a provider writes it. + // + // Immutable. A server recabled to another port needs a new claim. + // + // +kubebuilder:validation:Optional + AttachmentPoint *NetworkInterfaceAttachmentPoint `json:"attachmentPoint,omitempty"` + + // purpose is what a Physical claim is for. Boot attaches the port to a + // boot network for as long as the claim exists, ahead of any other claim on + // the port. Left unset, the claim attaches the port to its network whenever + // no boot claim names the port. + // + // Only allowed for attachmentMode Physical. Immutable. + // + // +kubebuilder:validation:Optional + Purpose NetworkInterfacePurpose `json:"purpose,omitempty"` + + // isolation is whether a Physical claim's port shares its network's segment + // with the network's other members. Private gives the port a segment of its + // own that reaches only the network's gateway. Shared joins the network's + // shared segment. Left unset, a boot claim is private and any other claim is + // shared; nothing is written back to the claim. + // + // Only allowed for attachmentMode Physical. Immutable. + // + // +kubebuilder:validation:Optional + Isolation NetworkInterfaceIsolation `json:"isolation,omitempty"` + // ipFamilies are the address families the interface must carry, in priority // order. List [IPv6, IPv4] for a dual-stack interface. The first family // listed holds the interface's primary address, which is the one reported in diff --git a/api/v1alpha/zz_generated.deepcopy.go b/api/v1alpha/zz_generated.deepcopy.go index 77bca37a..4941bc39 100644 --- a/api/v1alpha/zz_generated.deepcopy.go +++ b/api/v1alpha/zz_generated.deepcopy.go @@ -1598,6 +1598,21 @@ func (in *NetworkInterfaceAddressRequest) DeepCopy() *NetworkInterfaceAddressReq return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *NetworkInterfaceAttachmentPoint) DeepCopyInto(out *NetworkInterfaceAttachmentPoint) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new NetworkInterfaceAttachmentPoint. +func (in *NetworkInterfaceAttachmentPoint) DeepCopy() *NetworkInterfaceAttachmentPoint { + if in == nil { + return nil + } + out := new(NetworkInterfaceAttachmentPoint) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NetworkInterfaceAttachmentRef) DeepCopyInto(out *NetworkInterfaceAttachmentRef) { *out = *in @@ -1726,6 +1741,11 @@ func (in *NetworkInterfaceClaimRef) DeepCopy() *NetworkInterfaceClaimRef { func (in *NetworkInterfaceClaimSpec) DeepCopyInto(out *NetworkInterfaceClaimSpec) { *out = *in out.Network = in.Network + if in.AttachmentPoint != nil { + in, out := &in.AttachmentPoint, &out.AttachmentPoint + *out = new(NetworkInterfaceAttachmentPoint) + **out = **in + } if in.IPFamilies != nil { in, out := &in.IPFamilies, &out.IPFamilies *out = make([]IPFamily, len(*in)) @@ -1898,6 +1918,11 @@ func (in *NetworkInterfaceSpec) DeepCopyInto(out *NetworkInterfaceSpec) { *out = new(NetworkInterfaceClaimRef) **out = **in } + if in.AttachmentPoint != nil { + in, out := &in.AttachmentPoint, &out.AttachmentPoint + *out = new(NetworkInterfaceAttachmentPoint) + **out = **in + } if in.Addresses != nil { in, out := &in.Addresses, &out.Addresses *out = make([]NetworkInterfaceAddress, len(*in)) diff --git a/config/crd/bases/networking.datumapis.com_networkinterfaceclaims.yaml b/config/crd/bases/networking.datumapis.com_networkinterfaceclaims.yaml index 44d659b8..50d20720 100644 --- a/config/crd/bases/networking.datumapis.com_networkinterfaceclaims.yaml +++ b/config/crd/bases/networking.datumapis.com_networkinterfaceclaims.yaml @@ -117,6 +117,8 @@ spec: virtual machine or microVM guest needs. HypervisorDeclared also hands it to a hypervisor, and additionally has the realizer state the device to that hypervisor instead of letting it discover the device from the node. + Physical is a physical NIC that reaches its network through the switch + port named in attachmentPoint. It is copied to the bound interface and never interpreted here. Whoever realizes the interface decides what each mode means on its data plane. @@ -126,11 +128,47 @@ spec: - Netns - Hypervisor - HypervisorDeclared + - Physical type: string x-kubernetes-validations: - message: attachmentMode is immutable and cannot be changed after creation rule: self == oldSelf + attachmentPoint: + description: |- + attachmentPoint is the physical NIC and the switch port it is cabled to. + It is required for attachmentMode Physical and not allowed for any other + mode. It is copied to the bound interface and never checked against any + inventory, so only a provider writes it. + + Immutable. A server recabled to another port needs a new claim. + properties: + macAddress: + description: |- + macAddress is the NIC's MAC address, as six colon-separated hexadecimal + pairs. + pattern: ^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$ + type: string + port: + description: |- + 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. + maxLength: 253 + minLength: 1 + type: string + switch: + description: |- + switch is the switch the NIC is cabled to, in the name network operations + uses for it. + maxLength: 253 + minLength: 1 + type: string + required: + - macAddress + - port + - switch + type: object egress: default: internet: @@ -205,6 +243,19 @@ spec: rule: self.all(f, self.exists_one(g, g == f)) - message: ipFamilies is immutable and cannot be changed after creation rule: self == oldSelf + isolation: + description: |- + isolation is whether a Physical claim's port shares its network's segment + with the network's other members. Private gives the port a segment of its + own that reaches only the network's gateway. Shared joins the network's + shared segment. Left unset, a boot claim is private and any other claim is + shared; nothing is written back to the claim. + + Only allowed for attachmentMode Physical. Immutable. + enum: + - Private + - Shared + type: string network: description: |- network is the network the interface attaches to. The network must already @@ -238,6 +289,17 @@ spec: maxLength: 253 minLength: 1 type: string + purpose: + description: |- + purpose is what a Physical claim is for. Boot attaches the port to a + boot network for as long as the claim exists, ahead of any other claim on + the port. Left unset, the claim attaches the port to its network whenever + no boot claim names the port. + + Only allowed for attachmentMode Physical. Immutable. + enum: + - Boot + type: string reclaimPolicy: default: Delete description: |- @@ -282,6 +344,28 @@ spec: after creation rule: has(self.addresses) == has(oldSelf.addresses) && (!has(self.addresses) || self.addresses == oldSelf.addresses) + - 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) + - 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) + - 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) + - message: attachmentPoint is required for attachmentMode Physical and + not allowed for any other mode + rule: has(self.attachmentPoint) == (has(self.attachmentMode) && self.attachmentMode + == 'Physical') + - message: purpose is only allowed for attachmentMode Physical + rule: '!has(self.purpose) || (has(self.attachmentMode) && self.attachmentMode + == ''Physical'')' + - message: isolation is only allowed for attachmentMode Physical + rule: '!has(self.isolation) || (has(self.attachmentMode) && self.attachmentMode + == ''Physical'')' status: default: conditions: diff --git a/config/crd/bases/networking.datumapis.com_networkinterfaces.yaml b/config/crd/bases/networking.datumapis.com_networkinterfaces.yaml index dba16ddc..8a306c42 100644 --- a/config/crd/bases/networking.datumapis.com_networkinterfaces.yaml +++ b/config/crd/bases/networking.datumapis.com_networkinterfaces.yaml @@ -157,7 +157,40 @@ spec: - Netns - Hypervisor - HypervisorDeclared + - Physical type: string + attachmentPoint: + description: |- + 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. + properties: + macAddress: + description: |- + macAddress is the NIC's MAC address, as six colon-separated hexadecimal + pairs. + pattern: ^[0-9a-fA-F]{2}(:[0-9a-fA-F]{2}){5}$ + type: string + port: + description: |- + 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. + maxLength: 253 + minLength: 1 + type: string + switch: + description: |- + switch is the switch the NIC is cabled to, in the name network operations + uses for it. + maxLength: 253 + minLength: 1 + type: string + required: + - macAddress + - port + - switch + type: object claimRef: description: |- claimRef is the claim currently holding this interface. It is empty while a @@ -261,6 +294,15 @@ spec: maxLength: 15 minLength: 1 type: string + isolation: + description: |- + 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. + enum: + - Private + - Shared + type: string mtu: description: |- mtu is the MTU, in bytes, the interface must be configured with. It is @@ -284,6 +326,12 @@ spec: x-kubernetes-validations: - message: network is immutable and cannot be changed after creation rule: self == oldSelf + purpose: + description: purpose is what a Physical interface is for. It comes + from the claim. + enum: + - Boot + type: string reclaimPolicy: default: Delete description: |- @@ -297,6 +345,29 @@ spec: required: - network type: object + x-kubernetes-validations: + - 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) + - 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) + - 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) + - message: attachmentPoint is required for attachmentMode Physical and + not allowed for any other mode + rule: has(self.attachmentPoint) == (has(self.attachmentMode) && self.attachmentMode + == 'Physical') + - message: purpose is only allowed for attachmentMode Physical + rule: '!has(self.purpose) || (has(self.attachmentMode) && self.attachmentMode + == ''Physical'')' + - message: isolation is only allowed for attachmentMode Physical + rule: '!has(self.isolation) || (has(self.attachmentMode) && self.attachmentMode + == ''Physical'')' status: default: conditions: diff --git a/docs/api/networkinterfaceclaims.md b/docs/api/networkinterfaceclaims.md index 9abb41a9..1c70619a 100644 --- a/docs/api/networkinterfaceclaims.md +++ b/docs/api/networkinterfaceclaims.md @@ -74,7 +74,7 @@ it. To change one of those fields, delete the claim and create a new one, accepting that the workload gets new addresses unless the interface is retained.

- Validations:
  • has(self.networkInterfaceName) == has(oldSelf.networkInterfaceName) && (!has(self.networkInterfaceName) || self.networkInterfaceName == oldSelf.networkInterfaceName): networkInterfaceName is immutable and cannot be set, changed, or cleared after creation
  • has(self.addresses) == has(oldSelf.addresses) && (!has(self.addresses) || self.addresses == oldSelf.addresses): addresses is immutable and cannot be set, changed, or cleared after creation
  • + Validations:
  • has(self.networkInterfaceName) == has(oldSelf.networkInterfaceName) && (!has(self.networkInterfaceName) || self.networkInterfaceName == oldSelf.networkInterfaceName): networkInterfaceName is immutable and cannot be set, changed, or cleared after creation
  • has(self.addresses) == has(oldSelf.addresses) && (!has(self.addresses) || self.addresses == oldSelf.addresses): addresses is immutable and cannot be set, changed, or cleared after creation
  • has(self.attachmentPoint) == has(oldSelf.attachmentPoint) && (!has(self.attachmentPoint) || self.attachmentPoint == oldSelf.attachmentPoint): attachmentPoint is immutable and cannot be set, changed, or cleared after creation
  • has(self.purpose) == has(oldSelf.purpose) && (!has(self.purpose) || self.purpose == oldSelf.purpose): purpose is immutable and cannot be set, changed, or cleared after creation
  • has(self.isolation) == has(oldSelf.isolation) && (!has(self.isolation) || self.isolation == oldSelf.isolation): isolation is immutable and cannot be set, changed, or cleared after creation
  • has(self.attachmentPoint) == (has(self.attachmentMode) && self.attachmentMode == 'Physical'): attachmentPoint is required for attachmentMode Physical and not allowed for any other mode
  • !has(self.purpose) || (has(self.attachmentMode) && self.attachmentMode == 'Physical'): purpose is only allowed for attachmentMode Physical
  • !has(self.isolation) || (has(self.attachmentMode) && self.attachmentMode == 'Physical'): isolation is only allowed for attachmentMode Physical
  • true @@ -152,6 +152,8 @@ expects. Hypervisor hands it to a hypervisor as a device, which is what a virtual machine or microVM guest needs. HypervisorDeclared also hands it to a hypervisor, and additionally has the realizer state the device to that hypervisor instead of letting it discover the device from the node. +Physical is a physical NIC that reaches its network through the switch +port named in attachmentPoint. It is copied to the bound interface and never interpreted here. Whoever realizes the interface decides what each mode means on its data plane. @@ -159,10 +161,22 @@ realizes the interface decides what each mode means on its data plane. Immutable, because the guest and the attachment are both built against it.

    Validations:
  • self == oldSelf: attachmentMode is immutable and cannot be changed after creation
  • - Enum: Netns, Hypervisor, HypervisorDeclared
    + Enum: Netns, Hypervisor, HypervisorDeclared, Physical
    Default: Netns
    false + + attachmentPoint + object + + attachmentPoint is the physical NIC and the switch port it is cabled to. +It is required for attachmentMode Physical and not allowed for any other +mode. It is copied to the bound interface and never checked against any +inventory, so only a provider writes it. + +Immutable. A server recabled to another port needs a new claim.
    + + false egress object @@ -214,6 +228,21 @@ published.
    Default: [IPv6]
    false + + isolation + enum + + isolation is whether a Physical claim's port shares its network's segment +with the network's other members. Private gives the port a segment of its +own that reaches only the network's gateway. Shared joins the network's +shared segment. Left unset, a boot claim is private and any other claim is +shared; nothing is written back to the claim. + +Only allowed for attachmentMode Physical. Immutable.
    +
    + Enum: Private, Shared
    + + false networkInterfaceName string @@ -230,6 +259,20 @@ Immutable, including from empty to set. Rebinding a workload to a different interface means a new claim.
    false + + purpose + enum + + purpose is what a Physical claim is for. Boot attaches the port to a +boot network for as long as the claim exists, ahead of any other claim on +the port. Left unset, the claim attaches the port to its network whenever +no boot claim names the port. + +Only allowed for attachmentMode Physical. Immutable.
    +
    + Enum: Boot
    + + false reclaimPolicy enum @@ -329,6 +372,56 @@ CIDR, so a class cannot be used to ask for a particular address.
    +### NetworkInterfaceClaim.spec.attachmentPoint +[↩ Parent](#networkinterfaceclaimspec) + + + +attachmentPoint is the physical NIC and the switch port it is cabled to. +It is required for attachmentMode Physical and not allowed for any other +mode. It is copied to the bound interface and never checked against any +inventory, so only a provider writes it. + +Immutable. A server recabled to another port needs a new claim. + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescriptionRequired
    macAddressstring + macAddress is the NIC's MAC address, as six colon-separated hexadecimal +pairs.
    +
    true
    portstring + 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.
    +
    true
    switchstring + switch is the switch the NIC is cabled to, in the name network operations +uses for it.
    +
    true
    + + ### NetworkInterfaceClaim.spec.egress [↩ Parent](#networkinterfaceclaimspec) diff --git a/docs/api/networkinterfaces.md b/docs/api/networkinterfaces.md index 817a0e4e..71c7db2c 100644 --- a/docs/api/networkinterfaces.md +++ b/docs/api/networkinterfaces.md @@ -66,6 +66,8 @@ that asked for it depends on spec.reclaimPolicy. 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.
    +
    + Validations:
  • has(self.attachmentPoint) == has(oldSelf.attachmentPoint) && (!has(self.attachmentPoint) || self.attachmentPoint == oldSelf.attachmentPoint): attachmentPoint is immutable and cannot be set, changed, or cleared after creation
  • has(self.purpose) == has(oldSelf.purpose) && (!has(self.purpose) || self.purpose == oldSelf.purpose): purpose is immutable and cannot be set, changed, or cleared after creation
  • has(self.isolation) == has(oldSelf.isolation) && (!has(self.isolation) || self.isolation == oldSelf.isolation): isolation is immutable and cannot be set, changed, or cleared after creation
  • has(self.attachmentPoint) == (has(self.attachmentMode) && self.attachmentMode == 'Physical'): attachmentPoint is required for attachmentMode Physical and not allowed for any other mode
  • !has(self.purpose) || (has(self.attachmentMode) && self.attachmentMode == 'Physical'): purpose is only allowed for attachmentMode Physical
  • !has(self.isolation) || (has(self.attachmentMode) && self.attachmentMode == 'Physical'): isolation is only allowed for attachmentMode Physical
  • true @@ -135,10 +137,19 @@ microVM guest needs. HypervisorDeclared also hands it to a hypervisor, and additionally has the realizer state the device to that hypervisor instead of letting it discover the device from the node.

    - Enum: Netns, Hypervisor, HypervisorDeclared
    + Enum: Netns, Hypervisor, HypervisorDeclared, Physical
    Default: Netns
    false + + attachmentPoint + object + + 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.
    + + false claimRef object @@ -185,6 +196,17 @@ operating system, such as eth0 or eth1. It comes from the claim.
    Default: eth0
    false + + isolation + enum + + 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.
    +
    + Enum: Private, Shared
    + + false mtu integer @@ -198,6 +220,15 @@ configure the NIC.
    Maximum: 8856
    false + + purpose + enum + + purpose is what a Physical interface is for. It comes from the claim.
    +
    + Enum: Boot
    + + false reclaimPolicy enum @@ -316,6 +347,53 @@ spec.ipFamilies.
    +### NetworkInterface.spec.attachmentPoint +[↩ Parent](#networkinterfacespec) + + + +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. + + + + + + + + + + + + + + + + + + + + + + + + + + +
    NameTypeDescriptionRequired
    macAddressstring + macAddress is the NIC's MAC address, as six colon-separated hexadecimal +pairs.
    +
    true
    portstring + 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.
    +
    true
    switchstring + switch is the switch the NIC is cabled to, in the name network operations +uses for it.
    +
    true
    + + ### NetworkInterface.spec.claimRef [↩ Parent](#networkinterfacespec) diff --git a/internal/controller/networkinterface_projection_test.go b/internal/controller/networkinterface_projection_test.go index 4cb21a9d..5129e21e 100644 --- a/internal/controller/networkinterface_projection_test.go +++ b/internal/controller/networkinterface_projection_test.go @@ -814,3 +814,23 @@ func TestAPublishedInterfaceIsHeldUntilItsCopyIsCollected(t *testing.T) { _, found = v.hubCopy(boundInterfaceName) require.False(t, found, "the hold is released once nothing is left behind") } + +// The switch and port name the provider's hardware, which a consumer has no +// business seeing. +func TestProjectedInterfaceOmitsThePhysicalAttachment(t *testing.T) { + source := &networkingv1alpha.NetworkInterface{Spec: networkingv1alpha.NetworkInterfaceSpec{ + AttachmentMode: networkingv1alpha.NetworkInterfaceAttachmentModePhysical, + AttachmentPoint: &networkingv1alpha.NetworkInterfaceAttachmentPoint{ + MACAddress: "00:00:5e:00:53:01", + Switch: "rack-a1-tor-1", + Port: "Ethernet1/12", + }, + Purpose: networkingv1alpha.NetworkInterfacePurposeBoot, + Isolation: networkingv1alpha.NetworkInterfaceIsolationPrivate, + }} + + projection := projectedInterface(source, "ord-1") + require.Nil(t, projection.Spec.AttachmentPoint) + require.Empty(t, projection.Spec.Purpose) + require.Empty(t, projection.Spec.Isolation) +} diff --git a/internal/controller/networkinterfaceclaim_controller.go b/internal/controller/networkinterfaceclaim_controller.go index 57212650..52100753 100644 --- a/internal/controller/networkinterfaceclaim_controller.go +++ b/internal/controller/networkinterfaceclaim_controller.go @@ -149,6 +149,12 @@ func (r *NetworkInterfaceClaimReconciler) fulfill( recorder events.EventRecorder, claim *networkingv1alpha.NetworkInterfaceClaim, ) (ctrl.Result, error) { + if claim.Spec.AttachmentMode == networkingv1alpha.NetworkInterfaceAttachmentModePhysical { + return r.reject(ctx, cl, claim, + networkingv1alpha.NetworkInterfaceClaimReasonPhysicalAttachmentUnsupported, + "Physical attachment is not available yet, so no address is allocated for this claim") + } + location, err := r.location(ctx) if err != nil { var unresolved *locationidentity.LocationUnresolved @@ -499,6 +505,22 @@ func interfaceSatisfies( } } + // A physical interface is wired to one port. Handing it to a claim for + // another port, or for no port, would attach the wrong server. + physical := networkingv1alpha.NetworkInterfaceAttachmentModePhysical + if (iface.Spec.AttachmentMode == physical || claim.Spec.AttachmentMode == physical) && + (iface.Spec.AttachmentMode != claim.Spec.AttachmentMode || + !apiequality.Semantic.DeepEqual(iface.Spec.AttachmentPoint, claim.Spec.AttachmentPoint) || + iface.Spec.Purpose != claim.Spec.Purpose || + iface.Spec.Isolation != claim.Spec.Isolation) { + return &bindingRefused{ + reason: "AttachmentPointMismatch", + message: fmt.Sprintf( + "Network interface %q is attached differently from what this claim asks for, so it cannot be bound", + iface.Name), + } + } + for _, family := range claim.Spec.IPFamilies { if !slices.ContainsFunc(iface.Spec.Addresses, func(a networkingv1alpha.NetworkInterfaceAddress) bool { return a.Family == family @@ -593,15 +615,7 @@ func (r *NetworkInterfaceClaimReconciler) bindInterface( iface.Annotations = map[string]string{allocationClaimAnnotation: claim.Name} iface.Labels = propagatedInterfaceLabels(claim, location) iface.Finalizers = []string{networkInterfaceFinalizer} - iface.Spec = networkingv1alpha.NetworkInterfaceSpec{ - Network: networkingv1alpha.LocalNetworkRef{Name: networkContext.Spec.Network.Name}, - ClaimRef: &networkingv1alpha.NetworkInterfaceClaimRef{Name: claim.Name}, - InterfaceName: claim.Spec.InterfaceName, - AttachmentMode: claim.Spec.AttachmentMode, - MTU: networkContext.Spec.MTU, - ReclaimPolicy: claim.Spec.ReclaimPolicy, - Egress: interfaceEgressFromClaim(claim), - } + iface.Spec = interfaceSpecForClaim(claim, networkContext) for _, entry := range allocated { if entry.request.external { @@ -628,6 +642,24 @@ func (r *NetworkInterfaceClaimReconciler) bindInterface( return iface, nil } +func interfaceSpecForClaim( + claim *networkingv1alpha.NetworkInterfaceClaim, + networkContext *networkingv1alpha.NetworkContext, +) networkingv1alpha.NetworkInterfaceSpec { + return networkingv1alpha.NetworkInterfaceSpec{ + Network: networkingv1alpha.LocalNetworkRef{Name: networkContext.Spec.Network.Name}, + ClaimRef: &networkingv1alpha.NetworkInterfaceClaimRef{Name: claim.Name}, + InterfaceName: claim.Spec.InterfaceName, + AttachmentMode: claim.Spec.AttachmentMode, + AttachmentPoint: claim.Spec.AttachmentPoint.DeepCopy(), + Purpose: claim.Spec.Purpose, + Isolation: claim.Spec.Isolation, + MTU: networkContext.Spec.MTU, + ReclaimPolicy: claim.Spec.ReclaimPolicy, + Egress: interfaceEgressFromClaim(claim), + } +} + type allocationRequest struct { discriminator string family networkingv1alpha.IPFamily diff --git a/internal/controller/networkinterfaceclaim_controller_test.go b/internal/controller/networkinterfaceclaim_controller_test.go index f197132a..d41a047e 100644 --- a/internal/controller/networkinterfaceclaim_controller_test.go +++ b/internal/controller/networkinterfaceclaim_controller_test.go @@ -2575,3 +2575,152 @@ func TestFindAllocationPrefersTheNameHoldingTheAddress(t *testing.T) { require.Equal(t, qualified, found.Name, "with both names held here, the one carrying the interface's address is the interface's") } + +func physicalAttachmentPoint(port string) *networkingv1alpha.NetworkInterfaceAttachmentPoint { + return &networkingv1alpha.NetworkInterfaceAttachmentPoint{ + MACAddress: "00:00:5e:00:53:01", + Switch: "rack-a1-tor-1", + Port: port, + } +} + +// Nothing yet decides which project holds a switch port or detaches it before +// release, so a physical claim must not reach IPAM or produce an interface a +// realizer could act on. +func TestPhysicalClaimIsRefusedWithoutAllocating(t *testing.T) { + s := newScenario(t, true, []networkingv1alpha.IPFamily{networkingv1alpha.IPv4Protocol}) + + claim := s.createClaim("server-data", networkingv1alpha.NetworkInterfaceClaimSpec{ + InterfaceName: "data", + AttachmentMode: networkingv1alpha.NetworkInterfaceAttachmentModePhysical, + AttachmentPoint: physicalAttachmentPoint("Ethernet1/12"), + IPFamilies: []networkingv1alpha.IPFamily{networkingv1alpha.IPv4Protocol}, + ReclaimPolicy: networkingv1alpha.NetworkInterfaceReclaimPolicyDelete, + }) + s.reconcile(claim) + s.reconcile(claim) + + require.Zero(t, s.ipam.createdAnywhere()) + _, err := s.getInterface("server-data") + require.True(t, apierrors.IsNotFound(err), "no interface may exist for a refused physical claim") + + refused := s.getClaim("server-data") + for _, conditionType := range []string{ + networkingv1alpha.NetworkInterfaceClaimBound, + networkingv1alpha.NetworkInterfaceClaimAllocated, + networkingv1alpha.NetworkInterfaceClaimReady, + } { + condition := conditionOf(refused, conditionType) + require.NotNil(t, condition, conditionType) + require.Equal(t, metav1.ConditionFalse, condition.Status, conditionType) + require.Equal(t, networkingv1alpha.NetworkInterfaceClaimReasonPhysicalAttachmentUnsupported, condition.Reason, conditionType) + } + + s.deleteClaim(refused) + require.Zero(t, s.ipam.createdAnywhere()) + var gone networkingv1alpha.NetworkInterfaceClaim + require.True(t, apierrors.IsNotFound(s.client.Get(s.ctx, client.ObjectKeyFromObject(claim), &gone)), + "a refused physical claim allocated nothing and must delete at once") +} + +func TestAdoptionRefusesADifferentAttachment(t *testing.T) { + physical := networkingv1alpha.NetworkInterfaceAttachmentModePhysical + netns := networkingv1alpha.NetworkInterfaceAttachmentModeNetns + + iface := func(mode networkingv1alpha.NetworkInterfaceAttachmentMode, point *networkingv1alpha.NetworkInterfaceAttachmentPoint, + purpose networkingv1alpha.NetworkInterfacePurpose, isolation networkingv1alpha.NetworkInterfaceIsolation, + ) *networkingv1alpha.NetworkInterface { + return &networkingv1alpha.NetworkInterface{ + ObjectMeta: metav1.ObjectMeta{Name: "nic"}, + Spec: networkingv1alpha.NetworkInterfaceSpec{ + Network: networkingv1alpha.LocalNetworkRef{Name: "default"}, + InterfaceName: "data", + AttachmentMode: mode, + AttachmentPoint: point, + Purpose: purpose, + Isolation: isolation, + ReclaimPolicy: networkingv1alpha.NetworkInterfaceReclaimPolicyRetain, + }, + } + } + claim := func(mode networkingv1alpha.NetworkInterfaceAttachmentMode, point *networkingv1alpha.NetworkInterfaceAttachmentPoint, + purpose networkingv1alpha.NetworkInterfacePurpose, isolation networkingv1alpha.NetworkInterfaceIsolation, + ) *networkingv1alpha.NetworkInterfaceClaim { + return &networkingv1alpha.NetworkInterfaceClaim{Spec: networkingv1alpha.NetworkInterfaceClaimSpec{ + Network: networkingv1alpha.LocalNetworkRef{Name: "default"}, + InterfaceName: "data", + AttachmentMode: mode, + AttachmentPoint: point, + Purpose: purpose, + Isolation: isolation, + ReclaimPolicy: networkingv1alpha.NetworkInterfaceReclaimPolicyRetain, + }} + } + + portA := physicalAttachmentPoint("Ethernet1/12") + portB := physicalAttachmentPoint("Ethernet1/13") + boot := networkingv1alpha.NetworkInterfacePurposeBoot + private := networkingv1alpha.NetworkInterfaceIsolationPrivate + + tests := []struct { + name string + iface *networkingv1alpha.NetworkInterface + claim *networkingv1alpha.NetworkInterfaceClaim + ok bool + }{ + {name: "same port", ok: true, + iface: iface(physical, portA, boot, private), claim: claim(physical, portA, boot, private)}, + {name: "virtual modes are untouched", ok: true, + iface: iface(netns, nil, "", ""), claim: claim(networkingv1alpha.NetworkInterfaceAttachmentModeHypervisor, nil, "", "")}, + {name: "another port", + iface: iface(physical, portA, "", ""), claim: claim(physical, portB, "", "")}, + {name: "physical interface for a virtual claim", + iface: iface(physical, portA, "", ""), claim: claim(netns, nil, "", "")}, + {name: "virtual interface for a physical claim", + iface: iface(netns, nil, "", ""), claim: claim(physical, portA, "", "")}, + {name: "another purpose", + iface: iface(physical, portA, boot, ""), claim: claim(physical, portA, "", "")}, + {name: "another isolation", + iface: iface(physical, portA, "", private), claim: claim(physical, portA, "", "")}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := interfaceSatisfies(tt.iface, tt.claim) + if tt.ok { + require.NoError(t, err) + return + } + var refused *bindingRefused + require.ErrorAs(t, err, &refused) + require.Equal(t, "AttachmentPointMismatch", refused.reason) + }) + } +} + +func TestInterfaceSpecCarriesThePhysicalAttachment(t *testing.T) { + claim := &networkingv1alpha.NetworkInterfaceClaim{ + ObjectMeta: metav1.ObjectMeta{Name: "server-boot"}, + Spec: networkingv1alpha.NetworkInterfaceClaimSpec{ + Network: networkingv1alpha.LocalNetworkRef{Name: "boot"}, + InterfaceName: "boot", + AttachmentMode: networkingv1alpha.NetworkInterfaceAttachmentModePhysical, + AttachmentPoint: physicalAttachmentPoint("Ethernet1/11"), + Purpose: networkingv1alpha.NetworkInterfacePurposeBoot, + Isolation: networkingv1alpha.NetworkInterfaceIsolationPrivate, + ReclaimPolicy: networkingv1alpha.NetworkInterfaceReclaimPolicyDelete, + }, + } + networkContext := &networkingv1alpha.NetworkContext{} + networkContext.Spec.Network.Name = "boot" + + spec := interfaceSpecForClaim(claim, networkContext) + require.Equal(t, networkingv1alpha.NetworkInterfaceAttachmentModePhysical, spec.AttachmentMode) + require.Equal(t, claim.Spec.AttachmentPoint, spec.AttachmentPoint) + require.Equal(t, networkingv1alpha.NetworkInterfacePurposeBoot, spec.Purpose) + require.Equal(t, networkingv1alpha.NetworkInterfaceIsolationPrivate, spec.Isolation) + + spec.AttachmentPoint.Port = "Ethernet1/99" + require.Equal(t, "Ethernet1/11", claim.Spec.AttachmentPoint.Port, + "the interface must hold its own copy of the attachment point") +} diff --git a/internal/galactic/controller/networkinterface_controller.go b/internal/galactic/controller/networkinterface_controller.go index be08f641..b4ef00e6 100644 --- a/internal/galactic/controller/networkinterface_controller.go +++ b/internal/galactic/controller/networkinterface_controller.go @@ -146,6 +146,11 @@ func (r *NetworkInterfaceReconciler) reconcile(ctx context.Context, req ctrl.Req if !networkInterface.DeletionTimestamp.IsZero() { return ctrl.Result{}, nil } + // A physical NIC reaches its network through a switch port, which galactic + // does not program. + if networkInterface.Spec.AttachmentMode == networkingv1alpha.NetworkInterfaceAttachmentModePhysical { + return ctrl.Result{}, nil + } if !claimFulfilled(&networkInterface) { return ctrl.Result{}, r.clearConsumerLabels(ctx, &networkInterface) } diff --git a/internal/galactic/controller/networkinterface_controller_test.go b/internal/galactic/controller/networkinterface_controller_test.go index b204deca..dcc38dc8 100644 --- a/internal/galactic/controller/networkinterface_controller_test.go +++ b/internal/galactic/controller/networkinterface_controller_test.go @@ -592,3 +592,53 @@ func TestReconcileAttachmentLeavesDNSLabelForTheDNSControllerWhenOff(t *testing. t.Fatalf("labels = %v, want the consumer label left in place", got.Labels) } } + +func TestReconcileLeavesPhysicalInterfacesAlone(t *testing.T) { + scheme := runtime.NewScheme() + if err := cloudv1alpha1.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + if err := networkingv1alpha.AddToScheme(scheme); err != nil { + t.Fatal(err) + } + iface := &networkingv1alpha.NetworkInterface{ + ObjectMeta: metav1.ObjectMeta{Name: "nic", Namespace: "tenant", UID: types.UID("iface")}, + Spec: networkingv1alpha.NetworkInterfaceSpec{ + AttachmentMode: networkingv1alpha.NetworkInterfaceAttachmentModePhysical, + AttachmentPoint: &networkingv1alpha.NetworkInterfaceAttachmentPoint{ + MACAddress: "00:00:5e:00:53:01", Switch: "rack-a1-tor-1", Port: "Ethernet1/12", + }, + }, + Status: networkingv1alpha.NetworkInterfaceStatus{ + Phase: networkingv1alpha.NetworkInterfacePhaseBound, + NetworkContextRef: &networkingv1alpha.LocalNetworkContextRef{Name: "app-loc"}, + Conditions: []metav1.Condition{{ + Type: networkingv1alpha.NetworkInterfaceAllocated, Status: metav1.ConditionTrue, + Reason: "Allocated", LastTransitionTime: metav1.Now(), + }}, + }, + } + vpc := &cloudv1alpha1.VPC{ObjectMeta: metav1.ObjectMeta{Name: "app-loc", Namespace: "tenant"}} + vpc.Status.VPC = "3kF9qP2x" + cl := fake.NewClientBuilder().WithScheme(scheme).WithObjects(iface, vpc).WithStatusSubresource(iface).Build() + r := &NetworkInterfaceReconciler{Client: cl, APIReader: cl, Scheme: scheme} + + if _, err := r.reconcile(context.Background(), ctrl.Request{NamespacedName: types.NamespacedName{Namespace: "tenant", Name: "nic"}}); err != nil { + t.Fatal(err) + } + + var attachments cloudv1alpha1.VPCAttachmentList + if err := cl.List(context.Background(), &attachments); err != nil { + t.Fatal(err) + } + if len(attachments.Items) != 0 { + t.Fatalf("a physical interface got %d VPC attachments", len(attachments.Items)) + } + var after networkingv1alpha.NetworkInterface + if err := cl.Get(context.Background(), types.NamespacedName{Namespace: "tenant", Name: "nic"}, &after); err != nil { + t.Fatal(err) + } + if apimeta.FindStatusCondition(after.Status.Conditions, networkingv1alpha.NetworkInterfacePrepared) != nil { + t.Fatalf("galactic wrote Prepared on a physical interface: %v", after.Status.Conditions) + } +} diff --git a/internal/networkagent/catalog.go b/internal/networkagent/catalog.go index 7089eab0..aac3dd52 100644 --- a/internal/networkagent/catalog.go +++ b/internal/networkagent/catalog.go @@ -105,6 +105,7 @@ const ( reasonAddressClassMissing = "AddressClassMissing" reasonReclaimPolicyMismatch = "ReclaimPolicyMismatch" reasonInterfaceHeldByAnother = "InterfaceHeldByAnotherClaim" + reasonAttachmentPointMismatch = "AttachmentPointMismatch" reasonInvalidSelector = "InvalidSelector" reasonResolved = "Resolved" conditionSubnetClaimReady = "Ready" @@ -474,6 +475,13 @@ func claimRefusals() []ReasonInfo { {reasonAddressFamilyMissing, retained("it holds no address of a kind the workload now asks for")}, {reasonAddressClassMissing, retained("it holds no address of a class the workload now asks for, such as a public address")}, {reasonReclaimPolicyMismatch, retained("its addresses were kept under a different rule for what happens when the instance goes away")}, + {reasonAttachmentPointMismatch, retained("it is wired to a different switch port, or for a different purpose, from the one the server's claim now names")}, + {networkingv1alpha.NetworkInterfaceClaimReasonPhysicalAttachmentUnsupported, ReasonInfo{ + Actionability: ActionabilityPlatform, Scope: ScopeInstance, + Explanation: "This server's network interface asks to reach its network through a switch port, " + + "which the platform cannot attach yet, so no address was given to it.", + Remediation: remediationEscalate, Skill: SkillInterfaceTriage, + }}, {reasonInterfaceHeldByAnother, ReasonInfo{ Actionability: ActionabilityPlatform, Scope: ScopeInstance, Explanation: "The network interface this instance should use is already held by another instance.",