From 50f8ff260998fb9943092ad03aa95aeb10ccb65b Mon Sep 17 00:00:00 2001 From: Andreas Fritzler Date: Wed, 17 Apr 2024 16:33:24 +0200 Subject: [PATCH] Handle `Server` deletion - Add `Server` deletion flow in `Server` reconciler - Fix wrong print columns --- api/v1alpha1/serverbootconfiguration_types.go | 4 ++-- ...ironcore.dev_serverbootconfigurations.yaml | 4 ++-- internal/controller/server_controller.go | 21 +++++++++++++++++++ 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/api/v1alpha1/serverbootconfiguration_types.go b/api/v1alpha1/serverbootconfiguration_types.go index 855a605..78a15a9 100644 --- a/api/v1alpha1/serverbootconfiguration_types.go +++ b/api/v1alpha1/serverbootconfiguration_types.go @@ -43,9 +43,9 @@ type ServerBootConfigurationStatus struct { //+kubebuilder:object:root=true //+kubebuilder:subresource:status -//+kubebuilder:printcolumn:name="ServerRef",type=string,JSONPath=`.spec.serverRef` +//+kubebuilder:printcolumn:name="ServerRef",type=string,JSONPath=`.spec.serverRef.name` //+kubebuilder:printcolumn:name="Image",type=string,JSONPath=`.spec.image` -//+kubebuilder:printcolumn:name="IgnitionRef",type=string,JSONPath=`.spec.ingitionRef` +//+kubebuilder:printcolumn:name="IgnitionRef",type=string,JSONPath=`.spec.ignitionSecretRef.name` //+kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state` //+kubebuilder:printcolumn:name="Age",type=date,JSONPath=`.metadata.creationTimestamp` diff --git a/config/crd/bases/metal.ironcore.dev_serverbootconfigurations.yaml b/config/crd/bases/metal.ironcore.dev_serverbootconfigurations.yaml index 16cbeb4..584af6d 100644 --- a/config/crd/bases/metal.ironcore.dev_serverbootconfigurations.yaml +++ b/config/crd/bases/metal.ironcore.dev_serverbootconfigurations.yaml @@ -15,13 +15,13 @@ spec: scope: Namespaced versions: - additionalPrinterColumns: - - jsonPath: .spec.serverRef + - jsonPath: .spec.serverRef.name name: ServerRef type: string - jsonPath: .spec.image name: Image type: string - - jsonPath: .spec.ingitionRef + - jsonPath: .spec.ignitionSecretRef.name name: IgnitionRef type: string - jsonPath: .status.state diff --git a/internal/controller/server_controller.go b/internal/controller/server_controller.go index 1330a86..fb9b534 100644 --- a/internal/controller/server_controller.go +++ b/internal/controller/server_controller.go @@ -22,6 +22,8 @@ import ( "fmt" "net/http" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "github.com/afritzler/metal-operator/internal/api/registry" metalv1alpha1 "github.com/afritzler/metal-operator/api/v1alpha1" @@ -88,6 +90,25 @@ func (r *ServerReconciler) reconcileExists(ctx context.Context, log logr.Logger, } func (r *ServerReconciler) delete(ctx context.Context, log logr.Logger, server *metalv1alpha1.Server) (ctrl.Result, error) { + log.V(1).Info("Deleting server") + + if server.Spec.BootConfigurationRef != nil { + if err := r.Delete(ctx, &metalv1alpha1.ServerBootConfiguration{ + ObjectMeta: metav1.ObjectMeta{ + Namespace: server.Spec.BootConfigurationRef.Namespace, + Name: server.Spec.BootConfigurationRef.Name, + }}); err != nil && !apierrors.IsNotFound(err) { + return ctrl.Result{}, fmt.Errorf("failed to delete server bootconfiguration: %w", err) + } + log.V(1).Info("Deleted server bootconfiguration") + } + + if modified, err := clientutils.PatchEnsureNoFinalizer(ctx, r.Client, server, ServerFinalizer); !apierrors.IsNotFound(err) || modified { + return ctrl.Result{}, err + } + log.V(1).Info("Ensured that the finalizer has been removed") + + log.V(1).Info("Deleted server") return ctrl.Result{}, nil }