From 2db36188afcffad49226a8b1af81f66493dc1ee9 Mon Sep 17 00:00:00 2001 From: Vatsal Parekh Date: Fri, 29 Nov 2024 16:16:32 +0530 Subject: [PATCH] Add Version field to ETCDSnapshotRestoreSpec Signed-off-by: Vatsal Parekh --- ...er-turtles-exp-etcdrestore-components.yaml | 2 ++ .../api/v1alpha1/etcdmachinesnapshot_types.go | 1 + ...s-capi.cattle.io_etcdmachinesnapshots.yaml | 2 ++ .../etcdmachinesnapshot_controller.go | 20 +++++++++++++++---- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/charts/rancher-turtles/templates/rancher-turtles-exp-etcdrestore-components.yaml b/charts/rancher-turtles/templates/rancher-turtles-exp-etcdrestore-components.yaml index b76d38a1..0d935c47 100644 --- a/charts/rancher-turtles/templates/rancher-turtles-exp-etcdrestore-components.yaml +++ b/charts/rancher-turtles/templates/rancher-turtles-exp-etcdrestore-components.yaml @@ -49,6 +49,8 @@ spec: type: string machineName: type: string + version: + type: string required: - clusterName type: object diff --git a/exp/etcdrestore/api/v1alpha1/etcdmachinesnapshot_types.go b/exp/etcdrestore/api/v1alpha1/etcdmachinesnapshot_types.go index 7ee4b80d..6093d3ee 100644 --- a/exp/etcdrestore/api/v1alpha1/etcdmachinesnapshot_types.go +++ b/exp/etcdrestore/api/v1alpha1/etcdmachinesnapshot_types.go @@ -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 diff --git a/exp/etcdrestore/config/crd/bases/turtles-capi.cattle.io_etcdmachinesnapshots.yaml b/exp/etcdrestore/config/crd/bases/turtles-capi.cattle.io_etcdmachinesnapshots.yaml index 357cf63f..d4151937 100644 --- a/exp/etcdrestore/config/crd/bases/turtles-capi.cattle.io_etcdmachinesnapshots.yaml +++ b/exp/etcdrestore/config/crd/bases/turtles-capi.cattle.io_etcdmachinesnapshots.yaml @@ -46,6 +46,8 @@ spec: type: string machineName: type: string + version: + type: string required: - clusterName type: object diff --git a/exp/etcdrestore/controllers/etcdmachinesnapshot_controller.go b/exp/etcdrestore/controllers/etcdmachinesnapshot_controller.go index 95fedb6d..e4df9605 100644 --- a/exp/etcdrestore/controllers/etcdmachinesnapshot_controller.go +++ b/exp/etcdrestore/controllers/etcdmachinesnapshot_controller.go @@ -68,6 +68,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. @@ -144,6 +147,10 @@ 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() @@ -156,10 +163,11 @@ func (r *ETCDMachineSnapshotReconciler) newScope(ctx context.Context, etcdMachin } return &snapshotScope{ - cluster: cluster, - machines: controlPlaneMachines, - machine: targetMachineCandidates[0], - snapshot: etcdMachineSnapshot, + cluster: cluster, + machines: controlPlaneMachines, + machine: targetMachineCandidates[0], + snapshot: etcdMachineSnapshot, + controlPlaneVersion: controlPlaneMachines.LowestVersion(), }, nil } @@ -190,6 +198,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