From df6e730e0fe94e0ffd0871a7864345382cc007b2 Mon Sep 17 00:00:00 2001 From: calvix Date: Tue, 3 Aug 2021 12:04:26 +0200 Subject: [PATCH 1/3] awsmachinepool-use-subnets-filters --- pkg/cloud/scope/machinepool.go | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/pkg/cloud/scope/machinepool.go b/pkg/cloud/scope/machinepool.go index d70743d39e..82da3b4314 100644 --- a/pkg/cloud/scope/machinepool.go +++ b/pkg/cloud/scope/machinepool.go @@ -22,6 +22,7 @@ import ( "strings" "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" "github.com/go-logr/logr" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" @@ -30,6 +31,7 @@ import ( "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha4" expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1alpha4" + "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/filter" clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4" "sigs.k8s.io/cluster-api/controllers/remote" capierrors "sigs.k8s.io/cluster-api/errors" @@ -221,9 +223,28 @@ func (m *MachinePoolScope) IsEKSManaged() bool { // SubnetIDs returns the machine pool subnet IDs. func (m *MachinePoolScope) SubnetIDs() ([]string, error) { - subnetIDs := make([]string, len(m.AWSMachinePool.Spec.Subnets)) - for i, v := range m.AWSMachinePool.Spec.Subnets { - subnetIDs[i] = aws.StringValue(v.ID) + ec2Client := ec2.New(m.InfraCluster.Session()) + + var subnetIDs []string + for _, v := range m.AWSMachinePool.Spec.Subnets { + if v.ID != nil { + subnetIDs = append(subnetIDs, aws.StringValue(v.ID)) + } else if v.Filters != nil { + criteria := []*ec2.Filter{ + filter.EC2.SubnetStates(ec2.SubnetStatePending, ec2.SubnetStateAvailable), + filter.EC2.VPC(m.InfraCluster.VPC().ID), + } + for _, f := range v.Filters { + criteria = append(criteria, &ec2.Filter{Name: aws.String(f.Name), Values: aws.StringSlice(f.Values)}) + } + out, err := ec2Client.DescribeSubnets(&ec2.DescribeSubnetsInput{Filters: criteria}) + if err != nil { + return subnetIDs, fmt.Errorf("describing subnets with filters: %w", err) + } + for _, s := range out.Subnets { + subnetIDs = append(subnetIDs, aws.StringValue(s.SubnetId)) + } + } } strategy, err := newDefaultSubnetPlacementStrategy(m.Logger) From 326ab4ed3f33dff9d55dab98d95b5c6e677be425 Mon Sep 17 00:00:00 2001 From: calvix Date: Mon, 15 Nov 2021 13:07:05 +0100 Subject: [PATCH 2/3] filter by az as well --- pkg/cloud/scope/machinepool.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/cloud/scope/machinepool.go b/pkg/cloud/scope/machinepool.go index 82da3b4314..d0308e41a2 100644 --- a/pkg/cloud/scope/machinepool.go +++ b/pkg/cloud/scope/machinepool.go @@ -41,6 +41,10 @@ import ( "sigs.k8s.io/controller-runtime/pkg/client" ) +const ( + filterAvailabilityZone = "availability-zone" +) + // MachinePoolScope defines a scope defined around a machine and its cluster. type MachinePoolScope struct { logr.Logger @@ -237,6 +241,7 @@ func (m *MachinePoolScope) SubnetIDs() ([]string, error) { for _, f := range v.Filters { criteria = append(criteria, &ec2.Filter{Name: aws.String(f.Name), Values: aws.StringSlice(f.Values)}) } + criteria = append(criteria, &ec2.Filter{Name: aws.String(filterAvailabilityZone), Values: aws.StringSlice(m.AWSMachinePool.Spec.AvailabilityZones)}) out, err := ec2Client.DescribeSubnets(&ec2.DescribeSubnetsInput{Filters: criteria}) if err != nil { return subnetIDs, fmt.Errorf("describing subnets with filters: %w", err) From d6e0cab810a6051fb658e49b45a58e0bfe47f189 Mon Sep 17 00:00:00 2001 From: calvix Date: Mon, 15 Nov 2021 13:17:10 +0100 Subject: [PATCH 3/3] gofmt --- pkg/cloud/scope/machinepool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cloud/scope/machinepool.go b/pkg/cloud/scope/machinepool.go index feab669d91..19931b53cf 100644 --- a/pkg/cloud/scope/machinepool.go +++ b/pkg/cloud/scope/machinepool.go @@ -31,7 +31,7 @@ import ( "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1" - "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/filter" + "sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/filter" clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" "sigs.k8s.io/cluster-api/controllers/remote" capierrors "sigs.k8s.io/cluster-api/errors"