Implement ability to update Load Balancer subnets

This commit is contained in:
Kashif Saadat 2018-01-16 15:43:30 +00:00
parent 59440bbc68
commit e315c350be
3 changed files with 65 additions and 1 deletions

View File

@ -160,6 +160,7 @@ k8s.io/kops/upup/pkg/kutil
k8s.io/kops/upup/tools/generators/fitask
k8s.io/kops/upup/tools/generators/pkg/codegen
k8s.io/kops/util/pkg/hashing
k8s.io/kops/util/pkg/slice
k8s.io/kops/util/pkg/tables
k8s.io/kops/util/pkg/ui
k8s.io/kops/util/pkg/vfs

View File

@ -31,6 +31,7 @@ import (
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"k8s.io/kops/util/pkg/slice"
)
// LoadBalancer manages an ELB. We find the existing ELB using the Name tag.
@ -527,7 +528,29 @@ func (_ *LoadBalancer) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *LoadBalan
actualSubnets = append(actualSubnets, fi.StringValue(s.ID))
}
return fmt.Errorf("subnet changes on LoadBalancer not yet implemented: actual=%s -> expected=%s", actualSubnets, expectedSubnets)
oldSubnetIDs := slice.GetUniqueStrings(expectedSubnets, actualSubnets)
if len(oldSubnetIDs) > 0 {
request := &elb.DetachLoadBalancerFromSubnetsInput{}
request.SetLoadBalancerName(loadBalancerName)
request.SetSubnets(aws.StringSlice(oldSubnetIDs))
glog.V(2).Infof("Detaching Load Balancer from old subnets")
if _, err := t.Cloud.ELB().DetachLoadBalancerFromSubnets(request); err != nil {
return fmt.Errorf("Error detaching Load Balancer from old subnets: %v", err)
}
}
newSubnetIDs := slice.GetUniqueStrings(actualSubnets, expectedSubnets)
if len(newSubnetIDs) > 0 {
request := &elb.AttachLoadBalancerToSubnetsInput{}
request.SetLoadBalancerName(loadBalancerName)
request.SetSubnets(aws.StringSlice(newSubnetIDs))
glog.V(2).Infof("Attaching Load Balancer to new subnets")
if _, err := t.Cloud.ELB().AttachLoadBalancerToSubnets(request); err != nil {
return fmt.Errorf("Error attaching Load Balancer to new subnets: %v", err)
}
}
}
if changes.Listeners != nil {

40
util/pkg/slice/slice.go Normal file
View File

@ -0,0 +1,40 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package slice provides utility methods for common operations on slices.
package slice
// GetUniqueStrings returns a slice of strings in the extra slice that are not
// present in the main slice
func GetUniqueStrings(main, extra []string) []string {
unique := []string{}
for _, item := range extra {
found := false
for _, s := range main {
if item == s {
found = true
}
}
if !found {
unique = append(unique, item)
}
}
return unique
}