From 3d171fdddd4b2e58f7a6e4e4e608386cc6c7e453 Mon Sep 17 00:00:00 2001 From: Andreas Fritzler Date: Tue, 30 Apr 2024 13:32:52 +0200 Subject: [PATCH] Improve `BMCReconciler` status patching --- internal/controller/server_controller.go | 25 +++++++++++-------- internal/controller/server_controller_test.go | 3 ++- internal/controller/serverclaim_controller.go | 16 ++++++++++-- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index f5781f7..c45bafd 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -44,8 +44,10 @@ import ( ) const ( - DefaultIgnitionSecretKeyName = "ignition" - ServerFinalizer = "metal.ironcore.dev/server" + DefaultIgnitionSecretKeyName = "ignition" + ServerFinalizer = "metal.ironcore.dev/server" + InternalAnnotationTypeKeyName = "metal.ironcore.dev/type" + InternalAnnotationTypeValue = "Internal" ) const ( @@ -325,6 +327,9 @@ func (r *ServerReconciler) applyBootConfigurationAndIgnitionForDiscovery(ctx con ObjectMeta: metav1.ObjectMeta{ Name: server.Name, Namespace: r.ManagerNamespace, + Annotations: map[string]string{ + InternalAnnotationTypeKeyName: InternalAnnotationTypeValue, + }, }, Spec: metalv1alpha1.ServerBootConfigurationSpec{ ServerRef: v1.LocalObjectReference{ @@ -538,19 +543,17 @@ func (r *ServerReconciler) ensureInitialBootConfigurationIsDeleted(ctx context.C return nil } - if server.Spec.BootConfigurationRef.Namespace != r.ManagerNamespace && server.Spec.BootConfigurationRef.Name != server.Name { - // hit a non initial boot config - return nil + config := &metalv1alpha1.ServerBootConfiguration{} + if err := r.Get(ctx, client.ObjectKey{Namespace: server.Spec.BootConfigurationRef.Namespace, Name: server.Spec.BootConfigurationRef.Name}, config); err != nil { + return err } - config := &metalv1alpha1.ServerBootConfiguration{ - ObjectMeta: metav1.ObjectMeta{ - Namespace: server.Spec.BootConfigurationRef.Namespace, - Name: server.Spec.BootConfigurationRef.Name, - }, + if val, ok := config.Annotations[InternalAnnotationTypeKeyName]; !ok || val != InternalAnnotationTypeValue { + // hit a non-initial boot config + return nil } - if err := r.Delete(ctx, config); !apierrors.IsNotFound(err) { + if err := r.Delete(ctx, config); err != nil { return err } diff --git a/internal/controller/server_controller_test.go b/internal/controller/server_controller_test.go index f3a23c2..07ff397 100644 --- a/internal/controller/server_controller_test.go +++ b/internal/controller/server_controller_test.go @@ -33,7 +33,7 @@ import ( . "sigs.k8s.io/controller-runtime/pkg/envtest/komega" ) -var _ = FDescribe("Server Controller", func() { +var _ = Describe("Server Controller", func() { ns := SetupTest() var endpoint *metalv1alpha1.Endpoint @@ -75,6 +75,7 @@ var _ = FDescribe("Server Controller", func() { }, } Eventually(Object(bootConfig)).Should(SatisfyAll( + HaveField("Annotations", HaveKeyWithValue(InternalAnnotationTypeKeyName, InternalAnnotationTypeValue)), HaveField("Spec.ServerRef", v1.LocalObjectReference{Name: server.Name}), HaveField("Spec.Image", "fooOS:latest"), HaveField("Spec.IgnitionSecretRef", &v1.LocalObjectReference{Name: server.Name}), diff --git a/internal/controller/serverclaim_controller.go b/internal/controller/serverclaim_controller.go index f2478d3..1c9b128 100644 --- a/internal/controller/serverclaim_controller.go +++ b/internal/controller/serverclaim_controller.go @@ -157,7 +157,7 @@ func (r *ServerClaimReconciler) reconcile(ctx context.Context, log logr.Logger, return ctrl.Result{}, err } - if err := r.applyBootConfiguration(ctx, claim); err != nil { + if err := r.applyBootConfiguration(ctx, server, claim); err != nil { return ctrl.Result{}, fmt.Errorf("failed to apply boot configuration: %w", err) } @@ -177,7 +177,7 @@ func (r *ServerClaimReconciler) patchServerClaimPhase(ctx context.Context, claim return true, nil } -func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, claim *metalv1alpha1.ServerClaim) error { +func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, server *metalv1alpha1.Server, claim *metalv1alpha1.ServerClaim) error { config := &metalv1alpha1.ServerBootConfiguration{ TypeMeta: metav1.TypeMeta{ APIVersion: "metal.ironcore.dev/v1alpha1", @@ -203,6 +203,18 @@ func (r *ServerClaimReconciler) applyBootConfiguration(ctx context.Context, clai return fmt.Errorf("failed to apply boot configuration: %w", err) } + serverBase := server.DeepCopy() + server.Spec.BootConfigurationRef = &v1.ObjectReference{ + Namespace: config.Namespace, + Name: config.Name, + UID: config.UID, + APIVersion: "metal.ironcore.dev/v1alpha1", + Kind: "ServerBootConfiguration", + } + if err := r.Patch(ctx, server, client.MergeFrom(serverBase)); err != nil { + return err + } + return nil }