Skip to content

Commit fd12474

Browse files
authored
Merge pull request #4404 from zac-nixon/znixon/gw-no-sg
[gw api] add disable security group flag to lb config for gateway users
2 parents d07b7e8 + 6fab865 commit fd12474

File tree

10 files changed

+86
-14
lines changed

10 files changed

+86
-14
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ scripts/aws_sdk_model_override/*
2828
/gomock_reflect*
2929
config/crd/bases/gateway.k8s.aws_listenerruleconfigurations.yaml
3030
config/crd/bases/gateway.k8s.aws_loadbalancerconfigurations.yaml
31-
config/crd/bases/gateway.k8s.aws_targetgroupconfigurations.yaml
31+
config/crd/bases/aga.k8s.aws_globalaccelerators.yaml
32+
config/crd/bases/gateway.k8s.aws_targetgroupconfigurations.yaml

apis/gateway/v1beta1/loadbalancerconfig_types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ type LoadBalancerConfigurationSpec struct {
233233
// +optional
234234
ListenerConfigurations *[]ListenerConfiguration `json:"listenerConfigurations,omitempty"`
235235

236+
// disableSecurityGroup provisions a load balancer with no security groups.
237+
// Allows an NLB to be provisioned with no security groups.
238+
// [Network Load Balancer]
239+
DisableSecurityGroup *bool `json:"disableSecurityGroup,omitempty"`
240+
236241
// securityGroups an optional list of security group ids or names to apply to the LB
237242
// +optional
238243
SecurityGroups *[]string `json:"securityGroups,omitempty"`

apis/gateway/v1beta1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/gateway/gateway-crds.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ spec:
449449
customerOwnedIpv4Pool [Application LoadBalancer]
450450
is the ID of the customer-owned address for Application Load Balancers on Outposts pool.
451451
type: string
452+
disableSecurityGroup:
453+
description: |-
454+
disableSecurityGroup provisions a load balancer with no security groups.
455+
Allows an NLB to be provisioned with no security groups.
456+
[Network Load Balancer]
457+
type: boolean
452458
enableICMP:
453459
description: |-
454460
EnableICMP [Network LoadBalancer]

config/crd/gateway/gateway.k8s.aws_loadbalancerconfigurations.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ spec:
5050
customerOwnedIpv4Pool [Application LoadBalancer]
5151
is the ID of the customer-owned address for Application Load Balancers on Outposts pool.
5252
type: string
53+
disableSecurityGroup:
54+
description: |-
55+
disableSecurityGroup provisions a load balancer with no security groups.
56+
Allows an NLB to be provisioned with no security groups.
57+
[Network Load Balancer]
58+
type: boolean
5359
enableICMP:
5460
description: |-
5561
EnableICMP [Network LoadBalancer]

helm/aws-load-balancer-controller/crds/gateway-crds.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,12 @@ spec:
449449
customerOwnedIpv4Pool [Application LoadBalancer]
450450
is the ID of the customer-owned address for Application Load Balancers on Outposts pool.
451451
type: string
452+
disableSecurityGroup:
453+
description: |-
454+
disableSecurityGroup provisions a load balancer with no security groups.
455+
Allows an NLB to be provisioned with no security groups.
456+
[Network Load Balancer]
457+
type: boolean
452458
enableICMP:
453459
description: |-
454460
EnableICMP [Network LoadBalancer]

pkg/gateway/lb_config_merger.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,4 +177,10 @@ func (merger *loadBalancerConfigMergerImpl) performTakeOneMerges(merged *elbv2gw
177177
} else {
178178
merged.ShieldAdvanced = lowPriority.Spec.ShieldAdvanced
179179
}
180+
181+
if highPriority.Spec.DisableSecurityGroup != nil {
182+
merged.DisableSecurityGroup = highPriority.Spec.DisableSecurityGroup
183+
} else {
184+
merged.DisableSecurityGroup = lowPriority.Spec.DisableSecurityGroup
185+
}
180186
}

pkg/gateway/model/base_model_builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func NewModelBuilder(subnetsResolver networking.SubnetsResolver,
4545

4646
gwTagHelper := newTagHelper(sets.New(lbcConfig.ExternalManagedTags...), lbcConfig.DefaultTags, featureGates.Enabled(config.EnableDefaultTagsLowPriority))
4747
subnetBuilder := newSubnetModelBuilder(loadBalancerType, trackingProvider, subnetsResolver, elbv2TaggingManager)
48-
sgBuilder := newSecurityGroupBuilder(gwTagHelper, clusterName, enableBackendSG, sgResolver, backendSGProvider, logger)
48+
sgBuilder := newSecurityGroupBuilder(gwTagHelper, clusterName, loadBalancerType, enableBackendSG, sgResolver, backendSGProvider, logger)
4949
lbBuilder := newLoadBalancerBuilder(loadBalancerType, gwTagHelper, clusterName)
5050
tgConfigConstructor := config2.NewTargetGroupConfigConstructor()
5151

@@ -163,7 +163,7 @@ func (baseBuilder *baseModelBuilder) Build(ctx context.Context, gw *gwv1.Gateway
163163

164164
/* Security Groups */
165165

