|
1 | 1 | package scaleway |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "errors" |
4 | 5 | "fmt" |
5 | 6 | "net/url" |
6 | 7 | "slices" |
@@ -253,6 +254,13 @@ const ( |
253 | 254 | // - "<pn-id>,<pn-id>": will attach the two Private Networks to the LB. |
254 | 255 | serviceAnnotationPrivateNetworkIDs = "service.beta.kubernetes.io/scw-loadbalancer-pn-ids" |
255 | 256 |
|
| 257 | + // serviceAnnotationLoadBalancerFlexibleIPTypes is the annotation to choose which flexible IP(s) to book for the LB |
| 258 | + // when it is created and no static IP is provided via service.beta.kubernetes.io/scw-loadbalancer-ip-ids. |
| 259 | + // The default value is "ipv4" and the possible values are "ipv4" or "ipv4,ipv6". |
| 260 | + // A flexible IPv4 is always required, an IPv6-only LB is not supported. |
| 261 | + // This annotation is only used at LB creation and has no effect on an already existing LB. |
| 262 | + serviceAnnotationLoadBalancerFlexibleIPTypes = "service.beta.kubernetes.io/scw-loadbalancer-flexible-ip-types" |
| 263 | + |
256 | 264 | // serviceAnnotationLoadBalancerHealthCheckFromService is the annotation to use healthCheckNodePort from the service |
257 | 265 | // The possible values are "false", "true" or "*" for all ports or a comma delimited list of the service port |
258 | 266 | // (for instance "80,443"). When enabled for a port, the health check will use the service's healthCheckNodePort |
@@ -329,6 +337,32 @@ func getIPIDs(service *v1.Service) []string { |
329 | 337 | return strings.Split(ipIDs, ",") |
330 | 338 | } |
331 | 339 |
|
| 340 | +// getFlexibleIPTypes returns whether a flexible IPv4 and/or a flexible IPv6 should be |
| 341 | +// booked for the LB, based on the serviceAnnotationLoadBalancerFlexibleIPTypes annotation. |
| 342 | +func getFlexibleIPTypes(service *v1.Service) (assignIPv4 bool, assignIPv6 bool, err error) { |
| 343 | + flexibleIPTypes := service.Annotations[serviceAnnotationLoadBalancerFlexibleIPTypes] |
| 344 | + if flexibleIPTypes == "" { |
| 345 | + return true, false, nil |
| 346 | + } |
| 347 | + |
| 348 | + for ipType := range strings.SplitSeq(flexibleIPTypes, ",") { |
| 349 | + switch strings.ToLower(strings.TrimSpace(ipType)) { |
| 350 | + case "ipv4": |
| 351 | + assignIPv4 = true |
| 352 | + case "ipv6": |
| 353 | + assignIPv6 = true |
| 354 | + default: |
| 355 | + return false, false, fmt.Errorf("unsupported value %q", strings.ToLower(strings.TrimSpace(ipType))) |
| 356 | + } |
| 357 | + } |
| 358 | + |
| 359 | + if !assignIPv4 { |
| 360 | + return false, false, errors.New("an IPv4 is always required") |
| 361 | + } |
| 362 | + |
| 363 | + return assignIPv4, assignIPv6, nil |
| 364 | +} |
| 365 | + |
332 | 366 | func getPrivateIPIDs(service *v1.Service) sets.Set[string] { |
333 | 367 | privateIPIDs := service.Annotations[serviceAnnotationLoadBalancerPrivateIPIDs] |
334 | 368 | if privateIPIDs == "" { |
|
0 commit comments