Skip to content

Commit 2dc4e6e

Browse files
committed
AWS cloudprovider allow nlb-ip and external type lbs to be managed externally
1 parent 205d5c5 commit 2dc4e6e

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

staging/src/k8s.io/legacy-cloud-providers/aws/aws.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3678,6 +3678,9 @@ func buildListener(port v1.ServicePort, annotations map[string]string, sslPorts
36783678
// EnsureLoadBalancer implements LoadBalancer.EnsureLoadBalancer
36793679
func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiService *v1.Service, nodes []*v1.Node) (*v1.LoadBalancerStatus, error) {
36803680
annotations := apiService.Annotations
3681+
if isLBExternal(annotations) {
3682+
return nil, cloudprovider.ImplementedElsewhere
3683+
}
36813684
klog.V(2).Infof("EnsureLoadBalancer(%v, %v, %v, %v, %v, %v, %v)",
36823685
clusterName, apiService.Namespace, apiService.Name, c.region, apiService.Spec.LoadBalancerIP, apiService.Spec.Ports, annotations)
36833686

@@ -3689,7 +3692,6 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
36893692
if len(apiService.Spec.Ports) == 0 {
36903693
return nil, fmt.Errorf("requested load balancer with no ports")
36913694
}
3692-
36933695
// Figure out what mappings we want on the load balancer
36943696
listeners := []*elb.Listener{}
36953697
v2Mappings := []nlbPortMapping{}
@@ -4075,6 +4077,9 @@ func (c *Cloud) EnsureLoadBalancer(ctx context.Context, clusterName string, apiS
40754077

40764078
// GetLoadBalancer is an implementation of LoadBalancer.GetLoadBalancer
40774079
func (c *Cloud) GetLoadBalancer(ctx context.Context, clusterName string, service *v1.Service) (*v1.LoadBalancerStatus, bool, error) {
4080+
if isLBExternal(service.Annotations) {
4081+
return nil, false, nil
4082+
}
40784083
loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service)
40794084

40804085
if isNLB(service.Annotations) {
@@ -4335,6 +4340,9 @@ func (c *Cloud) updateInstanceSecurityGroupsForLoadBalancer(lb *elb.LoadBalancer
43354340

43364341
// EnsureLoadBalancerDeleted implements LoadBalancer.EnsureLoadBalancerDeleted.
43374342
func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName string, service *v1.Service) error {
4343+
if isLBExternal(service.Annotations) {
4344+
return nil
4345+
}
43384346
loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service)
43394347

43404348
if isNLB(service.Annotations) {
@@ -4519,11 +4527,13 @@ func (c *Cloud) EnsureLoadBalancerDeleted(ctx context.Context, clusterName strin
45194527

45204528
// UpdateLoadBalancer implements LoadBalancer.UpdateLoadBalancer
45214529
func (c *Cloud) UpdateLoadBalancer(ctx context.Context, clusterName string, service *v1.Service, nodes []*v1.Node) error {
4530+
if isLBExternal(service.Annotations) {
4531+
return cloudprovider.ImplementedElsewhere
4532+
}
45224533
instances, err := c.findInstancesForELB(nodes, service.Annotations)
45234534
if err != nil {
45244535
return err
45254536
}
4526-
45274537
loadBalancerName := c.GetLoadBalancerName(ctx, clusterName, service)
45284538
if isNLB(service.Annotations) {
45294539
lb, err := c.describeLoadBalancerv2(loadBalancerName)

staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ func isNLB(annotations map[string]string) bool {
6969
return false
7070
}
7171

72+
func isLBExternal(annotations map[string]string) bool {
73+
if val := annotations[ServiceAnnotationLoadBalancerType]; val == "nlb-ip" || val == "external" {
74+
return true
75+
}
76+
return false
77+
}
78+
7279
type nlbPortMapping struct {
7380
FrontendPort int64
7481
FrontendProtocol string

staging/src/k8s.io/legacy-cloud-providers/aws/aws_loadbalancer_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,43 @@ func TestIsNLB(t *testing.T) {
168168
}
169169
}
170170

171+
func TestIsLBExternal(t *testing.T) {
172+
tests := []struct {
173+
name string
174+
annotations map[string]string
175+
want bool
176+
}{
177+
{
178+
name: "No annotation",
179+
annotations: map[string]string{},
180+
want: false,
181+
},
182+
{
183+
name: "Type NLB",
184+
annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "nlb"},
185+
want: false,
186+
},
187+
{
188+
name: "Type NLB-IP",
189+
annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "nlb-ip"},
190+
want: true,
191+
},
192+
{
193+
name: "Type External",
194+
annotations: map[string]string{"service.beta.kubernetes.io/aws-load-balancer-type": "external"},
195+
want: true,
196+
},
197+
}
198+
for _, test := range tests {
199+
t.Logf("Running test case %s", test.name)
200+
got := isLBExternal(test.annotations)
201+
202+
if got != test.want {
203+
t.Errorf("Incorrect value for isLBExternal() case %s. Got %t, expected %t.", test.name, got, test.want)
204+
}
205+
}
206+
}
207+
171208
func TestSyncElbListeners(t *testing.T) {
172209
tests := []struct {
173210
name string

0 commit comments

Comments
 (0)