Skip to content

Commit b8aa4b6

Browse files
zac-nixonniclask25
authored and
niclask25
committed
refactor duplication of deletion protection attribute
1 parent 2b8d4f5 commit b8aa4b6

File tree

7 files changed

+19
-22
lines changed

7 files changed

+19
-22
lines changed

pkg/deploy/elbv2/load_balancer_synthesizer.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,10 @@ import (
1414
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
1515
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
1616
"sigs.k8s.io/aws-load-balancer-controller/pkg/runtime"
17+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
1718
"strings"
1819
)
1920

20-
const (
21-
lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
22-
)
23-
2421
// NewLoadBalancerSynthesizer constructs loadBalancerSynthesizer
2522
func NewLoadBalancerSynthesizer(elbv2Client services.ELBV2, trackingProvider tracking.Provider, taggingManager TaggingManager,
2623
lbManager LoadBalancerManager, logger logr.Logger, featureGates config.FeatureGates, controllerConfig config.ControllerConfig, stack core.Stack) *loadBalancerSynthesizer {
@@ -116,7 +113,7 @@ func (s *loadBalancerSynthesizer) disableDeletionProtection(ctx context.Context,
116113
input := &elbv2sdk.ModifyLoadBalancerAttributesInput{
117114
Attributes: []elbv2types.LoadBalancerAttribute{
118115
{
119-
Key: awssdk.String(lbAttrsDeletionProtectionEnabled),
116+
Key: awssdk.String(shared_constants.LBAttributeDeletionProtection),
120117
Value: awssdk.String("false"),
121118
},
122119
},

pkg/gateway/model/base_model_builder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/core"
1616
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
1717
"sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
18+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
1819
gwv1 "sigs.k8s.io/gateway-api/apis/v1"
1920
"strconv"
2021
)
@@ -118,7 +119,7 @@ func (baseBuilder *baseModelBuilder) isDeleteProtected(lbConf *elbv2gw.LoadBalan
118119
}
119120

120121
for _, attr := range lbConf.Spec.LoadBalancerAttributes {
121-
if attr.Key == deletionProtectionAttributeKey {
122+
if attr.Key == shared_constants.LBAttributeDeletionProtection {
122123
deletionProtectionEnabled, err := strconv.ParseBool(attr.Value)
123124

124125
if err != nil {

pkg/gateway/model/constants.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

pkg/ingress/model_builder.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ingress
33
import (
44
"context"
55
"reflect"
6+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
67
"strconv"
78

89
elbv2types "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancingv2/types"
@@ -30,8 +31,7 @@ import (
3031
)
3132

3233
const (
33-
lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
34-
controllerName = "ingress"
34+
controllerName = "ingress"
3535
)
3636

3737
// ModelBuilder is responsible for build mode stack for a IngressGroup.
@@ -458,8 +458,8 @@ func (t *defaultModelBuildTask) getDeletionProtectionViaAnnotation(ing *networki
458458
if err != nil {
459459
return false, err
460460
}
461-
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtectionEnabled]; deletionProtectionSpecified {
462-
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtectionEnabled])
461+
if _, deletionProtectionSpecified := lbAttributes[shared_constants.LBAttributeDeletionProtection]; deletionProtectionSpecified {
462+
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[shared_constants.LBAttributeDeletionProtection])
463463
if err != nil {
464464
return false, err
465465
}

pkg/service/model_build_load_balancer_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
2626
)
2727

28-
const lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
29-
3028
func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
3129
tests := []struct {
3230
testName string
@@ -79,7 +77,7 @@ func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
7977
Value: "true",
8078
},
8179
{
82-
Key: lbAttrsDeletionProtectionEnabled,
80+
Key: shared_constants.LBAttributeDeletionProtection,
8381
Value: "true",
8482
},
8583
},
@@ -113,7 +111,7 @@ func Test_defaultModelBuilderTask_buildLBAttributes(t *testing.T) {
113111
Value: "true",
114112
},
115113
{
116-
Key: lbAttrsDeletionProtectionEnabled,
114+
Key: shared_constants.LBAttributeDeletionProtection,
117115
Value: "true",
118116
},
119117
{

pkg/service/model_builder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package service
22

33
import (
44
"context"
5+
"sigs.k8s.io/aws-load-balancer-controller/pkg/shared_constants"
56
"strconv"
67
"sync"
78

@@ -29,7 +30,6 @@ const (
2930
LoadBalancerTypeExternal = "external"
3031
LoadBalancerTargetTypeIP = "ip"
3132
LoadBalancerTargetTypeInstance = "instance"
32-
lbAttrsDeletionProtection = "deletion_protection.enabled"
3333
controllerName = "service"
3434
)
3535

@@ -274,8 +274,8 @@ func (t *defaultModelBuildTask) getDeletionProtectionViaAnnotation(svc corev1.Se
274274
if err != nil {
275275
return false, err
276276
}
277-
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtection]; deletionProtectionSpecified {
278-
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtection])
277+
if _, deletionProtectionSpecified := lbAttributes[shared_constants.LBAttributeDeletionProtection]; deletionProtectionSpecified {
278+
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[shared_constants.LBAttributeDeletionProtection])
279279
if err != nil {
280280
return false, err
281281
}

pkg/shared_constants/attributes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package shared_constants
2+
3+
const (
4+
// LBAttributeDeletionProtection deletion protection attribute name
5+
LBAttributeDeletionProtection = "deletion_protection.enabled"
6+
)

0 commit comments

Comments
 (0)