Consolidates the ipam configuration as a group from different related configurations user can configure network with multiple non-overlapping subnets and hence it is possible to correlate the various related parameters and consolidate them. createIPAMConfig consolidates subnets, ip-ranges, gateways
(options ipamOptions)
| 137 | // |
| 138 | //nolint:gocyclo |
| 139 | func createIPAMConfig(options ipamOptions) (*network.IPAM, error) { |
| 140 | if len(options.subnets) < len(options.ipRanges) || len(options.subnets) < len(options.gateways) { |
| 141 | return nil, errors.New("every ip-range or gateway must have a corresponding subnet") |
| 142 | } |
| 143 | iData := map[string]*network.IPAMConfig{} |
| 144 | |
| 145 | // Populate non-overlapping subnets into consolidation map |
| 146 | for _, s := range options.subnets { |
| 147 | for k := range iData { |
| 148 | ok1, err := subnetMatches(s, k) |
| 149 | if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | ok2, err := subnetMatches(k, s) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | if ok1 || ok2 { |
| 157 | return nil, errors.New("multiple overlapping subnet configuration is not supported") |
| 158 | } |
| 159 | } |
| 160 | sn, err := netip.ParsePrefix(s) |
| 161 | if err != nil { |
| 162 | return nil, err |
| 163 | } |
| 164 | iData[s] = &network.IPAMConfig{Subnet: sn, AuxAddress: map[string]netip.Addr{}} |
| 165 | } |
| 166 | |
| 167 | // Validate and add valid ip ranges |
| 168 | for _, r := range options.ipRanges { |
| 169 | match := false |
| 170 | for _, s := range options.subnets { |
| 171 | ok, err := subnetMatches(s, r.String()) |
| 172 | if err != nil { |
| 173 | return nil, err |
| 174 | } |
| 175 | if !ok { |
| 176 | continue |
| 177 | } |
| 178 | |
| 179 | // Using "IsValid" to check if a valid IPRange was already set. |
| 180 | if iData[s].IPRange.IsValid() { |
| 181 | return nil, fmt.Errorf("cannot configure multiple ranges (%s, %s) on the same subnet (%s)", r.String(), iData[s].IPRange.String(), s) |
| 182 | } |
| 183 | if ipRange, ok := toPrefix(r); ok { |
| 184 | iData[s].IPRange = ipRange |
| 185 | match = true |
| 186 | } |
| 187 | } |
| 188 | if !match { |
| 189 | return nil, fmt.Errorf("no matching subnet for range %s", r.String()) |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // Validate and add valid gateways |
| 194 | for _, g := range options.gateways { |
| 195 | match := false |
| 196 | for _, s := range options.subnets { |
no test coverage detected
searching dependent graphs…