Skip to content

Commit d19ccc6

Browse files
committed
check deletion_protection in model builder step
1 parent dae2125 commit d19ccc6

File tree

5 files changed

+60
-55
lines changed

5 files changed

+60
-55
lines changed

controllers/ingress/group_controller.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"sigs.k8s.io/controller-runtime/pkg/controller"
3030
"sigs.k8s.io/controller-runtime/pkg/event"
3131
"sigs.k8s.io/controller-runtime/pkg/source"
32-
"strconv"
3332
)
3433

3534
const (
@@ -39,7 +38,6 @@ const (
3938
// the groupVersion of used Ingress & IngressClass resource.
4039
ingressResourcesGroupVersion = "networking.k8s.io/v1beta1"
4140
ingressClassKind = "IngressClass"
42-
lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
4341
)
4442

4543
// NewGroupReconciler constructs new GroupReconciler
@@ -128,17 +126,6 @@ func (r *groupReconciler) reconcile(ctx context.Context, req ctrl.Request) error
128126
r.recordIngressGroupEvent(ctx, ingGroup, corev1.EventTypeWarning, k8s.IngressEventReasonFailedAddFinalizer, fmt.Sprintf("Failed add finalizer due to %v", err))
129127
return err
130128
}
131-
for _, inactiveMember := range ingGroup.InactiveMembers {
132-
if !inactiveMember.DeletionTimestamp.IsZero() {
133-
deletionProtectionEnabled, err := r.getDeletionProtectionViaAnnotation(inactiveMember)
134-
if err != nil {
135-
return err
136-
}
137-
if deletionProtectionEnabled {
138-
return errors.Errorf("deletion_protection is enabled, cannot delete the ingress: %v", inactiveMember.Name)
139-
}
140-
}
141-
}
142129
_, lb, err := r.buildAndDeployModel(ctx, ingGroup)
143130
if err != nil {
144131
return err
@@ -327,22 +314,6 @@ func (r *groupReconciler) setupWatches(_ context.Context, c controller.Controlle
327314
return nil
328315
}
329316

330-
func (r *groupReconciler) getDeletionProtectionViaAnnotation(ing *networking.Ingress) (bool, error) {
331-
var lbAttributes map[string]string
332-
_, err := r.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixLoadBalancerAttributes, &lbAttributes, ing.Annotations)
333-
if err != nil {
334-
return false, err
335-
}
336-
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtectionEnabled]; deletionProtectionSpecified {
337-
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtectionEnabled])
338-
if err != nil {
339-
return false, err
340-
}
341-
return deletionProtectionEnabled, nil
342-
}
343-
return false, nil
344-
}
345-
346317
// isResourceKindAvailable checks whether specific kind is available.
347318
func isResourceKindAvailable(resList *metav1.APIResourceList, kind string) bool {
348319
for _, res := range resList.APIResources {

controllers/service/service_controller.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@ import (
2424
"sigs.k8s.io/controller-runtime/pkg/client"
2525
"sigs.k8s.io/controller-runtime/pkg/controller"
2626
"sigs.k8s.io/controller-runtime/pkg/source"
27-
"strconv"
2827
)
2928

3029
const (
3130
serviceFinalizer = "service.k8s.aws/resources"
3231
serviceTagPrefix = "service.k8s.aws"
3332
serviceAnnotationPrefix = "service.beta.kubernetes.io"
3433
controllerName = "service"
35-
lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
3634
)
3735

3836
func NewServiceReconciler(cloud aws.Cloud, k8sClient client.Client, eventRecorder record.EventRecorder,
@@ -90,13 +88,6 @@ func (r *serviceReconciler) reconcile(ctx context.Context, req ctrl.Request) err
9088
return client.IgnoreNotFound(err)
9189
}
9290
if !svc.DeletionTimestamp.IsZero() {
93-
deletionProtectionEnabled, err := r.getDeletionProtectionViaAnnotation(*svc)
94-
if err != nil {
95-
return err
96-
}
97-
if deletionProtectionEnabled {
98-
return errors.Errorf("deletion_protection is enabled, cannot delete the service: %v", svc.Name)
99-
}
10091
return r.cleanupLoadBalancerResources(ctx, svc)
10192
}
10293
return r.reconcileLoadBalancerResources(ctx, svc)
@@ -200,18 +191,3 @@ func (r *serviceReconciler) setupWatches(_ context.Context, c controller.Control
200191
return nil
201192
}
202193

203-
func (r *serviceReconciler) getDeletionProtectionViaAnnotation(svc corev1.Service) (bool, error) {
204-
var lbAttributes map[string]string
205-
_, err := r.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixLoadBalancerAttributes, &lbAttributes, svc.Annotations)
206-
if err != nil {
207-
return false, err
208-
}
209-
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtectionEnabled]; deletionProtectionSpecified {
210-
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtectionEnabled])
211-
if err != nil {
212-
return false, err
213-
}
214-
return deletionProtectionEnabled, nil
215-
}
216-
return false, nil
217-
}

