diff --git a/apis/ipam/v1alpha1/network_types.go b/apis/ipam/v1alpha1/network_types.go index bcaa11c688..362ac6335a 100644 --- a/apis/ipam/v1alpha1/network_types.go +++ b/apis/ipam/v1alpha1/network_types.go @@ -40,6 +40,11 @@ type NetworkSpec struct { // CIDR is the desired CIDR for the remote cluster. // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="CIDR field is immutable" CIDR networkingv1beta1.CIDR `json:"cidr"` + // PreAllocated is the number of IPs to pre-allocate (reserve) in the CIDR, starting from the first IP. + // +kubebuilder:validation:Optional + // +kubebuilder:validation:Minimum=0 + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Reserved field is immutable" + PreAllocated uint `json:"preAllocated"` } // NetworkStatus defines the observed state of Network. diff --git a/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml b/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml index 940b077a4e..b7567b8954 100644 --- a/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml +++ b/deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml @@ -60,6 +60,14 @@ spec: x-kubernetes-validations: - message: CIDR field is immutable rule: self == oldSelf + preAllocated: + description: PreAllocated is the number of IPs to pre-allocate (reserve) + in the CIDR, starting from the first IP. + minimum: 0 + type: integer + x-kubernetes-validations: + - message: Reserved field is immutable + rule: self == oldSelf required: - cidr type: object diff --git a/deployments/liqo/templates/liqo-ipam-networks.yaml b/deployments/liqo/templates/liqo-ipam-networks.yaml index 4ceee13929..cae42f4504 100644 --- a/deployments/liqo/templates/liqo-ipam-networks.yaml +++ b/deployments/liqo/templates/liqo-ipam-networks.yaml @@ -31,6 +31,7 @@ metadata: ipam.liqo.io/network-type: external-cidr spec: cidr: {{ .Values.ipam.externalCIDR }} + preAllocated: 1 # the first IP of the external CIDR is reserved for the unknown source traffic --- apiVersion: ipam.liqo.io/v1alpha1 kind: Network @@ -53,4 +54,5 @@ metadata: ipam.liqo.io/network-not-remapped: "true" spec: cidr: {{ $value }} +--- {{- end }} diff --git a/pkg/ipam/ipam.go b/pkg/ipam/ipam.go index e4945f394d..6a928f1fce 100644 --- a/pkg/ipam/ipam.go +++ b/pkg/ipam/ipam.go @@ -90,7 +90,7 @@ func (lipam *LiqoIPAM) IPRelease(_ context.Context, req *IPReleaseRequest) (*IPR // NetworkAcquire acquires a network. If it is already reserved, it allocates and reserves a new free one with the same prefix length. func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequest) (*NetworkAcquireResponse, error) { - remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), req.GetImmutable()) + remappedCidr, err := lipam.acquireNetwork(req.GetCidr(), uint(req.GetPreAllocated()), req.GetImmutable()) if err != nil { return &NetworkAcquireResponse{}, err } @@ -100,14 +100,14 @@ func (lipam *LiqoIPAM) NetworkAcquire(_ context.Context, req *NetworkAcquireRequ // NetworkRelease releases a network. func (lipam *LiqoIPAM) NetworkRelease(_ context.Context, req *NetworkReleaseRequest) (*NetworkReleaseResponse, error) { - lipam.freeNetwork(req.GetCidr()) + lipam.freeNetwork(network{cidr: req.GetCidr()}) return &NetworkReleaseResponse{}, nil } // NetworkIsAvailable checks if a network is available. func (lipam *LiqoIPAM) NetworkIsAvailable(_ context.Context, req *NetworkAvailableRequest) (*NetworkAvailableResponse, error) { - available := lipam.isNetworkAvailable(req.GetCidr()) + available := lipam.isNetworkAvailable(network{cidr: req.GetCidr()}) return &NetworkAvailableResponse{Available: available}, nil } diff --git a/pkg/ipam/ipam.pb.go b/pkg/ipam/ipam.pb.go index 06336a84d4..9748d89735 100644 --- a/pkg/ipam/ipam.pb.go +++ b/pkg/ipam/ipam.pb.go @@ -258,8 +258,9 @@ type NetworkAcquireRequest struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Cidr string `protobuf:"bytes,1,opt,name=cidr,proto3" json:"cidr,omitempty"` - Immutable bool `protobuf:"varint,2,opt,name=immutable,proto3" json:"immutable,omitempty"` // If true, the network cannot be remapped. It will be allocated if available, or an error will be returned. + Cidr string `protobuf:"bytes,1,opt,name=cidr,proto3" json:"cidr,omitempty"` + Immutable bool `protobuf:"varint,2,opt,name=immutable,proto3" json:"immutable,omitempty"` // If true, the network cannot be remapped. It will be allocated if available, or an error will be returned. + PreAllocated uint32 `protobuf:"varint,3,opt,name=preAllocated,proto3" json:"preAllocated,omitempty"` // The number of IPs to pre-allocate (reserve) in the CIDR, starting from the first IP of the CIDR. } func (x *NetworkAcquireRequest) Reset() { @@ -306,6 +307,13 @@ func (x *NetworkAcquireRequest) GetImmutable() bool { return false } +func (x *NetworkAcquireRequest) GetPreAllocated() uint32 { + if x != nil { + return x.PreAllocated + } + return 0 +} + type NetworkAcquireResponse struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -567,54 +575,56 @@ var file_pkg_ipam_ipam_proto_rawDesc = []byte{ 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x06, 0x72, 0x65, 0x73, - 0x75, 0x6c, 0x74, 0x22, 0x49, 0x0a, 0x15, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, + 0x75, 0x6c, 0x74, 0x22, 0x6d, 0x0a, 0x15, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x12, 0x1c, 0x0a, 0x09, 0x69, 0x6d, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x08, 0x52, 0x09, 0x69, 0x6d, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x22, 0x55, - 0x0a, 0x16, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x12, 0x27, 0x0a, 0x06, - 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x06, 0x72, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2b, 0x0a, 0x15, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, - 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, - 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, - 0x64, 0x72, 0x22, 0x41, 0x0a, 0x16, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, - 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, 0x0a, 0x06, - 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x52, - 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, 0x06, 0x72, - 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2d, 0x0a, 0x17, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, - 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, - 0x63, 0x69, 0x64, 0x72, 0x22, 0x61, 0x0a, 0x18, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, - 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x01, 0x20, - 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x27, - 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, - 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x52, - 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0xbf, 0x02, 0x0a, 0x04, 0x49, 0x50, 0x41, 0x4d, - 0x12, 0x32, 0x0a, 0x09, 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x12, 0x11, 0x2e, - 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, - 0x1a, 0x12, 0x2e, 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, - 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x32, 0x0a, 0x09, 0x49, 0x50, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, - 0x65, 0x12, 0x11, 0x2e, 0x49, 0x50, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x71, - 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x49, 0x50, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, - 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x41, 0x0a, 0x0e, 0x4e, 0x65, 0x74, 0x77, - 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x12, 0x16, 0x2e, 0x4e, 0x65, 0x74, - 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, - 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, - 0x69, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x41, 0x0a, 0x0e, 0x4e, - 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x12, 0x16, 0x2e, - 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, - 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, - 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x49, - 0x0a, 0x12, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x49, 0x73, 0x41, 0x76, 0x61, 0x69, 0x6c, - 0x61, 0x62, 0x6c, 0x65, 0x12, 0x18, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x76, - 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, - 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, - 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, 0x06, 0x2e, 0x2f, 0x69, - 0x70, 0x61, 0x6d, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x01, 0x28, 0x08, 0x52, 0x09, 0x69, 0x6d, 0x6d, 0x75, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x22, + 0x0a, 0x0c, 0x70, 0x72, 0x65, 0x41, 0x6c, 0x6c, 0x6f, 0x63, 0x61, 0x74, 0x65, 0x64, 0x18, 0x03, + 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0c, 0x70, 0x72, 0x65, 0x41, 0x6c, 0x6c, 0x6f, 0x63, 0x61, 0x74, + 0x65, 0x64, 0x22, 0x55, 0x0a, 0x16, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, + 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, + 0x12, 0x27, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x0f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, + 0x74, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2b, 0x0a, 0x15, 0x4e, 0x65, 0x74, + 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, + 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x22, 0x41, 0x0a, 0x16, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, + 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x27, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x0f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, 0x75, 0x6c, + 0x74, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x22, 0x2d, 0x0a, 0x17, 0x4e, 0x65, 0x74, + 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x69, 0x64, 0x72, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x04, 0x63, 0x69, 0x64, 0x72, 0x22, 0x61, 0x0a, 0x18, 0x4e, 0x65, 0x74, 0x77, + 0x6f, 0x72, 0x6b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, + 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, + 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, + 0x6c, 0x65, 0x12, 0x27, 0x0a, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x52, 0x65, 0x73, + 0x75, 0x6c, 0x74, 0x52, 0x06, 0x72, 0x65, 0x73, 0x75, 0x6c, 0x74, 0x32, 0xbf, 0x02, 0x0a, 0x04, + 0x49, 0x50, 0x41, 0x4d, 0x12, 0x32, 0x0a, 0x09, 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, + 0x65, 0x12, 0x11, 0x2e, 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x49, 0x50, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, + 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x32, 0x0a, 0x09, 0x49, 0x50, 0x52, 0x65, + 0x6c, 0x65, 0x61, 0x73, 0x65, 0x12, 0x11, 0x2e, 0x49, 0x50, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, + 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x12, 0x2e, 0x49, 0x50, 0x52, 0x65, 0x6c, + 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x41, 0x0a, 0x0e, + 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x12, 0x16, + 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, + 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, + 0x41, 0x63, 0x71, 0x75, 0x69, 0x72, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, + 0x41, 0x0a, 0x0e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, + 0x65, 0x12, 0x16, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, + 0x73, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x4e, 0x65, 0x74, 0x77, + 0x6f, 0x72, 0x6b, 0x52, 0x65, 0x6c, 0x65, 0x61, 0x73, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x49, 0x0a, 0x12, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x49, 0x73, 0x41, + 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x12, 0x18, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, + 0x72, 0x6b, 0x41, 0x76, 0x61, 0x69, 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, + 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x41, 0x76, 0x61, 0x69, + 0x6c, 0x61, 0x62, 0x6c, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x08, 0x5a, + 0x06, 0x2e, 0x2f, 0x69, 0x70, 0x61, 0x6d, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/pkg/ipam/ipam.proto b/pkg/ipam/ipam.proto index ac53a74f05..fc054405fe 100644 --- a/pkg/ipam/ipam.proto +++ b/pkg/ipam/ipam.proto @@ -34,6 +34,7 @@ message IPReleaseResponse { message NetworkAcquireRequest { string cidr = 1; bool immutable = 2; // If true, the network cannot be remapped. It will be allocated if available, or an error will be returned. + uint32 preAllocated = 3; // The number of IPs to pre-allocate (reserve) in the CIDR, starting from the first IP of the CIDR. } message NetworkAcquireResponse { diff --git a/pkg/ipam/networks.go b/pkg/ipam/networks.go index d0c785d3c8..f81fd592fa 100644 --- a/pkg/ipam/networks.go +++ b/pkg/ipam/networks.go @@ -25,29 +25,39 @@ import ( ) type networkInfo struct { - cidr string + network creationTimestamp time.Time } +type network struct { + cidr string + preAllocated uint +} + +func (n network) String() string { + return n.cidr +} + // reserveNetwork reserves a network, saving it in the cache. -func (lipam *LiqoIPAM) reserveNetwork(cidr string) error { +func (lipam *LiqoIPAM) reserveNetwork(nw network) error { lipam.mutex.Lock() defer lipam.mutex.Unlock() + // TODO: implement real network reserve logic if lipam.cacheNetworks == nil { lipam.cacheNetworks = make(map[string]networkInfo) } - lipam.cacheNetworks[cidr] = networkInfo{ - cidr: cidr, + lipam.cacheNetworks[nw.String()] = networkInfo{ + network: nw, creationTimestamp: time.Now(), } - klog.Infof("Reserved network %q", cidr) + klog.Infof("Reserved network %q", nw) return nil } // acquireNetwork acquires a network, eventually remapped if conflicts are found. -func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool) (string, error) { +func (lipam *LiqoIPAM) acquireNetwork(cidr string, preAllocated uint, immutable bool) (string, error) { lipam.mutex.Lock() defer lipam.mutex.Unlock() @@ -56,39 +66,45 @@ func (lipam *LiqoIPAM) acquireNetwork(cidr string, immutable bool) (string, erro if lipam.cacheNetworks == nil { lipam.cacheNetworks = make(map[string]networkInfo) } - lipam.cacheNetworks[cidr] = networkInfo{ - cidr: cidr, + nw := network{ + cidr: cidr, + preAllocated: preAllocated, + } + lipam.cacheNetworks[nw.String()] = networkInfo{ + network: nw, creationTimestamp: time.Now(), } - klog.Infof("Acquired network %q", cidr) - return cidr, nil + klog.Infof("Acquired network %q", nw.cidr) + return nw.cidr, nil } // freeNetwork frees a network, removing it from the cache. -func (lipam *LiqoIPAM) freeNetwork(cidr string) { +func (lipam *LiqoIPAM) freeNetwork(nw network) { lipam.mutex.Lock() defer lipam.mutex.Unlock() - delete(lipam.cacheNetworks, cidr) - klog.Infof("Freed network %q", cidr) + // TODO: implement real network free logic + delete(lipam.cacheNetworks, nw.String()) + klog.Infof("Freed network %q", nw.cidr) } // isNetworkAvailable checks if a network is available. -func (lipam *LiqoIPAM) isNetworkAvailable(cidr string) bool { +func (lipam *LiqoIPAM) isNetworkAvailable(nw network) bool { lipam.mutex.Lock() defer lipam.mutex.Unlock() + // TODO: implement real network availability check logic if lipam.cacheNetworks == nil { return true } - _, ok := lipam.cacheNetworks[cidr] + _, ok := lipam.cacheNetworks[nw.String()] return ok } -func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, error) { - var nets []string +func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]network, error) { + var nets []network var networks ipamv1alpha1.NetworkList if err := cl.List(ctx, &networks); err != nil { return nil, err @@ -103,7 +119,10 @@ func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]string, err continue } - nets = append(nets, cidr) + nets = append(nets, network{ + cidr: cidr, + preAllocated: net.Spec.PreAllocated, + }) } return nets, nil diff --git a/pkg/ipam/sync.go b/pkg/ipam/sync.go index a8d061629b..38de0a74d6 100644 --- a/pkg/ipam/sync.go +++ b/pkg/ipam/sync.go @@ -65,18 +65,18 @@ func (lipam *LiqoIPAM) syncNetworks(ctx context.Context, expiredThreshold time.T // Add networks that are present in the cluster but not in the cache. for _, net := range clusterNetworks { - if _, inCache := lipam.cacheNetworks[net]; !inCache { + if _, inCache := lipam.cacheNetworks[net.String()]; !inCache { if err := lipam.reserveNetwork(net); err != nil { return err } } - setClusterNetworks[net] = struct{}{} // add network to the set + setClusterNetworks[net.String()] = struct{}{} // add network to the set } // Remove networks that are present in the cache but not in the cluster, and were added before the threshold. for key := range lipam.cacheNetworks { if _, inCluster := setClusterNetworks[key]; !inCluster && lipam.cacheNetworks[key].creationTimestamp.Before(expiredThreshold) { - lipam.freeNetwork(lipam.cacheNetworks[key].cidr) + lipam.freeNetwork(lipam.cacheNetworks[key].network) } } diff --git a/pkg/ipam/sync_test.go b/pkg/ipam/sync_test.go index 4ff86ec37e..374efd7bac 100644 --- a/pkg/ipam/sync_test.go +++ b/pkg/ipam/sync_test.go @@ -45,7 +45,9 @@ var _ = Describe("Sync routine tests", func() { addNetowrkToCache = func(ipamServer *LiqoIPAM, cidr string, creationTimestamp time.Time) { ipamServer.cacheNetworks[cidr] = networkInfo{ - cidr: cidr, + network: network{ + cidr: cidr, + }, creationTimestamp: creationTimestamp, } }