Skip to content

Commit

Permalink
Merge branch 'development' into feat/125-prune
Browse files Browse the repository at this point in the history
  • Loading branch information
tobru authored Nov 17, 2020
2 parents b8cdc05 + 1bf4d24 commit 47d88b3
Show file tree
Hide file tree
Showing 11 changed files with 680 additions and 144 deletions.
38 changes: 31 additions & 7 deletions api/v1alpha1/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,45 @@ func (s *S3Spec) EnvVars(vars map[string]*corev1.EnvVarSource) map[string]*corev
return vars
}

func (s *S3Spec) RestoreEnvVars() map[string]*corev1.EnvVarSource {
vars := make(map[string]*corev1.EnvVarSource)

func (s *S3Spec) RestoreEnvVars() map[string]*corev1.EnvVar {
vars := make(map[string]*corev1.EnvVar)
if s.AccessKeyIDSecretRef != nil {
vars[constants.RestoreS3AccessKeyIDEnvName] = &corev1.EnvVarSource{
SecretKeyRef: s.AccessKeyIDSecretRef,
vars[constants.RestoreS3AccessKeyIDEnvName] = &corev1.EnvVar{
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: s.AccessKeyIDSecretRef,
},
}
} else {
vars[constants.RestoreS3AccessKeyIDEnvName] = &corev1.EnvVar{
Value: constants.GetGlobalRestoreS3AccessKey(),
}
}

if s.SecretAccessKeySecretRef != nil {
vars[constants.RestoreS3SecretAccessKeyEnvName] = &corev1.EnvVarSource{
SecretKeyRef: s.SecretAccessKeySecretRef,
vars[constants.RestoreS3SecretAccessKeyEnvName] = &corev1.EnvVar{
ValueFrom: &corev1.EnvVarSource{
SecretKeyRef: s.SecretAccessKeySecretRef,
},
}
} else {
vars[constants.RestoreS3SecretAccessKeyEnvName] = &corev1.EnvVar{
Value: constants.GetGlobalRestoreS3SecretAccessKey(),
}
}

bucket := s.Bucket
endpoint := s.Endpoint
if bucket == "" {
bucket = constants.GetGlobalRestoreS3Bucket()
}
if endpoint == "" {
endpoint = constants.GetGlobalRestoreS3Endpoint()
}

vars[constants.RestoreS3EndpointEnvName] = &corev1.EnvVar{
Value: fmt.Sprintf("%v/%v", endpoint, bucket),
}

return vars
}

Expand Down
36 changes: 26 additions & 10 deletions api/v1alpha1/restore_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ type RestoreSpec struct {
Tags []string `json:"tags,omitempty"`
}

func (r RestoreSpec) CreateObject(name, namespace string) runtime.Object {
return &Restore{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: r,
}
}

// RestoreMethod contains how and where the restore should happen
// all the settings are mutual exclusive.
type RestoreMethod struct {
Expand Down Expand Up @@ -63,6 +73,22 @@ type Restore struct {
Status RestoreStatus `json:"status,omitempty"`
}

func (r *Restore) GetRuntimeObject() runtime.Object {
return r
}

func (r *Restore) GetMetaObject() metav1.Object {
return r
}

func (r *Restore) GetType() string {
return "restore"
}

func (r *Restore) GetK8upStatus() *K8upStatus {
return &r.Status.K8upStatus
}

// +kubebuilder:object:root=true

// RestoreList contains a list of Restore
Expand All @@ -75,13 +101,3 @@ type RestoreList struct {
func init() {
SchemeBuilder.Register(&Restore{}, &RestoreList{})
}

func (r RestoreSpec) CreateObject(name, namespace string) runtime.Object {
return &Restore{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: r,
}
}
141 changes: 46 additions & 95 deletions config/crd/bases/backup.appuio.ch_restores.yaml

Large diffs are not rendered by default.

54 changes: 27 additions & 27 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ const (
// TODO: this will be replaced with a CRD in the future.

var (
mountPath = getEnvVar("BACKUP_DATAPATH", "/data")
backupAnnotation = getEnvVar("BACKUP_ANNOTATION", "k8up.syn.tools/backup")
backupCommandAnnotation = getEnvVar("BACKUP_BACKUPCOMMANDANNOTATION", "k8up.syn.tools/backupcommand")
fileExtensionAnnotation = getEnvVar("BACKUP_FILEEXTENSIONANNOTATION", "k8up.syn.tools/file-extension")
serviceAccount = getEnvVar("BACKUP_PODEXECACCOUNTNAME", "pod-executor")
backupCheckSchedule = getEnvVar("BACKUP_CHECKSCHEDULE", "0 0 * * 0")
globalAccessKey = getEnvVar("BACKUP_GLOBALACCESSKEYID", "")
globalKeepJobs = getEnvVar("BACKUP_GLOBALKEEPJOBS", "6")
globalRepoPassword = getEnvVar("BACKUP_GLOBALREPOPASSWORD", "")
globalRestoreAccessKey = getEnvVar("BACKUP_GLOBALRESTORES3ACCESKEYID", "")
globalRestoreS3Bucket = getEnvVar("BACKUP_GLOBALRESTORES3BUCKET", "")
globalRestoreS3Endpoitn = getEnvVar("BACKUP_GLOBALRESTORES3ENDPOINT", "")
globalRestoreS3AccessKey = getEnvVar("BACKUP_GLOBALRESTORES3SECRETACCESSKEY", "")
globalS3Bucket = getEnvVar("BACKUP_GLOBALS3BUCKET", "")
globalS3Endpoint = getEnvVar("BACKUP_GLOBALS3ENDPOINT", "")
globalSecretAccessKey = getEnvVar("BACKUP_GLOBALSECRETACCESSKEY", "")
globalStatsURL = getEnvVar("BACKUP_GLOBALSTATSURL", "")
backupImage = getEnvVar("BACKUP_IMAGE", "172.30.1.1:5000/myproject/restic")
podExecRoleName = getEnvVar("BACKUP_PODEXECROLENAME", "pod-executor")
podFilter = getEnvVar("BACKUP_PODFILTER", "backupPod=true")
promURL = getEnvVar("BACKUP_PROMURL", "")
restartPolicy = getEnvVar("BACKUP_RESTARTPOLICY", "OnFailure")
mountPath = getEnvVar("BACKUP_DATAPATH", "/data")
backupAnnotation = getEnvVar("BACKUP_ANNOTATION", "k8up.syn.tools/backup")
backupCommandAnnotation = getEnvVar("BACKUP_BACKUPCOMMANDANNOTATION", "k8up.syn.tools/backupcommand")
fileExtensionAnnotation = getEnvVar("BACKUP_FILEEXTENSIONANNOTATION", "k8up.syn.tools/file-extension")
serviceAccount = getEnvVar("BACKUP_PODEXECACCOUNTNAME", "pod-executor")
backupCheckSchedule = getEnvVar("BACKUP_CHECKSCHEDULE", "0 0 * * 0")
globalAccessKey = getEnvVar("BACKUP_GLOBALACCESSKEYID", "")
globalKeepJobs = getEnvVar("BACKUP_GLOBALKEEPJOBS", "6")
globalRepoPassword = getEnvVar("BACKUP_GLOBALREPOPASSWORD", "")
globalRestoreS3AccessKey = getEnvVar("BACKUP_GLOBALRESTORES3ACCESKEYID", "")
globalRestoreS3Bucket = getEnvVar("BACKUP_GLOBALRESTORES3BUCKET", "")
globalRestoreS3Endpoint = getEnvVar("BACKUP_GLOBALRESTORES3ENDPOINT", "")
globalRestoreS3SecretAccessKey = getEnvVar("BACKUP_GLOBALRESTORES3SECRETACCESSKEY", "")
globalS3Bucket = getEnvVar("BACKUP_GLOBALS3BUCKET", "")
globalS3Endpoint = getEnvVar("BACKUP_GLOBALS3ENDPOINT", "")
globalSecretAccessKey = getEnvVar("BACKUP_GLOBALSECRETACCESSKEY", "")
globalStatsURL = getEnvVar("BACKUP_GLOBALSTATSURL", "")
backupImage = getEnvVar("BACKUP_IMAGE", "172.30.1.1:5000/myproject/restic")
podExecRoleName = getEnvVar("BACKUP_PODEXECROLENAME", "pod-executor")
podFilter = getEnvVar("BACKUP_PODFILTER", "backupPod=true")
promURL = getEnvVar("BACKUP_PROMURL", "")
restartPolicy = getEnvVar("BACKUP_RESTARTPOLICY", "OnFailure")
)

func GetRestartPolicy() string {
Expand Down Expand Up @@ -82,20 +82,20 @@ func GetGlobalS3Bucket() string {
return globalS3Bucket
}

func GetGlobalRestoreS3AccessKey() string {
return globalRestoreS3AccessKey
func GetGlobalRestoreS3SecretAccessKey() string {
return globalRestoreS3SecretAccessKey
}

func GetGlobalRestoreS3Endpoint() string {
return globalRestoreS3Endpoitn
return globalRestoreS3Endpoint
}

func GetGlobalRestoreS3Bucket() string {
return globalRestoreS3Bucket
}

func GetGlobalRestoreAccessKey() string {
return globalRestoreAccessKey
func GetGlobalRestoreS3AccessKey() string {
return globalRestoreS3AccessKey
}

func GetGlobalRepoPassword() string {
Expand Down
27 changes: 23 additions & 4 deletions controllers/restore_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@ package controllers

import (
"context"
"time"

"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

k8upv1alpha1 "github.com/vshn/k8up/api/v1alpha1"
"github.com/vshn/k8up/handler"
"github.com/vshn/k8up/job"
)

// RestoreReconciler reconciles a Restore object
Expand All @@ -38,12 +42,27 @@ type RestoreReconciler struct {
// +kubebuilder:rbac:groups=backup.appuio.ch,resources=restores/status,verbs=get;update;patch

func (r *RestoreReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
_ = context.Background()
_ = r.Log.WithValues("restore", req.NamespacedName)
ctx := context.Background()
log := r.Log.WithValues("restore", req.NamespacedName)

// your logic here
restore := &k8upv1alpha1.Restore{}
err := r.Get(ctx, req.NamespacedName, restore)
if err != nil {
if errors.IsNotFound(err) {
return ctrl.Result{}, nil
}
return ctrl.Result{}, err
}

return ctrl.Result{}, nil
if restore.Status.Started {
return ctrl.Result{}, nil
}

config := job.NewConfig(ctx, r.Client, log, restore, r.Scheme)

restoreHandler := handler.NewHandler(config)

return ctrl.Result{RequeueAfter: time.Second * 30}, restoreHandler.Handle()
}

func (r *RestoreReconciler) SetupWithManager(mgr ctrl.Manager) error {
Expand Down
13 changes: 13 additions & 0 deletions executor/executor_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package executor

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestExecutor(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Executor Suite")
}
2 changes: 2 additions & 0 deletions executor/generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ func NewExecutor(config job.Config) queue.Executor {
return NewArchiveExecutor(config)
case "prune":
return NewPruneExecutor(config)
case "restore":
return NewRestoreExecutor(config)
}
return nil
}
Expand Down
Loading

0 comments on commit 47d88b3

Please sign in to comment.