@@ -39,8 +39,8 @@ import (
39
39
snapshotrestorev1 "github.com/rancher/turtles/exp/etcdrestore/api/v1alpha1"
40
40
)
41
41
42
- // InitMachine is a filter matching on init machine of the ETCD snapshot
43
- func InitMachine (etcdMachineSnapshot * snapshotrestorev1.ETCDMachineSnapshot ) collections.Func {
42
+ // initMachine is a filter matching on init machine of the ETCD snapshot
43
+ func initMachine (etcdMachineSnapshot * snapshotrestorev1.ETCDMachineSnapshot ) collections.Func {
44
44
return func (machine * clusterv1.Machine ) bool {
45
45
return machine .Name == etcdMachineSnapshot .Spec .MachineName
46
46
}
@@ -104,6 +104,7 @@ type scope struct {
104
104
//+kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters/status,verbs=get;list;watch;create;update;patch;delete
105
105
//+kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machines,verbs=get;list;watch;create;update;patch;delete
106
106
//+kubebuilder:rbac:groups="",resources=secrets;events;configmaps;serviceaccounts,verbs=get;list;watch;create;update;patch;delete
107
+ //+kubebuilder:rbac:groups="",resources=serviceaccounts/token,verbs=create
107
108
//+kubebuilder:rbac:groups="rbac.authorization.k8s.io",resources=roles;rolebindings,verbs=get;list;watch;create;update;patch;delete
108
109
//+kubebuilder:rbac:groups="management.cattle.io",resources=*,verbs=get;list;watch;create;update;patch;delete
109
110
//+kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=rke2configs;rke2configs/status;rke2configs/finalizers,verbs=get;list;watch;create;update;patch;delete
@@ -159,7 +160,7 @@ func (r *ETCDSnapshotRestoreReconciler) reconcileNormal(ctx context.Context, etc
159
160
return ctrl.Result {RequeueAfter : 30 * time .Second }, nil
160
161
}
161
162
162
- if scope .machines .Filter (InitMachine (scope .etcdMachineSnapshot )).Len () != 1 {
163
+ if scope .machines .Filter (initMachine (scope .etcdMachineSnapshot )).Len () != 1 {
163
164
return ctrl.Result {}, fmt .Errorf (
164
165
"init machine %s for snapshot %s is not found" ,
165
166
scope .etcdMachineSnapshot .Spec .MachineName ,
@@ -191,15 +192,13 @@ func (r *ETCDSnapshotRestoreReconciler) reconcileNormal(ctx context.Context, etc
191
192
192
193
return ctrl.Result {}, nil
193
194
case snapshotrestorev1 .ETCDSnapshotRestorePhaseStarted :
194
- etcdSnapshotRestore .Status .Phase = snapshotrestorev1 .ETCDSnapshotRestorePhaseShutdown
195
-
196
- return ctrl.Result {}, nil
195
+ return r .preparePlanPermissions (ctx , scope , etcdSnapshotRestore )
197
196
case snapshotrestorev1 .ETCDSnapshotRestorePhaseShutdown :
198
197
// Stop RKE2 on all the machines.
199
198
return r .stopRKE2OnAllMachines (ctx , scope , etcdSnapshotRestore )
200
199
case snapshotrestorev1 .ETCDSnapshotRestorePhaseRunning :
201
200
// Restore the etcd snapshot on the init machine.
202
- return r .restoreSnaphotOnInitMachine (ctx , scope , etcdSnapshotRestore )
201
+ return r .restoreSnapshotOnInitMachine (ctx , scope , etcdSnapshotRestore )
203
202
case snapshotrestorev1 .ETCDSnapshotRestorePhaseAgentRestart :
204
203
// Start RKE2 on all the machines.
205
204
return r .startRKE2OnAllMachines (ctx , scope , etcdSnapshotRestore )
@@ -212,7 +211,7 @@ func (r *ETCDSnapshotRestoreReconciler) reconcileNormal(ctx context.Context, etc
212
211
case snapshotrestorev1 .ETCDSnapshotRestorePhaseJoinAgents :
213
212
return r .waitForMachinesToJoin (ctx , scope , etcdSnapshotRestore )
214
213
case snapshotrestorev1 .ETCDSnapshotRestorePhaseFinished , snapshotrestorev1 .ETCDSnapshotRestorePhaseFailed :
215
- return ctrl. Result {}, nil
214
+ return r . revokePlanPermissions ( ctx , scope , etcdSnapshotRestore )
216
215
}
217
216
218
217
return ctrl.Result {}, nil
@@ -251,6 +250,24 @@ func initScope(ctx context.Context, c client.Client, etcdSnapshotRestore *snapsh
251
250
}, nil
252
251
}
253
252
253
+ func (r * ETCDSnapshotRestoreReconciler ) preparePlanPermissions (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
254
+ if err := Plan (ctx , r .Client , "restore" + etcdSnapshotRestore .Name , scope .machines .Newest (), scope .machines ).Permit (ctx ); err != nil {
255
+ return ctrl.Result {}, err
256
+ }
257
+
258
+ etcdSnapshotRestore .Status .Phase = snapshotrestorev1 .ETCDSnapshotRestorePhaseShutdown
259
+
260
+ return ctrl.Result {}, nil
261
+ }
262
+
263
+ func (r * ETCDSnapshotRestoreReconciler ) revokePlanPermissions (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
264
+ if err := Plan (ctx , r .Client , "restore" + etcdSnapshotRestore .Name , scope .machines .Newest (), scope .machines ).Revoke (ctx ); err != nil {
265
+ return ctrl.Result {}, err
266
+ }
267
+
268
+ return ctrl.Result {}, nil
269
+ }
270
+
254
271
func (r * ETCDSnapshotRestoreReconciler ) stopRKE2OnAllMachines (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
255
272
log := log .FromContext (ctx )
256
273
@@ -259,7 +276,7 @@ func (r *ETCDSnapshotRestoreReconciler) stopRKE2OnAllMachines(ctx context.Contex
259
276
log .Info ("Stopping RKE2 on machine" , "machine" , machine .Name )
260
277
261
278
// Get the plan secret for the machine.
262
- applied , err := Plan (ctx , r .Client , machine ).Apply (ctx , RKE2KillAll ())
279
+ applied , err := Plan (ctx , r .Client , "restore" + etcdSnapshotRestore . Name , machine , scope . machines ).Apply (ctx , RKE2KillAll ())
263
280
if err != nil {
264
281
return ctrl.Result {}, fmt .Errorf ("failed to get plan secret for machine: %w" , err )
265
282
}
@@ -286,15 +303,15 @@ func (r *ETCDSnapshotRestoreReconciler) stopRKE2OnAllMachines(ctx context.Contex
286
303
return ctrl.Result {}, nil
287
304
}
288
305
289
- func (r * ETCDSnapshotRestoreReconciler ) restoreSnaphotOnInitMachine (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
306
+ func (r * ETCDSnapshotRestoreReconciler ) restoreSnapshotOnInitMachine (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
290
307
log := log .FromContext (ctx )
291
308
292
- initMachine := scope .machines .Filter (InitMachine (scope .etcdMachineSnapshot )).UnsortedList ()[0 ]
309
+ initMachine := scope .machines .Filter (initMachine (scope .etcdMachineSnapshot )).UnsortedList ()[0 ]
293
310
294
311
log .Info ("Filling plan secret with etcd restore instructions" , "machine" , initMachine .Name )
295
312
296
313
// Get the plan secret for the machine.
297
- applied , err := Plan (ctx , r .Client , initMachine ).Apply (
314
+ applied , err := Plan (ctx , r .Client , "restore" + etcdSnapshotRestore . Name , initMachine , scope . machines ).Apply (
298
315
ctx ,
299
316
RemoveServerURL (),
300
317
ManifestRemoval (),
@@ -318,7 +335,7 @@ func (r *ETCDSnapshotRestoreReconciler) restoreSnaphotOnInitMachine(ctx context.
318
335
func (r * ETCDSnapshotRestoreReconciler ) startRKE2OnAllMachines (ctx context.Context , scope * scope , etcdSnapshotRestore * snapshotrestorev1.ETCDSnapshotRestore ) (ctrl.Result , error ) {
319
336
log := log .FromContext (ctx )
320
337
321
- initMachine := scope .machines .Filter (InitMachine (scope .etcdMachineSnapshot )).UnsortedList ()[0 ]
338
+ initMachine := scope .machines .Filter (initMachine (scope .etcdMachineSnapshot )).UnsortedList ()[0 ]
322
339
323
340
// TODO: other registration methods
324
341
initMachineIP := getInternalMachineIP (initMachine )
@@ -350,7 +367,7 @@ func (r *ETCDSnapshotRestoreReconciler) startRKE2OnAllMachines(ctx context.Conte
350
367
StartRKE2 ())
351
368
}
352
369
353
- applied , err := Plan (ctx , r .Client , machine ).Apply (ctx , instructions ... )
370
+ applied , err := Plan (ctx , r .Client , "restore" + etcdSnapshotRestore . Name , machine , scope . machines ).Apply (ctx , instructions ... )
354
371
if err != nil {
355
372
return ctrl.Result {}, fmt .Errorf ("failed to patch plan secret: %w" , err )
356
373
} else if ! applied .Finished {
0 commit comments