Skip to content
Merged
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
82 changes: 82 additions & 0 deletions api/core/v1alpha1/interface_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package v1alpha1

import (
"fmt"
"sort"
"sync"

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

// AddAllowedVLAN adds a VLAN to the explicit allowed VLAN list.
// It reports whether the list changed.
func (s *Switchport) AddAllowedVLAN(vlan int32) (bool, error) {
if err := s.ValidateAllowedVLAN(vlan); err != nil {
return false, err
}
value := int64(vlan)
// An existing range already allows the VLAN, so no mutation is needed.
for _, vlanRange := range s.AllowedVlans {
if vlanRange.Start <= value && value <= vlanRange.End {
return false, nil
}
}
// Appending a singleton makes an implicit all-VLAN list explicit and takes ownership of it.
s.AllowedVlans = append(s.AllowedVlans, IndexRange{Start: value, End: value})
s.SortAllowedVLANs()
return true, nil
}

// RemoveAllowedVLAN removes a VLAN from the explicit allowed VLAN list.
// It reports whether the list changed.
func (s *Switchport) RemoveAllowedVLAN(vlan int32) (bool, error) {
if err := s.ValidateAllowedVLAN(vlan); err != nil {
return false, err
}
value := int64(vlan)
// Find the single range containing the VLAN and remove only that value from it.
for i, vlanRange := range s.AllowedVlans {
if value < vlanRange.Start || value > vlanRange.End {
continue
}
switch {
// Remove the whole singleton range.
case vlanRange.Start == value && vlanRange.End == value:
s.AllowedVlans = append(s.AllowedVlans[:i], s.AllowedVlans[i+1:]...)
// Shrink the range past its first VLAN.
case vlanRange.Start == value:
s.AllowedVlans[i].Start++
// Shrink the range before its last VLAN.
case vlanRange.End == value:
s.AllowedVlans[i].End--
// Split the range around an interior VLAN.
default:
s.AllowedVlans[i].End = value - 1
s.AllowedVlans = append(s.AllowedVlans, IndexRange{Start: value + 1, End: vlanRange.End})
s.SortAllowedVLANs()
}
if len(s.AllowedVlans) == 0 {
// nil restores the API default after the final explicit VLAN is removed.
s.AllowedVlans = nil
}
return true, nil
}
return false, nil
}

// SortAllowedVLANs orders the explicit allowed VLAN ranges by start and then end in-place.
func (s *Switchport) SortAllowedVLANs() {
sort.Slice(s.AllowedVlans, func(i, j int) bool {
if s.AllowedVlans[i].Start != s.AllowedVlans[j].Start {
return s.AllowedVlans[i].Start < s.AllowedVlans[j].Start
}
return s.AllowedVlans[i].End < s.AllowedVlans[j].End
})
}

// ValidateAllowedVLAN validates that the switchport permits changing the given VLAN.
func (s *Switchport) ValidateAllowedVLAN(vlan int32) error {
if s.Mode != SwitchportModeTrunk {
return fmt.Errorf("switchport mode %q does not support allowed VLANs", s.Mode)
}
if s.AllowedVlansMode == AllowedVlansModeUnmanaged {
return fmt.Errorf("allowed VLAN mode %q does not permit changes", s.AllowedVlansMode)
}
if vlan < 1 || vlan > 4094 {
return fmt.Errorf("VLAN %d is outside the valid range 1..4094", vlan)
}
return nil
}

// AllowedVlansMode defines how trunk allowed VLANs are managed.
// +kubebuilder:validation:Enum=Exact;Unmanaged
type AllowedVlansMode string
Expand Down
193 changes: 193 additions & 0 deletions api/core/v1alpha1/interface_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
// SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: Apache-2.0

package v1alpha1

import (
"reflect"
"testing"
)

func TestSwitchportAddAllowedVLAN(t *testing.T) {
tests := []struct {
name string
switchport Switchport
vlan int32
want []IndexRange
changed bool
wantErr bool
}{
{
name: "takes ownership of nil list",
switchport: Switchport{Mode: SwitchportModeTrunk},
vlan: 100,
want: []IndexRange{{Start: 100, End: 100}},
changed: true,
},
{
name: "keeps containing range",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 90, End: 110}},
},
vlan: 100,
want: []IndexRange{{Start: 90, End: 110}},
},
{
name: "adds sorted singleton",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 200, End: 210}},
},
vlan: 100,
want: []IndexRange{{Start: 100, End: 100}, {Start: 200, End: 210}},
changed: true,
},
{
name: "rejects access mode",
switchport: Switchport{Mode: SwitchportModeAccess},
vlan: 100,
wantErr: true,
},
{
name: "rejects unmanaged mode",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlansMode: AllowedVlansModeUnmanaged,
},
vlan: 100,
wantErr: true,
},
{
name: "rejects invalid VLAN",
switchport: Switchport{Mode: SwitchportModeTrunk},
vlan: 4095,
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
switchport := test.switchport
changed, err := switchport.AddAllowedVLAN(test.vlan)
if test.wantErr {
if err == nil {
t.Fatalf("AddAllowedVLAN(%d) returned no error", test.vlan)
}
return
}
if err != nil {
t.Fatalf("AddAllowedVLAN(%d): %v", test.vlan, err)
}
if changed != test.changed {
t.Fatalf("AddAllowedVLAN(%d) changed = %t, want %t", test.vlan, changed, test.changed)
}
if !reflect.DeepEqual(switchport.AllowedVlans, test.want) {
t.Fatalf("AddAllowedVLAN(%d) ranges = %#v, want %#v", test.vlan, switchport.AllowedVlans, test.want)
}
})
}
}

