Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements a temporary hack to resolve an issue where the LoadBalance… #836

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions pkg/cloudprovider/aws/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/awslabs/karpenter/pkg/cloudprovider/aws/apis/v1alpha1"
"github.com/awslabs/karpenter/pkg/utils/options"
"github.com/mitchellh/hashstructure/v2"
"github.com/patrickmn/go-cache"
"knative.dev/pkg/logging"
Expand All @@ -45,14 +46,20 @@ func (s *SecurityGroupProvider) Get(ctx context.Context, constraints *v1alpha1.C
if err != nil {
return nil, err
}
// This hack works around
// https://github.com/kubernetes-sigs/aws-load-balancer-controller/issues/2367
// The LoadBalancer Controller expects a single security group with the
// cluster tag, but provisioning tools like eksctl and kops create multiple.
securityGroups = filterClusterTaggedGroups(ctx, securityGroups)

// Fail if no security groups found
if len(securityGroups) == 0 {
return nil, fmt.Errorf("no security groups exist given constraints")
}
// Convert to IDs
securityGroupIds := []string{}
for _, securityGroup := range securityGroups {
securityGroupIds = append(securityGroupIds, *securityGroup.GroupId)
securityGroupIds = append(securityGroupIds, aws.StringValue(securityGroup.GroupId))
}
return securityGroupIds, nil
}
Expand Down Expand Up @@ -88,11 +95,37 @@ func (s *SecurityGroupProvider) getSecurityGroups(ctx context.Context, filters [
return nil, fmt.Errorf("describing security groups %+v, %w", filters, err)
}
s.cache.SetDefault(fmt.Sprint(hash), output.SecurityGroups)
logging.FromContext(ctx).Debugf("Discovered security groups: %s", s.securityGroupIds(output.SecurityGroups))
logging.FromContext(ctx).Debugf("Discovered security groups: %s", securityGroupIds(output.SecurityGroups))
return output.SecurityGroups, nil
}

func (s *SecurityGroupProvider) securityGroupIds(securityGroups []*ec2.SecurityGroup) []string {
func filterClusterTaggedGroups(ctx context.Context, securityGroups []*ec2.SecurityGroup) []*ec2.SecurityGroup {
Copy link
Contributor

@bwagner5 bwagner5 Nov 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No receivers to confine this to securitygroups instead of exposing to all of aws pkg?

filtered := []*ec2.SecurityGroup{}
foundClusterTag := false
for _, securityGroup := range securityGroups {
if hasClusterTag(ctx, securityGroup) {
if foundClusterTag {
logging.FromContext(ctx).Debugf("Ignoring security group %s, only one group with tag %s is allowed", aws.StringValue(securityGroup.GroupId),
fmt.Sprint(v1alpha1.ClusterDiscoveryTagKeyFormat, options.Get(ctx).ClusterName))
continue
}
foundClusterTag = true
}
filtered = append(filtered, securityGroup)
}
return filtered
}

func hasClusterTag(ctx context.Context, securityGroup *ec2.SecurityGroup) bool {
for _, tag := range securityGroup.Tags {
if aws.StringValue(tag.Key) == fmt.Sprintf(v1alpha1.ClusterDiscoveryTagKeyFormat, options.Get(ctx).ClusterName) {
return true
}
}
return false
}

func securityGroupIds(securityGroups []*ec2.SecurityGroup) []string {
names := []string{}
for _, securityGroup := range securityGroups {
names = append(names, aws.StringValue(securityGroup.GroupId))
Expand Down