Skip to content

Commit

Permalink
Move restic repo init into restic pkg (#4068)
Browse files Browse the repository at this point in the history
* Move restic repo init into restic pkg

* Address review comments
  • Loading branch information
Vaibhav Kamra authored and Ilya Kislenko committed Oct 10, 2018
1 parent 9b69028 commit cfa7d63
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 39 deletions.
19 changes: 19 additions & 0 deletions pkg/format/format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package format

import (
"regexp"
"strings"

log "github.com/sirupsen/logrus"
)

func Log(podName string, containerName string, output string) {
if output != "" {
logs := regexp.MustCompile("[\r\n]").Split(output, -1)
for _, l := range logs {
if strings.TrimSpace(l) != "" {
log.Info("Pod: ", podName, " Container: ", containerName, " Out: ", l)
}
}
}
}
28 changes: 4 additions & 24 deletions pkg/function/backup_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"

"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"

kanister "github.com/kanisterio/kanister/pkg"
crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
"github.com/kanisterio/kanister/pkg/restic"
Expand Down Expand Up @@ -50,27 +50,7 @@ func validateProfile(profile *param.Profile) error {
return nil
}

func getOrCreateRepository(cli kubernetes.Interface, namespace, pod, container, artifactPrefix string, profile *param.Profile) error {
// Use the snapshots command to check if the repository exists
cmd := restic.SnapshotsCommand(profile, artifactPrefix)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
if err != nil {
// Create a repository
cmd := restic.InitCommand(profile, artifactPrefix)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
if err != nil {
return errors.Wrapf(err, "Failed to create object store backup location")
}
}
return nil
}

func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) {

var namespace, pod, container, includePath, backupArtifactPrefix, backupIdentifier string
var err error
if err = Arg(args, BackupDataNamespaceArg, &namespace); err != nil {
Expand Down Expand Up @@ -100,15 +80,15 @@ func (*backupDataFunc) Exec(ctx context.Context, tp param.TemplateParams, args m
return nil, errors.Wrapf(err, "Failed to create Kubernetes client")
}

if err = getOrCreateRepository(cli, namespace, pod, container, backupArtifactPrefix, tp.Profile); err != nil {
if err = restic.GetOrCreateRepository(cli, namespace, pod, container, backupArtifactPrefix, tp.Profile); err != nil {
return nil, err
}

// Create backup and dump it on the object store
cmd := restic.BackupCommand(tp.Profile, backupArtifactPrefix, backupIdentifier, includePath)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
formatAndLog(pod, container, stdout)
formatAndLog(pod, container, stderr)
format.Log(pod, container, stdout)
format.Log(pod, container, stderr)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create and upload backup")
}
Expand Down
18 changes: 3 additions & 15 deletions pkg/function/kube_exec_all.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ package function

import (
"context"
"regexp"
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"

kanister "github.com/kanisterio/kanister/pkg"
"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/param"
)
Expand Down Expand Up @@ -71,8 +70,8 @@ func execAll(cli kubernetes.Interface, namespace string, ps []string, cs []strin
for _, c := range cs {
go func(p string, c string) {
stdout, stderr, err := kube.Exec(cli, namespace, p, c, cmd)
formatAndLog(p, c, stdout)
formatAndLog(p, c, stderr)
format.Log(p, c, stdout)
format.Log(p, c, stderr)
errChan <- err
}(p, c)
}
Expand All @@ -89,14 +88,3 @@ func execAll(cli kubernetes.Interface, namespace string, ps []string, cs []strin
}
return nil
}

func formatAndLog(podName string, containerName string, output string) {
if output != "" {
logs := regexp.MustCompile("[\r\n]").Split(output, -1)
for _, l := range logs {
if strings.TrimSpace(l) != "" {
log.Info("Pod: ", podName, " Container: ", containerName, " Out: ", l)
}
}
}
}
23 changes: 23 additions & 0 deletions pkg/restic/restic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"fmt"
"strings"

"github.com/pkg/errors"
"k8s.io/client-go/kubernetes"

"github.com/kanisterio/kanister/pkg/format"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/location"
"github.com/kanisterio/kanister/pkg/param"
)
Expand Down Expand Up @@ -82,3 +87,21 @@ func resticArgs(profile *param.Profile, repository string) []string {
ResticCommand,
}
}

// GetOrCreateRepository will check if the repository already exists and initialize one if not
func GetOrCreateRepository(cli kubernetes.Interface, namespace, pod, container, artifactPrefix string, profile *param.Profile) error {
// Use the snapshots command to check if the repository exists
cmd := SnapshotsCommand(profile, artifactPrefix)
stdout, stderr, err := kube.Exec(cli, namespace, pod, container, cmd)
format.Log(pod, container, stdout)
format.Log(pod, container, stderr)
if err == nil {
return nil
}
// Create a repository
cmd = InitCommand(profile, artifactPrefix)
stdout, stderr, err = kube.Exec(cli, namespace, pod, container, cmd)
format.Log(pod, container, stdout)
format.Log(pod, container, stderr)
return errors.Wrapf(err, "Failed to create object store backup location")
}

0 comments on commit cfa7d63

Please sign in to comment.