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

Bug 2239140: Store application namespace as an annotation #148

Merged
merged 1 commit into from
Oct 12, 2023
Merged
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
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