Merge pull request #92839 from kishorj/nlb_external

AWS cloudprovider allow external management of LB types nlb-ip and external
This commit is contained in:
Kubernetes Prow Robot 2020-08-27 19:07:12 -07:00 committed by GitHub
commit f3c63b1372
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 2 deletions

View File

@ -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)

View File

@ -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

View File

@ -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