Skip to content

Commit

Permalink
Handle Server deletion
Browse files Browse the repository at this point in the history
- Add `Server` deletion flow in `Server` reconciler
- Fix wrong print columns
  • Loading branch information
afritzler committed Apr 17, 2024
1 parent a42669f commit 50f8ff2
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/v1alpha1/serverbootconfiguration_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions internal/controller/server_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down

0 comments on commit 50f8ff2

Please sign in to comment.