Skip to content

Commit 2234824

Browse files
committed
feat(backups): restore vmi to copy in another namespace
Signed-off-by: Andrey Kolkov <[email protected]>
1 parent d27b01c commit 2234824

19 files changed

Lines changed: 2361 additions & 31 deletions

‎api/backups/v1alpha1/restorejob_types.go‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ type RestoreJobSpec struct {
4242
// application as referenced by backup.spec.applicationRef.
4343
// +optional
4444
TargetApplicationRef *corev1.TypedLocalObjectReference `json:"targetApplicationRef,omitempty"`
45+
46+
// Options is a driver-specific blob of restore options, typed based on
47+
// targetApplicationRef and the current controller implementation.
48+
// +optional
49+
// +kubebuilder:pruning:PreserveUnknownFields
50+
Options *runtime.RawExtension `json:"options,omitempty"`
4551
}
4652

4753
// RestoreJobStatus represents the observed state of a RestoreJob.

‎examples/backups/vmi/00-helpers.sh‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#!/bin/bash
2+
# Helper functions and variables for the VMInstance backup/restore demo
3+
# Source this file in other scripts: source "$(dirname "$0")/00-helpers.sh"
4+
5+
# ANSI color codes
6+
export RED='\033[0;31m'
7+
export GREEN='\033[0;32m'
8+
export YELLOW='\033[1;33m'
9+
export BLUE='\033[0;34m'
10+
export MAGENTA='\033[0;35m'
11+
export CYAN='\033[0;36m'
12+
export WHITE='\033[1;37m'
13+
export NC='\033[0m' # No Color
14+
export BOLD='\033[1m'
15+
16+
# Default settings
17+
export NAMESPACE="${NAMESPACE:-tenant-root}"
18+
export BACKUP_STORAGE_LOCATION="${BACKUP_STORAGE_LOCATION:-default}"
19+
20+
# Logging functions (output to stderr to avoid polluting captured output)
21+
log_info() {
22+
echo -e "${BLUE}ℹ${NC} $*" >&2
23+
}
24+
25+
log_success() {
26+
echo -e "${GREEN}✔${NC} $*" >&2
27+
}
28+
29+
log_warning() {
30+
echo -e "${YELLOW}⚠${NC} $*" >&2
31+
}
32+
33+
log_error() {
34+
echo -e "${RED}✖${NC} $*" >&2
35+
}
36+
37+
log_step() {
38+
echo -e "\n${MAGENTA}${BOLD}▶ $*${NC}" >&2
39+
}
40+
41+
log_substep() {
42+
echo -e "${CYAN} → $*${NC}" >&2
43+
}
44+
45+
log_command() {
46+
echo -e "${WHITE} \$ $*${NC}" >&2
47+
}
48+
49+
# Wait for user to press Enter
50+
wait_for_enter() {
51+
echo -e "\n${CYAN}Press Enter to continue...${NC}" >&2
52+
read -r
53+
}
54+
55+
# Check if a Kubernetes resource exists
56+
resource_exists() {
57+
local resource_type="$1"
58+
local resource_name="$2"
59+
local namespace="${3:-}"
60+
61+
if [[ -n "$namespace" ]]; then
62+
kubectl get "$resource_type" "$resource_name" -n "$namespace" &>/dev/null
63+
else
64+
kubectl get "$resource_type" "$resource_name" &>/dev/null
65+
fi
66+
}
67+
68+
# Wait for a resource field to reach a desired value
69+
wait_for_field() {
70+
local resource_type="$1"
71+
local resource_name="$2"
72+
local jsonpath="$3"
73+
local desired="$4"
74+
local namespace="${5:-}"
75+
local timeout="${6:-300}"
76+
77+
log_substep "Waiting for $resource_type/$resource_name $jsonpath to become '$desired'..."
78+
79+
local elapsed=0
80+
local ns_flag=""
81+
[[ -n "$namespace" ]] && ns_flag="-n $namespace"
82+
83+
while true; do
84+
local current
85+
# shellcheck disable=SC2086
86+
current=$(kubectl get "$resource_type" "$resource_name" $ns_flag -o jsonpath="$jsonpath" 2>/dev/null || true)
87+
if [[ "$current" == "$desired" ]]; then
88+
log_success "$resource_type/$resource_name reached '$desired'"
89+
return 0
90+
fi
91+
if [[ $elapsed -ge $timeout ]]; then
92+
log_error "Timeout waiting for $resource_type/$resource_name (current: '$current', expected: '$desired')"
93+
return 1
94+
fi
95+
sleep 5
96+
elapsed=$((elapsed + 5))
97+
echo -n "." >&2
98+
done
99+
}
100+
101+
# Print a separator line
102+
separator() {
103+
echo -e "\n${CYAN}────────────────────────────────────────────────────────────${NC}\n" >&2
104+
}
105+
106+
# Print script header
107+
print_header() {
108+
local title="$1"
109+
echo -e "\n${MAGENTA}${BOLD}╔════════════════════════════════════════════════════════════╗${NC}" >&2
110+
echo -e "${MAGENTA}${BOLD}║${NC} ${WHITE}${BOLD}$title${NC}" >&2
111+
echo -e "${MAGENTA}${BOLD}╚════════════════════════════════════════════════════════════╝${NC}\n" >&2
112+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
#!/bin/bash
2+
# Step 01: Create Velero backup/restore strategies for VMInstance and VMDisk
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
source "$SCRIPT_DIR/00-helpers.sh"
7+
8+
print_header "Step 1: Create Velero Strategies"
9+
10+
log_step "Creating VMInstance strategy..."
11+
log_command "kubectl apply -f - (Velero strategy: vminstance-strategy)"
12+
13+
kubectl apply -f - <<'EOF'
14+
apiVersion: strategy.backups.cozystack.io/v1alpha1
15+
kind: Velero
16+
metadata:
17+
name: vminstance-strategy
18+
spec:
19+
template:
20+
# Symmetric restore filters: kubevirt-velero-plugin requires launcher pods in the backup,
21+
# but restore orLabelSelectors limit what is applied from the tarball (e.g. skip another
22+
# VM's pod that Velero added via PVC item action). VMDisk OR branches are appended by
23+
# the controller from backup.status.underlyingResources or the Velero backup annotation.
24+
restoreSpec:
25+
existingResourcePolicy: update
26+
includedNamespaces:
27+
- '{{ .Application.metadata.namespace }}'
28+
orLabelSelectors:
29+
- matchLabels:
30+
app.kubernetes.io/instance: 'vm-instance-{{ .Application.metadata.name }}'
31+
- matchLabels:
32+
apps.cozystack.io/application.kind: '{{ .Application.kind }}'
33+
apps.cozystack.io/application.name: '{{ .Application.metadata.name }}'
34+
includedResources:
35+
- helmreleases.helm.toolkit.fluxcd.io
36+
- virtualmachines.kubevirt.io
37+
- virtualmachineinstances.kubevirt.io
38+
- pods
39+
- persistentvolumeclaims
40+
- configmaps
41+
- secrets
42+
- controllerrevisions.apps
43+
includeClusterResources: false
44+
excludedResources:
45+
# Required to avoid conflict with restored DV from HR VMDisk
46+
- datavolumes.cdi.kubevirt.io
47+
48+
spec: # see https://velero.io/docs/v1.17/api-types/backup/
49+
includedNamespaces:
50+
- '{{ .Application.metadata.namespace }}'
51+
orLabelSelectors:
52+
# VM resources (VirtualMachine, DataVolume, PVC, etc.)
53+
- matchLabels:
54+
app.kubernetes.io/instance: 'vm-instance-{{ .Application.metadata.name }}'
55+
# HelmRelease (the Cozystack app object)
56+
- matchLabels:
57+
apps.cozystack.io/application.kind: '{{ .Application.kind }}'
58+
apps.cozystack.io/application.name: '{{ .Application.metadata.name }}'
59+
includedResources:
60+
- helmreleases.helm.toolkit.fluxcd.io
61+
- virtualmachines.kubevirt.io
62+
- virtualmachineinstances.kubevirt.io
63+
# Required by kubevirt-velero-plugin for running VMs ("launcher pod must be in backup").
64+
- pods
65+
# Required by kubevirt-velero-plugin requires DV to be in backup of VM, but it excludes in restores
66+
- datavolumes.cdi.kubevirt.io
67+
- persistentvolumeclaims
68+
- configmaps
69+
- secrets
70+
- controllerrevisions.apps
71+
includeClusterResources: false
72+
storageLocation: '{{ .Parameters.backupStorageLocationName }}'
73+
volumeSnapshotLocations:
74+
- '{{ .Parameters.backupStorageLocationName }}'
75+
snapshotVolumes: true
76+
snapshotMoveData: true
77+
ttl: 720h0m0s
78+
itemOperationTimeout: 24h0m0s
79+
EOF
80+
81+
log_success "VMInstance strategy created"
82+
83+
separator
84+
85+
log_step "Creating VMDisk strategy..."
86+
log_command "kubectl apply -f - (Velero strategy: vmdisk-strategy)"
87+
88+
kubectl apply -f - <<'EOF'
89+
apiVersion: strategy.backups.cozystack.io/v1alpha1
90+
kind: Velero
91+
metadata:
92+
name: vmdisk-strategy
93+
spec:
94+
template:
95+
restoreSpec:
96+
existingResourcePolicy: update
97+
includedNamespaces:
98+
- '{{ .Application.metadata.namespace }}'
99+
orLabelSelectors:
100+
- matchLabels:
101+
app.kubernetes.io/instance: 'vm-disk-{{ .Application.metadata.name }}'
102+
- matchLabels:
103+
apps.cozystack.io/application.kind: '{{ .Application.kind }}'
104+
apps.cozystack.io/application.name: '{{ .Application.metadata.name }}'
105+
includedResources:
106+
- helmreleases.helm.toolkit.fluxcd.io
107+
- persistentvolumeclaims
108+
- configmaps
109+
includeClusterResources: false
110+
111+
spec:
112+
includedNamespaces:
113+
- '{{ .Application.metadata.namespace }}'
114+
orLabelSelectors:
115+
- matchLabels:
116+
app.kubernetes.io/instance: 'vm-disk-{{ .Application.metadata.name }}'
117+
- matchLabels:
118+
apps.cozystack.io/application.kind: '{{ .Application.kind }}'
119+
apps.cozystack.io/application.name: '{{ .Application.metadata.name }}'
120+
includedResources:
121+
- helmreleases.helm.toolkit.fluxcd.io
122+
- persistentvolumeclaims
123+
- configmaps
124+
includeClusterResources: false
125+
storageLocation: '{{ .Parameters.backupStorageLocationName }}'
126+
volumeSnapshotLocations:
127+
- '{{ .Parameters.backupStorageLocationName }}'
128+
snapshotVolumes: true
129+
snapshotMoveData: true
130+
ttl: 720h0m0s
131+
itemOperationTimeout: 24h0m0s
132+
EOF
133+
134+
log_success "VMDisk strategy created"
135+
136+
separator
137+
138+
log_step "Verifying strategies..."
139+
log_command "kubectl get velero.strategy.backups.cozystack.io"
140+
kubectl get velero.strategy.backups.cozystack.io
141+
142+
separator
143+
144+
log_success "Velero strategies are ready"
145+
echo -e "\n${GREEN}${BOLD}Next step:${NC} ./02-create-backupclass.sh"
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/bin/bash
2+
# Step 02: Create BackupClass that binds strategies to application types
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
source "$SCRIPT_DIR/00-helpers.sh"
7+
8+
print_header "Step 2: Create BackupClass"
9+
10+
log_step "Creating BackupClass 'velero'..."
11+
log_info "BackupClass maps application kinds (VMInstance, VMDisk) to their Velero strategies"
12+
log_command "kubectl apply -f - (BackupClass: velero)"
13+
14+
kubectl apply -f - <<EOF
15+
apiVersion: backups.cozystack.io/v1alpha1
16+
kind: BackupClass
17+
metadata:
18+
name: velero
19+
spec:
20+
strategies:
21+
- strategyRef:
22+
apiGroup: strategy.backups.cozystack.io
23+
kind: Velero
24+
name: vminstance-strategy
25+
application:
26+
kind: VMInstance
27+
apiGroup: apps.cozystack.io
28+
parameters:
29+
backupStorageLocationName: ${BACKUP_STORAGE_LOCATION}
30+
- strategyRef:
31+
apiGroup: strategy.backups.cozystack.io
32+
kind: Velero
33+
name: vmdisk-strategy
34+
application:
35+
kind: VMDisk
36+
apiGroup: apps.cozystack.io
37+
parameters:
38+
backupStorageLocationName: ${BACKUP_STORAGE_LOCATION}
39+
EOF
40+
41+
log_success "BackupClass created"
42+
43+
separator
44+
45+
log_step "Verifying BackupClass..."
46+
log_command "kubectl get backupclass velero -o yaml"
47+
kubectl get backupclass velero -o yaml
48+
49+
separator
50+
51+
log_success "BackupClass is ready"
52+
echo -e "\n${GREEN}${BOLD}Next step:${NC} ./03-create-vmdisk.sh"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# Step 03: Create a VMDisk with Ubuntu cloud image
3+
set -euo pipefail
4+
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
source "$SCRIPT_DIR/00-helpers.sh"
7+
8+
print_header "Step 3: Create VMDisk"
9+
10+
log_step "Creating VMDisk 'ubuntu-source' in namespace $NAMESPACE..."
11+
log_info "This will download the Ubuntu Noble cloud image (~700MB)"
12+
log_command "kubectl apply -f - (VMDisk: ubuntu-source)"
13+
14+
kubectl apply -f - <<EOF
15+
apiVersion: apps.cozystack.io/v1alpha1
16+
kind: VMDisk
17+
metadata:
18+
name: ubuntu-source
19+
namespace: ${NAMESPACE}
20+
spec:
21+
optical: false
22+
source:
23+
http:
24+
url: https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img
25+
storage: 20Gi
26+
storageClass: replicated
27+
EOF
28+
29+
log_success "VMDisk created"
30+
31+
separator
32+
33+
log_step "Verifying VMDisk..."
34+
log_command "kubectl get vmdisk ubuntu-source -n $NAMESPACE"
35+
kubectl get vmdisk ubuntu-source -n "$NAMESPACE"
36+
37+
separator
38+
39+
log_success "VMDisk is ready (image download may still be in progress)"
40+
echo -e "\n${GREEN}${BOLD}Next step:${NC} ./04-create-vminstance.sh"

0 commit comments

Comments
 (0)