Skip to content

Commit

Permalink
Store application namespace as an annotation
Browse files Browse the repository at this point in the history
This helps when an ApplicationSet is deleted prior to actual
drpc deletion, as the application namespace, and VRG naming
using the same, can continue to find the required resources to
delete and cleanup.

Signed-off-by: Shyamsundar Ranganathan <[email protected]>
  • Loading branch information
ShyamsundarR authored and BenamarMk committed Oct 12, 2023
1 parent c3e57ee commit 24f81bb
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
3 changes: 3 additions & 0 deletions controllers/drplacementcontrol.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const (

// Annotation for the last cluster on which the application was running
LastAppDeploymentCluster = "drplacementcontrol.ramendr.openshift.io/last-app-deployment-cluster"

// Annotation for application namespace on the managed cluster
DRPCAppNamespace = "drplacementcontrol.ramendr.openshift.io/app-namespace"
)

var (
Expand Down
29 changes: 21 additions & 8 deletions controllers/drplacementcontrol_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,23 +923,32 @@ func (r *DRPlacementControlReconciler) getDRPolicy(ctx context.Context,
return drPolicy, nil
}

func (r DRPlacementControlReconciler) addLabelsAndFinalizers(ctx context.Context,
// updateObjectMetadata updates drpc labels, annotations and finalizer, and also updates placementObj finalizer
func (r DRPlacementControlReconciler) updateObjectMetadata(ctx context.Context,
drpc *rmn.DRPlacementControl, placementObj client.Object, log logr.Logger,
) error {
// add label and finalizer to DRPC
labelAdded := rmnutil.AddLabel(drpc, rmnutil.OCMBackupLabelKey, rmnutil.OCMBackupLabelValue)
finalizerAdded := rmnutil.AddFinalizer(drpc, DRPCFinalizer)
update := false

if labelAdded || finalizerAdded {
update = rmnutil.AddLabel(drpc, rmnutil.OCMBackupLabelKey, rmnutil.OCMBackupLabelValue)
update = rmnutil.AddFinalizer(drpc, DRPCFinalizer) || update

vrgNamespace, err := selectVRGNamespace(r.Client, r.Log, drpc, placementObj)
if err != nil {
return err
}

update = rmnutil.AddAnnotation(drpc, DRPCAppNamespace, vrgNamespace) || update

if update {
if err := r.Update(ctx, drpc); err != nil {
log.Error(err, "Failed to add label and finalizer to drpc")
log.Error(err, "Failed to add annotations, labels, or finalizer to drpc")

return fmt.Errorf("%w", err)
}
}

// add finalizer to User PlacementRule/Placement
finalizerAdded = rmnutil.AddFinalizer(placementObj, DRPCFinalizer)
finalizerAdded := rmnutil.AddFinalizer(placementObj, DRPCFinalizer)
if finalizerAdded {
if err := r.Update(ctx, placementObj); err != nil {
log.Error(err, "Failed to add finalizer to user placement rule")
Expand Down Expand Up @@ -1147,7 +1156,7 @@ func (r *DRPlacementControlReconciler) updateAndSetOwner(
return false, err
}

if err := r.addLabelsAndFinalizers(ctx, drpc, usrPlacement, log); err != nil {
if err := r.updateObjectMetadata(ctx, drpc, usrPlacement, log); err != nil {
return false, err
}

Expand Down Expand Up @@ -1960,6 +1969,10 @@ func selectVRGNamespace(
drpc *rmn.DRPlacementControl,
placementObj client.Object,
) (string, error) {
if drpc.GetAnnotations() != nil && drpc.GetAnnotations()[DRPCAppNamespace] != "" {
return drpc.GetAnnotations()[DRPCAppNamespace], nil
}

switch placementObj.(type) {
case *clrapiv1beta1.Placement:
vrgNamespace, err := getApplicationDestinationNamespace(client, log, placementObj)
Expand Down
1 change: 1 addition & 0 deletions controllers/drplacementcontrol_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,7 @@ func verifyInitialDRPCDeployment(userPlacement client.Object, preferredCluster s
_, condition := getDRPCCondition(&latestDRPC.Status, rmn.ConditionAvailable)
Expect(condition.Reason).To(Equal(string(rmn.Deployed)))
Expect(latestDRPC.GetAnnotations()[controllers.LastAppDeploymentCluster]).To(Equal(preferredCluster))
Expect(latestDRPC.GetAnnotations()[controllers.DRPCAppNamespace]).To(Equal(getVRGNameSpace()))
}

func verifyFailoverToSecondary(placementObj client.Object, toCluster string,
Expand Down
18 changes: 18 additions & 0 deletions controllers/util/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,24 @@ func AddLabel(obj client.Object, key, value string) bool {
return !labelAdded
}

func AddAnnotation(obj client.Object, key, value string) bool {
const added = true

annotations := obj.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}

if keyValue, ok := annotations[key]; !ok || keyValue != value {
annotations[key] = value
obj.SetAnnotations(annotations)

return added
}

return !added
}

func AddFinalizer(obj client.Object, finalizer string) bool {
const finalizerAdded = true

Expand Down

0 comments on commit 24f81bb

Please sign in to comment.