pkg/deploy/elbv2/load_balancer_synthesizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (s *loadBalancerSynthesizer) Synthesize(ctx context.Context) error {
6363
for _, sdkLB := range unmatchedSDKLBs {
6464
if err := s.lbManager.Delete(ctx, sdkLB); err != nil {
6565
errMessage := err.Error()
66-
if strings.Contains(errMessage,"OperationNotPermitted") {
66+
if strings.Contains(errMessage,"OperationNotPermitted") && strings.Contains(errMessage, "deletion protection") {
6767
s.disableDeletionProtection(sdkLB.LoadBalancer)
6868
if err = s.lbManager.Delete(ctx, sdkLB); err != nil {
6969
return err

pkg/ingress/model_builder.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/go-logr/logr"
88
"github.com/pkg/errors"
99
corev1 "k8s.io/api/core/v1"
10+
networking "k8s.io/api/networking/v1beta1"
1011
"k8s.io/apimachinery/pkg/types"
1112
"k8s.io/apimachinery/pkg/util/sets"
1213
"k8s.io/client-go/tools/record"
@@ -20,6 +21,11 @@ import (
2021
elbv2model "sigs.k8s.io/aws-load-balancer-controller/pkg/model/elbv2"
2122
networkingpkg "sigs.k8s.io/aws-load-balancer-controller/pkg/networking"
2223
"sigs.k8s.io/controller-runtime/pkg/client"
24+
"strconv"
25+
)
26+
27+
const (
28+
lbAttrsDeletionProtectionEnabled = "deletion_protection.enabled"
2329
)
2430

2531
// ModelBuilder is responsible for build mode stack for a IngressGroup.
@@ -179,10 +185,20 @@ type defaultModelBuildTask struct {
179185
}
180186

181187
func (t *defaultModelBuildTask) run(ctx context.Context) error {
188+
for _, inactiveMember := range t.ingGroup.InactiveMembers {
189+
if !inactiveMember.DeletionTimestamp.IsZero() {
190+
deletionProtectionEnabled, err := t.getDeletionProtectionViaAnnotation(inactiveMember)
191+
if err != nil {
192+
return err
193+
}
194+
if deletionProtectionEnabled {
195+
return errors.Errorf("deletion_protection is enabled, cannot delete the ingress: %v", inactiveMember.Name)
196+
}
197+
}
198+
}
182199
if len(t.ingGroup.Members) == 0 {
183200
return nil
184201
}
185-
186202
ingListByPort := make(map[int64][]ClassifiedIngress)
187203
listenPortConfigsByPort := make(map[int64][]listenPortConfigWithIngress)
188204
for _, member := range t.ingGroup.Members {
@@ -340,6 +356,22 @@ func (t *defaultModelBuildTask) buildSSLRedirectConfig(ctx context.Context, list
340356
}, nil
341357
}
342358

359+
func (t *defaultModelBuildTask) getDeletionProtectionViaAnnotation(ing *networking.Ingress) (bool, error) {
360+
var lbAttributes map[string]string
361+
_, err := t.annotationParser.ParseStringMapAnnotation(annotations.IngressSuffixLoadBalancerAttributes, &lbAttributes, ing.Annotations)
362+
if err != nil {
363+
return false, err
364+
}
365+
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtectionEnabled]; deletionProtectionSpecified {
366+
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtectionEnabled])
367+
if err != nil {
368+
return false, err
369+
}
370+
return deletionProtectionEnabled, nil
371+
}
372+
return false, nil
373+
}
374+
343375
// the listen port config for specific Ingress's listener port.
344376
type listenPortConfigWithIngress struct {
345377
ingKey types.NamespacedName

pkg/service/model_builder.go

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

33
import (
44
"context"
5+
"github.com/pkg/errors"
56
"k8s.io/apimachinery/pkg/util/sets"
67
"strconv"
78
"sync"
@@ -22,6 +23,7 @@ const (
2223
LoadBalancerTypeExternal = "external"
2324
LoadBalancerTargetTypeIP = "ip"
2425
LoadBalancerTargetTypeInstance = "instance"
26+
lbAttrsDeletionProtection = "deletion_protection.enabled"
2527
)
2628

2729
// ModelBuilder builds the model stack for the service resource.
@@ -156,6 +158,13 @@ type defaultModelBuildTask struct {
156158

157159
func (t *defaultModelBuildTask) run(ctx context.Context) error {
158160
if !t.service.DeletionTimestamp.IsZero() {
161+
deletionProtectionEnabled, err := t.getDeletionProtectionViaAnnotation(*t.service)
162+
if err != nil {
163+
return err
164+
}
165+
if deletionProtectionEnabled {
166+
return errors.Errorf("deletion_protection is enabled, cannot delete the service: %v", t.service.Name)
167+
}
159168
return nil
160169
}
161170
err := t.buildModel(ctx)
@@ -181,3 +190,20 @@ func (t *defaultModelBuildTask) buildModel(ctx context.Context) error {
181190
}
182191
return nil
183192
}
193+
194+
func (t *defaultModelBuildTask) getDeletionProtectionViaAnnotation(svc corev1.Service) (bool, error) {
195+
var lbAttributes map[string]string
196+
_, err := t.annotationParser.ParseStringMapAnnotation(annotations.SvcLBSuffixLoadBalancerAttributes, &lbAttributes, svc.Annotations)
197+
if err != nil {
198+
return false, err
199+
}
200+
if _, deletionProtectionSpecified := lbAttributes[lbAttrsDeletionProtection]; deletionProtectionSpecified {
201+
deletionProtectionEnabled, err := strconv.ParseBool(lbAttributes[lbAttrsDeletionProtection])
202+
if err != nil {
203+
return false, err
204+
}
205+
return deletionProtectionEnabled, nil
206+
}
207+
return false, nil
208+
}
209+

0 commit comments

Comments
 (0)