-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation for the restore queue
Adds the estimated queue to the NAB restore object Signed-off-by: Michal Pryc <mpryc@redhat.com>
- Loading branch information
Showing
8 changed files
with
274 additions
and
6 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
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 handler contains all event handlers of the project | ||
package handler | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/util/workqueue" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/migtools/oadp-non-admin/internal/common/constant" | ||
"github.com/migtools/oadp-non-admin/internal/common/function" | ||
) | ||
|
||
// VeleroRestoreQueueHandler contains event handlers for Velero Restore objects | ||
type VeleroRestoreQueueHandler struct { | ||
Client client.Client | ||
OADPNamespace string | ||
} | ||
|
||
// Create event handler | ||
func (VeleroRestoreQueueHandler) Create(_ context.Context, _ event.CreateEvent, _ workqueue.RateLimitingInterface) { | ||
// Create event handler for the Restore object | ||
} | ||
|
||
// Update event handler adds Velero Restore's NonAdminRestore to controller queue | ||
func (h VeleroRestoreQueueHandler) Update(ctx context.Context, evt event.UpdateEvent, q workqueue.RateLimitingInterface) { | ||
// Only update to the first in the queue Velero Restore should trigger changes to the | ||
// NonAdminRestore objects. Updates to the Velero Restore 2nd and 3rd does not lower the | ||
// queue. This optimizes the number of times we need to update the NonAdminRestore objects | ||
// and the number of Velero Restore objects we need to react on. | ||
|
||
logger := function.GetLogger(ctx, evt.ObjectNew, "VeleroBackupQueueHandler") | ||
|
||
// Fetching Velero Restores triggered by NonAdminRestore to optimize our reconcile cycles | ||
restores, err := function.GetActiveVeleroRestoresByLabel(ctx, h.Client, h.OADPNamespace, constant.ManagedByLabel, constant.ManagedByLabelValue) | ||
if err != nil { | ||
logger.Error(err, "Failed to get Velero Restores by label") | ||
return | ||
} | ||
|
||
if restores == nil { | ||
// That should't really be the case as our Update event was triggered by a Velero Restore | ||
// object that has a new CompletionTimestamp. | ||
logger.V(1).Info("No pending velero restores found in namespace.", constant.NamespaceString, h.OADPNamespace) | ||
} else { | ||
nabEventAnnotations := evt.ObjectNew.GetAnnotations() | ||
nabEventOriginNamespace := nabEventAnnotations[constant.NabOriginNamespaceAnnotation] | ||
nabEventOriginName := nabEventAnnotations[constant.NabOriginNameAnnotation] | ||
|
||
for _, restore := range restores { | ||
annotations := restore.GetAnnotations() | ||
nabOriginNamespace := annotations[constant.NabOriginNamespaceAnnotation] | ||
nabOriginName := annotations[constant.NabOriginNameAnnotation] | ||
|
||
// This object is within current queue, so there is no need to trigger changes to it. | ||
// The VeleroBackupHandler will serve for that. | ||
if nabOriginNamespace != nabEventOriginNamespace || nabOriginName != nabEventOriginName { | ||
logger.V(1).Info("Processing Queue update for the NonAdmin Restore referenced by Velero Restore", "Name", restore.Name, constant.NamespaceString, restore.Namespace, "CreatedAt", restore.CreationTimestamp) | ||
q.Add(reconcile.Request{NamespacedName: types.NamespacedName{ | ||
Name: nabOriginName, | ||
Namespace: nabOriginNamespace, | ||
}}) | ||
} else { | ||
logger.V(1).Info("Ignoring Queue update for the NonAdmin Restore that triggered this event", "Name", restore.Name, constant.NamespaceString, restore.Namespace, "CreatedAt", restore.CreationTimestamp) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Delete event handler | ||
func (VeleroRestoreQueueHandler) Delete(_ context.Context, _ event.DeleteEvent, _ workqueue.RateLimitingInterface) { | ||
// Delete event handler for the Restore object | ||
} | ||
|
||
// Generic event handler | ||
func (VeleroRestoreQueueHandler) Generic(_ context.Context, _ event.GenericEvent, _ workqueue.RateLimitingInterface) { | ||
// Generic event handler for the Restore object | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
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 predicate | ||
|
||
import ( | ||
"context" | ||
|
||
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/event" | ||
|
||
"github.com/migtools/oadp-non-admin/internal/common/function" | ||
) | ||
|
||
// VeleroRestoreQueuePredicate contains event filters for Velero Restore objects | ||
type VeleroRestoreQueuePredicate struct { | ||
OADPNamespace string | ||
} | ||
|
||
// Update event filter only accepts Velero Restore update events from the OADP namespace | ||
// and from Velero Restores that have a new CompletionTimestamp. We are not interested in | ||
// checking if the Velero Restore contains NonAdminRestore metadata, because every Velero Restore | ||
// may change the Queue position of the NonAdminRestore object. | ||
func (p VeleroRestoreQueuePredicate) Update(ctx context.Context, evt event.UpdateEvent) bool { | ||
logger := function.GetLogger(ctx, evt.ObjectNew, "VeleroRestoreQueuePredicate") | ||
|
||
// Ensure the new and old objects are of the expected type | ||
newRestore, okNew := evt.ObjectNew.(*velerov1.Restore) | ||
oldRestore, okOld := evt.ObjectOld.(*velerov1.Restore) | ||
|
||
if !okNew || !okOld { | ||
logger.V(1).Info("Rejected Restore Update event: invalid object type") | ||
return false | ||
} | ||
|
||
namespace := newRestore.GetNamespace() | ||
|
||
if namespace == p.OADPNamespace { | ||
if oldRestore.Status.CompletionTimestamp == nil && newRestore.Status.CompletionTimestamp != nil { | ||
logger.V(1).Info("Accepted Restore Update event: new completion timestamp") | ||
return true | ||
} | ||
} | ||
|
||
logger.V(1).Info("Rejected Restore Update event: no changes to the CompletionTimestamp in the VeleroRestore object") | ||
return false | ||
} |