Skip to content
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
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"flag"
"fmt"
"os"
"time"

// TODO when to update oadp-operator version in go.mod?
"github.com/openshift/oadp-operator/api/v1alpha1"
Expand Down Expand Up @@ -174,6 +175,16 @@ func main() {
os.Exit(1)
}
// +kubebuilder:scaffold:builder
if err = (&controller.NonAdminBackupSynchronizerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OADPNamespace: oadpNamespace,
// TODO user input
SyncPeriod: 5 * time.Minute,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup NonAdminBackupSynchronizer controller with manager")
os.Exit(1)
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand Down
1 change: 1 addition & 0 deletions internal/common/constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const (
NabOriginNACUUIDLabel = v1alpha1.OadpOperatorLabel + "-nab-origin-nacuuid"
NarOriginNACUUIDLabel = v1alpha1.OadpOperatorLabel + "-nar-origin-nacuuid"
NabslOriginNACUUIDLabel = v1alpha1.OadpOperatorLabel + "-nabsl-origin-nacuuid"
NabSyncLabel = v1alpha1.OadpOperatorLabel + "-nab-synced-from-nacuuid"

NabOriginNameAnnotation = v1alpha1.OadpOperatorLabel + "-nab-origin-name"
NabOriginNamespaceAnnotation = v1alpha1.OadpOperatorLabel + "-nab-origin-namespace"
Expand Down
17 changes: 16 additions & 1 deletion internal/controller/nonadminbackup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
return ctrl.Result{}, err
}

_, syncBackup := nab.Labels[constant.NabSyncLabel]

// Determine which path to take
var reconcileSteps []nonAdminBackupReconcileStepFunction

Expand Down Expand Up @@ -117,6 +119,14 @@ func (r *NonAdminBackupReconciler) Reconcile(ctx context.Context, req ctrl.Reque
r.removeNabFinalizerUponVeleroBackupDeletion,
}

case syncBackup:
logger.V(1).Info("Executing nab sync path")
reconcileSteps = []nonAdminBackupReconcileStepFunction{
r.setBackupUUIDInStatus,
r.setFinalizerOnNonAdminBackup,
r.createVeleroBackupAndSyncWithNonAdminBackup,
}

default:
// Standard creation/update path
logger.V(1).Info("Executing nab creation/update path")
Expand Down Expand Up @@ -555,7 +565,12 @@ func (r *NonAdminBackupReconciler) setBackupUUIDInStatus(ctx context.Context, lo
}

if nab.Status.VeleroBackup == nil || nab.Status.VeleroBackup.NACUUID == constant.EmptyString {
veleroBackupNACUUID := function.GenerateNacObjectUUID(nab.Namespace, nab.Name)
var veleroBackupNACUUID string
if value, ok := nab.Labels[constant.NabSyncLabel]; ok {
veleroBackupNACUUID = value
} else {
veleroBackupNACUUID = function.GenerateNacObjectUUID(nab.Namespace, nab.Name)
}
nab.Status.VeleroBackup = &nacv1alpha1.VeleroBackup{
NACUUID: veleroBackupNACUUID,
Namespace: r.OADPNamespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type naBSLReconcileStepFunction func(ctx context.Context, logger logr.Logger, na
// move the current state of the cluster closer to the desired state.
func (r *NonAdminBackupStorageLocationReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)
logger.V(1).Info("NonAdminBackup Reconcile start")
logger.V(1).Info("NonAdminBackupStorageLocation Reconcile start")

// Get the NonAdminBackupStorageLocation object
nabsl := &nacv1alpha1.NonAdminBackupStorageLocation{}
Expand Down Expand Up @@ -123,7 +123,7 @@ func (r *NonAdminBackupStorageLocationReconciler) Reconcile(ctx context.Context,
}
}

logger.V(1).Info("NonAdminBackup Reconcile exit")
logger.V(1).Info("NonAdminBackupStorageLocation Reconcile exit")
return ctrl.Result{}, nil
}

Expand Down
168 changes: 168 additions & 0 deletions internal/controller/nonadminbackupsynchronizer_controller.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write tests

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) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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{}).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discuss what events to watch

Complete(r)
}