Skip to content

Commit b0b7977

Browse files
committed
feature: ipam support reserved networks
1 parent c6c6174 commit b0b7977

File tree

7 files changed

+93
-8
lines changed

7 files changed

+93
-8
lines changed

apis/ipam/v1alpha1/network_types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ type NetworkSpec struct {
4343
// PreAllocated is the number of IPs to pre-allocate (reserve) in the CIDR, starting from the first IP.
4444
// +kubebuilder:validation:Optional
4545
// +kubebuilder:validation:Minimum=0
46-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="Reserved field is immutable"
47-
PreAllocated uint `json:"preAllocated"`
46+
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="PreAllocated field is immutable"
47+
PreAllocated uint32 `json:"preAllocated"`
4848
}
4949

5050
// NetworkStatus defines the observed state of Network.

cmd/liqo-controller-manager/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func main() {
269269
ipamClient = ipam.NewIPAMClient(conn)
270270
}
271271

272-
if err := modules.SetupNetworkingModule(mgr, &modules.NetworkingOption{
272+
if err := modules.SetupNetworkingModule(ctx, mgr, uncachedClient, &modules.NetworkingOption{
273273
DynClient: dynClient,
274274
Factory: factory,
275275

cmd/liqo-controller-manager/modules/authentication.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func SetupAuthenticationModule(ctx context.Context, mgr manager.Manager, uncache
6161
}
6262

6363
if err := enforceAuthenticationKeys(ctx, uncachedClient, opts.LiqoNamespace); err != nil {
64+
klog.Errorf("Unable to enforce authentication keys: %v", err)
6465
return err
6566
}
6667

@@ -135,7 +136,7 @@ func SetupAuthenticationModule(ctx context.Context, mgr manager.Manager, uncache
135136

136137
func enforceAuthenticationKeys(ctx context.Context, cl client.Client, liqoNamespace string) error {
137138
if err := authentication.InitClusterKeys(ctx, cl, liqoNamespace); err != nil {
138-
klog.Errorf("Unable to initialize cluster authentication keys: %v", err)
139+
return err
139140
}
140141

141142
klog.Info("Enforced cluster authentication keys")

cmd/liqo-controller-manager/modules/networking.go

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@
1515
package modules
1616

1717
import (
18+
"context"
19+
"fmt"
20+
1821
"k8s.io/client-go/dynamic"
1922
"k8s.io/klog/v2"
23+
"sigs.k8s.io/controller-runtime/pkg/client"
2024
"sigs.k8s.io/controller-runtime/pkg/manager"
2125

26+
ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1"
2227
"github.com/liqotech/liqo/pkg/ipam"
2328
clientoperator "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/external-network/client-operator"
2429
configuration "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/external-network/configuration"
@@ -36,6 +41,7 @@ import (
3641
ipctrl "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/ip-controller"
3742
networkctrl "github.com/liqotech/liqo/pkg/liqo-controller-manager/networking/network-controller"
3843
dynamicutils "github.com/liqotech/liqo/pkg/utils/dynamic"
44+
ipamutils "github.com/liqotech/liqo/pkg/utils/ipam"
3945
)
4046

4147
// NetworkingOption defines the options to setup the Networking module.
@@ -59,7 +65,13 @@ type NetworkingOption struct {
5965
}
6066

6167
// SetupNetworkingModule setup the networking module and initializes its controllers .
62-
func SetupNetworkingModule(mgr manager.Manager, opts *NetworkingOption) error {
68+
func SetupNetworkingModule(ctx context.Context, mgr manager.Manager, uncachedClient client.Client, opts *NetworkingOption) error {
69+
// Initialize reserved networks
70+
if err := initializeReservedNetworks(ctx, uncachedClient, opts.IpamClient); err != nil {
71+
klog.Errorf("Unable to initialize reserved networks: %v", err)
72+
return err
73+
}
74+
6375
networkReconciler := networkctrl.NewNetworkReconciler(mgr.GetClient(), mgr.GetScheme(), opts.IpamClient)
6476
if err := networkReconciler.SetupWithManager(mgr, opts.NetworkWorkers); err != nil {
6577
klog.Errorf("Unable to start the networkReconciler: %v", err)
@@ -209,3 +221,73 @@ func SetupNetworkingModule(mgr manager.Manager, opts *NetworkingOption) error {
209221

210222
return nil
211223
}
224+
225+
func initializeReservedNetworks(ctx context.Context, cl client.Client, ipamClient ipam.IPAMClient) error {
226+
var networksToReserve []ipamv1alpha1.Network
227+
228+
// PodCIDR is a special case of reserved network
229+
podCidr, err := ipamutils.GetPodCIDRNetwork(ctx, cl)
230+
if err != nil {
231+
return err
232+
}
233+
networksToReserve = append(networksToReserve, *podCidr)
234+
235+
// ServiceCIDR is a special case of reserved network
236+
serviceCidr, err := ipamutils.GetServiceCIDRNetwork(ctx, cl)
237+
if err != nil {
238+
return err
239+
}
240+
networksToReserve = append(networksToReserve, *serviceCidr)
241+
242+
// Get the reserved networks
243+
reservedNetworks, err := ipamutils.GetReservedSubnetNetworks(ctx, cl)
244+
if err != nil {
245+
return err
246+
}
247+
networksToReserve = append(networksToReserve, reservedNetworks...)
248+
249+
// Reserve the networks and fill their status CIDR.
250+
for i := range networksToReserve {
251+
nw := &networksToReserve[i]
252+
253+
// If the status CIDR is already set, we do not need to reserve the network
254+
// as it will be reserved when the ipam server is initialized.
255+
if nw.Status.CIDR != "" {
256+
continue
257+
}
258+
259+
if ipamClient == nil {
260+
nw.Status.CIDR = nw.Spec.CIDR
261+
} else {
262+
// First check if the network is already reserved
263+
res, err := ipamClient.NetworkIsAvailable(ctx, &ipam.NetworkAvailableRequest{
264+
Cidr: nw.Spec.CIDR.String(),
265+
})
266+
if err != nil {
267+
return err
268+
}
269+
270+
if res.Available {
271+
// Network is not reserved, reserve it
272+
_, err := ipamClient.NetworkAcquire(ctx, &ipam.NetworkAcquireRequest{
273+
Cidr: nw.Spec.CIDR.String(),
274+
Immutable: true,
275+
PreAllocated: nw.Spec.PreAllocated,
276+
})
277+
if err != nil {
278+
return err
279+
}
280+
}
281+
282+
// Since reserved network must not be remapped (immutable), we can set the status CIDR to the spec CIDR
283+
nw.Status.CIDR = nw.Spec.CIDR
284+
}
285+
286+
if err := cl.Status().Update(ctx, nw); err != nil {
287+
return fmt.Errorf("unable to update the reserved network %s: %w", nw.Name, err)
288+
}
289+
}
290+
291+
klog.Info("Reserved networks initialized")
292+
return nil
293+
}

deployments/liqo/charts/liqo-crds/crds/ipam.liqo.io_networks.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ spec:
6363
preAllocated:
6464
description: PreAllocated is the number of IPs to pre-allocate (reserve)
6565
in the CIDR, starting from the first IP.
66+
format: int32
6667
minimum: 0
6768
type: integer
6869
x-kubernetes-validations:
69-
- message: Reserved field is immutable
70+
- message: PreAllocated field is immutable
7071
rule: self == oldSelf
7172
required:
7273
- cidr

deployments/liqo/templates/liqo-ipam-networks.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ metadata:
5454
ipam.liqo.io/network-not-remapped: "true"
5555
spec:
5656
cidr: {{ $value }}
57+
---
5758
{{- end }}

pkg/ipam/networks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,9 @@ func (lipam *LiqoIPAM) isNetworkAvailable(nw network) bool {
9898
if lipam.cacheNetworks == nil {
9999
return true
100100
}
101-
_, ok := lipam.cacheNetworks[nw.String()]
101+
_, exists := lipam.cacheNetworks[nw.String()]
102102

103-
return ok
103+
return !exists
104104
}
105105

106106
func listNetworksOnCluster(ctx context.Context, cl client.Client) ([]network, error) {

0 commit comments

Comments
 (0)