Skip to content

Commit

Permalink
Merge params of backup-config and tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
diptadas committed Mar 19, 2019
1 parent 1748579 commit 5617a4a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
9 changes: 5 additions & 4 deletions pkg/controller/backup_session.go
Expand Up @@ -132,15 +132,16 @@ func (c *StashController) executeBackupSession(backupSession *api.BackupSession)
return nil, nil
}

implicitInputs, err := c.inputsForBackupConfig(*backupConfig)
if err != nil {
return nil, fmt.Errorf("cannot resolve implicit inputs for BackupConfiguration %s/%s, reason: %s", backupConfig.Namespace, backupConfig.Name, err)
}
explicitInputs := make(map[string]string)
for _, param := range backupConfig.Spec.Task.Params {
explicitInputs[param.Name] = param.Value
}

implicitInputs, err := c.inputsForBackupConfig(*backupConfig)
if err != nil {
return nil, fmt.Errorf("cannot resolve implicit inputs for BackupConfiguration %s/%s, reason: %s", backupConfig.Namespace, backupConfig.Name, err)
}

taskResolver := resolve.TaskResolver{
StashClient: c.stashClient,
TaskName: backupConfig.Spec.Task.Name,
Expand Down
66 changes: 54 additions & 12 deletions pkg/controller/inputs.go
@@ -1,6 +1,7 @@
package controller

import (
"strconv"
"strings"

apiAlpha "github.com/appscode/stash/apis/stash/v1alpha1"
Expand All @@ -17,11 +18,23 @@ const (
RepositoryBucket = "REPOSITORY_BUCKET"
RepositoryPrefix = "REPOSITORY_PREFIX"
RepositoryEndpoint = "REPOSITORY_ENDPOINT"
TargetName = "TARGET_NAME"
TargetDirectories = "TARGET_DIRECTORIES"
TargetMountPath = "TARGET_MOUNT_PATH"
RetentionStrategy = "RETENTION_STRATEGY"
RetentionValue = "RETENTION_VALUE"

Hostname = "HOSTNAME"

TargetName = "TARGET_NAME"
TargetDirectories = "TARGET_DIRECTORIES"
TargetMountPath = "TARGET_MOUNT_PATH"

RestoreDirectories = "RESTORE_DIRECTORIES"
RestoreSnapshots = "RESTORE_SNAPSHOTS"

RetentionKeepLast = "RETENTION_KEEP_LAST"
RetentionKeepHourly = "RETENTION_KEEP_HOURLY"
RetentionKeepDaily = "RETENTION_KEEP_DAILY"
RetentionKeepWeekly = "RETENTION_KEEP_WEEKLY"
RetentionKeepMonthly = "RETENTION_KEEP_MONTHLY"
RetentionKeepYearly = "RETENTION_KEEP_YEARLY"
RetentionKeepTags = "RETENTION_KEEP_TAGS"
RetentionPrune = "RETENTION_PRUNE"
RetentionDryRun = "RETENTION_DRY_RUN"
)
Expand All @@ -47,7 +60,7 @@ func (c *StashController) inputsForBackupConfig(backupConfig api.BackupConfigura
return inputs, nil
}

func (c *StashController) inputsForRestoreSession(restoreSession api.RestoreSession) (map[string]string, error) {
func (c *StashController) inputsForRestoreSession(restoreSession api.RestoreSession, host string) (map[string]string, error) {
// get repository for restoreSession
repository, err := c.stashClient.StashV1alpha1().Repositories(restoreSession.Namespace).Get(
restoreSession.Spec.Repository.Name,
Expand All @@ -63,6 +76,12 @@ func (c *StashController) inputsForRestoreSession(restoreSession api.RestoreSess
}
// append inputs for target
inputs = core_util.UpsertMap(inputs, c.inputsForTarget(restoreSession.Spec.Target))
// append inputs from RestoreOptions
restoreOptions := util.RestoreOptionsForHost(host, restoreSession.Spec.Rules)
inputs[Hostname] = restoreOptions.Host
inputs[RestoreDirectories] = strings.Join(restoreOptions.RestoreDirs, ",")
inputs[RestoreSnapshots] = strings.Join(restoreOptions.Snapshots, ",")

return inputs, nil
}

Expand Down Expand Up @@ -102,12 +121,35 @@ func (c *StashController) inputsForTarget(target *api.Target) map[string]string
return inputs
}

// TODO
func (c *StashController) inputsForRetentionPolicy(policy apiAlpha.RetentionPolicy) map[string]string {
func (c *StashController) inputsForRetentionPolicy(retentionPolicy apiAlpha.RetentionPolicy) map[string]string {
inputs := make(map[string]string)
inputs[RetentionStrategy] = "keep-last"
inputs[RetentionValue] = "5"
inputs[RetentionDryRun] = "false"
inputs[RetentionPrune] = "false"

if retentionPolicy.KeepLast > 0 {
inputs[RetentionKeepLast] = strconv.Itoa(retentionPolicy.KeepLast)
}
if retentionPolicy.KeepHourly > 0 {
inputs[RetentionKeepHourly] = strconv.Itoa(retentionPolicy.KeepHourly)
}
if retentionPolicy.KeepDaily > 0 {
inputs[RetentionKeepDaily] = strconv.Itoa(retentionPolicy.KeepDaily)
}
if retentionPolicy.KeepWeekly > 0 {
inputs[RetentionKeepWeekly] = strconv.Itoa(retentionPolicy.KeepWeekly)
}
if retentionPolicy.KeepMonthly > 0 {
inputs[RetentionKeepMonthly] = strconv.Itoa(retentionPolicy.KeepMonthly)
}
if retentionPolicy.KeepYearly > 0 {
inputs[RetentionKeepYearly] = strconv.Itoa(retentionPolicy.KeepYearly)
}
if len(retentionPolicy.KeepTags) > 0 {
inputs[RetentionKeepTags] = strings.Join(retentionPolicy.KeepTags, ",")
}
if retentionPolicy.Prune {
inputs[RetentionPrune] = "true"
}
if retentionPolicy.DryRun {
inputs[RetentionDryRun] = "true"
}
return inputs
}
10 changes: 6 additions & 4 deletions pkg/controller/restore_session.go
Expand Up @@ -124,15 +124,17 @@ func (c *StashController) executeRestoreSession(restoreSession *api.RestoreSessi
return nil, nil
}

implicitInputs, err := c.inputsForRestoreSession(*restoreSession)
if err != nil {
return nil, fmt.Errorf("cannot resolve implicit inputs for RestoreSession %s/%s, reason: %s", restoreSession.Namespace, restoreSession.Name, err)
}
explicitInputs := make(map[string]string)
for _, param := range restoreSession.Spec.Task.Params {
explicitInputs[param.Name] = param.Value
}

// TODO: hostname from spec?
implicitInputs, err := c.inputsForRestoreSession(*restoreSession, explicitInputs[Hostname])
if err != nil {
return nil, fmt.Errorf("cannot resolve implicit inputs for RestoreSession %s/%s, reason: %s", restoreSession.Namespace, restoreSession.Name, err)
}

taskResolver := resolve.TaskResolver{
StashClient: c.stashClient,
TaskName: restoreSession.Spec.Task.Name,
Expand Down
7 changes: 6 additions & 1 deletion pkg/resolve/task.go
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/drone/envsubst"
core "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
core_util "kmodules.xyz/client-go/core/v1"
ofst "kmodules.xyz/offshoot-api/api/v1"
)

Expand Down Expand Up @@ -44,11 +45,15 @@ func (o TaskResolver) GetPodSpec() (core.PodSpec, error) {
return core.PodSpec{}, fmt.Errorf("can't get Function %s for Task %s, reason: %s", fn.Name, task.Name, err)
}

// resolve Function with inputs, modify in place
// inputs from params
inputs := make(map[string]string)
for _, param := range fn.Params {
inputs[param.Name] = param.Value
}
// merge/replace backup config inputs
inputs = core_util.UpsertMap(o.Inputs, inputs)

// resolve Function with inputs, modify in place
if err = resolveWithInputs(function, inputs); err != nil {
return core.PodSpec{}, fmt.Errorf("can't resolve Function %s for Task %s, reason: %s", fn.Name, task.Name, err)
}
Expand Down

0 comments on commit 5617a4a

Please sign in to comment.