Skip to content

Commit 6d29c43

Browse files
Realize NetworkAttachments through EVPN Fabrics
Resolve selected Physical and Aggregate Exact trunk Interfaces through Devices to a single Fabric and record the typed forwarding context. Reject competing attachments requesting the same Device-local VLAN, including attachments belonging to different Networks. Allocate a shared Network-scoped VNI Claim owned by the Fabric and validate its allocation before creating attachment-owned VLANs and bridged EVPNInstances. Set RD to Auto and explicitly derive import and export route targets per RFC 8365 using two-octet iBGP AS numbers. Retain VNI Claims across attachment and Network deletion until their Fabric is removed. Persist Interface/VLAN ownership in status before device realization. Preserve unrelated allowed VLANs, clean stale memberships and owned resources on selector changes, and remove Interface memberships during finalization. Garbage collection handles attachment-owned children. Add indexed dependency watches, switchport VLAN mutation methods, generated API artifacts, and resource-driven controller tests. Signed-off-by: Felix Kästner <felix.kaestner@sap.com>
1 parent 02a4ba3 commit 6d29c43

20 files changed

Lines changed: 4205 additions & 25 deletions

‎api/core/v1alpha1/interface_types.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package v1alpha1
55

66
import (
7+
"fmt"
8+
"sort"
79
"sync"
810

911
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -236,6 +238,86 @@ type Encapsulation struct {
236238
OuterTag int32 `json:"outerTag,omitempty"`
237239
}
238240

241+
// AddAllowedVLAN adds a VLAN to the explicit allowed VLAN list.
242+
// It reports whether the list changed.
243+
func (s *Switchport) AddAllowedVLAN(vlan int32) (bool, error) {
244+
if err := s.ValidateAllowedVLAN(vlan); err != nil {
245+
return false, err
246+
}
247+
value := int64(vlan)
248+
// An existing range already allows the VLAN, so no mutation is needed.
249+
for _, vlanRange := range s.AllowedVlans {
250+
if vlanRange.Start <= value && value <= vlanRange.End {
251+
return false, nil
252+
}
253+
}
254+
// Appending a singleton makes an implicit all-VLAN list explicit and takes ownership of it.
255+
s.AllowedVlans = append(s.AllowedVlans, IndexRange{Start: value, End: value})
256+
s.SortAllowedVLANs()
257+
return true, nil
258+
}
259+
260+
// RemoveAllowedVLAN removes a VLAN from the explicit allowed VLAN list.
261+
// It reports whether the list changed.
262+
func (s *Switchport) RemoveAllowedVLAN(vlan int32) (bool, error) {
263+
if err := s.ValidateAllowedVLAN(vlan); err != nil {
264+
return false, err
265+
}
266+
value := int64(vlan)
267+
// Find the single range containing the VLAN and remove only that value from it.
268+
for i, vlanRange := range s.AllowedVlans {
269+
if value < vlanRange.Start || value > vlanRange.End {
270+
continue
271+
}
272+
switch {
273+
// Remove the whole singleton range.
274+
case vlanRange.Start == value && vlanRange.End == value:
275+
s.AllowedVlans = append(s.AllowedVlans[:i], s.AllowedVlans[i+1:]...)
276+
// Shrink the range past its first VLAN.
277+
case vlanRange.Start == value:
278+
s.AllowedVlans[i].Start++
279+
// Shrink the range before its last VLAN.
280+
case vlanRange.End == value:
281+
s.AllowedVlans[i].End--
282+
// Split the range around an interior VLAN.
283+
default:
284+
s.AllowedVlans[i].End = value - 1
285+
s.AllowedVlans = append(s.AllowedVlans, IndexRange{Start: value + 1, End: vlanRange.End})
286+
s.SortAllowedVLANs()
287+
}
288+
if len(s.AllowedVlans) == 0 {
289+
// nil restores the API default after the final explicit VLAN is removed.
290+
s.AllowedVlans = nil
291+
}
292+
return true, nil
293+
}
294+
return false, nil
295+
}
296+
297+
// SortAllowedVLANs orders the explicit allowed VLAN ranges by start and then end in-place.
298+
func (s *Switchport) SortAllowedVLANs() {
299+
sort.Slice(s.AllowedVlans, func(i, j int) bool {
300+
if s.AllowedVlans[i].Start != s.AllowedVlans[j].Start {
301+
return s.AllowedVlans[i].Start < s.AllowedVlans[j].Start
302+
}
303+
return s.AllowedVlans[i].End < s.AllowedVlans[j].End
304+
})
305+
}
306+
307+
// ValidateAllowedVLAN validates that the switchport permits changing the given VLAN.
308+
func (s *Switchport) ValidateAllowedVLAN(vlan int32) error {
309+
if s.Mode != SwitchportModeTrunk {
310+
return fmt.Errorf("switchport mode %q does not support allowed VLANs", s.Mode)
311+
}
312+
if s.AllowedVlansMode == AllowedVlansModeUnmanaged {
313+
return fmt.Errorf("allowed VLAN mode %q does not permit changes", s.AllowedVlansMode)
314+
}
315+
if vlan < 1 || vlan > 4094 {
316+
return fmt.Errorf("VLAN %d is outside the valid range 1..4094", vlan)
317+
}
318+
return nil
319+
}
320+
239321
// AllowedVlansMode defines how trunk allowed VLANs are managed.
240322
// +kubebuilder:validation:Enum=Exact;Unmanaged
241323
type AllowedVlansMode string
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package v1alpha1
5+
6+
import (
7+
"reflect"
8+
"testing"
9+
)
10+
11+
func TestSwitchportAddAllowedVLAN(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
switchport Switchport
15+
vlan int32
16+
want []IndexRange
17+
changed bool
18+
wantErr bool
19+
}{
20+
{
21+
name: "takes ownership of nil list",
22+
switchport: Switchport{Mode: SwitchportModeTrunk},
23+
vlan: 100,
24+
want: []IndexRange{{Start: 100, End: 100}},
25+
changed: true,
26+
},
27+
{
28+
name: "keeps containing range",
29+
switchport: Switchport{
30+
Mode: SwitchportModeTrunk,
31+
AllowedVlans: []IndexRange{{Start: 90, End: 110}},
32+
},
33+
vlan: 100,
34+
want: []IndexRange{{Start: 90, End: 110}},
35+
},
36+
{
37+
name: "adds sorted singleton",
38+
switchport: Switchport{
39+
Mode: SwitchportModeTrunk,
40+
AllowedVlans: []IndexRange{{Start: 200, End: 210}},
41+
},
42+
vlan: 100,
43+
want: []IndexRange{{Start: 100, End: 100}, {Start: 200, End: 210}},
44+
changed: true,
45+
},
46+
{
47+
name: "rejects access mode",
48+
switchport: Switchport{Mode: SwitchportModeAccess},
49+
vlan: 100,
50+
wantErr: true,
51+
},
52+
{
53+
name: "rejects unmanaged mode",
54+
switchport: Switchport{
55+
Mode: SwitchportModeTrunk,
56+
AllowedVlansMode: AllowedVlansModeUnmanaged,
57+
},
58+
vlan: 100,
59+
wantErr: true,
60+
},
61+
{
62+
name: "rejects invalid VLAN",
63+
switchport: Switchport{Mode: SwitchportModeTrunk},
64+
vlan: 4095,
65+
wantErr: true,
66+
},
67+
}
68+
69+
for _, test := range tests {
70+
t.Run(test.name, func(t *testing.T) {
71+
switchport := test.switchport
72+
changed, err := switchport.AddAllowedVLAN(test.vlan)
73+
if test.wantErr {
74+
if err == nil {
75+
t.Fatalf("AddAllowedVLAN(%d) returned no error", test.vlan)
76+
}
77+
return
78+
}
79+
if err != nil {
80+
t.Fatalf("AddAllowedVLAN(%d): %v", test.vlan, err)
81+
}
82+
if changed != test.changed {
83+
t.Fatalf("AddAllowedVLAN(%d) changed = %t, want %t", test.vlan, changed, test.changed)
84+
}
85+
if !reflect.DeepEqual(switchport.AllowedVlans, test.want) {
86+
t.Fatalf("AddAllowedVLAN(%d) ranges = %#v, want %#v", test.vlan, switchport.AllowedVlans, test.want)
87+
}
88+
})
89+
}
90+
}
91+
92+
func TestSwitchportRemoveAllowedVLAN(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
switchport Switchport
96+
vlan int32
97+
want []IndexRange
98+
changed bool
99+
wantErr bool
100+
}{
101+
{
102+
name: "keeps absent VLAN",
103+
switchport: Switchport{
104+
Mode: SwitchportModeTrunk,
105+
AllowedVlans: []IndexRange{{Start: 10, End: 20}},
106+
},
107+
vlan: 100,
108+
want: []IndexRange{{Start: 10, End: 20}},
109+
},
110+
{
111+
name: "removes final VLAN",
112+
switchport: Switchport{
113+
Mode: SwitchportModeTrunk,
114+
AllowedVlans: []IndexRange{{Start: 100, End: 100}},
115+
},
116+
vlan: 100,
117+
changed: true,
118+
},
119+
{
120+
name: "shrinks range start",
121+
switchport: Switchport{
122+
Mode: SwitchportModeTrunk,
123+
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
124+
},
125+
vlan: 100,
126+
want: []IndexRange{{Start: 101, End: 110}},
127+
changed: true,
128+
},
129+
{
130+
name: "shrinks range end",
131+
switchport: Switchport{
132+
Mode: SwitchportModeTrunk,
133+
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
134+
},
135+
vlan: 110,
136+
want: []IndexRange{{Start: 100, End: 109}},
137+
changed: true,
138+
},
139+
{
140+
name: "splits range",
141+
switchport: Switchport{
142+
Mode: SwitchportModeTrunk,
143+
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
144+
},
145+
vlan: 105,
146+
want: []IndexRange{{Start: 100, End: 104}, {Start: 106, End: 110}},
147+
changed: true,
148+
},
149+
{
150+
name: "rejects access mode",
151+
switchport: Switchport{Mode: SwitchportModeAccess},
152+
vlan: 100,
153+
wantErr: true,
154+
},
155+
{
156+
name: "rejects unmanaged mode",
157+
switchport: Switchport{
158+
Mode: SwitchportModeTrunk,
159+
AllowedVlansMode: AllowedVlansModeUnmanaged,
160+
},
161+
vlan: 100,
162+
wantErr: true,
163+
},
164+
{
165+
name: "rejects invalid VLAN",
166+
switchport: Switchport{Mode: SwitchportModeTrunk},
167+
vlan: 0,
168+
wantErr: true,
169+
},
170+
}
171+
172+
for _, test := range tests {
173+
t.Run(test.name, func(t *testing.T) {
174+
switchport := test.switchport
175+
changed, err := switchport.RemoveAllowedVLAN(test.vlan)
176+
if test.wantErr {
177+
if err == nil {
178+
t.Fatalf("RemoveAllowedVLAN(%d) returned no error", test.vlan)
179+
}
180+
return
181+
}
182+
if err != nil {
183+
t.Fatalf("RemoveAllowedVLAN(%d): %v", test.vlan, err)
184+
}
185+
if changed != test.changed {
186+
t.Fatalf("RemoveAllowedVLAN(%d) changed = %t, want %t", test.vlan, changed, test.changed)
187+
}
188+
if !reflect.DeepEqual(switchport.AllowedVlans, test.want) {
189+
t.Fatalf("RemoveAllowedVLAN(%d) ranges = %#v, want %#v", test.vlan, switchport.AllowedVlans, test.want)
190+
}
191+
})
192+
}
193+
}

