-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: NonAdminBackup sync controller #138
Open
mateusoliveira43
wants to merge
4
commits into
migtools:master
Choose a base branch
from
mateusoliveira43:feat/non-admin-backup-sync-controller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f9713c5
feat: NonAdminBackup sync controller
mateusoliveira43 db30aeb
fixup! feat: NonAdminBackup sync controller
mateusoliveira43 073e532
fixup! feat: NonAdminBackup sync controller
mateusoliveira43 3732cbe
fixup! feat: NonAdminBackup sync controller
mateusoliveira43 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
168 changes: 168 additions & 0 deletions
168
internal/controller/nonadminbackupsynchronizer_controller.go
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,168 @@ | ||
/* | ||
Copyright 2024. | ||
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 controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
"time" | ||
|
||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
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/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
|
||
nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1" | ||
"github.com/migtools/oadp-non-admin/internal/common/constant" | ||
"github.com/migtools/oadp-non-admin/internal/common/function" | ||
) | ||
|
||
// NonAdminBackupSynchronizerReconciler reconciles BackupStorageLocation objects | ||
type NonAdminBackupSynchronizerReconciler struct { | ||
client.Client | ||
Scheme *runtime.Scheme | ||
OADPNamespace string | ||
SyncPeriod time.Duration | ||
} | ||
|
||
var previousBackupSyncRun *time.Time | ||
|
||
// Reconcile is part of the main kubernetes reconciliation loop which aims to | ||
// move the current state of the cluster closer to the desired state. | ||
func (r *NonAdminBackupSynchronizerReconciler) Reconcile(ctx context.Context, _ ctrl.Request) (ctrl.Result, error) { | ||
logger := log.FromContext(ctx) | ||
|
||
labelSelector := client.MatchingLabels(function.GetNonAdminLabels()) | ||
|
||
if previousBackupSyncRun == nil || time.Now().After(previousBackupSyncRun.Add(r.SyncPeriod)) { | ||
previousBackupSyncRun = ptr.To(time.Now()) | ||
logger.V(1).Info("NonAdminBackup Synchronization start") | ||
|
||
veleroBackupStorageLocationList := &velerov1.BackupStorageLocationList{} | ||
if err := r.List(ctx, veleroBackupStorageLocationList, client.InNamespace(r.OADPNamespace)); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
var watchedBackupStorageLocations []string | ||
relatedNonAdminBackupStorageLocations := map[string]string{} | ||
for _, backupStorageLocation := range veleroBackupStorageLocationList.Items { | ||
if backupStorageLocation.Spec.Default { | ||
watchedBackupStorageLocations = append(watchedBackupStorageLocations, backupStorageLocation.Name) | ||
} | ||
if function.CheckVeleroBackupStorageLocationMetadata(&backupStorageLocation) { | ||
err := r.Get(ctx, types.NamespacedName{ | ||
Name: backupStorageLocation.Annotations[constant.NabslOriginNameAnnotation], | ||
Namespace: backupStorageLocation.Annotations[constant.NabslOriginNamespaceAnnotation], | ||
}, &nacv1alpha1.NonAdminBackupStorageLocation{}) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
continue | ||
} | ||
logger.Error(err, "Unable to fetch NonAdminBackupStorageLocation") | ||
return ctrl.Result{}, err | ||
} | ||
watchedBackupStorageLocations = append(watchedBackupStorageLocations, backupStorageLocation.Name) | ||
relatedNonAdminBackupStorageLocations[backupStorageLocation.Name] = backupStorageLocation.Annotations[constant.NabslOriginNameAnnotation] | ||
} | ||
} | ||
|
||
veleroBackupList := &velerov1.BackupList{} | ||
if err := r.List(ctx, veleroBackupList, client.InNamespace(r.OADPNamespace), labelSelector); err != nil { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
var backupsToSync []velerov1.Backup | ||
var possibleBackupsToSync []velerov1.Backup | ||
for _, backup := range veleroBackupList.Items { | ||
if backup.Status.CompletionTimestamp != nil && | ||
slices.Contains(watchedBackupStorageLocations, backup.Spec.StorageLocation) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check non admin annotations |
||
possibleBackupsToSync = append(possibleBackupsToSync, backup) | ||
} | ||
} | ||
logger.V(1).Info(fmt.Sprintf("%v possible Backup(s) to be synced to NonAdmin namespaces", len(possibleBackupsToSync))) | ||
|
||
for _, backup := range possibleBackupsToSync { | ||
err := r.Get(ctx, types.NamespacedName{ | ||
Name: backup.Annotations[constant.NabOriginNamespaceAnnotation], | ||
}, &corev1.Namespace{}) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
continue | ||
} | ||
logger.Error(err, "Unable to fetch Namespace") | ||
return ctrl.Result{}, err | ||
} | ||
|
||
err = r.Get(ctx, types.NamespacedName{ | ||
Namespace: backup.Annotations[constant.NabOriginNamespaceAnnotation], | ||
Name: backup.Annotations[constant.NabOriginNameAnnotation], | ||
}, &nacv1alpha1.NonAdminBackup{}) | ||
if err != nil { | ||
if apierrors.IsNotFound(err) { | ||
backupsToSync = append(backupsToSync, backup) | ||
continue | ||
} | ||
logger.Error(err, "Unable to fetch NonAdminBackup") | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
logger.V(1).Info(fmt.Sprintf("%v Backup(s) to sync to NonAdmin namespaces", len(backupsToSync))) | ||
for _, backup := range backupsToSync { | ||
nab := &nacv1alpha1.NonAdminBackup{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: backup.Annotations[constant.NabOriginNameAnnotation], | ||
Namespace: backup.Annotations[constant.NabOriginNamespaceAnnotation], | ||
// TODO sync operation does not preserve labels | ||
Labels: map[string]string{ | ||
constant.NabSyncLabel: backup.Labels[constant.NabOriginNACUUIDLabel], | ||
}, | ||
}, | ||
Spec: nacv1alpha1.NonAdminBackupSpec{ | ||
BackupSpec: &backup.Spec, | ||
}, | ||
} | ||
value, exist := relatedNonAdminBackupStorageLocations[nab.Spec.BackupSpec.StorageLocation] | ||
if exist { | ||
nab.Spec.BackupSpec.StorageLocation = value | ||
} | ||
err := r.Create(ctx, nab) | ||
if err != nil { | ||
logger.Error(err, "Failed to create NonAdminBackup") | ||
return ctrl.Result{}, err | ||
} | ||
} | ||
|
||
logger.V(1).Info("NonAdminBackup Synchronization exit") | ||
return ctrl.Result{RequeueAfter: r.SyncPeriod}, nil | ||
} | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
// SetupWithManager sets up the controller with the Manager. | ||
func (r *NonAdminBackupSynchronizerReconciler) SetupWithManager(mgr ctrl.Manager) error { | ||
return ctrl.NewControllerManagedBy(mgr). | ||
For(&velerov1.BackupStorageLocation{}). | ||
// WithEventFilter(predicate.GarbageCollectorPredicate{}). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. discuss what events to watch |
||
Complete(r) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write tests