Skip to content

Commit f4228ff

Browse files
committed
[backups] Add templating of velero backups
## What this PR does This patch narrows the scope of the Velero backup strategy controller to simply template Velero Backups according to the application being backed up and the template in the strategy. Creating storage locations is now out of scope. ### Release note ```release-note [backups] Implement templating for Velero backups and remove creation of backup storage locations and volume snapshot locations. ``` Signed-off-by: Timofei Larkin <[email protected]>
1 parent bfafcaa commit f4228ff

10 files changed

Lines changed: 268 additions & 171 deletions

File tree

‎api/backups/strategy/v1alpha1/velero_types.go‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package v1alpha1
77

88
import (
9+
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
910
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1011
"k8s.io/apimachinery/pkg/runtime"
1112
)
@@ -47,7 +48,15 @@ type VeleroList struct {
4748
}
4849

4950
// VeleroSpec specifies the desired strategy for backing up with Velero.
50-
type VeleroSpec struct{}
51+
type VeleroSpec struct {
52+
Template VeleroTemplate `json:"template"`
53+
}
54+
55+
// VeleroTemplate describes the data a backup.velero.io should have when
56+
// templated from a Velero backup strategy.
57+
type VeleroTemplate struct {
58+
Spec velerov1.BackupSpec `json:"spec"`
59+
}
5160

5261
type VeleroStatus struct {
5362
Conditions []metav1.Condition `json:"conditions,omitempty"`

‎api/backups/strategy/v1alpha1/zz_generated.deepcopy.go‎

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎api/backups/v1alpha1/backupjob_types.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ func init() {
2121
})
2222
}
2323

24+
const (
25+
OwningJobNameLabel = thisGroup + "/owned-by.BackupJobName"
26+
OwningJobNamespaceLabel = thisGroup + "/owned-by.BackupJobNamespace"
27+
)
28+
2429
// BackupJobPhase represents the lifecycle phase of a BackupJob.
2530
type BackupJobPhase string
2631

‎api/backups/v1alpha1/groupversion_info.go‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,13 @@ import (
2525
"k8s.io/apimachinery/pkg/runtime/schema"
2626
)
2727

28+
const (
29+
thisGroup = "backups.cozystack.io"
30+
thisVersion = "v1alpha1"
31+
)
32+
2833
var (
29-
GroupVersion = schema.GroupVersion{Group: "backups.cozystack.io", Version: "v1alpha1"}
34+
GroupVersion = schema.GroupVersion{Group: thisGroup, Version: thisVersion}
3035
SchemeBuilder = runtime.NewSchemeBuilder(addGroupVersion)
3136
AddToScheme = SchemeBuilder.AddToScheme
3237
)

‎hack/e2e-apps/bucket.bats‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,14 @@ EOF
2525
SECRET_KEY=$(jq -r '.spec.secretS3.accessSecretKey' bucket-test-credentials.json)
2626
BUCKET_NAME=$(jq -r '.spec.bucketName' bucket-test-credentials.json)
2727

28-
# Tmp for test via s3 endpoint from bucket credentials
29-
S3_ENDPOINT=$(jq -r '.spec.secretS3.endpoint' bucket-test-credentials.json)
30-
echo "# S3 endopint = $S3_ENDPOINT" >&3
31-
3228
# Start port-forwarding
3329
bash -c 'timeout 100s kubectl port-forward service/seaweedfs-s3 -n tenant-root 8333:8333 > /dev/null 2>&1 &'
3430

3531
# Wait for port-forward to be ready
3632
timeout 30 sh -ec 'until nc -z localhost 8333; do sleep 1; done'
3733

3834
# Set up MinIO alias with error handling
39-
mc alias set local $S3_ENDPOINT $ACCESS_KEY $SECRET_KEY --insecure
35+
mc alias set local https://localhost:8333 $ACCESS_KEY $SECRET_KEY --insecure
4036

4137
# Upload file to bucket
4238
mc cp bucket-test-credentials.json $BUCKET_NAME/bucket-test-credentials.json

‎internal/backupcontroller/backupjob_controller.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package backupcontroller
22

33
import (
44
"context"
5+
"net/http"
56

67
apierrors "k8s.io/apimachinery/pkg/api/errors"
8+
"k8s.io/apimachinery/pkg/api/meta"
79
"k8s.io/apimachinery/pkg/runtime"
810
"k8s.io/apimachinery/pkg/types"
11+
"k8s.io/client-go/dynamic"
12+
"k8s.io/client-go/rest"
913
"k8s.io/client-go/tools/record"
1014
ctrl "sigs.k8s.io/controller-runtime"
1115
"sigs.k8s.io/controller-runtime/pkg/client"
16+
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
1217
"sigs.k8s.io/controller-runtime/pkg/log"
1318

1419
strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
@@ -19,6 +24,8 @@ import (
1924
// Velero.strategy.backups.cozystack.io objects.
2025
type BackupJobReconciler struct {
2126
client.Client
27+
dynamic.Interface
28+
meta.RESTMapper
2229
Scheme *runtime.Scheme
2330
Recorder record.EventRecorder
2431
}
@@ -68,6 +75,18 @@ func (r *BackupJobReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
6875

6976
// SetupWithManager registers our controller with the Manager and sets up watches.
7077
func (r *BackupJobReconciler) SetupWithManager(mgr ctrl.Manager) error {
78+
cfg := mgr.GetConfig()
79+
var err error
80+
if r.Interface, err = dynamic.NewForConfig(cfg); err != nil {
81+
return err
82+
}
83+
var h *http.Client
84+
if h, err = rest.HTTPClientFor(cfg); err != nil {
85+
return err
86+
}
87+
if r.RESTMapper, err = apiutil.NewDynamicRESTMapper(cfg, h); err != nil {
88+
return err
89+
}
7190
return ctrl.NewControllerManagedBy(mgr).
7291
For(&backupsv1alpha1.BackupJob{}).
7392
Complete(r)

0 commit comments

Comments
 (0)