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

Support reservation of IpRanges #130

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
16 changes: 16 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ resources:
kind: PrefixClaim
path: github.com/netbox-community/netbox-operator/api/v1
version: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: netbox.dev
kind: IpRangeClaim
path: github.com/netbox-community/netbox-operator/api/v1
version: v1
- api:
crdVersion: v1
namespaced: true
controller: true
domain: netbox.dev
kind: IpRange
path: github.com/netbox-community/netbox-operator/api/v1
version: v1
version: "3"
113 changes: 113 additions & 0 deletions api/v1/iprange_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
Copyright 2024 Swisscom (Schweiz) AG.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

henrybear327 marked this conversation as resolved.
Show resolved Hide resolved
// IpRangeSpec defines the desired state of IpRange
type IpRangeSpec struct {
// the startAddress is the first ip address included in the ip range
//+kubebuilder:validation:Format=cidr
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'startAddress' is immutable"
//+kubebuilder:validation:Required
StartAddress string `json:"startAddress"`

// the endAddress is the last ip address included in the ip range
//+kubebuilder:validation:Format=cidr
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'endAddress' is immutable"
//+kubebuilder:validation:Required
EndAddress string `json:"endAddress"`
alexandernorth marked this conversation as resolved.
Show resolved Hide resolved

//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'tenant' is immutable"
Tenant string `json:"tenant,omitempty"`

CustomFields map[string]string `json:"customFields,omitempty"`

Comments string `json:"comments,omitempty"`

Description string `json:"description,omitempty"`

PreserveInNetbox bool `json:"preserveInNetbox,omitempty"`
}

// IpRangeStatus defines the observed state of IpRange
type IpRangeStatus struct {
IpRangeId int64 `json:"id,omitempty"`

IpRangeUrl string `json:"url,omitempty"`

Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion
//+kubebuilder:printcolumn:name="StartAddress",type=string,JSONPath=`.spec.startAddress`
//+kubebuilder:printcolumn:name="EndAddress",type=string,JSONPath=`.spec.endAddress`
//+kubebuilder:printcolumn:name="Ready",type=string,JSONPath=`.status.conditions[?(@.type=="Ready")].status`
//+kubebuilder:printcolumn:name="ID",type=string,JSONPath=`.status.id`
//+kubebuilder:printcolumn:name="URL",type=string,JSONPath=`.status.url`
//+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:resource:shortName=ir
alexandernorth marked this conversation as resolved.
Show resolved Hide resolved

// IpRange is the Schema for the ipranges API
type IpRange struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec IpRangeSpec `json:"spec,omitempty"`
Status IpRangeStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// IpRangeList contains a list of IpRange
type IpRangeList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []IpRange `json:"items"`
}

func init() {
SchemeBuilder.Register(&IpRange{}, &IpRangeList{})
}

var ConditionIpRangeReadyTrue = metav1.Condition{
Type: "Ready",
Status: "True",
Reason: "IpRangeReservedInNetbox",
Message: "Ip Range was reserved/updated in NetBox",
}

var ConditionIpRangeReadyFalse = metav1.Condition{
Type: "Ready",
Status: "False",
Reason: "FailedToReserveIpRangeInNetbox",
Message: "Failed to reserve Ip Range in NetBox",
}

var ConditionIpRangeReadyFalseDeletionFailed = metav1.Condition{
Type: "Ready",
Status: "False",
Reason: "FailedToDeleteIpRangeInNetbox",
Message: "Failed to delete Ip Range in NetBox",
}
144 changes: 144 additions & 0 deletions api/v1/iprangeclaim_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
Copyright 2024 Swisscom (Schweiz) AG.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// IpRangeClaimSpec defines the desired state of IpRangeClaim
type IpRangeClaimSpec struct {
//+kubebuilder:validation:Required
//+kubebuilder:validation:Format=cidr
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'parentPrefix' is immutable"
ParentPrefix string `json:"parentPrefix"`

//+kubebuilder:validation:Required
//+kubebuilder:validation:Minimum=2
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'size' is immutable"
Size int `json:"size,omitempty"`
Copy link
Contributor

@jstudler jstudler Nov 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a current de-facto hard limit here which is at 50 (should be reproducible by trying to create a iprc with .spec.size 51 in an empty /16 prefix) and a soft limit that might be even lower (depending on the fragmentation of IPs/IP Ranges in the prefix). I assume this is due to pagination in the API. I propose to add validation Maximum=50 here and add a comment to this field but leave the code as is for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a validation rule for maximum value for size until pagination is added to the function searching an available iprange.


//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="Field 'tenant' is immutable"
Tenant string `json:"tenant,omitempty"`

CustomFields map[string]string `json:"customFields,omitempty"`

Comments string `json:"comments,omitempty"`

Description string `json:"description,omitempty"`

PreserveInNetbox bool `json:"preserveInNetbox,omitempty"`
}

