Skip to content

Commit

Permalink
Working backups
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Beck committed Oct 30, 2020
1 parent 461d1f3 commit 8a5a1a4
Show file tree
Hide file tree
Showing 16 changed files with 501 additions and 43 deletions.
9 changes: 8 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go",
"env": {},
"env": {
"BACKUP_IMAGE": "vshn/wrestic:v0.2.0",
"BACKUP_GLOBALS3ENDPOINT": "http://192.168.5.195:9000",
"BACKUP_GLOBALS3BUCKET": "baas",
"BACKUP_GLOBALSECRETACCESSKEY": "ip3cdrkXcHmH4S7if7erKPNoxDn27V0vrg6CHHem",
"BACKUP_GLOBALACCESSKEYID": "8U0UDNYPNUDTUS1LIAF3",
"BACKUP_GLOBALREPOPASSWORD": "password",
},
"args": []
}
]
Expand Down
153 changes: 146 additions & 7 deletions api/v1alpha1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v1alpha1
import (
"fmt"

"github.com/vshn/k8up/constants"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -18,6 +19,45 @@ type Backend struct {
Rest *RestServerSpec `json:"rest,omitempty"`
}

// GetCredentialEnv will return a map containing the correct
func (b *Backend) GetCredentialEnv() map[string]*corev1.EnvVarSource {
vars := make(map[string]*corev1.EnvVarSource)

vars[constants.ResticPasswordEnvName] = &corev1.EnvVarSource{
SecretKeyRef: b.RepoPasswordSecretRef,
}

if b.Azure != nil {
return b.Azure.EnvVars(vars)
}

if b.B2 != nil {
return b.B2.EnvVars(vars)
}

if b.GCS != nil {
return b.GCS.EnvVars(vars)
}

if b.Local != nil {
return b.Local.EnvVars(vars)
}

if b.Rest != nil {
return b.Rest.EnvVars(vars)
}

if b.S3 != nil {
return b.S3.EnvVars(vars)
}

if b.Swift != nil {
return b.Swift.EnvVars(vars)
}

return nil
}

// String returns the string representation of the repository. If no repo is
// defined it'll return empty string.
func (b *Backend) String() string {
Expand Down Expand Up @@ -58,29 +98,128 @@ type LocalSpec struct {
MountPath string `json:"mountPath,omitempty"`
}

func (l *LocalSpec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
return vars
}

type S3Spec struct {
Endpoint string `json:"endpoint,omitempty"`
Bucket string `json:"bucket,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Bucket string `json:"bucket,omitempty"`
AccessKeyIDSecretRef *corev1.SecretKeySelector `json:"accessKeyIDSecretRef,omitempty"`
SecretAccessKeySecretRef *corev1.SecretKeySelector `json:"secretAccessKeySecretRef,omitempty"`
}

func (s *S3Spec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
if s.AccessKeyIDSecretRef != nil {
vars["AWS_ACCESS_KEY_ID"] = &corev1.EnvVarSource{
SecretKeyRef: s.AccessKeyIDSecretRef,
}
}

if s.SecretAccessKeySecretRef != nil {
vars["AWS_SECRET_ACCESS_KEY"] = &corev1.EnvVarSource{
SecretKeyRef: s.SecretAccessKeySecretRef,
}
}

return vars
}

type GCSSpec struct {
Bucket string `json:"bucket,omitempty"`
Bucket string `json:"bucket,omitempty"`
ProjectIDSecretRef *corev1.SecretKeySelector `json:"projectIDSecretRef,omitempty"`
AccessTokenSecretRef *corev1.SecretKeySelector `json:"accessTokenSecretRef,omitempty"`
}

func (g *GCSSpec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
if g.ProjectIDSecretRef != nil {
vars["GOOGLE_PROJECT_ID"] = &corev1.EnvVarSource{
SecretKeyRef: g.ProjectIDSecretRef,
}
}

if g.AccessTokenSecretRef != nil {
vars["GOOGLE_ACCESS_TOKEN"] = &corev1.EnvVarSource{
SecretKeyRef: g.AccessTokenSecretRef,
}
}

return vars

}

type AzureSpec struct {
Container string `json:"container,omitempty"`
Container string `json:"container,omitempty"`
AccountNameSecretRef *corev1.SecretKeySelector `json:"accountNameSecretRef,omitempty"`
AccountKeySecreftRef *corev1.SecretKeySelector `json:"accountKeySecreftRef,omitempty"`
}

func (a *AzureSpec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
if a.AccountKeySecreftRef != nil {
vars["AZURE_ACCOUNT_KEY"] = &corev1.EnvVarSource{
SecretKeyRef: a.AccountKeySecreftRef,
}
}

if a.AccountNameSecretRef != nil {
vars["AZURE_ACCOUNT_NAME"] = &corev1.EnvVarSource{
SecretKeyRef: a.AccountNameSecretRef,
}
}

return vars
}

type SwiftSpec struct {
Container string `json:"container,omitempty"`
Path string `json:"path,omitempty"`
}

func (s *SwiftSpec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
return vars
}

type B2Spec struct {
Bucket string `json:"bucket,omitempty"`
Path string `json:"path,omitempty"`
Bucket string `json:"bucket,omitempty"`
Path string `json:"path,omitempty"`
AccountIDSecretRef *corev1.SecretKeySelector `json:"accountIDSecretRef,omitempty"`
AccountKeySecretRef *corev1.SecretKeySelector `json:"accountKeySecretRef,omitempty"`
}

func (b *B2Spec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
if b.AccountIDSecretRef != nil {
vars["B2_ACCOUNT_ID"] = &corev1.EnvVarSource{
SecretKeyRef: b.AccountIDSecretRef,
}
}

if b.AccountKeySecretRef != nil {
vars["B2_ACCOUNT_KEY"] = &corev1.EnvVarSource{
SecretKeyRef: b.AccountKeySecretRef,
}
}

return vars
}

type RestServerSpec struct {
URL string `json:"url,omitempty"`
URL string `json:"url,omitempty"`
UserSecretRef *corev1.SecretKeySelector `json:"userSecretRef,omitempty"`
PasswordSecretReg *corev1.SecretKeySelector `json:"passwordSecretReg,omitempty"`
}

func (r *RestServerSpec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev1.EnvVarSource {
if r.PasswordSecretReg != nil {
vars["PASSWORD"] = &corev1.EnvVarSource{
SecretKeyRef: r.PasswordSecretReg,
}
}

if r.UserSecretRef != nil {
vars["USER"] = &corev1.EnvVarSource{
SecretKeyRef: r.UserSecretRef,
}
}

return vars
}
2 changes: 1 addition & 1 deletion api/v1alpha1/backup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type BackupSpec struct {
// Backend contains the restic repo where the job should backup to.
Backend *Backend `json:"backend,omitempty"`
// KeepJobs amount of jobs to keep for later analysis
KeepJobs int `json:"keepJobs,omitempty"`
KeepJobs *int `json:"keepJobs,omitempty"`

// PromURL sets a prometheus push URL where the backup container send metrics to
// +optional
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/check_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type CheckSpec struct {
PromURL string `json:"promURL,omitempty"`
Backend *Backend `json:"backend,omitempty"`
KeepJobs int `json:"keepJobs,omitempty"`
KeepJobs *int `json:"keepJobs,omitempty"`
}

// CheckStatus defines the observed state of Check
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/prune_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type PruneSpec struct {
// Retention sets how many backups should be kept after a forget and prune
Retention RetentionPolicy `json:"retention,omitempty"`
Backend *Backend `json:"backend,omitempty"`
KeepJobs int `json:"keepJobs,omitempty"`
KeepJobs *int `json:"keepJobs,omitempty"`
}

type RetentionPolicy struct {
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/restore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type RestoreSpec struct {
RestoreMethod *RestoreMethod `json:"restoreMethod,omitempty"`
RestoreFilter string `json:"restoreFilter,omitempty"`
Snapshot string `json:"snapshot,omitempty"`
KeepJobs int `json:"keepJobs,omitempty"`
KeepJobs *int `json:"keepJobs,omitempty"`
// Tags is a list of arbitrary tags that get added to the backup via Restic's tagging system
Tags []string `json:"tags,omitempty"`
}
Expand Down
2 changes: 1 addition & 1 deletion api/v1alpha1/schedule_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ScheduleSpec struct {
Check *CheckSchedule `json:"check,omitempty"`
Prune *PruneSchedule `json:"prune,omitempty"`
Backend *Backend `json:"backend,omitempty"`
KeepJobs int `json:"keepJobs,omitempty"`
KeepJobs *int `json:"keepJobs,omitempty"`
}

// ScheduleCommon contains fields every schedule needs
Expand Down
4 changes: 2 additions & 2 deletions config/samples/k8up_v1alpha1_schedule.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ metadata:
spec:
backup:
schedule: '* * * * *'
archive:
schedule: '* * * * *'
# archive:
# schedule: '* * * * *'
Loading

0 comments on commit 8a5a1a4

Please sign in to comment.