-
Notifications
You must be signed in to change notification settings - Fork 61
/
restic.go
94 lines (78 loc) · 2.07 KB
/
restic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package cli
import (
"context"
"path"
"path/filepath"
"strings"
"github.com/go-logr/logr"
"github.com/k8up-io/k8up/v2/operator/utils"
"github.com/k8up-io/k8up/v2/restic/cfg"
"github.com/k8up-io/k8up/v2/restic/dto"
)
type ArrayOpts []string
func (a *ArrayOpts) String() string {
return strings.Join(*a, ", ")
}
func (a *ArrayOpts) BuildArgs() []string {
argList := make([]string, 0)
for _, elem := range *a {
argList = append(argList, "--tag", elem)
}
return argList
}
func (a *ArrayOpts) Set(value string) error {
*a = append(*a, value)
return nil
}
type Restic struct {
resticPath string
logger logr.Logger
snapshots []dto.Snapshot
ctx context.Context
bucket string
// globalFlags are applied to all invocations of restic
globalFlags Flags
statsHandler StatsHandler
caCert string
clientCert clientCert
}
type clientCert struct {
cert string
key string
pem string
}
// New returns a new Restic reference
func New(ctx context.Context, logger logr.Logger, statsHandler StatsHandler) *Restic {
globalFlags := Flags{}
options := strings.Split(cfg.Config.ResticOptions, ",")
if len(options) > 0 {
logger.Info("using the following restic options", "options", options)
globalFlags.AddFlag("--option", options...)
}
var caCert string
if cfg.Config.CACert != "" {
caCert = cfg.Config.CACert
globalFlags.AddFlag("--cacert", cfg.Config.CACert)
}
var cc clientCert
if cfg.Config.ClientCert != "" && cfg.Config.ClientKey != "" {
var pemFileName strings.Builder
pemFileName.WriteString("restic.repo.")
pemFileName.WriteString(utils.RandomStringGenerator(10))
pemFileName.WriteString(".pem")
cc.cert = cfg.Config.ClientCert
cc.key = cfg.Config.ClientKey
cc.pem = filepath.Join(cfg.Config.VarDir, pemFileName.String())
globalFlags.AddFlag("--tls-client-cert", cc.pem)
}
return &Restic{
logger: logger,
resticPath: cfg.Config.ResticBin,
ctx: ctx,
bucket: path.Base(cfg.Config.ResticRepository),
caCert: caCert,
clientCert: cc,
globalFlags: globalFlags,
statsHandler: statsHandler,
}
}