‎api/evpn/v1alpha1/groupversion_info.go‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const (
3030
// FabricLabel is applied to all sub-resources created by a Fabric controller,
3131
// with the value set to the owning Fabric's name.
3232
FabricLabel = "evpn.networking.metal.ironcore.dev/fabric"
33+
34+
// NetworkLabel identifies the Network associated with an EVPN resource.
35+
NetworkLabel = "evpn.networking.metal.ironcore.dev/network"
36+
37+
// NetworkAttachmentLabel identifies the NetworkAttachment that realizes an EVPN resource.
38+
NetworkAttachmentLabel = "evpn.networking.metal.ironcore.dev/network-attachment"
3339
)
3440

3541
// Fabric condition types.
@@ -51,4 +57,34 @@ const (
5157

5258
// NoResourcesReason indicates no child resources exist yet for this condition.
5359
NoResourcesReason = "NoResources"
60+
61+
// NoMatchingInterfacesReason indicates that an Interface selector matched no Interfaces.
62+
NoMatchingInterfacesReason = "NoMatchingInterfaces"
63+
64+
// InvalidInterfaceReason indicates that a selected Interface cannot realize the attachment.
65+
InvalidInterfaceReason = "InvalidInterface"
66+
67+
// DeviceResolutionFailedReason indicates that selected Interfaces reference unresolved Devices.
68+
DeviceResolutionFailedReason = "DeviceResolutionFailed"
69+
70+
// FabricResolutionFailedReason indicates that selected Devices do not resolve to exactly one Fabric.
71+
FabricResolutionFailedReason = "FabricResolutionFailed"
72+
73+
// EncapsulationConflictReason indicates that attachments request the same encapsulation on a Device.
74+
EncapsulationConflictReason = "EncapsulationConflict"
75+
76+
// NetworkNotFoundReason indicates that the referenced Network does not exist.
77+
NetworkNotFoundReason = "NetworkNotFound"
78+
79+
// InvalidVNIReason indicates that an allocated VNI is invalid.
80+
InvalidVNIReason = "InvalidVNI"
81+
82+
// UnsupportedASNumberReason indicates that the Fabric AS number cannot be used for route-target derivation.
83+
UnsupportedASNumberReason = "UnsupportedASNumber"
84+
85+
// ResourceConflictReason indicates that an existing resource prevents attachment realization.
86+
ResourceConflictReason = "ResourceConflict"
87+
88+
// StretchedNetworkNotSupportedReason indicates that a Network resolves to multiple Fabrics.
89+
StretchedNetworkNotSupportedReason = "StretchedNetworkNotSupported"
5490
)

