diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go index 6aeaaa7e09c..ab8d6d92d21 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws.go @@ -3668,6 +3668,9 @@ func buildListener(port v1.ServicePort, annotations map[string]string, sslPorts // EnsureLoadBalancer implements LoadBalancer.EnsureLoadBalancer func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiService *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) { annotations := apiService.Annotations + if isLBExternal(annotations) { + return nil, cloudprovider.ImplementedElsewhere + } klog.V(2).Infof("EnsureLoadBalancer(%v, %v, %v, %v, %v, %v, %v)", clusterName, apiService.Namespace, apiService.Name, c.region, apiService.Spec.LoadBalancerIP, apiService.Spec.Ports, annotations) @@ -3679,7 +3682,6 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS if len(apiService.Spec.Ports) == 0 { return nil, fmt.Errorf("requested load balancer with no ports") } - // Figure out what mappings we want on the load balancer listeners := []*elb.Listener{} v2Mappings := []nlbPortMapping{} @@ -4065,6 +4067,9 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS // GetLoadBalancer is an implementation of LoadBalancer.GetLoadBalancer func (c *Cloud) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (*v1.LoadBalancerStatus, bool, error) { + if isLBExternal(service.Annotations) { + return nil, false, nil + } loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service) if isNLB(service.Annotations) { @@ -4325,6 +4330,9 @@ func (c *Cloud) updateInstanceSecurityGroupsForLoadBalancer(lb *elb.LoadBalancer // EnsureLoadBalancerDeleted implements LoadBalancer.EnsureLoadBalancerDeleted. func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error { + if isLBExternal(service.Annotations) { + return nil + } loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service) if isNLB(service.Annotations) { @@ -4509,11 +4517,13 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin // UpdateLoadBalancer implements LoadBalancer.UpdateLoadBalancer func (c *Cloud) UpdateLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) error { + if isLBExternal(service.Annotations) { + return cloudprovider.ImplementedElsewhere + } instances, err := c.findInstancesForELB(nodes, service.Annotations) if err != nil { return err } - loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service) if isNLB(service.Annotations) { lb, err := c.describeLoadBalancerv2(loadBalancerName) diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go index 2b90906572e..d02fdceb315 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go @@ -69,6 +69,13 @@ func isNLB(annotations map[string]string) bool { return false } +func isLBExternal(annotations map[string]string) bool { + if val := annotations[ServiceAnnotationLoadBalancerType]; val == "nlb-ip" || val == "external" { + return true + } + return false +} + type nlbPortMapping struct { FrontendPort int64 FrontendProtocol string diff --git a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go index cfee659300a..df63d9b21f0 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go @@ -168,6 +168,43 @@ func TestIsNLB(t *testing.T) { } } +func TestIsLBExternal(t *testing.T) { + tests := []struct { + name string + annotations map[string]string + want bool + }{ + { + name: "No annotation", + annotations: map[string]string{}, + want: false, + }, + { + name: "Type NLB", + annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "nlb"}, + want: false, + }, + { + name: "Type NLB-IP", + annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "nlb-ip"}, + want: true, + }, + { + name: "Type External", + annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "external"}, + want: true, + }, + } + for _, test := range tests { + t.Logf("Running test case %s", test.name) + got := isLBExternal(test.annotations) + + if got != test.want { + t.Errorf("Incorrect value for isLBExternal() case %s. Got %t, expected %t.", test.name, got, test.want) + } + } +} + func TestSyncElbListeners(t *testing.T) { tests := []struct { name string