Skip to content

Commit

Permalink
Enhancements to support multiple DRPC in a NS for epic-6681
Browse files Browse the repository at this point in the history
Signed-off-by: pruthvitd <[email protected]>
  • Loading branch information
pruthvitd committed Feb 14, 2025
1 parent 0fd2352 commit d176819
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 7 deletions.
78 changes: 78 additions & 0 deletions internal/controller/api/hub_managed_cluster_apis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// SPDX-FileCopyrightText: The RamenDR authors
// SPDX-License-Identifier: Apache-2.0

package api

import (
// "context"
// "encoding/json"
// "fmt"
// "os"
// "strings"

// "github.com/go-logr/logr"
// ramen "github.com/ramendr/ramen/api/v1alpha1"
// "github.com/ramendr/ramen/internal/controller/kubeobjects"
// "github.com/ramendr/ramen/internal/controller/util"
// recipe "github.com/ramendr/recipe/api/v1alpha1"
// "golang.org/x/exp/slices"
// "k8s.io/apimachinery/pkg/types"
// "k8s.io/apimachinery/pkg/util/sets"
// "sigs.k8s.io/controller-runtime/pkg/builder"
// "sigs.k8s.io/controller-runtime/pkg/client"
// "sigs.k8s.io/controller-runtime/pkg/handler"
// "sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
VMRecipeName = "vm-recipe"
VMRecipe=`
apiVersion: ramendr.openshift.io/v1alpha1
kind: Recipe
metadata:
name: vm-recipe
namespace: ramen-ops
spec:
appType: vm-dv
groups:
- backupRef: vm-dv
excludedResourceTypes:
- events
- event.events.k8s.io
- persistentvolumes
- replicaset
- persistentvolumeclaims
- pods
includedNamespaces:
- vm-dv
labelSelector:
matchExpressions:
- key: appname
operator: In
values:
- vm
name: vm-dv
type: resource
workflows:
- failOn: any-error
name: backup
sequence:
- group: vm-dv
- failOn: any-error
name: restore
sequence:
- group: vm-dv
volumes:
includedNamespaces:
- vm-dv
name: varlog
type: volume
labelSelector:
matchExpressions:
- key: appname
operator: In
values:
- vm
`
)

29 changes: 24 additions & 5 deletions internal/controller/drplacementcontrol_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
rmn "github.com/ramendr/ramen/api/v1alpha1"
argocdv1alpha1hack "github.com/ramendr/ramen/internal/controller/argocd"
rmnutil "github.com/ramendr/ramen/internal/controller/util"
rmnapi "github.com/ramendr/ramen/internal/controller/api"
"github.com/ramendr/ramen/internal/controller/volsync"
clrapiv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
)
Expand Down Expand Up @@ -2421,7 +2422,6 @@ func (r *DRPlacementControlReconciler) ensureNoConflictingDRPCs(ctx context.Cont

for i := range drpcList.Items {
otherDRPC := &drpcList.Items[i]

// Skip the drpc itself
if otherDRPC.Name == drpc.Name && otherDRPC.Namespace == drpc.Namespace {
continue
Expand Down Expand Up @@ -2466,10 +2466,13 @@ func (r *DRPlacementControlReconciler) twoDRPCsConflict(ctx context.Context,
return fmt.Errorf("failed to get protected namespaces for drpc: %v, %w", otherDRPC.Name, err)
}

conflict := drpcsProtectCommonNamespace(drpcProtectedNamespaces, otherDRPCProtectedNamespaces)
if conflict {
return fmt.Errorf("drpc: %s and drpc: %s protect the same namespace",
drpc.Name, otherDRPC.Name)
potentialConflict := drpcsProtectCommonNamespace(drpcProtectedNamespaces, otherDRPCProtectedNamespaces)
if potentialConflict {
independentVMProtection := drpcProtectVMInNS(drpc, otherDRPC, ramenConfig, log)
if !independentVMProtection {
return fmt.Errorf("drpc: %s and drpc: %s protect common resources from the same namespace",
drpc.Name, otherDRPC.Name)
}
}

return nil
Expand Down Expand Up @@ -2499,3 +2502,19 @@ func (r *DRPlacementControlReconciler) drpcHaveCommonClusters(ctx context.Contex

return drpolicyClusters.Intersection(otherDrpolicyClusters).Len() > 0, nil
}

func drpcProtectVMInNS(drpc *rmn.DRPlacementControl, otherdrpc *rmn.DRPlacementControl, ramenConfig *rmn.RamenConfig, log logr.Logger) bool {
log.Info("In DRPC Protect VM in NS Validation")
if (drpc.Spec.KubeObjectProtection.RecipeRef.Name == rmnapi.VMRecipeName && otherdrpc.Spec.KubeObjectProtection.RecipeRef.Name == rmnapi.VMRecipeName) {
log.Info("It could be Independent VM protection.")
ramenOpsNS:=RamenOperandsNamespace(*ramenConfig)
log.Info("Ramen Ops namespace is : "+ramenOpsNS)
if (drpc.Spec.KubeObjectProtection.RecipeRef.Namespace == ramenOpsNS && otherdrpc.Spec.KubeObjectProtection.RecipeRef.Namespace == ramenOpsNS) {
log.Info("Its a valid Independent VM protection.")
return true

Check failure on line 2514 in internal/controller/drplacementcontrol_controller.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

return with no blank line before (nlreturn)
}
}

log.Info("It isn't a valid Independent VM protection.")
return false

Check failure on line 2519 in internal/controller/drplacementcontrol_controller.go

View workflow job for this annotation

GitHub Actions / Golangci Lint (.)

return with no blank line before (nlreturn)
}
12 changes: 10 additions & 2 deletions internal/controller/vrg_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
ramen "github.com/ramendr/ramen/api/v1alpha1"
"github.com/ramendr/ramen/internal/controller/kubeobjects"
"github.com/ramendr/ramen/internal/controller/util"
rmnapi "github.com/ramendr/ramen/internal/controller/api"
recipe "github.com/ramendr/recipe/api/v1alpha1"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -22,6 +23,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/yaml"
)

type RecipeElements struct {
Expand Down Expand Up @@ -116,8 +118,14 @@ func RecipeElementsGet(ctx context.Context, reader client.Reader, vrg ramen.Volu
}

recipe := recipe.Recipe{}
if err := reader.Get(ctx, recipeNamespacedName, &recipe); err != nil {
return recipeElements, fmt.Errorf("recipe %v get error: %w", recipeNamespacedName.String(), err)
if vrg.Spec.KubeObjectProtection.RecipeRef.Namespace == RamenOperandsNamespace(ramenConfig) && vrg.Spec.KubeObjectProtection.RecipeRef.Name==rmnapi.VMRecipeName {
if err:=yaml.Unmarshal([]byte(rmnapi.VMRecipe), &recipe); err != nil {
return recipeElements, fmt.Errorf("recipe %v get error: %w", recipeNamespacedName.String(), err)
}
} else {
if err := reader.Get(ctx, recipeNamespacedName, &recipe); err != nil {
return recipeElements, fmt.Errorf("recipe %v get error: %w", recipeNamespacedName.String(), err)
}
}

if err := RecipeParametersExpand(&recipe, vrg.Spec.KubeObjectProtection.RecipeParameters, log); err != nil {
Expand Down

0 comments on commit d176819

Please sign in to comment.