99
1010 corev1 "k8s.io/api/core/v1"
1111 "k8s.io/apimachinery/pkg/api/errors"
12+ "k8s.io/apimachinery/pkg/api/meta"
1213 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1314 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1415 "k8s.io/apimachinery/pkg/runtime/schema"
@@ -18,6 +19,7 @@ import (
1819
1920 strategyv1alpha1 "github.com/cozystack/cozystack/api/backups/strategy/v1alpha1"
2021 backupsv1alpha1 "github.com/cozystack/cozystack/api/backups/v1alpha1"
22+ "github.com/cozystack/cozystack/internal/template"
2123
2224 "github.com/go-logr/logr"
2325 velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
@@ -87,31 +89,6 @@ func (r *BackupJobReconciler) reconcileVelero(ctx context.Context, j *backupsv1a
8789 return ctrl.Result {}, nil
8890 }
8991
90- // For now implemented backup logic for apps.cozystack.io VirtualMachine only
91- logger .Debug ("validating BackupJob spec" ,
92- "applicationRef" , fmt .Sprintf ("%s/%s" , j .Spec .ApplicationRef .APIGroup , j .Spec .ApplicationRef .Kind ),
93- "storageRef" , fmt .Sprintf ("%s/%s" , j .Spec .StorageRef .APIGroup , j .Spec .StorageRef .Kind ))
94-
95- if j .Spec .ApplicationRef .Kind != "VirtualMachine" {
96- logger .Error (nil , "Unsupported application type" , "kind" , j .Spec .ApplicationRef .Kind )
97- return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("Unsupported application type: %s" , j .Spec .ApplicationRef .Kind ))
98- }
99- if j .Spec .ApplicationRef .APIGroup == nil || * j .Spec .ApplicationRef .APIGroup != "apps.cozystack.io" {
100- logger .Error (nil , "Unsupported application APIGroup" , "apiGroup" , j .Spec .ApplicationRef .APIGroup , "expected" , "apps.cozystack.io" )
101- return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("Unsupported application APIGroup: %v, expected apps.cozystack.io" , j .Spec .ApplicationRef .APIGroup ))
102- }
103-
104- if j .Spec .StorageRef .Kind != "Bucket" {
105- logger .Error (nil , "Unsupported storage type" , "kind" , j .Spec .StorageRef .Kind )
106- return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("Unsupported storage type: %s" , j .Spec .StorageRef .Kind ))
107- }
108- if j .Spec .StorageRef .APIGroup == nil || * j .Spec .StorageRef .APIGroup != "apps.cozystack.io" {
109- logger .Error (nil , "Unsupported storage APIGroup" , "apiGroup" , j .Spec .StorageRef .APIGroup , "expected" , "apps.cozystack.io" )
110- return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("Unsupported storage APIGroup: %v, expected apps.cozystack.io" , j .Spec .StorageRef .APIGroup ))
111- }
112-
113- logger .Debug ("BackupJob spec validation passed" )
114-
11592 // Step 1: On first reconcile, set startedAt (but not phase yet - phase will be set after backup creation)
11693 logger .Debug ("checking BackupJob status" , "startedAt" , j .Status .StartedAt , "phase" , j .Status .Phase )
11794 if j .Status .StartedAt == nil {
@@ -148,36 +125,44 @@ func (r *BackupJobReconciler) reconcileVelero(ctx context.Context, j *backupsv1a
148125 logger .Error (nil , "StartedAt is nil after status update, this should not happen" )
149126 return ctrl.Result {RequeueAfter : defaultRequeueAfter }, nil
150127 }
151- timestamp := j .Status .StartedAt .Time .Format ("2006-01-02-15-04-05" )
152- veleroBackupName := fmt .Sprintf ("%s-%s-%s" , j .Namespace , j .Name , timestamp )
128+ veleroBackupName := fmt .Sprintf ("%s.%s" , j .Namespace , j .Name )
153129 logger .Debug ("checking for existing Velero Backup" , "veleroBackupName" , veleroBackupName , "namespace" , veleroNamespace )
154130 veleroBackup := & velerov1.Backup {}
155- veleroBackupKey := client.ObjectKey {Namespace : veleroNamespace , Name : veleroBackupName }
131+ veleroBackupList := & velerov1.BackupList {}
132+ opts := []client.ListOption {
133+ client .InNamespace (veleroNamespace ),
134+ client.MatchingLabels {
135+ backupsv1alpha1 .OwningJobNamespaceLabel : j .Namespace ,
136+ backupsv1alpha1 .OwningJobNameLabel : j .Name ,
137+ },
138+ }
156139
157- if err := r .Get (ctx , veleroBackupKey , veleroBackup ); err != nil {
158- if errors .IsNotFound (err ) {
159- // Create Velero Backup
160- logger .Debug ("Velero Backup not found, creating new one" , "veleroBackupName" , veleroBackupName )
161- if err := r .createVeleroBackup (ctx , j , veleroBackupName ); err != nil {
162- logger .Error (err , "failed to create Velero Backup" )
163- return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("failed to create Velero Backup: %v" , err ))
164- }
165- // After successful Velero backup creation, set phase to Running
166- if j .Status .Phase != backupsv1alpha1 .BackupJobPhaseRunning {
167- logger .Debug ("setting BackupJob phase to Running after successful Velero backup creation" )
168- j .Status .Phase = backupsv1alpha1 .BackupJobPhaseRunning
169- if err := r .Status ().Update (ctx , j ); err != nil {
170- logger .Error (err , "failed to update BackupJob phase to Running" )
171- return ctrl.Result {}, err
172- }
173- }
174- logger .Debug ("created Velero Backup, requeuing" , "veleroBackupName" , veleroBackupName )
175- // Requeue to check status
176- return ctrl.Result {RequeueAfter : defaultRequeueAfter }, nil
177- }
140+ if err := r .List (ctx , veleroBackupList , opts ... ); err != nil {
178141 logger .Error (err , "failed to get Velero Backup" )
179142 return ctrl.Result {}, err
180143 }
144+
145+ if len (veleroBackupList .Items ) == 0 {
146+ // Create Velero Backup
147+ logger .Debug ("Velero Backup not found, creating new one" )
148+ if err := r .createVeleroBackup (ctx , j , veleroStrategy ); err != nil {
149+ logger .Error (err , "failed to create Velero Backup" )
150+ return r .markBackupJobFailed (ctx , j , fmt .Sprintf ("failed to create Velero Backup: %v" , err ))
151+ }
152+ // After successful Velero backup creation, set phase to Running
153+ if j .Status .Phase != backupsv1alpha1 .BackupJobPhaseRunning {
154+ logger .Debug ("setting BackupJob phase to Running after successful Velero backup creation" )
155+ j .Status .Phase = backupsv1alpha1 .BackupJobPhaseRunning
156+ if err := r .Status ().Update (ctx , j ); err != nil {
157+ logger .Error (err , "failed to update BackupJob phase to Running" )
158+ return ctrl.Result {}, err
159+ }
160+ }
161+ logger .Debug ("created Velero Backup, requeuing" , "veleroBackupName" , veleroBackupName )
162+ // Requeue to check status
163+ return ctrl.Result {RequeueAfter : defaultRequeueAfter }, nil
164+ }
165+
181166 logger .Debug ("found existing Velero Backup" , "veleroBackupName" , veleroBackupName , "phase" , veleroBackup .Status .Phase )
182167
183168 // If Velero backup exists but phase is not Running, set it to Running
@@ -519,126 +504,40 @@ func (r *BackupJobReconciler) markBackupJobFailed(ctx context.Context, backupJob
519504 return ctrl.Result {}, nil
520505}
521506
522- func (r * BackupJobReconciler ) createVeleroBackup (ctx context.Context , backupJob * backupsv1alpha1.BackupJob , name string ) error {
507+ func (r * BackupJobReconciler ) createVeleroBackup (ctx context.Context , backupJob * backupsv1alpha1.BackupJob , strategy * strategyv1alpha1. Velero ) error {
523508 logger := getLogger (ctx )
524- logger .Debug ("createVeleroBackup called" , "backupJob" , backupJob .Name , "veleroBackupName" , name )
525-
526- // Resolve StorageRef to get S3 credentials if it's a Bucket
527- // Prefix with namespace to avoid conflicts in cozy-velero namespace
528- var locationName string = fmt .Sprintf ("%s-%s" , backupJob .Namespace , backupJob .Name )
529- if backupJob .Spec .StorageRef .Kind == "Bucket" {
530- logger .Debug ("resolving Bucket storageRef" , "storageRef" , backupJob .Spec .StorageRef .Name )
531- creds , err := r .resolveBucketStorageRef (ctx , backupJob .Spec .StorageRef , backupJob .Namespace )
532- if err != nil {
533- logger .Error (err , "failed to resolve Bucket storageRef" )
534- return fmt .Errorf ("failed to resolve Bucket storageRef: %w" , err )
535- }
536-
537- logger .Debug ("discovered S3 credentials from Bucket storageRef" ,
538- "bucketName" , creds .BucketName ,
539- "endpoint" , creds .Endpoint ,
540- "region" , creds .Region )
509+ logger .Debug ("createVeleroBackup called" , "strategy" , strategy .Name )
541510
542- if err := r .createS3CredsForVelero (ctx , backupJob , creds ); err != nil {
543- return fmt .Errorf ("failed to create or update Velero credentials secret: %w" , err )
544- }
545- // Dynamically create a Velero BackupStorageLocation and VolumeSnapshotLocation using discovered credentials.
546-
547- // BackupStorageLocation manifest
548- // Note: Cannot set owner reference for cross-namespace resources
549- bsl := & velerov1.BackupStorageLocation {
550- ObjectMeta : metav1.ObjectMeta {
551- Name : locationName ,
552- Namespace : veleroNamespace ,
553- },
554- Spec : velerov1.BackupStorageLocationSpec {
555- Provider : "aws" ,
556- StorageType : velerov1.StorageType {
557- ObjectStorage : & velerov1.ObjectStorageLocation {
558- Bucket : creds .BucketName ,
559- },
560- },
561- Config : map [string ]string {
562- "checksumAlgorithm" : "" ,
563- "profile" : "default" ,
564- "s3ForcePathStyle" : "true" ,
565- "s3Url" : creds .Endpoint ,
566- },
567- Credential : & corev1.SecretKeySelector {
568- LocalObjectReference : corev1.LocalObjectReference {
569- Name : storageS3SecretName (backupJob .Namespace , backupJob .Name ),
570- },
571- Key : "cloud" ,
572- },
573- },
574- }
575-
576- // Create or update the BackupStorageLocation
577- if err := r .createBackupStorageLocation (ctx , bsl ); err != nil {
578- logger .Error (err , "failed to create or update BackupStorageLocation for Velero" )
579- r .Recorder .Event (backupJob , corev1 .EventTypeWarning , "BackupStorageLocationCreationFailed" ,
580- fmt .Sprintf ("Failed to create or update BackupStorageLocation %s/%s: %v" , veleroNamespace , locationName , err ))
581- return fmt .Errorf ("failed to create or update Velero BackupStorageLocation: %w" , err )
582- }
583- r .Recorder .Event (backupJob , corev1 .EventTypeNormal , "BackupStorageLocationCreated" ,
584- fmt .Sprintf ("Created or updated BackupStorageLocation %s/%s" , veleroNamespace , locationName ))
585-
586- // VolumeSnapshotLocation manifest
587- // Note: Cannot set owner reference for cross-namespace resources
588- vsl := & velerov1.VolumeSnapshotLocation {
589- ObjectMeta : metav1.ObjectMeta {
590- Name : locationName ,
591- Namespace : veleroNamespace ,
592- },
593- Spec : velerov1.VolumeSnapshotLocationSpec {
594- Provider : "aws" ,
595- Credential : & corev1.SecretKeySelector {
596- LocalObjectReference : corev1.LocalObjectReference {
597- Name : storageS3SecretName (backupJob .Namespace , backupJob .Name ),
598- },
599- Key : "cloud" ,
600- },
601- Config : map [string ]string {
602- "region" : creds .Region ,
603- "profile" : "default" ,
604- },
605- },
606- }
607-
608- // Create or update the VolumeSnapshotLocation
609- if err := r .createVolumeSnapshotLocation (ctx , vsl ); err != nil {
610- logger .Error (err , "failed to create or update VolumeSnapshotLocation for Velero" )
611- r .Recorder .Event (backupJob , corev1 .EventTypeWarning , "VolumeSnapshotLocationCreationFailed" ,
612- fmt .Sprintf ("Failed to create or update VolumeSnapshotLocation %s/%s: %v" , veleroNamespace , locationName , err ))
613- return fmt .Errorf ("failed to create or update Velero VolumeSnapshotLocation: %w" , err )
614- }
615- r .Recorder .Event (backupJob , corev1 .EventTypeNormal , "VolumeSnapshotLocationCreated" ,
616- fmt .Sprintf ("Created or updated VolumeSnapshotLocation %s/%s" , veleroNamespace , locationName ))
511+ mapping , err := r .RESTMapping (schema.GroupKind {Group : * backupJob .Spec .ApplicationRef .APIGroup , Kind : backupJob .Spec .ApplicationRef .Kind })
512+ if err != nil {
513+ return err
514+ }
515+ ns := backupJob .Namespace
516+ if mapping .Scope .Name () != meta .RESTScopeNameNamespace {
517+ ns = ""
617518 }
519+ app , err := r .Resource (mapping .Resource ).Namespace (ns ).Get (ctx , backupJob .Spec .ApplicationRef .Name , metav1.GetOptions {})
618520
619- // Create a Velero Backup (velero.io/v1) using typed object
620- // Now implemented only for backup of VirtualMachine resources
621- // Note: Cannot set owner reference for cross-namespace resources
521+ veleroBackupSpec , err := template .Template (& strategy .Spec .Template .Spec , app .Object )
522+ if err != nil {
523+ return err
524+ }
622525 veleroBackup := & velerov1.Backup {
623526 ObjectMeta : metav1.ObjectMeta {
624- Name : name ,
625- Namespace : veleroNamespace ,
626- },
627- Spec : velerov1.BackupSpec {
628- IncludedNamespaces : []string {backupJob .Namespace },
629- IncludedResources : []string {"virtualmachines.kubevirt.io" },
630- SnapshotVolumes : boolPtr (true ),
631- StorageLocation : locationName ,
632- VolumeSnapshotLocations : []string {locationName },
633- LabelSelector : & metav1.LabelSelector {
634- MatchLabels : map [string ]string {
635- "app.kubernetes.io/instance" : virtualMachinePrefix + backupJob .Spec .ApplicationRef .Name ,
636- },
527+ GenerateName : fmt .Sprintf ("%s.%s-" , backupJob .Namespace , backupJob .Name ),
528+ Namespace : veleroNamespace ,
529+ Labels : map [string ]string {
530+ backupsv1alpha1 .OwningJobNameLabel : backupJob .Name ,
531+ backupsv1alpha1 .OwningJobNamespaceLabel : backupJob .Namespace ,
637532 },
638533 },
534+ Spec : * veleroBackupSpec ,
639535 }
640-
536+ name := veleroBackup . GenerateName
641537 if err := r .Create (ctx , veleroBackup ); err != nil {
538+ if veleroBackup .Name != "" {
539+ name = veleroBackup .Name
540+ }
642541 logger .Error (err , "failed to create Velero Backup" , "name" , veleroBackup .Name )
643542 r .Recorder .Event (backupJob , corev1 .EventTypeWarning , "VeleroBackupCreationFailed" ,
644543 fmt .Sprintf ("Failed to create Velero Backup %s/%s: %v" , veleroNamespace , name , err ))
@@ -675,7 +574,7 @@ func (r *BackupJobReconciler) createBackupResource(ctx context.Context, backupJo
675574
676575 backup := & backupsv1alpha1.Backup {
677576 ObjectMeta : metav1.ObjectMeta {
678- Name : fmt .Sprintf ("%s-backup " , backupJob .Name ),
577+ Name : fmt .Sprintf ("%s" , backupJob .Name ),
679578 Namespace : backupJob .Namespace ,
680579 OwnerReferences : []metav1.OwnerReference {
681580 {
0 commit comments