Skip to content

Commit

Permalink
controller: update implementation to match with recipe CRD
Browse files Browse the repository at this point in the history
Changes are:
1. Find the backup and restore workflow by name as CaptureWorkflow and
   RecoverWorkflow don't exist anymore.
2. Change timeout to int
3. Change command to string type

Signed-off-by: Raghavendra Talur <raghavendra.talur@gmail.com>
Co-Authored-by: Annaraya Narasagond <annarayanarasagond@gmail.com>
  • Loading branch information
raghavendra-talur and asn1809 committed Sep 25, 2024
1 parent 73f7011 commit 16e3727
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 44 deletions.
4 changes: 2 additions & 2 deletions internal/controller/kubeobjects/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ type HookSpec struct {

Type string `json:"type,omitempty"`

Command []string `json:"command,omitempty"`
Command string `json:"command,omitempty"`

//+optional
Timeout *metav1.Duration `json:"timeout,omitempty"`
Timeout int `json:"timeout,omitempty"`

//+optional
Container *string `json:"container,omitempty"`
Expand Down
5 changes: 3 additions & 2 deletions internal/controller/kubeobjects/velero/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package velero
import (
"context"
"errors"
"time"

"github.com/go-logr/logr"
pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -398,8 +399,8 @@ func getBackupHooks(hooks []kubeobjects.HookSpec) velero.BackupHooks {
{
Exec: &velero.ExecHook{
Container: dereferenceOrZeroValueIfNil(hook.Container),
Timeout: dereferenceOrZeroValueIfNil(hook.Timeout),
Command: hook.Command,
Timeout: metav1.Duration{Duration: time.Duration(hook.Timeout)},
Command: []string{hook.Command},
},
},
},
Expand Down
32 changes: 30 additions & 2 deletions internal/controller/vrg_kubeobjects.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/builder"
)

var ErrWorkflowNotFound = fmt.Errorf("backup or restore workflow not found")

func kubeObjectsCaptureInterval(kubeObjectProtectionSpec *ramen.KubeObjectProtectionSpec) time.Duration {
if kubeObjectProtectionSpec.CaptureInterval == nil {
return ramen.KubeObjectProtectionCaptureIntervalDefault
Expand Down Expand Up @@ -709,7 +711,20 @@ func kubeObjectsRequestsWatch(
}

func getCaptureGroups(recipe Recipe.Recipe) ([]kubeobjects.CaptureSpec, error) {
workflow := recipe.Spec.CaptureWorkflow
var workflow *Recipe.Workflow

for _, w := range recipe.Spec.Workflows {
if w.Name == Recipe.BackupWorkflowName {
workflow = w

break
}
}

if workflow == nil {
return nil, ErrWorkflowNotFound
}

resources := make([]kubeobjects.CaptureSpec, len(workflow.Sequence))

for index, resource := range workflow.Sequence {
Expand All @@ -729,7 +744,20 @@ func getCaptureGroups(recipe Recipe.Recipe) ([]kubeobjects.CaptureSpec, error) {
}

func getRecoverGroups(recipe Recipe.Recipe) ([]kubeobjects.RecoverSpec, error) {
workflow := recipe.Spec.RecoverWorkflow
var workflow *Recipe.Workflow

for _, w := range recipe.Spec.Workflows {
if w.Name == Recipe.RestoreWorkflowName {
workflow = w

break
}
}

if workflow == nil {
return nil, ErrWorkflowNotFound
}

resources := make([]kubeobjects.RecoverSpec, len(workflow.Sequence))

for index, resource := range workflow.Sequence {
Expand Down
10 changes: 3 additions & 7 deletions internal/controller/vrg_kubeobjects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
package controllers //nolint: testpackage

import (
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

Expand All @@ -25,9 +23,7 @@ var _ = Describe("VRG_KubeObjectProtection", func() {
var group *Recipe.Group

BeforeEach(func() {
duration, err := time.ParseDuration("30s")

Expect(err).ToNot(HaveOccurred())
duration := 30

hook = &Recipe.Hook{
Namespace: namespaceName,
Expand All @@ -43,8 +39,8 @@ var _ = Describe("VRG_KubeObjectProtection", func() {
{
Name: "checkpoint",
Container: "main",
Timeout: &metav1.Duration{Duration: duration},
Command: []string{"bash", "/scripts/checkpoint.sh"},
Timeout: duration,
Command: "bash /scripts/checkpoint.sh",
},
},
Chks: []*Recipe.Check{},
Expand Down
31 changes: 16 additions & 15 deletions internal/controller/vrg_pvc_selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,7 @@ func getVRGDefinitionWithKubeObjectProtection(hasPVCSelectorLabels bool, namespa
}

func getTestHook() *Recipe.Hook {
duration, err := time.ParseDuration("30s")

Expect(err).ToNot(HaveOccurred())
duration := 30

return &Recipe.Hook{
Name: "hook-single",
Expand All @@ -218,8 +216,8 @@ func getTestHook() *Recipe.Hook {
{
Name: "checkpoint",
Container: "main",
Timeout: &metav1.Duration{Duration: duration},
Command: []string{"bash", "/scripts/checkpoint.sh"},
Timeout: duration,
Command: "bash /scripts/checkpoint.sh",
},
},
Chks: []*Recipe.Check{},
Expand Down Expand Up @@ -270,16 +268,19 @@ func getRecipeDefinition(namespace string) *Recipe.Recipe {
Groups: []*Recipe.Group{getTestGroup()},
Volumes: getTestVolumeGroup(),
Hooks: []*Recipe.Hook{getTestHook()},
CaptureWorkflow: &Recipe.Workflow{
Sequence: []map[string]string{
{
"group": "test-group-volume",
},
{
"group": "test-group",
},
{
"hook": "test-hook",
Workflows: []*Recipe.Workflow{
{
Name: "backup",
Sequence: []map[string]string{
{
"group": "test-group-volume",
},
{
"group": "test-group",
},
{
"hook": "test-hook",
},
},
},
},
Expand Down
26 changes: 13 additions & 13 deletions internal/controller/vrg_recipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,25 +188,25 @@ func recipeWorkflowsGet(recipe recipe.Recipe, recipeElements *RecipeElements, vr
) error {
var err error

if recipe.Spec.CaptureWorkflow == nil {
recipeElements.CaptureWorkflow, err = getCaptureGroups(recipe)
if err != nil && err != ErrWorkflowNotFound {
return fmt.Errorf("failed to get groups from capture workflow: %w", err)
}

if err != nil {
recipeElements.CaptureWorkflow = captureWorkflowDefault(vrg, ramenConfig)
} else {
recipeElements.CaptureWorkflow, err = getCaptureGroups(recipe)
if err != nil {
return fmt.Errorf("failed to get groups from capture workflow: %w", err)
}
}

if recipe.Spec.RecoverWorkflow == nil {
recipeElements.RecoverWorkflow, err = getRecoverGroups(recipe)
if err != nil && err != ErrWorkflowNotFound {
return fmt.Errorf("failed to get groups from recovery workflow: %w", err)
}

if err != nil {
recipeElements.RecoverWorkflow = recoverWorkflowDefault(vrg, ramenConfig)
} else {
recipeElements.RecoverWorkflow, err = getRecoverGroups(recipe)
if err != nil {
return fmt.Errorf("failed to get groups from recovery workflow: %w", err)
}
}

return err
return nil
}

func recipeNamespacesValidate(recipeElements RecipeElements, vrg ramen.VolumeReplicationGroup,
Expand Down
10 changes: 7 additions & 3 deletions internal/controller/vrg_recipe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ var _ = Describe("VolumeReplicationGroupRecipe", func() {
Ops: []*recipe.Operation{
{
Name: namespaceName,
Command: []string{namespaceName},
Command: namespaceName,
},
},
}
Expand All @@ -145,6 +145,8 @@ var _ = Describe("VolumeReplicationGroupRecipe", func() {
},
Spec: recipe.RecipeSpec{},
}

r.Spec.Workflows = make([]*recipe.Workflow, 0)
}
recipeVolumesDefine := func(volumes *recipe.Group) {
r.Spec.Volumes = volumes
Expand All @@ -171,10 +173,12 @@ var _ = Describe("VolumeReplicationGroupRecipe", func() {
}
}
recipeCaptureWorkflowDefine := func(workflow *recipe.Workflow) {
r.Spec.CaptureWorkflow = workflow
workflow.Name = recipe.BackupWorkflowName
r.Spec.Workflows = append(r.Spec.Workflows, workflow)
}
recipeRecoverWorkflowDefine := func(workflow *recipe.Workflow) {
r.Spec.RecoverWorkflow = workflow
workflow.Name = recipe.RestoreWorkflowName
r.Spec.Workflows = append(r.Spec.Workflows, workflow)
}
recipeCreate := func() {
Expect(k8sClient.Create(ctx, r)).To(Succeed())
Expand Down

0 comments on commit 16e3727

Please sign in to comment.