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

Volumes in recipes should be used to override LabelSelector and ignored for velero backups #1739

Merged
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
7 changes: 4 additions & 3 deletions internal/controller/volumereplicationgroup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,10 @@ func (v *VRGInstance) processVRG() ctrl.Result {
}
}

if err := RecipeElementsGet(
v.ctx, v.reconciler.Client, *v.instance, *v.ramenConfig, v.log, &v.recipeElements,
); err != nil {
var err error

v.recipeElements, err = RecipeElementsGet(v.ctx, v.reconciler.Client, *v.instance, *v.ramenConfig, v.log)
if err != nil {
return v.invalid(err, "Failed to get recipe", false)
}

Expand Down
25 changes: 25 additions & 0 deletions internal/controller/vrg_kubeobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,12 @@ func getCaptureGroups(recipe Recipe.Recipe) ([]kubeobjects.CaptureSpec, error) {
for resourceType, resourceName := range resource {
captureInstance, err := getResourceAndConvertToCaptureGroup(recipe, resourceType, resourceName)
if err != nil {
if errors.Is(err, ErrVolumeCaptureNotSupported) {
// we only use the volumes group for determining the label selector
// ignore it in the capture sequence
continue
}

return resources, err
}

Expand Down Expand Up @@ -852,6 +858,12 @@ func getRecoverGroups(recipe Recipe.Recipe) ([]kubeobjects.RecoverSpec, error) {
for resourceType, resourceName := range resource {
captureInstance, err := getResourceAndConvertToRecoverGroup(recipe, resourceType, resourceName)
if err != nil {
if errors.Is(err, ErrVolumeRecoverNotSupported) {
// we only use the volumes group for determining the label selector
// ignore it in the capture sequence
continue
}

return resources, err
}

Expand All @@ -862,6 +874,11 @@ func getRecoverGroups(recipe Recipe.Recipe) ([]kubeobjects.RecoverSpec, error) {
return resources, nil
}

var (
ErrVolumeCaptureNotSupported = errors.New("volume capture not supported")
ErrVolumeRecoverNotSupported = errors.New("volume recover not supported")
)

func getResourceAndConvertToCaptureGroup(
recipe Recipe.Recipe, resourceType, name string) (*kubeobjects.CaptureSpec, error,
) {
Expand All @@ -873,6 +890,10 @@ func getResourceAndConvertToCaptureGroup(
}
}

if name == recipe.Spec.Volumes.Name {
return nil, ErrVolumeCaptureNotSupported
}

return nil, k8serrors.NewNotFound(schema.GroupResource{Resource: "Recipe.Spec.Group.Name"}, name)
}

Expand Down Expand Up @@ -904,6 +925,10 @@ func getResourceAndConvertToRecoverGroup(
}
}

if name == recipe.Spec.Volumes.Name {
return nil, ErrVolumeRecoverNotSupported
}

return nil, k8serrors.NewNotFound(schema.GroupResource{Resource: "Recipe.Spec.Group.Name"}, name)
}

Expand Down
18 changes: 11 additions & 7 deletions internal/controller/vrg_pvc_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@ var _ = Describe("VolumeReplicationGroupPVCSelector", func() {
testCtx, cancel = context.WithCancel(context.TODO())
Expect(k8sClient).NotTo(BeNil())
vrgTestNamespace = createUniqueNamespace(testCtx)
ramenConfig.RamenOpsNamespace = vrgTestNamespace
})

AfterEach(func() {
Expect(k8sClient.Delete(testCtx, testNamespace)).To(Succeed())
ramenConfig.RamenOpsNamespace = ""

cancel()
})
Expand Down Expand Up @@ -164,8 +166,9 @@ func getBaseVRG(namespace string) *ramen.VolumeReplicationGroup {
Async: &ramen.VRGAsyncSpec{
SchedulingInterval: "5m",
},
ReplicationState: ramen.Primary,
S3Profiles: []string{"dummy-s3-profile"},
ReplicationState: ramen.Primary,
S3Profiles: []string{"dummy-s3-profile"},
ProtectedNamespaces: &[]string{namespace},
},
}
}
Expand Down Expand Up @@ -200,12 +203,13 @@ func getVRGDefinitionWithKubeObjectProtection(hasPVCSelectorLabels bool, namespa
return vrg
}

