From 728f070b0fe0c68bc52a986e61d08cf0ffcd4ea8 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Mon, 5 Nov 2018 17:58:41 +0000 Subject: [PATCH] Improve kops subnet importer - ensure AZs are treated as a set - respect kops subnet topology --- pkg/eks/api/api.go | 10 ++++++++++ pkg/kops/kops.go | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pkg/eks/api/api.go b/pkg/eks/api/api.go index 58e43fb847..7deeb70130 100644 --- a/pkg/eks/api/api.go +++ b/pkg/eks/api/api.go @@ -87,6 +87,16 @@ func NewClusterConfig() *ClusterConfig { return cfg } +// AppendAvailabilityZone appends a new AZ to the set +func (c *ClusterConfig) AppendAvailabilityZone(newAZ string) { + for _, az := range c.AvailabilityZones { + if az == newAZ { + return + } + } + c.AvailabilityZones = append(c.AvailabilityZones, newAZ) +} + // IsSupportedRegion check if given region is supported func (c *ClusterConfig) IsSupportedRegion() bool { for _, supportedRegion := range SupportedRegions() { diff --git a/pkg/kops/kops.go b/pkg/kops/kops.go index 002f5d3556..e5a040c80a 100644 --- a/pkg/kops/kops.go +++ b/pkg/kops/kops.go @@ -30,6 +30,15 @@ func (k *Wrapper) isOwned(t *ec2.Tag) bool { return *t.Key == "kubernetes.io/cluster/"+k.clusterName && *t.Value == "owned" } +func (k *Wrapper) topologyOf(s *ec2.Subnet) api.SubnetTopology { + for _, t := range s.Tags { + if *t.Key == "SubnetType" && *t.Value == "Private" { + return api.SubnetTopologyPrivate + } + } + return api.SubnetTopologyPublic // "Utility", "Public" or unspecified +} + // UseVPC finds VPC and subnets that give kops cluster uses and add those to EKS cluster config func (k *Wrapper) UseVPC(spec *api.ClusterConfig) error { allVPCs, err := aws.ListVPCs(k.cloud, k.clusterName) @@ -60,9 +69,9 @@ func (k *Wrapper) UseVPC(spec *api.ClusterConfig) error { for _, subnet := range allSubnets { subnet := subnet.Obj.(*ec2.Subnet) for _, tag := range subnet.Tags { - if k.isOwned(tag) && *subnet.VpcId == vpcs[0] { - spec.ImportSubnet(api.SubnetTopologyPublic, *subnet.AvailabilityZone, *subnet.SubnetId) - spec.AvailabilityZones = append(spec.AvailabilityZones, *subnet.AvailabilityZone) + if k.isOwned(tag) && *subnet.VpcId == spec.VPC.ID { + spec.ImportSubnet(k.topologyOf(subnet), *subnet.AvailabilityZone, *subnet.SubnetId) + spec.AppendAvailabilityZone(*subnet.AvailabilityZone) } } }