Skip to content

Commit 430412c

Browse files
Mario Nitchevfiunchinho
andcommitted
Update LoadBalancerReadyCondition on deletion
The LoadBalancerReadyCondition is sometimes explicitly patched and other times it only updates the awsCluster object. Furthermore the condition won't be patched by the patch helper in the AWSClusterReconciler, because it's not in the `patch.WithOwnedConditions` list. This change puts the condition in the list and also updates the condition reason to `Deleted` when the LB is not found. Without setting the condition in that case, the reconciler will set condition to `Deleting` and it will never go back to `Deleted`. Co-authored-by: Jose Armesto <[email protected]>
1 parent 6ebafab commit 430412c

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

controllers/awscluster_controller.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ import (
3838
"sigs.k8s.io/controller-runtime/pkg/reconcile"
3939
"sigs.k8s.io/controller-runtime/pkg/source"
4040

41+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
42+
"sigs.k8s.io/cluster-api/util"
43+
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
44+
"sigs.k8s.io/cluster-api/util/conditions"
45+
"sigs.k8s.io/cluster-api/util/patch"
46+
"sigs.k8s.io/cluster-api/util/predicates"
47+
4148
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
4249
"sigs.k8s.io/cluster-api-provider-aws/v2/feature"
4350
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
@@ -51,12 +58,6 @@ import (
5158
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/securitygroup"
5259
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger"
5360
infrautilconditions "sigs.k8s.io/cluster-api-provider-aws/v2/util/conditions"
54-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
55-
"sigs.k8s.io/cluster-api/util"
56-
capiannotations "sigs.k8s.io/cluster-api/util/annotations"
57-
"sigs.k8s.io/cluster-api/util/conditions"
58-
"sigs.k8s.io/cluster-api/util/patch"
59-
"sigs.k8s.io/cluster-api/util/predicates"
6061
)
6162

6263
var defaultAWSSecurityGroupRoles = []infrav1.SecurityGroupRole{
@@ -171,6 +172,7 @@ func (r *AWSClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request)
171172
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
172173
infrav1.PrincipalCredentialRetrievedCondition,
173174
infrav1.PrincipalUsageAllowedCondition,
175+
infrav1.LoadBalancerReadyCondition,
174176
}})
175177
if e != nil {
176178
fmt.Println(e.Error())

pkg/cloud/services/elb/loadbalancer.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ import (
3131
"github.com/pkg/errors"
3232
"k8s.io/apimachinery/pkg/util/sets"
3333

34+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
35+
"sigs.k8s.io/cluster-api/util/conditions"
36+
3437
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
3538
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/awserrors"
3639
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/converters"
3740
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
3841
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/services/wait"
3942
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/hash"
4043
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/record"
41-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
42-
"sigs.k8s.io/cluster-api/util/conditions"
4344
)
4445

4546
// ResourceGroups are filtered by ARN identifier: https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#arns-syntax
@@ -148,6 +149,8 @@ func (s *Service) deleteAPIServerELB() error {
148149

149150
apiELB, err := s.describeClassicELB(elbName)
150151
if IsNotFound(err) {
152+
s.scope.Debug("Control plane load balancer not found, skipping deletion")
153+
conditions.MarkFalse(s.scope.InfraCluster(), infrav1.LoadBalancerReadyCondition, clusterv1.DeletedReason, clusterv1.ConditionSeverityInfo, "")
151154
return nil
152155
}
153156
if err != nil {
@@ -156,6 +159,7 @@ func (s *Service) deleteAPIServerELB() error {
156159

157160
if apiELB.IsUnmanaged(s.scope.Name()) {
158161
s.scope.Debug("Found unmanaged classic load balancer for apiserver, skipping deletion", "api-server-elb-name", apiELB.Name)
162+
conditions.MarkFalse(s.scope.InfraCluster(), infrav1.LoadBalancerReadyCondition, clusterv1.DeletedReason, clusterv1.ConditionSeverityInfo, "")
159163
return nil
160164
}
161165

pkg/cloud/services/elb/loadbalancer_test.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ import (
3535
"k8s.io/utils/pointer"
3636
"sigs.k8s.io/controller-runtime/pkg/client/fake"
3737

38+
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
39+
"sigs.k8s.io/cluster-api/util/conditions"
40+
3841
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
3942
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope"
4043
"sigs.k8s.io/cluster-api-provider-aws/v2/test/mocks"
41-
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
4244
)
4345

4446
func TestELBName(t *testing.T) {
@@ -600,8 +602,9 @@ func TestDeleteAPIServerELB(t *testing.T) {
600602
clusterName := "bar" //nolint:goconst // does not need to be a package-level const
601603
elbName := "bar-apiserver"
602604
tests := []struct {
603-
name string
604-
elbAPIMocks func(m *mocks.MockELBAPIMockRecorder)
605+
name string
606+
elbAPIMocks func(m *mocks.MockELBAPIMockRecorder)
607+
verifyAWSCluster func(*infrav1.AWSCluster)
605608
}{
606609
{
607610
name: "if control plane ELB is not found, do nothing",
@@ -610,6 +613,16 @@ func TestDeleteAPIServerELB(t *testing.T) {
610613
LoadBalancerNames: aws.StringSlice([]string{elbName}),
611614
})).Return(nil, awserr.New(elb.ErrCodeAccessPointNotFoundException, "", nil))
612615
},
616+
verifyAWSCluster: func(awsCluster *infrav1.AWSCluster) {
617+
loadBalancerConditionReady := conditions.IsTrue(awsCluster, infrav1.LoadBalancerReadyCondition)
618+
if loadBalancerConditionReady {
619+
t.Fatalf("Expected LoadBalancerReady condition to be False, but was True")
620+
}
621+
loadBalancerConditionReason := conditions.GetReason(awsCluster, infrav1.LoadBalancerReadyCondition)
622+
if loadBalancerConditionReason != clusterv1.DeletedReason {
623+
t.Fatalf("Expected LoadBalancerReady condition reason to be Deleted, but was %s", loadBalancerConditionReason)
624+
}
625+
},
613626
},
614627
{
615628
name: "if control plane ELB is found, and it is not managed, do nothing",
@@ -649,6 +662,16 @@ func TestDeleteAPIServerELB(t *testing.T) {
649662
nil,
650663
)
651664
},
665+
verifyAWSCluster: func(awsCluster *infrav1.AWSCluster) {
666+
loadBalancerConditionReady := conditions.IsTrue(awsCluster, infrav1.LoadBalancerReadyCondition)
667+
if loadBalancerConditionReady {
668+
t.Fatalf("Expected LoadBalancerReady condition to be False, but was True")
669+
}
670+
loadBalancerConditionReason := conditions.GetReason(awsCluster, infrav1.LoadBalancerReadyCondition)
671+
if loadBalancerConditionReason != clusterv1.DeletedReason {
672+
t.Fatalf("Expected LoadBalancerReady condition reason to be Deleted, but was %s", loadBalancerConditionReason)
673+
}
674+
},
652675
},
653676
{
654677
name: "if control plane ELB is found, and it is managed, delete the ELB",
@@ -701,6 +724,16 @@ func TestDeleteAPIServerELB(t *testing.T) {
701724
nil,
702725
)
703726
},
727+
verifyAWSCluster: func(awsCluster *infrav1.AWSCluster) {
728+
loadBalancerConditionReady := conditions.IsTrue(awsCluster, infrav1.LoadBalancerReadyCondition)
729+
if loadBalancerConditionReady {
730+
t.Fatalf("Expected LoadBalancerReady condition to be False, but was True")
731+
}
732+
loadBalancerConditionReason := conditions.GetReason(awsCluster, infrav1.LoadBalancerReadyCondition)
733+
if loadBalancerConditionReason != clusterv1.DeletedReason {
734+
t.Fatalf("Expected LoadBalancerReady condition reason to be Deleted, but was %s", loadBalancerConditionReason)
735+
}
736+
},
704737
},
705738
}
706739

@@ -755,6 +788,8 @@ func TestDeleteAPIServerELB(t *testing.T) {
755788
if err != nil {
756789
t.Fatal(err)
757790
}
791+
792+
tc.verifyAWSCluster(awsCluster)
758793
})
759794
}
760795
}

0 commit comments

Comments
 (0)