1515package modules
1616
1717import (
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,72 @@ 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+ })
276+ if err != nil {
277+ return err
278+ }
279+ }
280+
281+ // Since reserved network must not be remapped (immutable), we can set the status CIDR to the spec CIDR
282+ nw .Status .CIDR = nw .Spec .CIDR
283+ }
284+
285+ if err := cl .Status ().Update (ctx , nw ); err != nil {
286+ return fmt .Errorf ("unable to update the reserved network %s: %w" , nw .Name , err )
287+ }
288+ }
289+
290+ klog .Info ("Reserved networks initialized" )
291+ return nil
292+ }
0 commit comments