forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
- Loading branch information
Showing
15 changed files
with
331 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
Copyright The Velero Contributors. | ||
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 config | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
/* | ||
High priorities: | ||
- Custom Resource Definitions come before Custom Resource so that they can be | ||
restored with their corresponding CRD. | ||
- Namespaces go second because all namespaced resources depend on them. | ||
- Storage Classes are needed to create PVs and PVCs correctly. | ||
- VolumeSnapshotClasses are needed to provision volumes using volumesnapshots | ||
- VolumeSnapshotContents are needed as they contain the handle to the volume snapshot in the | ||
storage provider | ||
- VolumeSnapshots are needed to create PVCs using the VolumeSnapshot as their data source. | ||
- DataUploads need to restore before PVC for Snapshot DataMover to work, because PVC needs the DataUploadResults to create DataDownloads. | ||
- PVs go before PVCs because PVCs depend on them. | ||
- PVCs go before pods or controllers so they can be mounted as volumes. | ||
- Service accounts go before secrets so service account token secrets can be filled automatically. | ||
- Secrets and ConfigMaps go before pods or controllers so they can be mounted | ||
as volumes. | ||
- Limit ranges go before pods or controllers so pods can use them. | ||
- Pods go before controllers so they can be explicitly restored and potentially | ||
have pod volume restores run before controllers adopt the pods. | ||
- Replica sets go before deployments/other controllers so they can be explicitly | ||
restored and be adopted by controllers. | ||
- CAPI ClusterClasses go before Clusters. | ||
- Endpoints go before Services so no new Endpoints will be created | ||
- Services go before Clusters so they can be adopted by AKO-operator and no new Services will be created | ||
for the same clusters | ||
Low priorities: | ||
- Tanzu ClusterBootstraps go last as it can reference any other kind of resources. | ||
- ClusterBootstraps go before CAPI Clusters otherwise a new default ClusterBootstrap object is created for the cluster | ||
- CAPI Clusters come before ClusterResourceSets because failing to do so means the CAPI controller-manager will panic. | ||
Both Clusters and ClusterResourceSets need to come before ClusterResourceSetBinding in order to properly restore workload clusters. | ||
See https://github.com/kubernetes-sigs/cluster-api/issues/4105 | ||
*/ | ||
var defaultRestorePriorities = Priorities{ | ||
HighPriorities: []string{ | ||
"customresourcedefinitions", | ||
"namespaces", | ||
"storageclasses", | ||
"volumesnapshotclass.snapshot.storage.k8s.io", | ||
"volumesnapshotcontents.snapshot.storage.k8s.io", | ||
"volumesnapshots.snapshot.storage.k8s.io", | ||
"datauploads.velero.io", | ||
"persistentvolumes", | ||
"persistentvolumeclaims", | ||
"serviceaccounts", | ||
"secrets", | ||
"configmaps", | ||
"limitranges", | ||
"pods", | ||
// we fully qualify replicasets.apps because prior to Kubernetes 1.16, replicasets also | ||
// existed in the extensions API group, but we back up replicasets from "apps" so we want | ||
// to ensure that we prioritize restoring from "apps" too, since this is how they're stored | ||
// in the backup. | ||
"replicasets.apps", | ||
"clusterclasses.cluster.x-k8s.io", | ||
"endpoints", | ||
"services", | ||
}, | ||
LowPriorities: []string{ | ||
"clusterbootstraps.run.tanzu.vmware.com", | ||
"clusters.cluster.x-k8s.io", | ||
"clusterresourcesets.addons.cluster.x-k8s.io", | ||
}, | ||
} | ||
|
||
const ( | ||
prioritySeparator = "-" | ||
) | ||
|
||
// Priorities defines the desired order of resource operations: | ||
// Resources in the HighPriorities list will be handled first | ||
// Resources in the LowPriorities list will be handled last | ||
// Other resources will be handled alphabetically after the high prioritized resources and before the low prioritized resources | ||
type Priorities struct { | ||
HighPriorities []string | ||
LowPriorities []string | ||
} | ||
|
||
// String returns a string representation of Priority. | ||
func (p *Priorities) String() string { | ||
priorities := p.HighPriorities | ||
if len(p.LowPriorities) > 0 { | ||
priorities = append(priorities, prioritySeparator) | ||
priorities = append(priorities, p.LowPriorities...) | ||
} | ||
return strings.Join(priorities, ",") | ||
} | ||
|
||
// Set parses the provided string to the priority object | ||
func (p *Priorities) Set(s string) error { | ||
if len(s) == 0 { | ||
return nil | ||
} | ||
strs := strings.Split(s, ",") | ||
separatorIndex := -1 | ||
for i, str := range strs { | ||
if str == prioritySeparator { | ||
if separatorIndex > -1 { | ||
return fmt.Errorf("multiple priority separator %q found", prioritySeparator) | ||
} | ||
separatorIndex = i | ||
} | ||
} | ||
// has no separator | ||
if separatorIndex == -1 { | ||
p.HighPriorities = strs | ||
return nil | ||
} | ||
// start with separator | ||
if separatorIndex == 0 { | ||
// contain only separator | ||
if len(strs) == 1 { | ||
return nil | ||
} | ||
p.LowPriorities = strs[1:] | ||
return nil | ||
} | ||
// end with separator | ||
if separatorIndex == len(strs)-1 { | ||
p.HighPriorities = strs[:len(strs)-1] | ||
return nil | ||
} | ||
|
||
// separator in the middle | ||
p.HighPriorities = strs[:separatorIndex] | ||
p.LowPriorities = strs[separatorIndex+1:] | ||
|
||
return nil | ||
} | ||
|
||
// Type specifies the flag type | ||
func (p *Priorities) Type() string { | ||
return "stringArray" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.