@@ -19,9 +19,12 @@ package docker
19
19
20
20
import (
21
21
"context"
22
+ "fmt"
22
23
"strconv"
24
+ "time"
23
25
24
26
"github.com/pkg/errors"
27
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
28
ctrl "sigs.k8s.io/controller-runtime"
26
29
"sigs.k8s.io/controller-runtime/pkg/client"
27
30
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -31,16 +34,14 @@ import (
31
34
infrav1 "sigs.k8s.io/cluster-api/test/infrastructure/docker/api/v1beta1"
32
35
"sigs.k8s.io/cluster-api/test/infrastructure/docker/internal/docker"
33
36
"sigs.k8s.io/cluster-api/util/conditions"
37
+ v1beta2conditions "sigs.k8s.io/cluster-api/util/conditions/v1beta2"
34
38
"sigs.k8s.io/cluster-api/util/patch"
35
39
)
36
40
37
41
// ClusterBackEndReconciler reconciles a DockerCluster object.
38
42
type ClusterBackEndReconciler struct {
39
43
client.Client
40
44
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
44
45
}
45
46
46
47
// ReconcileNormal handle docker backend for DevCluster not yet deleted.
@@ -61,19 +62,37 @@ func (r *ClusterBackEndReconciler) ReconcileNormal(ctx context.Context, cluster
61
62
strconv .Itoa (dockerCluster .Spec .ControlPlaneEndpoint .Port ))
62
63
if err != nil {
63
64
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
+ })
64
71
return ctrl.Result {}, errors .Wrapf (err , "failed to create helper for managing the externalLoadBalancer" )
65
72
}
66
73
67
74
// Create the docker container hosting the load balancer.
68
75
if err := externalLoadBalancer .Create (ctx ); err != nil {
69
76
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
+ })
70
83
return ctrl.Result {}, errors .Wrap (err , "failed to create load balancer" )
71
84
}
72
85
73
86
// Set APIEndpoints with the load balancer IP so the Cluster API Cluster Controller can pull it
74
87
lbIP , err := externalLoadBalancer .IP (ctx )
75
88
if err != nil {
76
89
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
+ })
77
96
return ctrl.Result {}, errors .Wrap (err , "failed to get ip for the load balancer" )
78
97
}
79
98
@@ -86,6 +105,11 @@ func (r *ClusterBackEndReconciler) ReconcileNormal(ctx context.Context, cluster
86
105
// Mark the dockerCluster ready
87
106
dockerCluster .Status .Ready = true
88
107
conditions .MarkTrue (dockerCluster , infrav1 .LoadBalancerAvailableCondition )
108
+ v1beta2conditions .Set (dockerCluster , metav1.Condition {
109
+ Type : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
110
+ Status : metav1 .ConditionTrue ,
111
+ Reason : infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Reason ,
112
+ })
89
113
90
114
return ctrl.Result {}, nil
91
115
}
@@ -103,28 +127,26 @@ func (r *ClusterBackEndReconciler) ReconcileDelete(ctx context.Context, cluster
103
127
strconv .Itoa (dockerCluster .Spec .ControlPlaneEndpoint .Port ))
104
128
if err != nil {
105
129
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
+
106
137
return ctrl.Result {}, errors .Wrapf (err , "failed to create helper for managing the externalLoadBalancer" )
107
138
}
108
139
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
110
141
// 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
128
150
}
129
151
130
152
// Delete the docker container hosting the load balancer
@@ -152,6 +174,24 @@ func (r *ClusterBackEndReconciler) PatchDevCluster(ctx context.Context, patchHel
152
174
),
153
175
conditions .WithStepCounterIf (dockerCluster .ObjectMeta .DeletionTimestamp .IsZero ()),
154
176
)
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
+ }
155
195
156
196
// Patch the object, ignoring conflicts on the conditions owned by this controller.
157
197
return patchHelper .Patch (
@@ -161,5 +201,9 @@ func (r *ClusterBackEndReconciler) PatchDevCluster(ctx context.Context, patchHel
161
201
clusterv1 .ReadyCondition ,
162
202
infrav1 .LoadBalancerAvailableCondition ,
163
203
}},
204
+ patch.WithOwnedV1Beta2Conditions {Conditions : []string {
205
+ infrav1 .DevClusterReadyV1Beta2Condition ,
206
+ infrav1 .DevClusterDockerLoadBalancerAvailableV1Beta2Condition ,
207
+ }},
164
208
)
165
209
}
0 commit comments