// IpRangeClaimStatus defines the observed state of IpRangeClaim
type IpRangeClaimStatus struct {
IpRange string `json:"ipRange,omitempty"`

IpRangeDotDecimal string `json:"ipRangeDotDecimal,omitempty"`

IpAddresses []string `json:"ipAddresses,omitempty"`

IpAddressesDotDecimal []string `json:"ipAddressesDotDecimal,omitempty"`

StartAddress string `json:"startAddress,omitempty"`

StartAddressDotDecimal string `json:"startAddressDotDecimal,omitempty"`

EndAddress string `json:"endAddress,omitempty"`

EndAddressDotDecimal string `json:"endAddressDotDecimal,omitempty"`

IpRangeName string `json:"ipAddressName,omitempty"`

Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type" protobuf:"bytes,1,rep,name=conditions"`
}

//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:storageversion
//+kubebuilder:printcolumn:name="IpRange",type=string,JSONPath=`.status.ipRange`
//+kubebuilder:printcolumn:name="IpRangeAssigned",type=string,JSONPath=`.status.conditions[?(@.type=="IpRangeAssigned")].status`
//+kubebuilder:printcolumn:name="Ready",type=string,JSONPath=`.status.conditions[?(@.type=="Ready")].status`
//+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp`
// +kubebuilder:resource:shortName=irc
alexandernorth marked this conversation as resolved.
Show resolved Hide resolved

// IpRangeClaim is the Schema for the iprangeclaims API
type IpRangeClaim struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec IpRangeClaimSpec `json:"spec,omitempty"`
Status IpRangeClaimStatus `json:"status,omitempty"`
}

//+kubebuilder:object:root=true

// IpRangeClaimList contains a list of IpRangeClaim
type IpRangeClaimList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []IpRangeClaim `json:"items"`
}

func init() {
SchemeBuilder.Register(&IpRangeClaim{}, &IpRangeClaimList{})
}

var ConditionIpRangeClaimReadyTrue = metav1.Condition{
Type: "Ready",
Status: "True",
Reason: "IpRangeResourceReady",
Message: "Ip Range Resource is ready",
}

var ConditionIpRangeClaimReadyFalse = metav1.Condition{
Type: "Ready",
Status: "False",
Reason: "IpRangeResourceNotReady",
Message: "Ip Range Resource is not ready",
}

var ConditionIpRangeClaimReadyFalseStatusGen = metav1.Condition{
Type: "Ready",
Status: "False",
Reason: "IpRangeClaimStatusGenerationFailed",
Message: "Failed to generate Ip Range Status",
}

var ConditionIpRangeAssignedTrue = metav1.Condition{
Type: "IpRangeAssigned",
Status: "True",
Reason: "IpRangeCRCreated",
Message: "New Ip Range fetched from NetBox and IpRange CR was created",
}

var ConditionIpRangeAssignedFalse = metav1.Condition{
Type: "IpRangeAssigned",
Status: "False",
Reason: "IpRangeCRNotCreated",
Message: "Failed to fetch new Ip Range from NetBox",
}

var ConditionIpRangeAssignedFalseSizeMissmatch = metav1.Condition{
Type: "IpRangeAssigned",
Status: "False",
Reason: "IpRangeCRNotCreated",
Message: "Assigned/Resored ip range has less available ip addresses than requested",
bruelea marked this conversation as resolved.
Show resolved Hide resolved
}
alexandernorth marked this conversation as resolved.
Show resolved Hide resolved
Loading