Skip to content

Commit

Permalink
allow wipe and reboot with talos client on removing server
Browse files Browse the repository at this point in the history
Ref: siderolabs#1294 siderolabs#1285
all deleted machine will receive a reset call from talos client.

Signed-off-by: Anthony ARNAUD <github@anthony-arnaud.fr>
  • Loading branch information
aarnaud committed Feb 8, 2024
1 parent ea8e126 commit 3106e07
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions app/caps-controller-manager/controllers/metalmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/go-logr/logr"
"github.com/siderolabs/go-pointer"
talosclient "github.com/siderolabs/talos/pkg/machinery/client"
clientconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/fields"
Expand Down Expand Up @@ -253,6 +255,9 @@ func (r *MetalMachineReconciler) reconcileDelete(ctx context.Context, metalMachi

err := r.Get(ctx, types.NamespacedName{Namespace: metalMachine.Spec.ServerRef.Namespace, Name: metalMachine.Spec.ServerRef.Name}, &serverBinding)
if err == nil {
if err = r.ResetServer(ctx, metalMachine, &serverBinding); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{Requeue: true}, r.Delete(ctx, &serverBinding)
}

Expand Down Expand Up @@ -467,3 +472,42 @@ func (r *MetalMachineReconciler) fetchServerClass(ctx context.Context, classRef

return serverClassResource, nil
}

func (r *MetalMachineReconciler) ResetServer(ctx context.Context, metalMachine *infrav1.MetalMachine, serverBinding *infrav1.ServerBinding) error {
var talosSecret corev1.Secret

cluster, err := util.GetClusterFromMetadata(ctx, r.Client, metalMachine.ObjectMeta)
if err != nil {
return fmt.Errorf("no cluster label or cluster does not exist")
}

if err = r.Get(ctx, types.NamespacedName{Namespace: cluster.Namespace, Name: fmt.Sprintf("%s-talosconfig", cluster.Name)}, &talosSecret); err != nil {
return err
}

config, ok := talosSecret.Data["talosconfig"]
if !ok {
return fmt.Errorf("failed to find talosconfig data in the talosconfig secret")
}

var clientConfig *clientconfig.Config
clientConfig, err = clientconfig.FromBytes(config)

if err != nil {
return err
}

var talosClient *talosclient.Client
talosClient, err = talosclient.New(ctx,
talosclient.WithConfig(clientConfig),
talosclient.WithEndpoints(serverBinding.Spec.Addresses...),
)
if err != nil {
return err
}

// ignore error if the machine is already reset, reboot, offline
_ = talosClient.Reset(ctx, false, true)

return nil
}

0 comments on commit 3106e07

Please sign in to comment.