Skip to content

Commit

Permalink
Set & Validate Version for etcdmachinesnapshot
Browse files Browse the repository at this point in the history
Signed-off-by: Vatsal Parekh <vatsalparekh@outlook.com>
  • Loading branch information
vatsalparekh committed Jan 23, 2025
1 parent 54b040b commit 3b79b3d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ spec:
type: string
machineName:
type: string
version:
type: string
required:
- clusterName
type: object
Expand Down
1 change: 1 addition & 0 deletions exp/etcdrestore/api/v1alpha1/etcdmachinesnapshot_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type ETCDMachineSnapshotSpec struct {
ClusterName string `json:"clusterName"`
MachineName string `json:"machineName,omitempty"`
Location string `json:"location,omitempty"`
Version string `json:"version,omitempty"`
}

// EtcdSnapshotRestoreStatus defines observed state of EtcdSnapshotRestore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ spec:
type: string
machineName:
type: string
version:
type: string
required:
- clusterName
type: object
Expand Down
45 changes: 37 additions & 8 deletions exp/etcdrestore/controllers/etcdmachinesnapshot_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
k3sv1 "github.com/rancher/turtles/api/rancher/k3s/v1"
snapshotrestorev1 "github.com/rancher/turtles/exp/etcdrestore/api/v1alpha1"
turtlesannotations "github.com/rancher/turtles/util/annotations"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
kerrors "k8s.io/apimachinery/pkg/util/errors"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/clustercache"
Expand Down Expand Up @@ -68,6 +70,9 @@ type snapshotScope struct {

// snapshot is the snapshot object which is used for reconcile
snapshot *snapshotrestorev1.ETCDMachineSnapshot

// controlPlaneVersion is lowest found kubernetes version among the Control plane machines
controlPlaneVersion *string
}

// SetupWithManager sets up the controller with the Manager.
Expand Down Expand Up @@ -144,22 +149,33 @@ func (r *ETCDMachineSnapshotReconciler) newScope(ctx context.Context, etcdMachin
}

controlPlaneMachines := machines.Filter(collections.ControlPlaneMachines(cluster.Name))
if len(controlPlaneMachines) == 0 {
return nil, fmt.Errorf("no control plane machines found for cluster: %s", cluster.Name)
}

targetMachineCandidates := controlPlaneMachines.Filter(func(machine *clusterv1.Machine) bool {
return machine.Name == etcdMachineSnapshot.Spec.MachineName
}).UnsortedList()

if len(targetMachineCandidates) < 1 {
return nil, fmt.Errorf(
"failed to found machine %s for cluster %s",
etcdMachineSnapshot.Spec.MachineName,
client.ObjectKeyFromObject(cluster).String())
return &snapshotScope{
snapshot: etcdMachineSnapshot,
controlPlaneVersion: controlPlaneMachines.LowestVersion(),
}, apierrors.NewNotFound(
schema.GroupResource{
Group: clusterv1.GroupVersion.Group,
Resource: "machines",
},
etcdMachineSnapshot.Spec.MachineName,
)
}

return &snapshotScope{
cluster: cluster,
machines: controlPlaneMachines,
machine: targetMachineCandidates[0],
snapshot: etcdMachineSnapshot,
cluster: cluster,
machines: controlPlaneMachines,
machine: targetMachineCandidates[0],
snapshot: etcdMachineSnapshot,
controlPlaneVersion: controlPlaneMachines.LowestVersion(),
}, nil
}

Expand All @@ -171,6 +187,15 @@ func (r *ETCDMachineSnapshotReconciler) reconcileNormal(
scope, err := r.newScope(ctx, etcdMachineSnapshot)
if err != nil {
log.Error(err, "Unable to intialize scope")
if apierrors.IsNotFound(err) {
log.Info("No valid control-plane machine found")
if etcdMachineSnapshot.Spec.Version != "" {
if etcdMachineSnapshot.Spec.Version != *scope.controlPlaneVersion {
log.Info("Cluster control-plane version changed & Node not found; marking snapshot for deletion")
return ctrl.Result{}, r.reconcileDelete(ctx, etcdMachineSnapshot)
}
}
}
return ctrl.Result{}, err
}

Expand All @@ -190,6 +215,10 @@ func (r *ETCDMachineSnapshotReconciler) reconcileNormal(
// Initial phase, set to Pending
etcdMachineSnapshot.Status.Phase = snapshotrestorev1.ETCDSnapshotPhasePending

// Set the Version field as k8s version of the cluster
fmt.Printf("\nsetting control-plane version as: %v\n", *scope.controlPlaneVersion)
etcdMachineSnapshot.Spec.Version = *scope.controlPlaneVersion

return ctrl.Result{}, nil
case snapshotrestorev1.ETCDSnapshotPhasePending, snapshotrestorev1.ETCDSnapshotPhasePlanning:
// Transition to Running
Expand Down

0 comments on commit 3b79b3d

Please sign in to comment.