Skip to content

Commit 73f01c5

Browse files
committed
Set maintenance needed label on ServerClaim
1 parent d80c34c commit 73f01c5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

internal/controller/servermaintenance_controller.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,9 @@ func (r *ServerMaintenanceReconciler) handlePendingState(ctx context.Context, lo
136136
annotations := map[string]string{
137137
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
138138
}
139+
labels := map[string]string{
140+
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
141+
}
139142
if maintenance.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey] != "" {
140143
annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey] = maintenance.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey]
141144
}
@@ -144,6 +147,11 @@ func (r *ServerMaintenanceReconciler) handlePendingState(ctx context.Context, lo
144147
}
145148
log.V(1).Info("Patched ServerClaim annotations", "ServerClaim", client.ObjectKeyFromObject(serverClaim))
146149

150+
if err := r.patchServerClaimLabels(ctx, serverClaim, labels); err != nil {
151+
return ctrl.Result{}, fmt.Errorf("failed to patch server claim labels: %w", err)
152+
}
153+
log.V(1).Info("Patched ServerClaim labels", "ServerClaim", client.ObjectKeyFromObject(serverClaim))
154+
147155
if maintenance.Spec.Policy == metalv1alpha1.ServerMaintenancePolicyOwnerApproval {
148156
annotations := serverClaim.GetAnnotations()
149157
if _, ok := annotations[metalv1alpha1.ServerMaintenanceApprovalKey]; !ok {
@@ -349,6 +357,9 @@ func (r *ServerMaintenanceReconciler) cleanup(ctx context.Context, log logr.Logg
349357
metalv1alpha1.ServerMaintenanceNeededLabelKey,
350358
metalv1alpha1.ServerMaintenanceReasonAnnotationKey,
351359
})
360+
metautils.DeleteLabels(serverClaim, []string{
361+
metalv1alpha1.ServerMaintenanceNeededLabelKey,
362+
})
352363
if err := r.Patch(ctx, serverClaim, client.MergeFrom(serverClaimBase)); err != nil {
353364
return fmt.Errorf("failed to patch ServerClaim annotations: %w", err)
354365
}
@@ -403,6 +414,18 @@ func (r *ServerMaintenanceReconciler) patchServerClaimAnnotations(ctx context.Co
403414
return nil
404415
}
405416

417+
func (r *ServerMaintenanceReconciler) patchServerClaimLabels(ctx context.Context, claim *metalv1alpha1.ServerClaim, set map[string]string) error {
418+
if claim == nil {
419+
return fmt.Errorf("ServerClaim is nil")
420+
}
421+
claimBase := claim.DeepCopy()
422+
metautils.SetLabels(claim, set)
423+
if err := r.Patch(ctx, claim, client.MergeFrom(claimBase)); err != nil {
424+
return fmt.Errorf("failed to patch ServerClaim labels: %w", err)
425+
}
426+
return nil
427+
}
428+
406429
func (r *ServerMaintenanceReconciler) enqueueMaintenanceByServerRefs() handler.EventHandler {
407430
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
408431
log := ctrl.LoggerFrom(ctx)

internal/controller/servermaintenance_controller_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,10 @@ var _ = Describe("ServerMaintenance Controller", func() {
178178
HaveField("Status.State", metalv1alpha1.ServerMaintenanceStatePending),
179179
))
180180

181-
By("Ensuring that the ServerClaim has the maintenance needed annotation")
181+
By("Ensuring that the ServerClaim has the maintenance needed label and annotation")
182182
Eventually(Object(serverClaim)).Should(SatisfyAll(
183183
HaveField("ObjectMeta.Annotations", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceNeededLabelKey, "true")),
184+
HaveField("ObjectMeta.Labels", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceNeededLabelKey, "true")),
184185
))
185186

186187
By("Checking the Server is not in maintenance")
@@ -193,11 +194,14 @@ var _ = Describe("ServerMaintenance Controller", func() {
193194
metautils.SetAnnotation(serverClaim, metalv1alpha1.ServerMaintenanceApprovalKey, "true")
194195
})).Should(Succeed())
195196

196-
maintenanceLabels := map[string]string{
197+
maintenanceAnnotations := map[string]string{
197198
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
198199
metalv1alpha1.ServerMaintenanceReasonAnnotationKey: "test-maintenance",
199200
metalv1alpha1.ServerMaintenanceApprovalKey: "true",
200201
}
202+
maintenanceLabels := map[string]string{
203+
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
204+
}
201205
Eventually(Object(server)).Should(SatisfyAll(
202206
HaveField("Spec.ServerMaintenanceRef.Name", serverMaintenance.Name),
203207
HaveField("Spec.MaintenanceBootConfigurationRef", Not(BeNil())),
@@ -222,9 +226,10 @@ var _ = Describe("ServerMaintenance Controller", func() {
222226
HaveField("Status.State", metalv1alpha1.ServerStateMaintenance),
223227
))
224228

225-
By("Checking the ServerClaim has the maintenance labels")
229+
By("Checking the ServerClaim has the maintenance labels and annotations")
226230
Eventually(Object(serverClaim)).Should(SatisfyAll(
227-
HaveField("ObjectMeta.Annotations", maintenanceLabels),
231+
HaveField("ObjectMeta.Annotations", maintenanceAnnotations),
232+
HaveField("ObjectMeta.Labels", maintenanceLabels),
228233
))
229234

230235
By("Checking the ServerMaintenance is in maintenance")
@@ -245,6 +250,7 @@ var _ = Describe("ServerMaintenance Controller", func() {
245250
By("Checking the ServerClaim is cleaned up")
246251
Eventually(Object(serverClaim)).Should(SatisfyAll(
247252
HaveField("ObjectMeta.Annotations", Not(HaveKey(metalv1alpha1.ServerMaintenanceNeededLabelKey))),
253+
HaveField("ObjectMeta.Labels", Not(HaveKey(metalv1alpha1.ServerMaintenanceNeededLabelKey))),
248254
))
249255

250256
By("Deleting the ServerClaim")

0 commit comments

Comments
 (0)