func TestSwitchportRemoveAllowedVLAN(t *testing.T) {
tests := []struct {
name string
switchport Switchport
vlan int32
want []IndexRange
changed bool
wantErr bool
}{
{
name: "keeps absent VLAN",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 10, End: 20}},
},
vlan: 100,
want: []IndexRange{{Start: 10, End: 20}},
},
{
name: "removes final VLAN",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 100, End: 100}},
},
vlan: 100,
changed: true,
},
{
name: "shrinks range start",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
},
vlan: 100,
want: []IndexRange{{Start: 101, End: 110}},
changed: true,
},
{
name: "shrinks range end",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
},
vlan: 110,
want: []IndexRange{{Start: 100, End: 109}},
changed: true,
},
{
name: "splits range",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlans: []IndexRange{{Start: 100, End: 110}},
},
vlan: 105,
want: []IndexRange{{Start: 100, End: 104}, {Start: 106, End: 110}},
changed: true,
},
{
name: "rejects access mode",
switchport: Switchport{Mode: SwitchportModeAccess},
vlan: 100,
wantErr: true,
},
{
name: "rejects unmanaged mode",
switchport: Switchport{
Mode: SwitchportModeTrunk,
AllowedVlansMode: AllowedVlansModeUnmanaged,
},
vlan: 100,
wantErr: true,
},
{
name: "rejects invalid VLAN",
switchport: Switchport{Mode: SwitchportModeTrunk},
vlan: 0,
wantErr: true,
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
switchport := test.switchport
changed, err := switchport.RemoveAllowedVLAN(test.vlan)
if test.wantErr {
if err == nil {
t.Fatalf("RemoveAllowedVLAN(%d) returned no error", test.vlan)
}
return
}
if err != nil {
t.Fatalf("RemoveAllowedVLAN(%d): %v", test.vlan, err)
}
if changed != test.changed {
t.Fatalf("RemoveAllowedVLAN(%d) changed = %t, want %t", test.vlan, changed, test.changed)
}
if !reflect.DeepEqual(switchport.AllowedVlans, test.want) {
t.Fatalf("RemoveAllowedVLAN(%d) ranges = %#v, want %#v", test.vlan, switchport.AllowedVlans, test.want)
}
})
}
}
8 changes: 3 additions & 5 deletions api/evpn/v1alpha1/fabric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ type FabricSpec struct {

// vniPool references an IndexPool from which VNIs are allocated for Networks
// realized by this Fabric.
// This field is introduced as optional until the NetworkAttachment
// controller is implemented. The field currently has no effect.
// TODO: Make this field required once the NetworkAttachment controller is implemented.
// +optional
VNIPool corev1alpha1.TypedLocalObjectReference `json:"vniPool,omitempty"`
// +required
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="vniPool is immutable"
VNIPool corev1alpha1.TypedLocalObjectReference `json:"vniPool"`
}

// FabricLoopbacksSpec configures IP address allocation for loopback interfaces.
Expand Down
36 changes: 36 additions & 0 deletions api/evpn/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ const (
// FabricLabel is applied to all sub-resources created by a Fabric controller,
// with the value set to the owning Fabric's name.
FabricLabel = "evpn.networking.metal.ironcore.dev/fabric"

// NetworkLabel identifies the Network associated with an EVPN resource.
NetworkLabel = "evpn.networking.metal.ironcore.dev/network"

// NetworkAttachmentLabel identifies the NetworkAttachment that realizes an EVPN resource.
NetworkAttachmentLabel = "evpn.networking.metal.ironcore.dev/network-attachment"
)

// Fabric condition types.
Expand All @@ -51,4 +57,34 @@ const (

// NoResourcesReason indicates no child resources exist yet for this condition.
NoResourcesReason = "NoResources"

// NoMatchingInterfacesReason indicates that an Interface selector matched no Interfaces.
NoMatchingInterfacesReason = "NoMatchingInterfaces"

// InvalidInterfaceReason indicates that a selected Interface cannot realize the attachment.
InvalidInterfaceReason = "InvalidInterface"

// DeviceResolutionFailedReason indicates that selected Interfaces reference unresolved Devices.
DeviceResolutionFailedReason = "DeviceResolutionFailed"

// FabricResolutionFailedReason indicates that selected Devices do not resolve to exactly one Fabric.
FabricResolutionFailedReason = "FabricResolutionFailed"

// EncapsulationConflictReason indicates that attachments request the same encapsulation on a Device.
EncapsulationConflictReason = "EncapsulationConflict"

// NetworkNotFoundReason indicates that the referenced Network does not exist.
NetworkNotFoundReason = "NetworkNotFound"

// InvalidVNIReason indicates that an allocated VNI is invalid.
InvalidVNIReason = "InvalidVNI"

// UnsupportedASNumberReason indicates that the Fabric AS number cannot be used for route-target derivation.
UnsupportedASNumberReason = "UnsupportedASNumber"

// ResourceConflictReason indicates that an existing resource prevents attachment realization.
ResourceConflictReason = "ResourceConflict"

// StretchedNetworkNotSupportedReason indicates that a Network resolves to multiple Fabrics.
StretchedNetworkNotSupportedReason = "StretchedNetworkNotSupported"
)
Loading
Loading