-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
mover.go
1204 lines (999 loc) · 41.8 KB
/
mover.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2020 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cluster
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/apimachinery/pkg/util/version"
"sigs.k8s.io/controller-runtime/pkg/client"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log"
"sigs.k8s.io/cluster-api/internal/controllers/topology/cluster/structuredmerge"
"sigs.k8s.io/cluster-api/util/annotations"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/yaml"
)
// ObjectMover defines methods for moving Cluster API objects to another management cluster.
type ObjectMover interface {
// Move moves all the Cluster API objects existing in a namespace (or from all the namespaces if empty) to a target management cluster.
Move(namespace string, toCluster Client, dryRun bool) error
// Backup saves all the Cluster API objects existing in a namespace (or from all the namespaces if empty) to a target management cluster.
Backup(namespace string, directory string) error
// Restore restores all the Cluster API objects existing in a configured directory to a target management cluster.
Restore(toCluster Client, directory string) error
}
// objectMover implements the ObjectMover interface.
type objectMover struct {
fromProxy Proxy
fromProviderInventory InventoryClient
dryRun bool
}
// ensure objectMover implements the ObjectMover interface.
var _ ObjectMover = &objectMover{}
func (o *objectMover) Move(namespace string, toCluster Client, dryRun bool) error {
log := logf.Log
log.Info("Performing move...")
o.dryRun = dryRun
if o.dryRun {
log.Info("********************************************************")
log.Info("This is a dry-run move, will not perform any real action")
log.Info("********************************************************")
}
// checks that all the required providers in place in the target cluster.
if !o.dryRun {
if err := o.checkTargetProviders(toCluster.ProviderInventory()); err != nil {
return errors.Wrap(err, "failed to check providers in target cluster")
}
}
objectGraph, err := o.getObjectGraph(namespace)
if err != nil {
return errors.Wrap(err, "failed to get object graph")
}
// Move the objects to the target cluster.
var proxy Proxy
if !o.dryRun {
proxy = toCluster.Proxy()
}
return o.move(objectGraph, proxy)
}
func (o *objectMover) Backup(namespace string, directory string) error {
log := logf.Log
log.Info("Performing backup...")
objectGraph, err := o.getObjectGraph(namespace)
if err != nil {
return errors.Wrap(err, "failed to get object graph")
}
return o.backup(objectGraph, directory)
}
func (o *objectMover) Restore(toCluster Client, directory string) error {
log := logf.Log
log.Info("Performing restore...")
// Build an empty object graph used for the restore sequence not tied to a specific namespace
objectGraph := newObjectGraph(o.fromProxy, o.fromProviderInventory)
// Gets all the types defined by the CRDs installed by clusterctl plus the ConfigMap/Secret core types.
err := objectGraph.getDiscoveryTypes()
if err != nil {
return errors.Wrap(err, "failed to retrieve discovery types")
}
objs, err := o.filesToObjs(directory)
if err != nil {
return errors.Wrap(err, "failed to process object files")
}
for i := range objs {
if err = objectGraph.addRestoredObj(&objs[i]); err != nil {
return err
}
}
// Completes rebuilding the graph from file by searching for soft ownership relations such as secrets linked to the cluster
// by a naming convention (without any explicit OwnerReference).
objectGraph.setSoftOwnership()
// Completes the graph by setting for each node the list of tenants the node belongs to.
objectGraph.setTenants()
// Check whether nodes are not included in GVK considered for restore.
objectGraph.checkVirtualNode()
// Restore the objects to the target cluster.
proxy := toCluster.Proxy()
return o.restore(objectGraph, proxy)
}
func (o *objectMover) filesToObjs(dir string) ([]unstructured.Unstructured, error) {
log := logf.Log
log.Info(fmt.Sprintf("Restoring files from %s", dir))
files, err := os.ReadDir(dir)
if err != nil {
return nil, err
}
rawYAMLs := make([][]byte, 0)
for i := range files {
path := filepath.Clean(filepath.Join(dir, files[i].Name()))
byObj, err := os.ReadFile(path)
if err != nil {
return nil, err
}
rawYAMLs = append(rawYAMLs, byObj)
}
processedYAMLs := yaml.JoinYaml(rawYAMLs...)
objs, err := yaml.ToUnstructured(processedYAMLs)
if err != nil {
return nil, err
}
return objs, nil
}
func (o *objectMover) getObjectGraph(namespace string) (*objectGraph, error) {
objectGraph := newObjectGraph(o.fromProxy, o.fromProviderInventory)
// Gets all the types defined by the CRDs installed by clusterctl plus the ConfigMap/Secret core types.
err := objectGraph.getDiscoveryTypes()
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve discovery types")
}
// Discovery the object graph for the selected types:
// - Nodes are defined the Kubernetes objects (Clusters, Machines etc.) identified during the discovery process.
// - Edges are derived by the OwnerReferences between nodes.
if err := objectGraph.Discovery(namespace); err != nil {
return nil, errors.Wrap(err, "failed to discover the object graph")
}
// Checks if Cluster API has already completed the provisioning of the infrastructure for the objects involved in the move/backup operation.
// This is required because if the infrastructure is provisioned, then we can reasonably assume that the objects we are moving/backing up are
// not currently waiting for long-running reconciliation loops, and so we can safely rely on the pause field on the Cluster object
// for blocking any further object reconciliation on the source objects.
if err := o.checkProvisioningCompleted(objectGraph); err != nil {
return nil, errors.Wrap(err, "failed to check for provisioned infrastructure")
}
// Check whether nodes are not included in GVK considered for move
objectGraph.checkVirtualNode()
return objectGraph, nil
}
func newObjectMover(fromProxy Proxy, fromProviderInventory InventoryClient) *objectMover {
return &objectMover{
fromProxy: fromProxy,
fromProviderInventory: fromProviderInventory,
}
}
// checkProvisioningCompleted checks if Cluster API has already completed the provisioning of the infrastructure for the objects involved in the move operation.
func (o *objectMover) checkProvisioningCompleted(graph *objectGraph) error {
if o.dryRun {
return nil
}
errList := []error{}
// Checking all the clusters have infrastructure is ready
readClusterBackoff := newReadBackoff()
clusters := graph.getClusters()
for i := range clusters {
cluster := clusters[i]
clusterObj := &clusterv1.Cluster{}
if err := retryWithExponentialBackoff(readClusterBackoff, func() error {
return getClusterObj(o.fromProxy, cluster, clusterObj)
}); err != nil {
return err
}
if !clusterObj.Status.InfrastructureReady {
errList = append(errList, errors.Errorf("cannot start the move operation while %q %s/%s is still provisioning the infrastructure", clusterObj.GroupVersionKind(), clusterObj.GetNamespace(), clusterObj.GetName()))
continue
}
// Note: can't use IsFalse here because we need to handle the absence of the condition as well as false.
if !conditions.IsTrue(clusterObj, clusterv1.ControlPlaneInitializedCondition) {
errList = append(errList, errors.Errorf("cannot start the move operation while the control plane for %q %s/%s is not yet initialized", clusterObj.GroupVersionKind(), clusterObj.GetNamespace(), clusterObj.GetName()))
continue
}
if clusterObj.Spec.ControlPlaneRef != nil && !clusterObj.Status.ControlPlaneReady {
errList = append(errList, errors.Errorf("cannot start the move operation while the control plane for %q %s/%s is not yet ready", clusterObj.GroupVersionKind(), clusterObj.GetNamespace(), clusterObj.GetName()))
continue
}
}
// Checking all the machine have a NodeRef
// Nb. NodeRef is considered a better signal than InfrastructureReady, because it ensures the node in the workload cluster is up and running.
readMachinesBackoff := newReadBackoff()
machines := graph.getMachines()
for i := range machines {
machine := machines[i]
machineObj := &clusterv1.Machine{}
if err := retryWithExponentialBackoff(readMachinesBackoff, func() error {
return getMachineObj(o.fromProxy, machine, machineObj)
}); err != nil {
return err
}
if machineObj.Status.NodeRef == nil {
errList = append(errList, errors.Errorf("cannot start the move operation while %q %s/%s is still provisioning the node", machineObj.GroupVersionKind(), machineObj.GetNamespace(), machineObj.GetName()))
}
}
return kerrors.NewAggregate(errList)
}
// getClusterObj retrieves the the clusterObj corresponding to a node with type Cluster.
func getClusterObj(proxy Proxy, cluster *node, clusterObj *clusterv1.Cluster) error {
c, err := proxy.NewClient()
if err != nil {
return err
}
clusterObjKey := client.ObjectKey{
Namespace: cluster.identity.Namespace,
Name: cluster.identity.Name,
}
if err := c.Get(ctx, clusterObjKey, clusterObj); err != nil {
return errors.Wrapf(err, "error reading Cluster %s/%s",
clusterObj.GetNamespace(), clusterObj.GetName())
}
return nil
}
// getMachineObj retrieves the the machineObj corresponding to a node with type Machine.
func getMachineObj(proxy Proxy, machine *node, machineObj *clusterv1.Machine) error {
c, err := proxy.NewClient()
if err != nil {
return err
}
machineObjKey := client.ObjectKey{
Namespace: machine.identity.Namespace,
Name: machine.identity.Name,
}
if err := c.Get(ctx, machineObjKey, machineObj); err != nil {
return errors.Wrapf(err, "error reading Machine %s/%s",
machineObj.GetNamespace(), machineObj.GetName())
}
return nil
}
// Move moves all the Cluster API objects existing in a namespace (or from all the namespaces if empty) to a target management cluster.
func (o *objectMover) move(graph *objectGraph, toProxy Proxy) error {
log := logf.Log
clusters := graph.getClusters()
log.Info("Moving Cluster API objects", "Clusters", len(clusters))
clusterClasses := graph.getClusterClasses()
log.Info("Moving Cluster API objects", "ClusterClasses", len(clusterClasses))
// Sets the pause field on the Cluster object in the source management cluster, so the controllers stop reconciling it.
log.V(1).Info("Pausing the source cluster")
if err := setClusterPause(o.fromProxy, clusters, true, o.dryRun); err != nil {
return err
}
log.V(1).Info("Pausing the source cluster classes")
if err := setClusterClassPause(o.fromProxy, clusterClasses, true, o.dryRun); err != nil {
return errors.Wrap(err, "error pausing cluser classes")
}
// Ensure all the expected target namespaces are in place before creating objects.
log.V(1).Info("Creating target namespaces, if missing")
if err := o.ensureNamespaces(graph, toProxy); err != nil {
return err
}
// Define the move sequence by processing the ownerReference chain, so we ensure that a Kubernetes object is moved only after its owners.
// The sequence is bases on object graph nodes, each one representing a Kubernetes object; nodes are grouped, so bulk of nodes can be moved in parallel. e.g.
// - All the Clusters should be moved first (group 1, processed in parallel)
// - All the MachineDeployments should be moved second (group 1, processed in parallel)
// - then all the MachineSets, then all the Machines, etc.
moveSequence := getMoveSequence(graph)
// Create all objects group by group, ensuring all the ownerReferences are re-created.
log.Info("Creating objects in the target cluster")
for groupIndex := 0; groupIndex < len(moveSequence.groups); groupIndex++ {
if err := o.createGroup(moveSequence.getGroup(groupIndex), toProxy); err != nil {
return err
}
}
// Delete all objects group by group in reverse order.
log.Info("Deleting objects from the source cluster")
for groupIndex := len(moveSequence.groups) - 1; groupIndex >= 0; groupIndex-- {
if err := o.deleteGroup(moveSequence.getGroup(groupIndex)); err != nil {
return err
}
}
// Resume the cluster classes in the target management cluster, so the controllers start reconciling it.
log.V(1).Info("Resuming the target cluter classes")
if err := setClusterClassPause(toProxy, clusterClasses, false, o.dryRun); err != nil {
return errors.Wrap(err, "error resuming cluster classes")
}
// Reset the pause field on the Cluster object in the target management cluster, so the controllers start reconciling it.
log.V(1).Info("Resuming the target cluster")
return setClusterPause(toProxy, clusters, false, o.dryRun)
}
func (o *objectMover) backup(graph *objectGraph, directory string) error {
log := logf.Log
clusters := graph.getClusters()
log.Info("Starting backup of Cluster API objects", "Clusters", len(clusters))
clusterClasses := graph.getClusterClasses()
log.Info("Moving Cluster API objects", "ClusterClasses", len(clusterClasses))
// Sets the pause field on the Cluster object in the source management cluster, so the controllers stop reconciling it.
log.V(1).Info("Pausing the source cluster")
if err := setClusterPause(o.fromProxy, clusters, true, o.dryRun); err != nil {
return err
}
log.V(1).Info("Pausing the source cluster classes")
if err := setClusterClassPause(o.fromProxy, clusterClasses, true, o.dryRun); err != nil {
return errors.Wrap(err, "error pausing cluser classes")
}
// Define the move sequence by processing the ownerReference chain, so we ensure that a Kubernetes object is moved only after its owners.
// The sequence is bases on object graph nodes, each one representing a Kubernetes object; nodes are grouped, so bulk of nodes can be moved in parallel. e.g.
// - All the Clusters should be moved first (group 1, processed in parallel)
// - All the MachineDeployments should be moved second (group 1, processed in parallel)
// - then all the MachineSets, then all the Machines, etc.
moveSequence := getMoveSequence(graph)
// Save all objects group by group
log.Info(fmt.Sprintf("Saving files to %s", directory))
for groupIndex := 0; groupIndex < len(moveSequence.groups); groupIndex++ {
if err := o.backupGroup(moveSequence.getGroup(groupIndex), directory); err != nil {
return err
}
}
// Resume the cluster classes in the target management cluster, so the controllers start reconciling it.
log.V(1).Info("Resuming the target cluter classes")
if err := setClusterClassPause(o.fromProxy, clusterClasses, false, o.dryRun); err != nil {
return errors.Wrap(err, "error resuming cluster classes")
}
// Reset the pause field on the Cluster object in the target management cluster, so the controllers start reconciling it.
log.V(1).Info("Resuming the source cluster")
return setClusterPause(o.fromProxy, clusters, false, o.dryRun)
}
func (o *objectMover) restore(graph *objectGraph, toProxy Proxy) error {
log := logf.Log
// Get clusters from graph
clusters := graph.getClusters()
// Get clusterclasses from graph
clusterClasses := graph.getClusterClasses()
// Ensure all the expected target namespaces are in place before creating objects.
log.V(1).Info("Creating target namespaces, if missing")
if err := o.ensureNamespaces(graph, toProxy); err != nil {
return err
}
// Define the move sequence by processing the ownerReference chain, so we ensure that a Kubernetes object is moved only after its owners.
// The sequence is bases on object graph nodes, each one representing a Kubernetes object; nodes are grouped, so bulk of nodes can be moved in parallel. e.g.
// - All the Clusters should be moved first (group 1, processed in parallel)
// - All the MachineDeployments should be moved second (group 1, processed in parallel)
// - then all the MachineSets, then all the Machines, etc.
moveSequence := getMoveSequence(graph)
// Create all objects group by group, ensuring all the ownerReferences are re-created.
log.Info("Restoring objects into the target cluster")
for groupIndex := 0; groupIndex < len(moveSequence.groups); groupIndex++ {
if err := o.restoreGroup(moveSequence.getGroup(groupIndex), toProxy); err != nil {
return err
}
}
// Resume reconciling the ClusterClasses after being restored from a backup.
// By default, during backup, ClusterClasses are paused so they must be unpaused to be used again
log.V(1).Info("Resuming the target cluter classes")
if err := setClusterClassPause(toProxy, clusterClasses, false, o.dryRun); err != nil {
return errors.Wrap(err, "error resuming cluster classes")
}
// Resume reconciling the Clusters after being restored from a backup.
// By default, during backup, Clusters are paused so they must be unpaused to be used again
log.V(1).Info("Resuming the target cluster")
return setClusterPause(toProxy, clusters, false, o.dryRun)
}
// moveSequence defines a list of group of moveGroups.
type moveSequence struct {
groups []moveGroup
nodesMap map[*node]empty
}
// moveGroup defines is a list of nodes read from the object graph that can be moved in parallel.
type moveGroup []*node
func (s *moveSequence) addGroup(group moveGroup) {
// Add the group
s.groups = append(s.groups, group)
// Add all the nodes in the group to the nodeMap so we can check if a node is already in the move sequence or not
for _, n := range group {
s.nodesMap[n] = empty{}
}
}
func (s *moveSequence) hasNode(n *node) bool {
_, ok := s.nodesMap[n]
return ok
}
func (s *moveSequence) getGroup(i int) moveGroup {
return s.groups[i]
}
// Define the move sequence by processing the ownerReference chain.
func getMoveSequence(graph *objectGraph) *moveSequence {
moveSequence := &moveSequence{
groups: []moveGroup{},
nodesMap: make(map[*node]empty),
}
for {
// Determine the next move group by processing all the nodes in the graph that belong to a Cluster.
// NB. it is necessary to filter out nodes not belonging to a cluster because e.g. discovery reads all the secrets,
// but only few of them are related to Clusters/Machines etc.
moveGroup := moveGroup{}
for _, n := range graph.getMoveNodes() {
// If the node was already included in the moveSequence, skip it.
if moveSequence.hasNode(n) {
continue
}
// Check if all the ownerReferences are already included in the move sequence; if yes, add the node to move group,
// otherwise skip it (the node will be re-processed in the next group).
ownersInPlace := true
for owner := range n.owners {
if !moveSequence.hasNode(owner) {
ownersInPlace = false
break
}
}
for owner := range n.softOwners {
if !moveSequence.hasNode(owner) {
ownersInPlace = false
break
}
}
if ownersInPlace {
moveGroup = append(moveGroup, n)
}
}
// If the resulting move group is empty it means that all the nodes are already in the sequence, so exit.
if len(moveGroup) == 0 {
break
}
moveSequence.addGroup(moveGroup)
}
return moveSequence
}
// setClusterPause sets the paused field on nodes referring to Cluster objects.
func setClusterPause(proxy Proxy, clusters []*node, value bool, dryRun bool) error {
if dryRun {
return nil
}
log := logf.Log
patch := client.RawPatch(types.MergePatchType, []byte(fmt.Sprintf("{\"spec\":{\"paused\":%t}}", value)))
setClusterPauseBackoff := newWriteBackoff()
for i := range clusters {
cluster := clusters[i]
log.V(5).Info("Set Cluster.Spec.Paused", "Paused", value, "Cluster", cluster.identity.Name, "Namespace", cluster.identity.Namespace)
// Nb. The operation is wrapped in a retry loop to make setClusterPause more resilient to unexpected conditions.
if err := retryWithExponentialBackoff(setClusterPauseBackoff, func() error {
return patchCluster(proxy, cluster, patch)
}); err != nil {
return errors.Wrapf(err, "error setting Cluster.Spec.Paused=%t", value)
}
}
return nil
}
// setClusterClassPause sets the paused annotation on nodes referring to ClusterClass objects.
func setClusterClassPause(proxy Proxy, clusterclasses []*node, pause bool, dryRun bool) error {
if dryRun {
return nil
}
log := logf.Log
setClusterClassPauseBackoff := newWriteBackoff()
for i := range clusterclasses {
clusterclass := clusterclasses[i]
if pause {
log.V(5).Info("Set Paused annotation", "ClusterClass", clusterclass.identity.Name, "Namespace", clusterclass.identity.Namespace)
} else {
log.V(5).Info("Remove Paused annotation", "ClusterClass", clusterclass.identity.Name, "Namespace", clusterclass.identity.Namespace)
}
// Nb. The operation is wrapped in a retry loop to make setClusterClassPause more resilient to unexpected conditions.
if err := retryWithExponentialBackoff(setClusterClassPauseBackoff, func() error {
return pauseClusterClass(proxy, clusterclass, pause)
}); err != nil {
return errors.Wrapf(err, "error updating ClusterClass %s/%s", clusterclass.identity.Namespace, clusterclass.identity.Name)
}
}
return nil
}
// patchCluster applies a patch to a node referring to a Cluster object.
func patchCluster(proxy Proxy, cluster *node, patch client.Patch) error {
cFrom, err := proxy.NewClient()
if err != nil {
return err
}
clusterObj := &clusterv1.Cluster{}
clusterObjKey := client.ObjectKey{
Namespace: cluster.identity.Namespace,
Name: cluster.identity.Name,
}
if err := cFrom.Get(ctx, clusterObjKey, clusterObj); err != nil {
return errors.Wrapf(err, "error reading Cluster %s/%s",
clusterObj.GetNamespace(), clusterObj.GetName())
}
if err := cFrom.Patch(ctx, clusterObj, patch); err != nil {
return errors.Wrapf(err, "error patching Cluster %s/%s",
clusterObj.GetNamespace(), clusterObj.GetName())
}
return nil
}
func pauseClusterClass(proxy Proxy, n *node, pause bool) error {
cFrom, err := proxy.NewClient()
if err != nil {
return errors.Wrap(err, "error creating client")
}
// Get the ClusterClass from the server
clusterClass := &clusterv1.ClusterClass{}
clusterClassObjKey := client.ObjectKey{
Name: n.identity.Name,
Namespace: n.identity.Namespace,
}
if err := cFrom.Get(ctx, clusterClassObjKey, clusterClass); err != nil {
return errors.Wrapf(err, "error reading ClusterClass %s/%s", n.identity.Namespace, n.identity.Name)
}
patchHelper, err := patch.NewHelper(clusterClass, cFrom)
if err != nil {
return errors.Wrapf(err, "error creating patcher for ClusterClass %s/%s", n.identity.Namespace, n.identity.Name)
}
// Update the annotation to the desired state
ccAnnotations := clusterClass.GetAnnotations()
if ccAnnotations == nil {
ccAnnotations = make(map[string]string)
}
if pause {
// Set the pause annotation.
ccAnnotations[clusterv1.PausedAnnotation] = ""
} else {
// Delete the pause annotation.
delete(ccAnnotations, clusterv1.PausedAnnotation)
}
// If the ClusterClass is already at desired state return early.
if !annotations.AddAnnotations(clusterClass, ccAnnotations) {
return nil
}
// Update the cluster class with the new annotations.
if err := patchHelper.Patch(ctx, clusterClass); err != nil {
return errors.Wrapf(err, "error patching ClusterClass %s/%s", n.identity.Namespace, n.identity.Name)
}
return nil
}
// ensureNamespaces ensures all the expected target namespaces are in place before creating objects.
func (o *objectMover) ensureNamespaces(graph *objectGraph, toProxy Proxy) error {
if o.dryRun {
return nil
}
ensureNamespaceBackoff := newWriteBackoff()
namespaces := sets.NewString()
for _, node := range graph.getMoveNodes() {
// ignore global/cluster-wide objects
if node.isGlobal {
continue
}
namespace := node.identity.Namespace
// If the namespace was already processed, skip it.
if namespaces.Has(namespace) {
continue
}
namespaces.Insert(namespace)
if err := retryWithExponentialBackoff(ensureNamespaceBackoff, func() error {
return o.ensureNamespace(toProxy, namespace)
}); err != nil {
return err
}
}
return nil
}
// ensureNamespace ensures a target namespaces is in place before creating objects.
func (o *objectMover) ensureNamespace(toProxy Proxy, namespace string) error {
log := logf.Log
cs, err := toProxy.NewClient()
if err != nil {
return err
}
// Otherwise check if namespace exists (also dealing with RBAC restrictions).
ns := &corev1.Namespace{}
key := client.ObjectKey{
Name: namespace,
}
err = cs.Get(ctx, key, ns)
if err == nil {
return nil
}
if apierrors.IsForbidden(err) {
namespaces := &corev1.NamespaceList{}
namespaceExists := false
for {
if err := cs.List(ctx, namespaces, client.Continue(namespaces.Continue)); err != nil {
return err
}
for _, ns := range namespaces.Items {
if ns.Name == namespace {
namespaceExists = true
break
}
}
if namespaces.Continue == "" {
break
}
}
if namespaceExists {
return nil
}
}
if !apierrors.IsNotFound(err) {
return err
}
// If the namespace does not exists, create it.
ns = &corev1.Namespace{
TypeMeta: metav1.TypeMeta{
APIVersion: "v1",
Kind: "Namespace",
},
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}
log.V(1).Info("Creating", ns.Kind, ns.Name)
if err := cs.Create(ctx, ns); err != nil && !apierrors.IsAlreadyExists(err) {
return err
}
return nil
}
// createGroup creates all the Kubernetes objects into the target management cluster corresponding to the object graph nodes in a moveGroup.
func (o *objectMover) createGroup(group moveGroup, toProxy Proxy) error {
createTargetObjectBackoff := newWriteBackoff()
errList := []error{}
for _, nodeToCreate := range group {
// Creates the Kubernetes object corresponding to the nodeToCreate.
// Nb. The operation is wrapped in a retry loop to make move more resilient to unexpected conditions.
err := retryWithExponentialBackoff(createTargetObjectBackoff, func() error {
return o.createTargetObject(nodeToCreate, toProxy)
})
if err != nil {
errList = append(errList, err)
}
}
if len(errList) > 0 {
return kerrors.NewAggregate(errList)
}
return nil
}
func (o *objectMover) backupGroup(group moveGroup, directory string) error {
backupTargetObjectBackoff := newWriteBackoff()
errList := []error{}
for _, nodeToBackup := range group {
// Backs-up the Kubernetes object corresponding to the nodeToBackup.
// Nb. The operation is wrapped in a retry loop to make move more resilient to unexpected conditions.
err := retryWithExponentialBackoff(backupTargetObjectBackoff, func() error {
return o.backupTargetObject(nodeToBackup, directory)
})
if err != nil {
errList = append(errList, err)
}
}
if len(errList) > 0 {
return kerrors.NewAggregate(errList)
}
return nil
}
func (o *objectMover) restoreGroup(group moveGroup, toProxy Proxy) error {
restoreTargetObjectBackoff := newWriteBackoff()
errList := []error{}
for _, nodeToRestore := range group {
// Creates the Kubernetes object corresponding to the nodeToRestore.
// Nb. The operation is wrapped in a retry loop to make move more resilient to unexpected conditions.
err := retryWithExponentialBackoff(restoreTargetObjectBackoff, func() error {
return o.restoreTargetObject(nodeToRestore, toProxy)
})
if err != nil {
errList = append(errList, err)
}
}
if len(errList) > 0 {
return kerrors.NewAggregate(errList)
}
return nil
}
// createTargetObject creates the Kubernetes object in the target Management cluster corresponding to the object graph node, taking care of restoring the OwnerReference with the owner nodes, if any.
func (o *objectMover) createTargetObject(nodeToCreate *node, toProxy Proxy) error {
log := logf.Log
log.V(1).Info("Creating", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
if o.dryRun {
return nil
}
cFrom, err := o.fromProxy.NewClient()
if err != nil {
return err
}
// Get the source object
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(nodeToCreate.identity.APIVersion)
obj.SetKind(nodeToCreate.identity.Kind)
objKey := client.ObjectKey{
Namespace: nodeToCreate.identity.Namespace,
Name: nodeToCreate.identity.Name,
}
if err := cFrom.Get(ctx, objKey, obj); err != nil {
return errors.Wrapf(err, "error reading %q %s/%s",
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
}
// New objects cannot have a specified resource version. Clear it out.
obj.SetResourceVersion("")
// Removes current OwnerReferences
obj.SetOwnerReferences(nil)
// Rebuild the owne reference chain
o.buildOwnerChain(obj, nodeToCreate)
// Save the old managed fields for topology managed fields migration
oldManagedFields := obj.GetManagedFields()
// FIXME Workaround for https://github.com/kubernetes/kubernetes/issues/32220. Remove when the issue is fixed.
// If the resource already exists, the API server ordinarily returns an AlreadyExists error. Due to the above issue, if the resource has a non-empty metadata.generateName field, the API server returns a ServerTimeoutError. To ensure that the API server returns an AlreadyExists error, we set the metadata.generateName field to an empty string.
if len(obj.GetName()) > 0 && len(obj.GetGenerateName()) > 0 {
obj.SetGenerateName("")
}
// Creates the targetObj into the target management cluster.
cTo, err := toProxy.NewClient()
if err != nil {
return err
}
if err := cTo.Create(ctx, obj); err != nil {
if !apierrors.IsAlreadyExists(err) {
return errors.Wrapf(err, "error creating %q %s/%s",
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
}
// If the object already exists, try to update it if it is node a global object / something belonging to a global object hierarchy (e.g. a secrets owned by a global identity object).
if nodeToCreate.isGlobal || nodeToCreate.isGlobalHierarchy {
log.V(5).Info("Object already exists, skipping upgrade because it is global/it is owned by a global object", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
} else {
// Nb. This should not happen, but it is supported to make move more resilient to unexpected interrupt/restarts of the move process.
log.V(5).Info("Object already exists, updating", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
// Retrieve the UID and the resource version for the update.
existingTargetObj := &unstructured.Unstructured{}
existingTargetObj.SetAPIVersion(obj.GetAPIVersion())
existingTargetObj.SetKind(obj.GetKind())
if err := cTo.Get(ctx, objKey, existingTargetObj); err != nil {
return errors.Wrapf(err, "error reading resource for %q %s/%s",
existingTargetObj.GroupVersionKind(), existingTargetObj.GetNamespace(), existingTargetObj.GetName())
}
obj.SetUID(existingTargetObj.GetUID())
obj.SetResourceVersion(existingTargetObj.GetResourceVersion())
if err := cTo.Update(ctx, obj); err != nil {
return errors.Wrapf(err, "error updating %q %s/%s",
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
}
}
}
// Stores the newUID assigned to the newly created object.
nodeToCreate.newUID = obj.GetUID()
if err := patchTopologyManagedFields(ctx, oldManagedFields, obj, cTo); err != nil {
return err
}
return nil
}
func (o *objectMover) backupTargetObject(nodeToCreate *node, directory string) error {
log := logf.Log
log.V(1).Info("Saving", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
cFrom, err := o.fromProxy.NewClient()
if err != nil {
return err
}
// Get the source object
obj := &unstructured.Unstructured{}
obj.SetAPIVersion(nodeToCreate.identity.APIVersion)
obj.SetKind(nodeToCreate.identity.Kind)
objKey := client.ObjectKey{
Namespace: nodeToCreate.identity.Namespace,
Name: nodeToCreate.identity.Name,
}
if err := cFrom.Get(ctx, objKey, obj); err != nil {
return errors.Wrapf(err, "error reading %q %s/%s",
obj.GroupVersionKind(), obj.GetNamespace(), obj.GetName())
}
// Get JSON for object and write it into the configured directory
byObj, err := obj.MarshalJSON()
if err != nil {
return err
}
filenameObj := nodeToCreate.getFilename()
objectFile := filepath.Join(directory, filenameObj)
// If file exists, then remove it to be written again
_, err = os.Stat(objectFile)
if err != nil && !os.IsNotExist(err) {
return err
}
if err == nil {
if err := os.Remove(objectFile); err != nil {
return err
}
}
err = os.WriteFile(objectFile, byObj, 0600)
if err != nil {
return err
}
return nil
}
func (o *objectMover) restoreTargetObject(nodeToCreate *node, toProxy Proxy) error {
log := logf.Log
log.V(1).Info("Restoring", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
// Creates the targetObj into the target management cluster.
cTo, err := toProxy.NewClient()
if err != nil {
return err
}
// Attempt to retrieve an existing object. If it exists, update the UID to rebuild the owner chain
objKey := client.ObjectKey{
Namespace: nodeToCreate.identity.Namespace,
Name: nodeToCreate.identity.Name,
}
existingTargetObj := &unstructured.Unstructured{}
existingTargetObj.SetAPIVersion(nodeToCreate.restoreObject.GetAPIVersion())
existingTargetObj.SetKind(nodeToCreate.restoreObject.GetKind())
if err := cTo.Get(ctx, objKey, existingTargetObj); err == nil {
log.V(5).Info("Object already exists, skipping restore", nodeToCreate.identity.Kind, nodeToCreate.identity.Name, "Namespace", nodeToCreate.identity.Namespace)
// Update the nodes UID since it already exits. Any nodes owned by this existing node will be updated when the owner chain is rebuilt
nodeToCreate.newUID = existingTargetObj.GetUID()
// Return early since the object already exists
return nil
}
// Rebuild the source object
obj := nodeToCreate.restoreObject
obj.SetAPIVersion(nodeToCreate.identity.APIVersion)
obj.SetKind(nodeToCreate.identity.Kind)
// New objects cannot have a specified resource version. Clear it out.
obj.SetResourceVersion("")