Skip to content

Commit

Permalink
Merge pull request RamenDR#398 from raghavendra-talur/rtalur-backport…
Browse files Browse the repository at this point in the history
…-1649

Backport of PR RamenDR#1649 Check hook implementation
  • Loading branch information
openshift-merge-bot[bot] authored Nov 15, 2024
2 parents 8ad1a4c + 8401d59 commit 28a80de
Show file tree
Hide file tree
Showing 9 changed files with 867 additions and 170 deletions.
16 changes: 16 additions & 0 deletions config/dr-cluster/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,22 @@ rules:
- get
- list
- watch
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
- statefulsets
verbs:
- get
- list
- watch
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
Expand Down
16 changes: 16 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ rules:
- get
- list
- watch
- apiGroups:
- apps
resources:
- deployments
verbs:
- get
- list
- watch
- apiGroups:
- apps
resources:
- statefulsets
verbs:
- get
- list
- watch
- apiGroups:
- apps.open-cluster-management.io
resources:
Expand Down
53 changes: 43 additions & 10 deletions internal/controller/kubeobjects/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,57 @@ type KubeResourcesSpec struct {
ExcludedResources []string `json:"excludedResources,omitempty"`

//+optional
Hooks []HookSpec `json:"hooks,omitempty"`
Hook HookSpec `json:"hooks,omitempty"`

//+optional
IsHook bool `json:"isHook,omitempty"`
}

// HookSpec provides spec of either check or exec hook that needs to be executed
type HookSpec struct {
Name string `json:"name,omitempty"`
Name string `json:"name"`
Namespace string `json:"namespace"`
Type string `json:"type"`
SelectResource string `json:"selectResource,omitempty"`
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
NameSelector string `json:"nameSelector,omitempty"`
SinglePodOnly bool `json:"singlePodOnly,omitempty"`
//+optional
OnError string `json:"onError,omitempty"`

Type string `json:"type,omitempty"`
//+optional
Timeout int `json:"timeout,omitempty"`
Essential *bool `json:"essential,omitempty"`

Command string `json:"command,omitempty"`
Op Operation `json:"operation,omitempty"`

//+optional
Timeout int `json:"timeout,omitempty"`
Chk Check `json:"check,omitempty"`
}

//+optional
Container *string `json:"container,omitempty"`
type Check struct {
// Name of the check. Needs to be unique within the hook
Name string `json:"name"`
// The condition to check for
Condition string `json:"condition,omitempty"`
// How to handle when check does not become true. Defaults to Fail.
OnError string `json:"onError,omitempty"`
// How long to wait for the check to execute, in seconds
Timeout int `json:"timeout,omitempty"`
}

//+optional
LabelSelector *metav1.LabelSelector `json:"labelSelector,omitempty"`
type Operation struct {
// Name of the operation. Needs to be unique within the hook
Name string `json:"name"`
// The container where the command should be executed
Container string `json:"container,omitempty"`
// The command to execute
Command string `json:"command"`
// How to handle command returning with non-zero exit code. Defaults to Fail.
OnError string `json:"onError,omitempty"`
// How long to wait for the command to execute, in seconds
Timeout int `json:"timeout,omitempty"`
// Name of another operation that reverts the effect of this operation (e.g. quiesce vs. unquiesce)
InverseOp string `json:"inverseOp,omitempty"`
}

func RequestProcessingErrorCreate(s string) RequestProcessingError { return RequestProcessingError{s} }
Expand Down
37 changes: 2 additions & 35 deletions internal/controller/kubeobjects/velero/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package velero
import (
"context"
"errors"
"time"

"github.com/go-logr/logr"
pkgerrors "github.com/pkg/errors"
Expand Down Expand Up @@ -378,46 +377,14 @@ func getBackupSpecFromObjectsSpec(objectsSpec kubeobjects.Spec) velero.BackupSpe
OrLabelSelectors: objectsSpec.OrLabelSelectors,
TTL: metav1.Duration{}, // TODO: set default here
IncludeClusterResources: objectsSpec.IncludeClusterResources,
Hooks: getBackupHooks(objectsSpec.KubeResourcesSpec.Hooks),
// TODO: Hooks should be handled by ramen code.
// Hooks: getBackupHooks(objectsSpec.KubeResourcesSpec.Hooks)
VolumeSnapshotLocations: []string{},
DefaultVolumesToRestic: new(bool),
OrderedResources: map[string]string{},
}
}

func getBackupHooks(hooks []kubeobjects.HookSpec) velero.BackupHooks {
hookSpec := velero.BackupHooks{}

for i := range hooks {
hook := &hooks[i] // exportloopref: fix variable into local variable

hookSpec.Resources = append(hookSpec.Resources, velero.BackupResourceHookSpec{
Name: hook.Name,
LabelSelector: hook.LabelSelector,
PreHooks: []velero.BackupResourceHook{},
PostHooks: []velero.BackupResourceHook{
{
Exec: &velero.ExecHook{
Container: dereferenceOrZeroValueIfNil(hook.Container),
Timeout: metav1.Duration{Duration: time.Duration(hook.Timeout)},
Command: []string{hook.Command},
},
},
},
})
}

return hookSpec
}

func dereferenceOrZeroValueIfNil[T any](pointer *T) (t T) {
if pointer == nil {
return
}

return *pointer
}

func backupRealStatusProcess(
backup *velero.Backup,
log logr.Logger,
Expand Down
Loading

0 comments on commit 28a80de

Please sign in to comment.