func getTestHook() *Recipe.Hook {
func getTestHook(testNamespace string) *Recipe.Hook {
duration := 30

return &Recipe.Hook{
Name: "hook-single",
Type: "exec",
Name: "hook-single",
Type: "exec",
Namespace: testNamespace,
LabelSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"myapp": "testapp",
Expand Down Expand Up @@ -267,7 +271,7 @@ func getRecipeDefinition(namespace string) *Recipe.Recipe {
Spec: Recipe.RecipeSpec{
Groups: []*Recipe.Group{getTestGroup()},
Volumes: getTestVolumeGroup(),
Hooks: []*Recipe.Hook{getTestHook()},
Hooks: []*Recipe.Hook{getTestHook(namespace)},
Workflows: []*Recipe.Workflow{
{
Name: "backup",
Expand All @@ -279,7 +283,7 @@ func getRecipeDefinition(namespace string) *Recipe.Recipe {
"group": "test-group",
},
{
"hook": "test-hook",
"hook": "hook-single/checkpoint",
},
},
},
Expand Down
49 changes: 22 additions & 27 deletions internal/controller/vrg_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,44 +79,35 @@ func GetPVCSelector(ctx context.Context, reader client.Reader, vrg ramen.VolumeR
ramenConfig ramen.RamenConfig,
log logr.Logger,
) (PvcSelector, error) {
var recipeElements RecipeElements
recipeElements, err := RecipeElementsGet(ctx, reader, vrg, ramenConfig, log)
if err != nil {
return recipeElements.PvcSelector, err
Copy link
Member

Choose a reason for hiding this comment

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

(nit) This line is a bit confusing. It references returning recipeElements.PvcSelector on an error, but since filterPVC() skips PvcSelector in case of an error, I suggest returning PvcSelector{} instead. This would improve readability.

Copy link
Member Author

Choose a reason for hiding this comment

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

I merged this PR and fixed this nit in the next PR #1738

}

return recipeElements.PvcSelector, recipeVolumesAndOptionallyWorkflowsGet(
ctx, reader, vrg, ramenConfig, log, &recipeElements,
func(recipe.Recipe, *RecipeElements, ramen.VolumeReplicationGroup, ramen.RamenConfig) error {
return nil
},
)
return recipeElements.PvcSelector, nil
}

func RecipeElementsGet(ctx context.Context, reader client.Reader, vrg ramen.VolumeReplicationGroup,
ramenConfig ramen.RamenConfig, log logr.Logger, recipeElements *RecipeElements,
) error {
return recipeVolumesAndOptionallyWorkflowsGet(ctx, reader, vrg, ramenConfig, log, recipeElements,
recipeWorkflowsGet,
)
}
ramenConfig ramen.RamenConfig, log logr.Logger,
) (RecipeElements, error) {
var recipeElements RecipeElements

func recipeVolumesAndOptionallyWorkflowsGet(ctx context.Context, reader client.Reader, vrg ramen.VolumeReplicationGroup,
ramenConfig ramen.RamenConfig, log logr.Logger, recipeElements *RecipeElements,
workflowsGet func(recipe.Recipe, *RecipeElements, ramen.VolumeReplicationGroup, ramen.RamenConfig) error,
) error {
if vrg.Spec.KubeObjectProtection == nil {
*recipeElements = RecipeElements{
recipeElements = RecipeElements{
PvcSelector: getPVCSelector(vrg, ramenConfig, nil, nil),
}

return nil
return recipeElements, nil
}

if vrg.Spec.KubeObjectProtection.RecipeRef == nil {
*recipeElements = RecipeElements{
recipeElements = RecipeElements{
PvcSelector: getPVCSelector(vrg, ramenConfig, nil, nil),
CaptureWorkflow: captureWorkflowDefault(vrg, ramenConfig),
RecoverWorkflow: recoverWorkflowDefault(vrg, ramenConfig),
}

return nil
return recipeElements, nil
}

recipeNamespacedName := types.NamespacedName{
Expand All @@ -126,11 +117,11 @@ func recipeVolumesAndOptionallyWorkflowsGet(ctx context.Context, reader client.R

recipe := recipe.Recipe{}
if err := reader.Get(ctx, recipeNamespacedName, &recipe); err != nil {
return fmt.Errorf("recipe %v get error: %w", recipeNamespacedName.String(), err)
return recipeElements, fmt.Errorf("recipe %v get error: %w", recipeNamespacedName.String(), err)
}

if err := RecipeParametersExpand(&recipe, vrg.Spec.KubeObjectProtection.RecipeParameters, log); err != nil {
return err
return recipeElements, fmt.Errorf("recipe %v parameters expansion error: %w", recipeNamespacedName.String(), err)
}

var selector PvcSelector
Expand All @@ -141,15 +132,19 @@ func recipeVolumesAndOptionallyWorkflowsGet(ctx context.Context, reader client.R
recipe.Spec.Volumes.LabelSelector)
}

*recipeElements = RecipeElements{
recipeElements = RecipeElements{
Copy link
Member

Choose a reason for hiding this comment

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

Should pvcSelector be added with a OR logic between selector defined in drpc and recipe?

Copy link
Member Author

Choose a reason for hiding this comment

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

The expectation is that recipe overrides everything that is defined in the drpc.

PvcSelector: selector,
}

if err := workflowsGet(recipe, recipeElements, vrg, ramenConfig); err != nil {
return err
if err := recipeWorkflowsGet(recipe, &recipeElements, vrg, ramenConfig); err != nil {
return recipeElements, fmt.Errorf("recipe %v workflows get error: %w", recipeNamespacedName.String(), err)
}

if err := recipeNamespacesValidate(recipeElements, vrg, ramenConfig); err != nil {
return recipeElements, fmt.Errorf("recipe %v namespaces validation error: %w", recipeNamespacedName.String(), err)
}

return recipeNamespacesValidate(*recipeElements, vrg, ramenConfig)
return recipeElements, nil
}

func RecipeParametersExpand(recipe *recipe.Recipe, parameters map[string][]string,
Expand Down