Skip to content

Commit 713e5d3

Browse files
authored
Merge pull request #3255 from shivi28/subnet_filter
Added subnet IDs lookup by AWSMachinePool Spec Subnets.Filters
2 parents 8da76c2 + 10af88c commit 713e5d3

File tree

5 files changed

+162
-44
lines changed

5 files changed

+162
-44
lines changed

exp/api/v1beta1/awsmachinepool_webhook.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ func (r *AWSMachinePool) validateRootVolume() field.ErrorList {
8181
return allErrs
8282
}
8383

84+
func (r *AWSMachinePool) validateSubnets() field.ErrorList {
85+
var allErrs field.ErrorList
86+
87+
if r.Spec.Subnets == nil {
88+
return allErrs
89+
}
90+
91+
for _, subnet := range r.Spec.Subnets {
92+
if subnet.ID != nil && subnet.Filters != nil {
93+
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec.subnets.filters"), "providing either subnet ID or filter is supported, should not provide both"))
94+
break
95+
}
96+
}
97+
98+
return allErrs
99+
}
100+
84101
// ValidateCreate will do any extra validation when creating a AWSMachinePool.
85102
func (r *AWSMachinePool) ValidateCreate() error {
86103
log.Info("AWSMachinePool validate create", "name", r.Name)
@@ -90,6 +107,7 @@ func (r *AWSMachinePool) ValidateCreate() error {
90107
allErrs = append(allErrs, r.validateDefaultCoolDown()...)
91108
allErrs = append(allErrs, r.validateRootVolume()...)
92109
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
110+
allErrs = append(allErrs, r.validateSubnets()...)
93111

94112
if len(allErrs) == 0 {
95113
return nil
@@ -108,6 +126,7 @@ func (r *AWSMachinePool) ValidateUpdate(old runtime.Object) error {
108126

109127
allErrs = append(allErrs, r.validateDefaultCoolDown()...)
110128
allErrs = append(allErrs, r.Spec.AdditionalTags.Validate()...)
129+
allErrs = append(allErrs, r.validateSubnets()...)
111130

112131
if len(allErrs) == 0 {
113132
return nil

pkg/cloud/scope/machinepool.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"strings"
2323

24-
"github.com/aws/aws-sdk-go/aws"
2524
"github.com/go-logr/logr"
2625
"github.com/pkg/errors"
2726
corev1 "k8s.io/api/core/v1"
@@ -222,12 +221,7 @@ func (m *MachinePoolScope) IsEKSManaged() bool {
222221
}
223222

224223
// SubnetIDs returns the machine pool subnet IDs.
225-
func (m *MachinePoolScope) SubnetIDs() ([]string, error) {
226-
subnetIDs := make([]string, len(m.AWSMachinePool.Spec.Subnets))
227-
for i, v := range m.AWSMachinePool.Spec.Subnets {
228-
subnetIDs[i] = aws.StringValue(v.ID)
229-
}
230-
224+
func (m *MachinePoolScope) SubnetIDs(subnetIDs []string) ([]string, error) {
231225
strategy, err := newDefaultSubnetPlacementStrategy(&m.Logger)
232226
if err != nil {
233227
return subnetIDs, fmt.Errorf("getting subnet placement strategy: %w", err)

pkg/cloud/services/autoscaling/autoscalinggroup.go

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/aws/aws-sdk-go/aws"
2424
"github.com/aws/aws-sdk-go/service/autoscaling"
25+
"github.com/aws/aws-sdk-go/service/ec2"
2526
"github.com/pkg/errors"
2627
"k8s.io/utils/pointer"
2728

@@ -144,7 +145,7 @@ func (s *Service) GetASGByName(scope *scope.MachinePoolScope) (*expinfrav1.AutoS
144145

145146
// CreateASG runs an autoscaling group.
146147
func (s *Service) CreateASG(scope *scope.MachinePoolScope) (*expinfrav1.AutoScalingGroup, error) {
147-
subnets, err := scope.SubnetIDs()
148+
subnets, err := s.SubnetIDs(scope)
148149
if err != nil {
149150
return nil, fmt.Errorf("getting subnets for ASG: %w", err)
150151
}
@@ -267,15 +268,9 @@ func (s *Service) DeleteASG(name string) error {
267268

268269
// UpdateASG will update the ASG of a service.
269270
func (s *Service) UpdateASG(scope *scope.MachinePoolScope) error {
270-
subnetIDs := make([]string, len(scope.AWSMachinePool.Spec.Subnets))
271-
for i, v := range scope.AWSMachinePool.Spec.Subnets {
272-
subnetIDs[i] = aws.StringValue(v.ID)
273-
}
274-
275-
if len(subnetIDs) == 0 {
276-
for _, subnet := range scope.InfraCluster.Subnets() {
277-
subnetIDs = append(subnetIDs, subnet.ID)
278-
}
271+
subnetIDs, err := s.SubnetIDs(scope)
272+
if err != nil {
273+
return fmt.Errorf("getting subnets for ASG: %w", err)
279274
}
280275

281276
input := &autoscaling.UpdateAutoScalingGroupInput{
@@ -465,3 +460,38 @@ func mapToTags(input map[string]string, resourceID *string) []*autoscaling.Tag {
465460
}
466461
return tags
467462
}
463+
464+
// SubnetIDs return subnet IDs of a AWSMachinePool based on given subnetIDs and filters.
465+
func (s *Service) SubnetIDs(scope *scope.MachinePoolScope) ([]string, error) {
466+
subnetIDs := make([]string, 0)
467+
var inputFilters = make([]*ec2.Filter, 0)
468+
469+
for _, subnet := range scope.AWSMachinePool.Spec.Subnets {
470+
switch {
471+
case subnet.ID != nil:
472+
subnetIDs = append(subnetIDs, aws.StringValue(subnet.ID))
473+
case subnet.Filters != nil:
474+
for _, eachFilter := range subnet.Filters {
475+
inputFilters = append(inputFilters, &ec2.Filter{
476+
Name: aws.String(eachFilter.Name),
477+
Values: aws.StringSlice(eachFilter.Values),
478+
})
479+
}
480+
}
481+
}
482+
483+
if len(inputFilters) > 0 {
484+
out, err := s.EC2Client.DescribeSubnets(&ec2.DescribeSubnetsInput{
485+
Filters: inputFilters,
486+
})
487+
if err != nil {
488+
return nil, err
489+
}
490+
491+
for _, subnet := range out.Subnets {
492+
subnetIDs = append(subnetIDs, *subnet.SubnetId)
493+
}
494+
}
495+
496+
return scope.SubnetIDs(subnetIDs)
497+
}

0 commit comments

Comments
 (0)