|
| 1 | +// Copyright 2019-2024 The Liqo Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package ipam |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + klog "k8s.io/klog/v2" |
| 21 | + |
| 22 | + ipamv1alpha1 "github.com/liqotech/liqo/apis/ipam/v1alpha1" |
| 23 | + "github.com/liqotech/liqo/pkg/consts" |
| 24 | +) |
| 25 | + |
| 26 | +// +kubebuilder:rbac:groups=ipam.liqo.io,resources=ips,verbs=get;list;watch |
| 27 | +// +kubebuilder:rbac:groups=ipam.liqo.io,resources=networks,verbs=get;list;watch |
| 28 | + |
| 29 | +type ipCidr struct { |
| 30 | + ip string |
| 31 | + cidr string |
| 32 | +} |
| 33 | + |
| 34 | +func (lipam *LiqoIPAM) initialize(ctx context.Context) error { |
| 35 | + if err := lipam.initializeNetworks(ctx); err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + |
| 39 | + if err := lipam.initializeIPs(ctx); err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + klog.Info("IPAM initialized") |
| 44 | + return nil |
| 45 | +} |
| 46 | + |
| 47 | +func (lipam *LiqoIPAM) initializeNetworks(ctx context.Context) error { |
| 48 | + // Initialize the networks. |
| 49 | + nets, err := lipam.getReservedNetworks(ctx) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + |
| 54 | + for _, net := range nets { |
| 55 | + if err := lipam.reserveNetwork(net); err != nil { |
| 56 | + klog.Errorf("Failed to reserve network %s: %v", net, err) |
| 57 | + return err |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func (lipam *LiqoIPAM) initializeIPs(ctx context.Context) error { |
| 65 | + // Initialize the IPs. |
| 66 | + ips, err := lipam.getReservedIPs(ctx) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + for _, ip := range ips { |
| 72 | + if err := lipam.reserveIP(ip.ip, ip.cidr); err != nil { |
| 73 | + klog.Errorf("Failed to reserve IP %s in network %s: %v", ip.ip, ip.cidr, err) |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (lipam *LiqoIPAM) getReservedNetworks(ctx context.Context) ([]string, error) { |
| 82 | + var nets []string |
| 83 | + var networks ipamv1alpha1.NetworkList |
| 84 | + if err := lipam.Options.Client.List(ctx, &networks); err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + |
| 88 | + for i := range networks.Items { |
| 89 | + net := &networks.Items[i] |
| 90 | + |
| 91 | + var cidr string |
| 92 | + switch { |
| 93 | + case net.Labels != nil && net.Labels[consts.NetworkNotRemappedLabelKey] == consts.NetworkNotRemappedLabelValue: |
| 94 | + cidr = net.Spec.CIDR.String() |
| 95 | + default: |
| 96 | + cidr = net.Status.CIDR.String() |
| 97 | + } |
| 98 | + if cidr == "" { |
| 99 | + klog.Warningf("Network %s has no CIDR", net.Name) |
| 100 | + continue |
| 101 | + } |
| 102 | + |
| 103 | + nets = append(nets, cidr) |
| 104 | + } |
| 105 | + |
| 106 | + return nets, nil |
| 107 | +} |
| 108 | + |
| 109 | +func (lipam *LiqoIPAM) getReservedIPs(ctx context.Context) ([]ipCidr, error) { |
| 110 | + var ips []ipCidr |
| 111 | + var ipList ipamv1alpha1.IPList |
| 112 | + if err := lipam.Options.Client.List(ctx, &ipList); err != nil { |
| 113 | + return nil, err |
| 114 | + } |
| 115 | + |
| 116 | + for i := range ipList.Items { |
| 117 | + ip := &ipList.Items[i] |
| 118 | + |
| 119 | + address := ip.Status.IP.String() |
| 120 | + if address == "" { |
| 121 | + klog.Warningf("IP %s has no address", ip.Name) |
| 122 | + continue |
| 123 | + } |
| 124 | + |
| 125 | + cidr := ip.Status.CIDR.String() |
| 126 | + if cidr == "" { |
| 127 | + klog.Warningf("IP %s has no CIDR", ip.Name) |
| 128 | + continue |
| 129 | + } |
| 130 | + |
| 131 | + ips = append(ips, ipCidr{ip: address, cidr: cidr}) |
| 132 | + } |
| 133 | + |
| 134 | + return ips, nil |
| 135 | +} |
| 136 | + |
| 137 | +func (lipam *LiqoIPAM) reserveNetwork(cidr string) error { |
| 138 | + // TODO: Reserve the network. |
| 139 | + klog.Infof("Reserved network %s", cidr) |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func (lipam *LiqoIPAM) reserveIP(ip, cidr string) error { |
| 144 | + // TODO: Reserve the IP. |
| 145 | + klog.Infof("Reserved IP %s in network %s", ip, cidr) |
| 146 | + return nil |
| 147 | +} |
0 commit comments