166-
securityGroups, err := baseBuilder.securityGroupBuilder.buildSecurityGroups(ctx, stack, lbConf, gw, routes, ipAddressType)
166+
securityGroups, err := baseBuilder.securityGroupBuilder.buildSecurityGroups(ctx, stack, lbConf, gw, ipAddressType)
167167

168168
if err != nil {
169169
return nil, nil, nil, false, nil, err

pkg/gateway/model/model_build_security_group.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"k8s.io/apimachinery/pkg/types"
1313
"regexp"
1414
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
15-
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils"
1615
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1716
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
1817
ec2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/ec2"
@@ -39,44 +38,50 @@ type securityGroupOutput struct {
3938
}
4039

4140
type securityGroupBuilder interface {
42-
buildSecurityGroups(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, routes map[int32][]routeutils.RouteDescriptor, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error)
41+
buildSecurityGroups(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error)
4342
}
4443

4544
type securityGroupBuilderImpl struct {
4645
tagHelper tagHelper
4746
clusterName string
4847
sgResolver networking.SecurityGroupResolver
4948
backendSGProvider networking.BackendSGProvider
49+
loadBalancerType elbv2model.LoadBalancerType
5050

5151
enableBackendSG bool
5252
logger logr.Logger
5353
}
5454

55-
func newSecurityGroupBuilder(tagHelper tagHelper, clusterName string, enableBackendSG bool, sgResolver networking.SecurityGroupResolver, backendSGProvider networking.BackendSGProvider, logger logr.Logger) securityGroupBuilder {
55+
func newSecurityGroupBuilder(tagHelper tagHelper, clusterName string, loadBalancerType elbv2model.LoadBalancerType, enableBackendSG bool, sgResolver networking.SecurityGroupResolver, backendSGProvider networking.BackendSGProvider, logger logr.Logger) securityGroupBuilder {
5656
return &securityGroupBuilderImpl{
5757
tagHelper: tagHelper,
5858
clusterName: clusterName,
5959
logger: logger,
6060
enableBackendSG: enableBackendSG,
6161
sgResolver: sgResolver,
6262
backendSGProvider: backendSGProvider,
63+
loadBalancerType: loadBalancerType,
6364
}
6465
}
6566

66-
func (builder *securityGroupBuilderImpl) buildSecurityGroups(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, routes map[int32][]routeutils.RouteDescriptor, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error) {
67+
func (builder *securityGroupBuilderImpl) buildSecurityGroups(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error) {
6768
var sgNameOrIds []string
6869
if lbConf.Spec.SecurityGroups != nil {
6970
sgNameOrIds = *lbConf.Spec.SecurityGroups
7071
}
7172

73+
if lbConf.Spec.DisableSecurityGroup != nil && *lbConf.Spec.DisableSecurityGroup && builder.loadBalancerType == elbv2model.LoadBalancerTypeNetwork {
74+
return securityGroupOutput{}, nil
75+
}
76+
7277
if len(sgNameOrIds) == 0 {
73-
return builder.handleManagedSecurityGroup(ctx, stack, lbConf, gw, routes, ipAddressType)
78+
return builder.handleManagedSecurityGroup(ctx, stack, lbConf, gw, ipAddressType)
7479
}
7580

7681
return builder.handleExplicitSecurityGroups(ctx, lbConf, gw, sgNameOrIds)
7782
}
7883

79-
func (builder *securityGroupBuilderImpl) handleManagedSecurityGroup(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, routes map[int32][]routeutils.RouteDescriptor, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error) {
84+
func (builder *securityGroupBuilderImpl) handleManagedSecurityGroup(ctx context.Context, stack core.Stack, lbConf elbv2gw.LoadBalancerConfiguration, gw *gwv1.Gateway, ipAddressType elbv2model.IPAddressType) (securityGroupOutput, error) {
8085
var lbSGTokens []core.StringToken
8186
managedSG, err := builder.buildManagedSecurityGroup(stack, lbConf, gw, ipAddressType)
8287
if err != nil {

pkg/gateway/model/model_build_security_group_test.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
"k8s.io/apimachinery/pkg/types"
1212
elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1"
13-
"sigs.k8s.io/aws-load-balancer-controller/pkg/gateway/routeutils"
1413
"sigs.k8s.io/aws-load-balancer-controller/pkg/k8s"
1514
coremodel "sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
1615
ec2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/ec2"
@@ -36,6 +35,7 @@ func Test_BuildSecurityGroups_Specified(t *testing.T) {
3635
testCases := []struct {
3736
name string
3837
lbConf elbv2gw.LoadBalancerConfiguration
38+
lbType elbv2model.LoadBalancerType
3939
ipAddressType elbv2model.IPAddressType
4040
expectedTags map[string]string
4141
tagErr error
@@ -70,6 +70,38 @@ func Test_BuildSecurityGroups_Specified(t *testing.T) {
7070
coremodel.LiteralStringToken("sg2"),
7171
},
7272
},
73+
{
74+
name: "sg disabled - nlb",
75+
lbConf: elbv2gw.LoadBalancerConfiguration{
76+
Spec: elbv2gw.LoadBalancerConfigurationSpec{
77+
DisableSecurityGroup: awssdk.Bool(true),
78+
},
79+
},
80+
lbType: elbv2model.LoadBalancerTypeNetwork,
81+
},
82+
{
83+
name: "sg disabled - alb",
84+
lbConf: elbv2gw.LoadBalancerConfiguration{
85+
Spec: elbv2gw.LoadBalancerConfigurationSpec{
86+
DisableSecurityGroup: awssdk.Bool(true),
87+
SecurityGroups: &[]string{
88+
"sg1",
89+
"sg2",
90+
},
91+
},
92+
},
93+
lbType: elbv2model.LoadBalancerTypeApplication,
94+
resolveSg: &resolveSgCall{
95+
securityGroups: []string{
96+
"sg1",
97+
"sg2",
98+
},
99+
},
100+
expectedSgTokens: []coremodel.StringToken{
101+
coremodel.LiteralStringToken("sg1"),
102+
coremodel.LiteralStringToken("sg2"),
103+
},
104+
},
73105
{
74106
name: "sg specified - with backend sg",
75107
enableBackendSg: true,
@@ -186,9 +218,9 @@ func Test_BuildSecurityGroups_Specified(t *testing.T) {
186218
}
187219

188220
stack := coremodel.NewDefaultStack(coremodel.StackID{Namespace: "namespace", Name: "name"})
189-
builder := newSecurityGroupBuilder(mockTagger, clusterName, tc.enableBackendSg, mockSgResolver, mockSgProvider, logr.Discard())
221+
builder := newSecurityGroupBuilder(mockTagger, clusterName, tc.lbType, tc.enableBackendSg, mockSgResolver, mockSgProvider, logr.Discard())
190222

191-
out, err := builder.buildSecurityGroups(context.Background(), stack, tc.lbConf, gw, make(map[int32][]routeutils.RouteDescriptor), tc.ipAddressType)
223+
out, err := builder.buildSecurityGroups(context.Background(), stack, tc.lbConf, gw, tc.ipAddressType)
192224

193225
if tc.expectErr {
194226
assert.Error(t, err)
@@ -291,9 +323,9 @@ func Test_BuildSecurityGroups_Allocate(t *testing.T) {
291323
}
292324

293325
stack := coremodel.NewDefaultStack(coremodel.StackID{Namespace: "namespace", Name: "name"})
294-
builder := newSecurityGroupBuilder(mockTagger, clusterName, tc.enableBackendSg, mockSgResolver, mockSgProvider, logr.Discard())
326+
builder := newSecurityGroupBuilder(mockTagger, clusterName, elbv2model.LoadBalancerTypeApplication, tc.enableBackendSg, mockSgResolver, mockSgProvider, logr.Discard())
295327

296-
out, err := builder.buildSecurityGroups(context.Background(), stack, tc.lbConf, gw, make(map[int32][]routeutils.RouteDescriptor), tc.ipAddressType)
328+
out, err := builder.buildSecurityGroups(context.Background(), stack, tc.lbConf, gw, tc.ipAddressType)
297329

298330
if tc.expectErr {
299331
assert.Error(t, err)

0 commit comments

Comments
 (0)