Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions internal/controller/servermaintenance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import (
const (
// ServerMaintenanceFinalizer is the finalizer for the ServerMaintenance resource.
ServerMaintenanceFinalizer = "metal.ironcore.dev/servermaintenance"

// trueValue represents the string value "true" used for labels and annotations
trueValue = "true"
)

// ServerMaintenanceReconciler reconciles a ServerMaintenance object
Expand Down Expand Up @@ -133,17 +136,22 @@ func (r *ServerMaintenanceReconciler) handlePendingState(ctx context.Context, lo
log.V(1).Info("ServerClaim gone")
return ctrl.Result{}, nil
}
annotations := map[string]string{
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
patch := client.MergeFrom(serverClaim.DeepCopy())
if serverClaim.Labels == nil {
serverClaim.Labels = make(map[string]string)
}
serverClaim.Labels[metalv1alpha1.ServerMaintenanceNeededLabelKey] = trueValue
if serverClaim.Annotations == nil {
serverClaim.Annotations = make(map[string]string)
}
serverClaim.Annotations[metalv1alpha1.ServerMaintenanceNeededLabelKey] = trueValue
if maintenance.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey] != "" {
annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey] = maintenance.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey]
serverClaim.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey] = maintenance.Annotations[metalv1alpha1.ServerMaintenanceReasonAnnotationKey]
}
if err := r.patchServerClaimAnnotations(ctx, serverClaim, annotations); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to patch server claim annotations: %w", err)
if err := r.Patch(ctx, serverClaim, patch); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to patch ServerClaim: %w", err)
}
log.V(1).Info("Patched ServerClaim annotations", "ServerClaim", client.ObjectKeyFromObject(serverClaim))

log.V(1).Info("Patched ServerClaim labels and annotations", "ServerClaim", client.ObjectKeyFromObject(serverClaim))
if maintenance.Spec.Policy == metalv1alpha1.ServerMaintenancePolicyOwnerApproval {
annotations := serverClaim.GetAnnotations()
if _, ok := annotations[metalv1alpha1.ServerMaintenanceApprovalKey]; !ok {
Expand Down Expand Up @@ -349,6 +357,9 @@ func (r *ServerMaintenanceReconciler) cleanup(ctx context.Context, log logr.Logg
metalv1alpha1.ServerMaintenanceNeededLabelKey,
metalv1alpha1.ServerMaintenanceReasonAnnotationKey,
})
metautils.DeleteLabels(serverClaim, []string{
metalv1alpha1.ServerMaintenanceNeededLabelKey,
})
if err := r.Patch(ctx, serverClaim, client.MergeFrom(serverClaimBase)); err != nil {
return fmt.Errorf("failed to patch ServerClaim annotations: %w", err)
}
Expand Down Expand Up @@ -391,18 +402,6 @@ func (r *ServerMaintenanceReconciler) patchMaintenanceState(ctx context.Context,
return true, nil
}

func (r *ServerMaintenanceReconciler) patchServerClaimAnnotations(ctx context.Context, claim *metalv1alpha1.ServerClaim, set map[string]string) error {
if claim == nil {
return fmt.Errorf("ServerClaim is nil")
}
claimBase := claim.DeepCopy()
metautils.SetAnnotations(claim, set)
if err := r.Patch(ctx, claim, client.MergeFrom(claimBase)); err != nil {
return fmt.Errorf("failed to patch ServerClaim annotations: %w", err)
}
return nil
}

func (r *ServerMaintenanceReconciler) enqueueMaintenanceByServerRefs() handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
log := ctrl.LoggerFrom(ctx)
Expand Down
22 changes: 14 additions & 8 deletions internal/controller/servermaintenance_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,10 @@ var _ = Describe("ServerMaintenance Controller", func() {
HaveField("Status.State", metalv1alpha1.ServerMaintenanceStatePending),
))

By("Ensuring that the ServerClaim has the maintenance needed annotation")
By("Ensuring that the ServerClaim has the maintenance needed label and annotation")
Eventually(Object(serverClaim)).Should(SatisfyAll(
HaveField("ObjectMeta.Annotations", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceNeededLabelKey, "true")),
HaveField("ObjectMeta.Annotations", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceNeededLabelKey, trueValue)),
HaveField("ObjectMeta.Labels", HaveKeyWithValue(metalv1alpha1.ServerMaintenanceNeededLabelKey, trueValue)),
))

By("Checking the Server is not in maintenance")
Expand All @@ -190,13 +191,16 @@ var _ = Describe("ServerMaintenance Controller", func() {

By("Approving the maintenance")
Eventually(Update(serverClaim, func() {
metautils.SetAnnotation(serverClaim, metalv1alpha1.ServerMaintenanceApprovalKey, "true")
metautils.SetAnnotation(serverClaim, metalv1alpha1.ServerMaintenanceApprovalKey, trueValue)
})).Should(Succeed())

maintenanceLabels := map[string]string{
metalv1alpha1.ServerMaintenanceNeededLabelKey: "true",
maintenanceAnnotations := map[string]string{
metalv1alpha1.ServerMaintenanceNeededLabelKey: trueValue,
metalv1alpha1.ServerMaintenanceReasonAnnotationKey: "test-maintenance",
metalv1alpha1.ServerMaintenanceApprovalKey: "true",
metalv1alpha1.ServerMaintenanceApprovalKey: trueValue,
}
maintenanceLabels := map[string]string{
metalv1alpha1.ServerMaintenanceNeededLabelKey: trueValue,
}
Eventually(Object(server)).Should(SatisfyAll(
HaveField("Spec.ServerMaintenanceRef.Name", serverMaintenance.Name),
Expand All @@ -222,9 +226,10 @@ var _ = Describe("ServerMaintenance Controller", func() {
HaveField("Status.State", metalv1alpha1.ServerStateMaintenance),
))

By("Checking the ServerClaim has the maintenance labels")
By("Checking the ServerClaim has the maintenance labels and annotations")
Eventually(Object(serverClaim)).Should(SatisfyAll(
HaveField("ObjectMeta.Annotations", maintenanceLabels),
HaveField("ObjectMeta.Annotations", maintenanceAnnotations),
HaveField("ObjectMeta.Labels", maintenanceLabels),
))

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

By("Deleting the ServerClaim")
Expand Down
Loading