Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipam nesting #37

Merged
merged 2 commits into from
Nov 22, 2024
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
2 changes: 1 addition & 1 deletion apis/backend/as/asindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (r *ASIndex) GetMinClaim() backend.ClaimObject {
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Kind: ASIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
2 changes: 1 addition & 1 deletion apis/backend/extcomm/extcommindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (r *EXTCOMMIndex) GetMinClaim() backend.ClaimObject {
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Kind: EXTCOMMIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
14 changes: 5 additions & 9 deletions apis/backend/ipam/ipam_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,21 +73,17 @@ func GetIPClaimSummaryTypeFromString(s string) IPClaimSummaryType {
type IPPrefixType string

const (
IPPrefixType_Invalid IPPrefixType = "invalid"
IPPrefixType_Other IPPrefixType = "other"
IPPrefixType_Pool IPPrefixType = "pool"
IPPrefixType_Network IPPrefixType = "network"
IPPrefixType_Aggregate IPPrefixType = "aggregate"
IPPrefixType_Invalid IPPrefixType = "invalid"
IPPrefixType_Other IPPrefixType = "other"
IPPrefixType_Network IPPrefixType = "network"
)

func GetIPPrefixTypeFromString(s string) *IPPrefixType {
switch s {
case string(IPPrefixType_Pool):
return ptr.To[IPPrefixType](IPPrefixType_Pool)
case string(IPPrefixType_Network):
return ptr.To[IPPrefixType](IPPrefixType_Network)
case string(IPPrefixType_Aggregate):
return ptr.To[IPPrefixType](IPPrefixType_Aggregate)
case string(IPPrefixType_Other):
return ptr.To[IPPrefixType](IPPrefixType_Other)
default:
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion apis/backend/ipam/ipclaim_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (r *IPClaim) GetIPPrefixType() IPPrefixType {
return IPPrefixType_Other
}
switch *r.Spec.PrefixType {
case IPPrefixType_Aggregate, IPPrefixType_Network, IPPrefixType_Pool:
case IPPrefixType_Network:
return *r.Spec.PrefixType
default:
return IPPrefixType_Invalid
Expand Down
9 changes: 4 additions & 5 deletions apis/backend/ipam/ipclaim_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import (
type IPClaimSpec struct {
// Index defines the index for the IP Entry
Index string `json:"index" yaml:"index" protobuf:"bytes,1,opt,name=index"`
// PrefixType defines the prefixtype of IPEntry; for address and range claims this is not relevant
// - network kind is used for physical, virtual nics on a device
// - pool kind is used for allocating dedicated IP addresses
// - aggregate kind is used for claiming an aggregate prefix; only used for networkInstance prefixes
// +kubebuilder:validation:Enum=`network`;`aggregate`;`pool`;
// PrefixType network indicates a special type of prefix for which network and broadcast addresses
// are claimed in the ipam, used for physical, virtual nics devices
// If no prefixes type is defined the internally this is defaulted to other
// +kubebuilder:validation:Enum=`network`;`other`;
// +optional
PrefixType *IPPrefixType `json:"prefixType,omitempty" yaml:"prefixType,omitempty" protobuf:"bytes,2,opt,name=prefixType"`
// Prefix defines the prefix for the IP claim
Expand Down
2 changes: 1 addition & 1 deletion apis/backend/ipam/ipentry_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (r *IPEntry) GetIPPrefixType() IPPrefixType {
return IPPrefixType_Other
}
switch *r.Spec.PrefixType {
case IPPrefixType_Aggregate, IPPrefixType_Network, IPPrefixType_Pool:
case IPPrefixType_Network:
return *r.Spec.PrefixType
default:
return IPPrefixType_Invalid
Expand Down
2 changes: 1 addition & 1 deletion apis/backend/ipam/ipindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *IPIndex) GetClaim(prefix Prefix) (*IPClaim, error) {
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Kind: IPIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
2 changes: 1 addition & 1 deletion apis/backend/vlan/vlanindex_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (r *VLANIndex) GetMaxClaim() backend.ClaimObject {
OwnerReferences: []metav1.OwnerReference{
{
APIVersion: schema.GroupVersion{Group: SchemeGroupVersion.Group, Version: "v1alpha1"}.Identifier(),
Kind: r.Kind,
Kind: VLANIndexKind,
Name: r.Name,
UID: r.UID,
},
Expand Down
5 changes: 5 additions & 0 deletions crds/as.be.kuid.dev_asentries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ spec:
index:
description: Index defines the index for the resource
type: string
indexEntry:
description: IndexEntry identifies if the entry is originated from
an IP Index
type: boolean
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -268,6 +272,7 @@ spec:
x-kubernetes-map-type: atomic
required:
- index
- indexEntry
type: object
status:
description: ASEntryStatus defines the observed state of ASEntry
Expand Down
5 changes: 5 additions & 0 deletions crds/extcomm.be.kuid.dev_extcommentries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ spec:
description: EXTCOMMIndex defines the EXTCOMM index for the EXTCOMM
Claim
type: string
indexEntry:
description: IndexEntry identifies if the entry is originated from
an IP Index
type: boolean
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -280,6 +284,7 @@ spec:
required:
- claim
- index
- indexEntry
type: object
status:
description: EXTCOMMEntryStatus defines the observed state of EXTCOMMEntry
Expand Down
5 changes: 5 additions & 0 deletions crds/genid.be.kuid.dev_genidentries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ spec:
index:
description: Index defines the index for the resource
type: string
indexEntry:
description: IndexEntry identifies if the entry is originated from
an IP Index
type: boolean
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -268,6 +272,7 @@ spec:
x-kubernetes-map-type: atomic
required:
- index
- indexEntry
type: object
status:
description: GENIDEntryStatus defines the observed state of GENIDEntry
Expand Down
10 changes: 4 additions & 6 deletions crds/ipam.be.kuid.dev_ipclaims.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,12 @@ spec:
type: integer
prefixType:
description: |-
PrefixType defines the prefixtype of IPEntry; for address and range claims this is not relevant
- network kind is used for physical, virtual nics on a device
- pool kind is used for allocating dedicated IP addresses
- aggregate kind is used for claiming an aggregate prefix; only used for networkInstance prefixes
PrefixType network indicates a special type of prefix for which network and broadcast addresses
are claimed in the ipam, used for physical, virtual nics devices
If no prefixes type is defined the internally this is defaulted to other
enum:
- network
- aggregate
- pool
- other
type: string
range:
description: Range defines the range for the IP claim
Expand Down
5 changes: 5 additions & 0 deletions crds/vlan.be.kuid.dev_vlanentries.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ spec:
index:
description: Index defines the index for the resource
type: string
indexEntry:
description: IndexEntry identifies if the entry is originated from
an IP Index
type: boolean
labels:
additionalProperties:
type: string
Expand Down Expand Up @@ -103,6 +107,7 @@ spec:
x-kubernetes-map-type: atomic
required:
- index
- indexEntry
type: object
status:
description: VLANEntryStatus defines the observed state of VLANEntry
Expand Down
File renamed without changes.
47 changes: 23 additions & 24 deletions pkg/backend/ipam/applicator_dynamic_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,31 @@ func (r *dynamicAddressApplicator) selectAddress(_ context.Context, claim *ipam.
return iputil.NewPrefixInfo(netip.PrefixFrom(addr, int(pi.GetAddressPrefixLength()))), nil

case ipam.IPClaimSummaryType_Prefix:
if parentIPPrefixType != nil && (*parentIPPrefixType == ipam.IPPrefixType_Network || *parentIPPrefixType == ipam.IPPrefixType_Pool) {
parentpi := iputil.NewPrefixInfo(parentRoute.Prefix())
if claim.Status.Address != nil {
statuspi, err := iputil.New(*claim.Status.Address)
if err != nil {
return nil, err
}
// check if the route is free in the rib
prefixLength := pi.GetAddressPrefixLength()
if _, ok := r.cacheInstanceCtx.rib.Get(netip.PrefixFrom(statuspi.Addr(), prefixLength.Int())); !ok {
return statuspi, nil
}
}

// gather the prefixLength - use address based prefixLength /32 or /128 to validate the rib
// for netowork allocations use the parent prefixLength
parentpi := iputil.NewPrefixInfo(parentRoute.Prefix())
if claim.Status.Address != nil {
statuspi, err := iputil.New(*claim.Status.Address)
if err != nil {
return nil, err
}
// check if the route is free in the rib
prefixLength := pi.GetAddressPrefixLength()
if isParentRouteSelectable(parentRoute, uint8(prefixLength)) {
p := r.cacheInstanceCtx.rib.GetAvailablePrefixByBitLen(pi.GetIPPrefix(), uint8(prefixLength.Int()))
if p.IsValid() {
// success, parentClaimType was already checked for non nil
if *parentIPPrefixType == ipam.IPPrefixType_Network {
return iputil.NewPrefixInfo(netip.PrefixFrom(p.Addr(), int(parentpi.GetPrefixLength()))), nil
} else {
return iputil.NewPrefixInfo(p), nil
}
if _, ok := r.cacheInstanceCtx.rib.Get(netip.PrefixFrom(statuspi.Addr(), prefixLength.Int())); !ok {
return statuspi, nil
}
}

// gather the prefixLength - use address based prefixLength /32 or /128 to validate the rib
// for netowork allocations use the parent prefixLength
prefixLength := pi.GetAddressPrefixLength()
if isParentRouteSelectable(parentRoute, uint8(prefixLength)) {
p := r.cacheInstanceCtx.rib.GetAvailablePrefixByBitLen(pi.GetIPPrefix(), uint8(prefixLength.Int()))
if p.IsValid() {
// success, parentClaimType was already checked for non nil
if *parentIPPrefixType == ipam.IPPrefixType_Network {
return iputil.NewPrefixInfo(netip.PrefixFrom(p.Addr(), int(parentpi.GetPrefixLength()))), nil
} else {
return iputil.NewPrefixInfo(p), nil
}
}
}
Expand Down
23 changes: 13 additions & 10 deletions pkg/backend/ipam/applicator_static_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,18 @@ func (r *staticPrefixApplicator) validateParent(_ context.Context, route table.R
parentClaimSummaryType := routeLabels[backend.KuidIPAMClaimSummaryTypeKey]
parentClaimPrefixType := routeLabels[backend.KuidIPAMIPPrefixTypeKey]
switch prefixType {
case ipam.IPPrefixType_Aggregate:
// aggregates can be aggregated by aggregates
case ipam.IPPrefixType_Other:
if parentClaimPrefixType == string(ipam.IPPrefixType_Network) {
return fmt.Errorf("parent %s/%s nesting %s/%s is not possible", route.Prefix().String(), *claim.Spec.Prefix, parentClaimPrefixType, prefixType)
}
return nil
case ipam.IPPrefixType_Network, ipam.IPPrefixType_Pool:
case ipam.IPPrefixType_Network:
// we only allow range and addresses -> these dont have a claimType
if parentClaimSummaryType == string(ipam.IPClaimSummaryType_Address) ||
parentClaimSummaryType == string(ipam.IPClaimSummaryType_Range) {
return fmt.Errorf("parent %s not allowed in claim of type %s", parentClaimSummaryType, prefixType)
}
if parentClaimPrefixType == string(ipam.IPPrefixType_Network) ||
parentClaimPrefixType == string(ipam.IPPrefixType_Pool) {
if parentClaimPrefixType == string(ipam.IPPrefixType_Network) {
return fmt.Errorf("parent %s/%s nesting %s/%s is not possible", route.Prefix().String(), *claim.Spec.Prefix, parentClaimPrefixType, prefixType)
}
default:
Expand All @@ -133,7 +134,7 @@ func (r *staticPrefixApplicator) validateParent(_ context.Context, route table.R
}

func (r *staticPrefixApplicator) validateChildren(_ context.Context, claim *ipam.IPClaim) error {
// network, aggregate, pool
// network, other
prefixType := claim.GetIPPrefixType()
pi, err := iputil.New(*claim.Spec.Prefix)
if err != nil {
Expand All @@ -148,18 +149,20 @@ func (r *staticPrefixApplicator) validateChildren(_ context.Context, claim *ipam
for _, childRoute := range childRoutes {
routeLabels := childRoute.Labels()
childClaimSummaryType := routeLabels[backend.KuidIPAMClaimSummaryTypeKey]
childPrefixType := routeLabels[backend.KuidIPAMIPPrefixTypeKey]
//childPrefixType := routeLabels[backend.KuidIPAMIPPrefixTypeKey]
switch prefixType {
case ipam.IPPrefixType_Aggregate: // the claim is of type aggregate
// we only allow prefixes -> validate aggregate type
case ipam.IPPrefixType_Other: // the claim is of type aggregate
// TODO insertion of prefixes
/*
if childClaimSummaryType == string(ipam.IPClaimSummaryType_Address) ||
childClaimSummaryType == string(ipam.IPClaimSummaryType_Range) {
return fmt.Errorf("child with addressing %s not allowed in claim of type %s", childClaimSummaryType, prefixType)
}
if childPrefixType == string(ipam.IPPrefixType_Aggregate) {
return fmt.Errorf("nesting %s is not possible", childPrefixType)
}
case ipam.IPPrefixType_Network, ipam.IPPrefixType_Pool:
*/
case ipam.IPPrefixType_Network:
// we only allow range and addresses -> these dont have a claimType
if childClaimSummaryType == string(ipam.IPClaimSummaryType_Prefix) {
return fmt.Errorf("child with addressing %s not allowed in claim of type %s", childClaimSummaryType, prefixType)
Expand Down
10 changes: 2 additions & 8 deletions pkg/backend/ipam/applicator_static_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"github.com/hansthienpondt/nipam/pkg/table"
"github.com/henderiw/iputil"
"github.com/henderiw/logger/log"
"github.com/kuidio/kuid/apis/backend"
"github.com/kuidio/kuid/apis/backend/ipam"
"go4.org/netipx"
)
Expand Down Expand Up @@ -111,13 +110,8 @@ func (r *staticRangeApplicator) validateParents(ctx context.Context, claim *ipam
return nil
}

func (r *staticRangeApplicator) validateParent(_ context.Context, route table.Route, _ *ipam.IPClaim) error {
routeLabels := route.Labels()
parentPrefixType := routeLabels[backend.KuidIPAMIPPrefixTypeKey]

if parentPrefixType == string(ipam.IPPrefixType_Aggregate) {
//return fmt.Errorf("a range is not possible with a parent of type %s", parentPrefixType)
}
func (r *staticRangeApplicator) validateParent(_ context.Context, _ table.Route, _ *ipam.IPClaim) error {
// a range can be allocated on any parent
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/backend/ipam/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (r *be) CreateIndex(ctx context.Context, obj runtime.Object) error {
return err
}
}
log.Debug("update IPIndex claims", "object", obj)
//log.Info("update IPIndex claims", "object", index)
return r.updateIPIndexClaims(ctx, index)
}

Expand Down
Loading
Loading