@@ -19,9 +19,12 @@ package docker
1919
2020import (
2121 "context"
22+ "fmt"
2223 "strconv"
24+ "time"
2325
2426 "github.com/pkg/errors"
27+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2528 ctrl "sigs.k8s.io/controller-runtime"
2629 "sigs.k8s.io/controller-runtime/pkg/client"
2730 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -31,16 +34,14 @@ import (
3134 infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
3235 "sigs.k8s.io/cluster-api/test/infrastructure/docker/internal/docker"
3336 "sigs.k8s.io/cluster-api/util/conditions"
37+ v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
3438 "sigs.k8s.io/cluster-api/util/patch"
3539)
3640
3741// ClusterBackEndReconciler reconciles a DockerCluster object.
3842type ClusterBackEndReconciler struct {
3943 client.Client
4044 ContainerRuntime container.Runtime
41-
42- NewPatchHelperFunc func (obj client.Object , crClient client.Client ) (* patch.Helper , error )
43- PatchDevClusterFunc func (ctx context.Context , patchHelper * patch.Helper , dockerCluster * infrav1.DevCluster ) error
4445}
4546
4647// ReconcileNormal handle docker backend for DevCluster not yet deleted.
@@ -61,19 +62,37 @@ func (r *ClusterBackEndReconciler) ReconcileNormal(ctx context.Context, cluster
6162 strconv .Itoa (dockerCluster .Spec .ControlPlaneEndpoint .Port ))
6263 if err != nil {
6364 conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , infrav1 .LoadBalancerProvisioningFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
65+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
66+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
67+ Status : metav1 .ConditionFalse ,
68+ Reason : infrav1 .DevClusterDockerLoadBalancerNotAvailableV1Beta2Reason ,
69+ Message : fmt .Sprintf ("Failed to create helper for managing the externalLoadBalancer: %v" , err ),
70+ })
6471 return ctrl.Result {}, errors .Wrapf (err , "failed to create helper for managing the externalLoadBalancer" )
6572 }
6673
6774 // Create the docker container hosting the load balancer.
6875 if err := externalLoadBalancer .Create (ctx ); err != nil {
6976 conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , infrav1 .LoadBalancerProvisioningFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
77+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
78+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
79+ Status : metav1 .ConditionFalse ,
80+ Reason : infrav1 .DevClusterDockerLoadBalancerNotAvailableV1Beta2Reason ,
81+ Message : fmt .Sprintf ("Failed to create load balancer: %v" , err ),
82+ })
7083 return ctrl.Result {}, errors .Wrap (err , "failed to create load balancer" )
7184 }
7285
7386 // Set APIEndpoints with the load balancer IP so the Cluster API Cluster Controller can pull it
7487 lbIP , err := externalLoadBalancer .IP (ctx )
7588 if err != nil {
7689 conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , infrav1 .LoadBalancerProvisioningFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
90+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
91+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
92+ Status : metav1 .ConditionFalse ,
93+ Reason : infrav1 .DevClusterDockerLoadBalancerNotAvailableV1Beta2Reason ,
94+ Message : fmt .Sprintf ("Failed to get ip for the load balancer: %v" , err ),
95+ })
7796 return ctrl.Result {}, errors .Wrap (err , "failed to get ip for the load balancer" )
7897 }
7998
@@ -86,6 +105,11 @@ func (r *ClusterBackEndReconciler) ReconcileNormal(ctx context.Context, cluster
86105 // Mark the dockerCluster ready
87106 dockerCluster .Status .Ready = true
88107 conditions .MarkTrue (dockerCluster , infrav1 .LoadBalancerAvailableCondition )
108+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
109+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
110+ Status : metav1 .ConditionTrue ,
111+ Reason : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Reason ,
112+ })
89113
90114 return ctrl.Result {}, nil
91115}
@@ -103,28 +127,26 @@ func (r *ClusterBackEndReconciler) ReconcileDelete(ctx context.Context, cluster
103127 strconv .Itoa (dockerCluster .Spec .ControlPlaneEndpoint .Port ))
104128 if err != nil {
105129 conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , infrav1 .LoadBalancerProvisioningFailedReason , clusterv1 .ConditionSeverityWarning , err .Error ())
130+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
131+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
132+ Status : metav1 .ConditionFalse ,
133+ Reason : infrav1 .DevClusterDockerLoadBalancerNotAvailableV1Beta2Reason ,
134+ Message : fmt .Sprintf ("Failed to create helper for managing the externalLoadBalancer: %v" , err ),
135+ })
136+
106137 return ctrl.Result {}, errors .Wrapf (err , "failed to create helper for managing the externalLoadBalancer" )
107138 }
108139
109- // Set the LoadBalancerAvailableCondition reporting delete is started, and issue a patch in order to make
140+ // Set the LoadBalancerAvailableCondition reporting delete is started, and requeue in order to make
110141 // this visible to the users.
111- // NB. The operation in docker is fast, so there is the chance the user will not notice the status change;
112- // nevertheless we are issuing a patch so we can test a pattern that will be used by other providers as well
113- newPatchHelperFunc := r .NewPatchHelperFunc
114- if newPatchHelperFunc == nil {
115- newPatchHelperFunc = patch .NewHelper
116- }
117- patchHelper , err := newPatchHelperFunc (dockerCluster , r .Client )
118- if err != nil {
119- return ctrl.Result {}, err
120- }
121- conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , clusterv1 .DeletingReason , clusterv1 .ConditionSeverityInfo , "" )
122- patchDevClusterFunc := r .PatchDevClusterFunc
123- if patchDevClusterFunc == nil {
124- patchDevClusterFunc = r .PatchDevCluster
125- }
126- if err := patchDevClusterFunc (ctx , patchHelper , dockerCluster ); err != nil {
127- return ctrl.Result {}, errors .Wrap (err , "failed to patch DockerCluster" )
142+ if conditions .GetReason (dockerCluster , infrav1 .LoadBalancerAvailableCondition ) != clusterv1 .DeletingReason {
143+ conditions .MarkFalse (dockerCluster , infrav1 .LoadBalancerAvailableCondition , clusterv1 .DeletingReason , clusterv1 .ConditionSeverityInfo , "" )
144+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
145+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
146+ Status : metav1 .ConditionFalse ,
147+ Reason : infrav1 .DevClusterDockerLoadBalancerDeletingV1Beta2Reason ,
148+ })
149+ return ctrl.Result {RequeueAfter : 1 * time .Second }, nil
128150 }
129151
130152 // Delete the docker container hosting the load balancer
@@ -152,6 +174,24 @@ func (r *ClusterBackEndReconciler) PatchDevCluster(ctx context.Context, patchHel
152174 ),
153175 conditions .WithStepCounterIf (dockerCluster .ObjectMeta .DeletionTimestamp .IsZero ()),
154176 )
177+ if err := v1beta2conditions .SetSummaryCondition (dockerCluster , dockerCluster , infrav1 .DevClusterReadyV1Beta2Condition ,
178+ v1beta2conditions.ForConditionTypes {
179+ infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
180+ },
181+ // Using a custom merge strategy to override reasons applied during merge.
182+ v1beta2conditions.CustomMergeStrategy {
183+ MergeStrategy : v1beta2conditions .DefaultMergeStrategy (
184+ // Use custom reasons.
185+ v1beta2conditions .ComputeReasonFunc (v1beta2conditions .GetDefaultComputeMergeReasonFunc (
186+ infrav1 .DevClusterNotReadyV1Beta2Reason ,
187+ infrav1 .DevClusterReadyUnknownV1Beta2Reason ,
188+ infrav1 .DevClusterReadyV1Beta2Reason ,
189+ )),
190+ ),
191+ },
192+ ); err != nil {
193+ return errors .Wrapf (err , "failed to set %s condition" , infrav1 .DevClusterReadyV1Beta2Condition )
194+ }
155195
156196 // Patch the object, ignoring conflicts on the conditions owned by this controller.
157197 return patchHelper .Patch (
@@ -161,5 +201,9 @@ func (r *ClusterBackEndReconciler) PatchDevCluster(ctx context.Context, patchHel
161201 clusterv1 .ReadyCondition ,
162202 infrav1 .LoadBalancerAvailableCondition ,
163203 }},
204+ patch.WithOwnedV1Beta2Conditions {Conditions : []string {
205+ infrav1 .DevClusterReadyV1Beta2Condition ,
206+ infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
207+ }},
164208 )
165209}
0 commit comments