fix: send service ports on lb allocate
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
@@ -163,10 +164,15 @@ func (c *Controller) sync(ctx context.Context, key string) error {
|
||||
if ingressIP(svc) != "" {
|
||||
return nil
|
||||
}
|
||||
ports, err := servicePortsToAllocateRules(svc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
lb, err := c.lbClient.Allocate(ctx, provisioner.AllocateRequest{
|
||||
Namespace: namespace,
|
||||
Name: name,
|
||||
Ports: ports,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -217,6 +223,29 @@ func ingressIP(svc *corev1.Service) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func servicePortsToAllocateRules(svc *corev1.Service) ([]provisioner.AllocatePortRule, error) {
|
||||
if svc == nil {
|
||||
return nil, fmt.Errorf("service is required")
|
||||
}
|
||||
if len(svc.Spec.Ports) == 0 {
|
||||
return nil, fmt.Errorf("service %s/%s has no ports", svc.Namespace, svc.Name)
|
||||
}
|
||||
out := make([]provisioner.AllocatePortRule, 0, len(svc.Spec.Ports))
|
||||
for _, p := range svc.Spec.Ports {
|
||||
protocol := string(p.Protocol)
|
||||
if protocol == "" {
|
||||
protocol = string(corev1.ProtocolTCP)
|
||||
}
|
||||
switch protocol {
|
||||
case string(corev1.ProtocolTCP), string(corev1.ProtocolUDP):
|
||||
out = append(out, provisioner.AllocatePortRule{Protocol: strings.ToLower(protocol), PortFrom: int(p.Port)})
|
||||
default:
|
||||
return nil, fmt.Errorf("service %s/%s has unsupported protocol %q for load balancer firewall", svc.Namespace, svc.Name, protocol)
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func containsString(items []string, target string) bool {
|
||||
for _, item := range items {
|
||||
if item == target {
|
||||
|
||||
Reference in New Issue
Block a user