‎api/overlay/v1alpha1/networkattachment_types.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package v1alpha1
55

66
import (
7+
"fmt"
8+
79
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
810
"k8s.io/apimachinery/pkg/runtime"
911

@@ -26,6 +28,7 @@ type NetworkAttachmentSpec struct {
2628

2729
// Encapsulation defines the encapsulation parameters for this attachment.
2830
// +required
31+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="encapsulation is immutable"
2932
Encapsulation Encapsulation `json:"encapsulation"`
3033
}
3134

@@ -51,6 +54,11 @@ type Encapsulation struct {
5154
ID int32 `json:"id"`
5255
}
5356

57+
// Key returns the stable identity of the encapsulation.
58+
func (e Encapsulation) Key() string {
59+
return fmt.Sprintf("%s/%d", e.Type, e.ID)
60+
}
61+
5462
// NetworkAttachmentStatus defines the observed state of NetworkAttachment.
5563
type NetworkAttachmentStatus struct {
5664
// For Kubernetes API conventions, see:
@@ -65,6 +73,27 @@ type NetworkAttachmentStatus struct {
6573
// +patchMergeKey=type
6674
// +optional
6775
Conditions []metav1.Condition `json:"conditions,omitempty"`
76+
77+
// forwardingContextRef references the forwarding context realizing this attachment.
78+
// +optional
79+
ForwardingContextRef *corev1alpha1.TypedLocalObjectReference `json:"forwardingContextRef,omitempty"`
80+
81+
// interfaces records Interface/VLAN memberships owned by this attachment.
82+
// Entries may represent memberships being provisioned or cleaned up.
83+
// +optional
84+
// +listType=atomic
85+
Interfaces []NetworkAttachmentInterfaceStatus `json:"interfaces,omitempty"`
86+
}
87+
88+
// NetworkAttachmentInterfaceStatus records realized VLAN membership on an Interface.
89+
type NetworkAttachmentInterfaceStatus struct {
90+
// deviceRef references the Device that owns the Interface.
91+
// +required
92+
DeviceRef corev1alpha1.LocalObjectReference `json:"deviceRef"`
93+
94+
// interfaceRef references the realized Interface.
95+
// +required
96+
InterfaceRef corev1alpha1.LocalObjectReference `json:"interfaceRef"`
6897
}
6998

7099
// +kubebuilder:object:root=true

0 commit comments

Comments
 (0)