Compare commits

...

2 Commits

Author SHA1 Message Date
477022347d Restore session test 2021-05-02 22:35:00 +02:00
4eb5f38795 Post restore steps work now 2021-05-02 22:34:34 +02:00
12 changed files with 182 additions and 64 deletions

View File

@ -17,6 +17,7 @@ limitations under the License.
package v1alpha1 package v1alpha1
import ( import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
) )
@ -25,7 +26,7 @@ import (
// BackupSessionSpec defines the desired state of BackupSession // BackupSessionSpec defines the desired state of BackupSession
type BackupSessionSpec struct { type BackupSessionSpec struct {
Ref string `json:"ref"` Ref corev1.ObjectReference `json:"ref"`
} }
// BackupSessionStatus defines the observed state of BackupSession // BackupSessionStatus defines the observed state of BackupSession
@ -45,7 +46,7 @@ type BackupSessionStatus struct {
// +kubebuilder:object:root=true // +kubebuilder:object:root=true
// +kubebuilder:resource:shortName="bs" // +kubebuilder:resource:shortName="bs"
// +kubebuilder:subresource:status // +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Ref",type=string,JSONPath=`.spec.ref` // +kubebuilder:printcolumn:name="Ref",type=string,JSONPath=`.spec.ref.name`
// +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state` // +kubebuilder:printcolumn:name="State",type=string,JSONPath=`.status.state`
// +kubebuilder:printcolumn:name="Started",type=string,format=date-time,JSONPath=`.status.startTime` // +kubebuilder:printcolumn:name="Started",type=string,format=date-time,JSONPath=`.status.startTime`
// +kubebuilder:printcolumn:name="Keep",type=string,JSONPath=`.status.keep` // +kubebuilder:printcolumn:name="Keep",type=string,JSONPath=`.status.keep`

View File

@ -10,6 +10,7 @@ const (
New SessionState = "New" New SessionState = "New"
Init SessionState = "Initializing" Init SessionState = "Initializing"
Running SessionState = "Running" Running SessionState = "Running"
Waiting SessionState = "Waiting"
Finalize SessionState = "Finalizing" Finalize SessionState = "Finalizing"
Success SessionState = "Success" Success SessionState = "Success"
Failure SessionState = "Failure" Failure SessionState = "Failure"

View File

@ -17,26 +17,30 @@ limitations under the License.
package v1alpha1 package v1alpha1
import ( import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
//"k8s.io/apimachinery/pkg/types" //"k8s.io/apimachinery/pkg/types"
) )
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN! type BackupSessionRef struct {
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized. // +optional
Ref corev1.ObjectReference `json:"ref,omitempty"`
// +optional
Spec BackupSessionSpec `json:"spec,omitempty"`
// +optional
Status BackupSessionStatus `json:"status,omitempty"`
}
// RestoreSessionSpec defines the desired state of RestoreSession // RestoreSessionSpec defines the desired state of RestoreSession
type RestoreSessionSpec struct { type RestoreSessionSpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster BackupSessionRef `json:"backupSession"`
// Important: Run "make" to regenerate code after modifying this file //Ref string `json:"backupSessionRef"`
// +optional
Ref string `json:"backupSessionRef"` //Targets []TargetStatus `json:"target,omitempty"`
} }
// RestoreSessionStatus defines the observed state of RestoreSession // RestoreSessionStatus defines the observed state of RestoreSession
type RestoreSessionStatus struct { type RestoreSessionStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
// +optional // +optional
SessionState `json:"state,omitempty"` SessionState `json:"state,omitempty"`
// +optional // +optional

View File

@ -207,9 +207,28 @@ func (in *BackupSessionList) DeepCopyObject() runtime.Object {
return nil return nil
} }
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *BackupSessionRef) DeepCopyInto(out *BackupSessionRef) {
*out = *in
out.Ref = in.Ref
out.Spec = in.Spec
in.Status.DeepCopyInto(&out.Status)
}
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupSessionRef.
func (in *BackupSessionRef) DeepCopy() *BackupSessionRef {
if in == nil {
return nil
}
out := new(BackupSessionRef)
in.DeepCopyInto(out)
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *BackupSessionSpec) DeepCopyInto(out *BackupSessionSpec) { func (in *BackupSessionSpec) DeepCopyInto(out *BackupSessionSpec) {
*out = *in *out = *in
out.Ref = in.Ref
} }
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupSessionSpec. // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new BackupSessionSpec.
@ -436,7 +455,7 @@ func (in *RestoreSession) DeepCopyInto(out *RestoreSession) {
*out = *in *out = *in
out.TypeMeta = in.TypeMeta out.TypeMeta = in.TypeMeta
in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) in.ObjectMeta.DeepCopyInto(&out.ObjectMeta)
out.Spec = in.Spec in.Spec.DeepCopyInto(&out.Spec)
in.Status.DeepCopyInto(&out.Status) in.Status.DeepCopyInto(&out.Status)
} }
@ -493,6 +512,7 @@ func (in *RestoreSessionList) DeepCopyObject() runtime.Object {
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *RestoreSessionSpec) DeepCopyInto(out *RestoreSessionSpec) { func (in *RestoreSessionSpec) DeepCopyInto(out *RestoreSessionSpec) {
*out = *in *out = *in
in.BackupSessionRef.DeepCopyInto(&out.BackupSessionRef)
} }
// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RestoreSessionSpec. // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new RestoreSessionSpec.

View File

@ -67,7 +67,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
backupConf := &formolv1alpha1.BackupConfiguration{} backupConf := &formolv1alpha1.BackupConfiguration{}
if err := r.Get(ctx, client.ObjectKey{ if err := r.Get(ctx, client.ObjectKey{
Namespace: backupSession.Namespace, Namespace: backupSession.Namespace,
Name: backupSession.Spec.Ref, Name: backupSession.Spec.Ref.Name,
}, backupConf); err != nil { }, backupConf); err != nil {
log.Error(err, "unable to get backupConfiguration") log.Error(err, "unable to get backupConfiguration")
return ctrl.Result{}, client.IgnoreNotFound(err) return ctrl.Result{}, client.IgnoreNotFound(err)
@ -281,7 +281,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
monthlyBackups.Counter = backupConf.Spec.Keep.Monthly monthlyBackups.Counter = backupConf.Spec.Keep.Monthly
yearlyBackups.Counter = backupConf.Spec.Keep.Yearly yearlyBackups.Counter = backupConf.Spec.Keep.Yearly
for _, session := range backupSessionList.Items { for _, session := range backupSessionList.Items {
if session.Spec.Ref != backupConf.Name { if session.Spec.Ref.Name != backupConf.Name {
continue continue
} }
deleteSession := true deleteSession := true

View File

@ -6,6 +6,7 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
//corev1 "k8s.io/api/core/v1" //corev1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
) )
@ -29,7 +30,9 @@ var _ = Describe("Testing BackupSession controller", func() {
Namespace: TestNamespace, Namespace: TestNamespace,
}, },
Spec: formolv1alpha1.BackupSessionSpec{ Spec: formolv1alpha1.BackupSessionSpec{
Ref: TestBackupConfName, Ref: corev1.ObjectReference{
Name: TestBackupConfName,
},
}, },
} }
}) })

View File

@ -26,6 +26,7 @@ import (
appsv1 "k8s.io/api/apps/v1" appsv1 "k8s.io/api/apps/v1"
batchv1 "k8s.io/api/batch/v1" batchv1 "k8s.io/api/batch/v1"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime" ctrl "sigs.k8s.io/controller-runtime"
@ -60,20 +61,31 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
log.Error(err, "unable to get restoresession") log.Error(err, "unable to get restoresession")
return ctrl.Result{}, client.IgnoreNotFound(err) return ctrl.Result{}, client.IgnoreNotFound(err)
} }
log.V(1).Info("got restoresession", "restoreSession", restoreSession)
// Get the BackupSession the RestoreSession references // Get the BackupSession the RestoreSession references
backupSession := &formolv1alpha1.BackupSession{} backupSession := &formolv1alpha1.BackupSession{}
if err := r.Get(ctx, client.ObjectKey{ if err := r.Get(ctx, client.ObjectKey{
Namespace: restoreSession.Namespace, Namespace: restoreSession.Namespace,
Name: restoreSession.Spec.Ref}, backupSession); err != nil { Name: restoreSession.Spec.BackupSessionRef.Ref.Name,
log.Error(err, "unable to get backupsession", "restoresession", restoreSession.Spec) }, backupSession); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err) if errors.IsNotFound(err) {
backupSession = &formolv1alpha1.BackupSession{
Spec: restoreSession.Spec.BackupSessionRef.Spec,
Status: restoreSession.Spec.BackupSessionRef.Status,
}
log.V(1).Info("generated backupsession", "backupsession", backupSession)
} else {
log.Error(err, "unable to get backupsession", "restoresession", restoreSession.Spec)
return ctrl.Result{}, client.IgnoreNotFound(err)
}
} }
// Get the BackupConfiguration linked to the BackupSession // Get the BackupConfiguration linked to the BackupSession
backupConf := &formolv1alpha1.BackupConfiguration{} backupConf := &formolv1alpha1.BackupConfiguration{}
if err := r.Get(ctx, client.ObjectKey{ if err := r.Get(ctx, client.ObjectKey{
Namespace: backupSession.Namespace, Namespace: backupSession.Spec.Ref.Namespace,
Name: backupSession.Spec.Ref}, backupConf); err != nil { Name: backupSession.Spec.Ref.Name,
log.Error(err, "unable to get backupConfiguration") }, backupConf); err != nil {
log.Error(err, "unable to get backupConfiguration", "name", backupSession.Spec.Ref, "namespace", backupSession.Namespace)
return ctrl.Result{}, client.IgnoreNotFound(err) return ctrl.Result{}, client.IgnoreNotFound(err)
} }
@ -229,50 +241,50 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
return nil return nil
} }
} }
var snapshotId string
for _, targetStatus := range backupSession.Status.Targets { for _, targetStatus := range backupSession.Status.Targets {
if targetStatus.Name == target.Name && targetStatus.Kind == target.Kind { if targetStatus.Name == target.Name && targetStatus.Kind == target.Kind {
snapshotId = targetStatus.SnapshotId snapshotId := targetStatus.SnapshotId
restoreSessionEnv := []corev1.EnvVar{
corev1.EnvVar{
Name: formolv1alpha1.TARGET_NAME,
Value: target.Name,
},
corev1.EnvVar{
Name: formolv1alpha1.RESTORESESSION_NAME,
Value: restoreSession.Name,
},
corev1.EnvVar{
Name: formolv1alpha1.RESTORESESSION_NAMESPACE,
Value: restoreSession.Namespace,
},
}
initContainer := corev1.Container{
Name: RESTORESESSION,
Image: formolutils.FORMOLCLI,
Args: []string{"volume", "restore", "--snapshot-id", snapshotId},
VolumeMounts: target.VolumeMounts,
Env: restoreSessionEnv,
}
repo := &formolv1alpha1.Repo{}
if err := r.Get(ctx, client.ObjectKey{
Namespace: backupConf.Namespace,
Name: backupConf.Spec.Repository,
}, repo); err != nil {
log.Error(err, "unable to get Repo from BackupConfiguration")
return err
}
// S3 backing storage
initContainer.Env = append(initContainer.Env, formolutils.ConfigureResticEnvVar(backupConf, repo)...)
deployment.Spec.Template.Spec.InitContainers = append([]corev1.Container{initContainer},
deployment.Spec.Template.Spec.InitContainers...)
if err := r.Update(ctx, deployment); err != nil {
log.Error(err, "unable to update deployment")
return err
}
return nil
} }
} }
restoreSessionEnv := []corev1.EnvVar{
corev1.EnvVar{
Name: formolv1alpha1.TARGET_NAME,
Value: target.Name,
},
corev1.EnvVar{
Name: formolv1alpha1.RESTORESESSION_NAME,
Value: restoreSession.Name,
},
corev1.EnvVar{
Name: formolv1alpha1.RESTORESESSION_NAMESPACE,
Value: restoreSession.Namespace,
},
}
initContainer := corev1.Container{
Name: RESTORESESSION,
Image: formolutils.FORMOLCLI,
Args: []string{"volume", "restore", "--snapshot-id", snapshotId},
VolumeMounts: target.VolumeMounts,
Env: restoreSessionEnv,
}
repo := &formolv1alpha1.Repo{}
if err := r.Get(ctx, client.ObjectKey{
Namespace: backupConf.Namespace,
Name: backupConf.Spec.Repository,
}, repo); err != nil {
log.Error(err, "unable to get Repo from BackupConfiguration")
return err
}
// S3 backing storage
initContainer.Env = append(initContainer.Env, formolutils.ConfigureResticEnvVar(backupConf, repo)...)
deployment.Spec.Template.Spec.InitContainers = append([]corev1.Container{initContainer},
deployment.Spec.Template.Spec.InitContainers...)
if err := r.Update(ctx, deployment); err != nil {
log.Error(err, "unable to update deployment")
return err
}
return nil return nil
} }
@ -333,7 +345,7 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
} }
} }
case formolv1alpha1.Running: case formolv1alpha1.Running:
currentTargetStatus := restoreSession.Status.Targets[len(restoreSession.Status.Targets)-1] currentTargetStatus := &restoreSession.Status.Targets[len(restoreSession.Status.Targets)-1]
switch currentTargetStatus.SessionState { switch currentTargetStatus.SessionState {
case formolv1alpha1.Failure: case formolv1alpha1.Failure:
log.V(0).Info("last restore task failed. Stop here", "target", currentTargetStatus.Name) log.V(0).Info("last restore task failed. Stop here", "target", currentTargetStatus.Name)
@ -345,6 +357,32 @@ func (r *RestoreSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, err
case formolv1alpha1.Running: case formolv1alpha1.Running:
log.V(0).Info("task is still running", "target", currentTargetStatus.Name) log.V(0).Info("task is still running", "target", currentTargetStatus.Name)
return ctrl.Result{}, nil return ctrl.Result{}, nil
case formolv1alpha1.Waiting:
target := backupConf.Spec.Targets[len(restoreSession.Status.Targets)-1]
if target.Kind == formolv1alpha1.SidecarKind {
deployment := &appsv1.Deployment{}
if err := r.Get(context.Background(), client.ObjectKey{
Namespace: restoreSession.Namespace,
Name: target.Name,
}, deployment); err != nil {
log.Error(err, "unable to get deployment")
return ctrl.Result{}, err
}
if deployment.Status.ReadyReplicas == *deployment.Spec.Replicas {
log.V(0).Info("The deployment is ready. We can resume the backup")
currentTargetStatus.SessionState = formolv1alpha1.Finalize
if err := r.Status().Update(ctx, restoreSession); err != nil {
log.Error(err, "unable to update restoresession")
return ctrl.Result{}, err
}
} else {
log.V(0).Info("Waiting for the sidecar to come back")
return ctrl.Result{RequeueAfter: 10 * time.Second}, nil
}
} else {
log.V(0).Info("not a SidecarKind. Ignoring Waiting")
}
case formolv1alpha1.Success: case formolv1alpha1.Success:
_ = endTask() _ = endTask()
log.V(0).Info("last task was a success. start a new one", "target", currentTargetStatus) log.V(0).Info("last task was a success. start a new one", "target", currentTargetStatus)

View File

@ -5,6 +5,7 @@ import (
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1" formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/types"
) )
@ -28,7 +29,11 @@ var _ = Describe("Testing RestoreSession controller", func() {
Namespace: TestNamespace, Namespace: TestNamespace,
}, },
Spec: formolv1alpha1.RestoreSessionSpec{ Spec: formolv1alpha1.RestoreSessionSpec{
Ref: TestBackupSessionName, BackupSessionRef: formolv1alpha1.BackupSessionRef{
Ref: corev1.ObjectReference{
Name: TestBackupSessionName,
},
},
}, },
} }
}) })

View File

@ -218,7 +218,10 @@ var (
Namespace: TestNamespace, Namespace: TestNamespace,
}, },
Spec: formolv1alpha1.BackupSessionSpec{ Spec: formolv1alpha1.BackupSessionSpec{
Ref: TestBackupConfName, Ref: corev1.ObjectReference{
Name: TestBackupConfName,
Namespace: TestNamespace,
},
}, },
} }
) )

View File

@ -75,6 +75,16 @@ spec:
--- ---
apiVersion: formol.desmojim.fr/v1alpha1 apiVersion: formol.desmojim.fr/v1alpha1
kind: Function kind: Function
metadata:
name: restore-pg
namespace: demo
spec:
name: restore-pg
image: desmo999r/formolcli:latest
args: ["postgres", "restore", "--hostname", $(PGHOST), "--database", $(PGDATABASE), "--username", $(PGUSER), "--password", $(PGPASSWD), "--file", "/output/backup-pg.sql"]
---
apiVersion: formol.desmojim.fr/v1alpha1
kind: Function
metadata: metadata:
name: backup-pg name: backup-pg
namespace: demo namespace: demo
@ -82,3 +92,21 @@ spec:
name: backup-pg name: backup-pg
image: desmo999r/formolcli:latest image: desmo999r/formolcli:latest
args: ["postgres", "backup", "--hostname", $(PGHOST), "--database", $(PGDATABASE), "--username", $(PGUSER), "--password", $(PGPASSWD), "--file", "/output/backup-pg.sql"] args: ["postgres", "backup", "--hostname", $(PGHOST), "--database", $(PGDATABASE), "--username", $(PGUSER), "--password", $(PGPASSWD), "--file", "/output/backup-pg.sql"]
---
apiVersion: formol.desmojim.fr/v1alpha1
kind: Function
metadata:
name: maintenance-off
namespace: demo
spec:
name: maintenance-off
command: ["/bin/bash", "-c", "echo $(date +%Y/%m/%d-%H:%M:%S) maintenance-off >> /data/logs.txt"]
---
apiVersion: formol.desmojim.fr/v1alpha1
kind: Function
metadata:
name: maintenance-on
namespace: demo
spec:
name: maintenance-on
command: ["/bin/bash", "-c", "echo $(date +%Y/%m/%d-%H:%M:%S) maintenance-on >> /data/logs.txt"]

View File

@ -11,6 +11,10 @@ spec:
- kind: Sidecar - kind: Sidecar
apiVersion: v1 apiVersion: v1
name: nginx-deployment name: nginx-deployment
steps:
- name: maintenance-on
- name: maintenance-off
finalize: true
volumeMounts: volumeMounts:
- name: demo-data - name: demo-data
mountPath: /data mountPath: /data

View File

@ -0,0 +1,11 @@
apiVersion: formol.desmojim.fr/v1alpha1
kind: RestoreSession
metadata:
namespace: demo
name: restore-demo
spec:
backupSession:
ref:
name: backupsession-backup-demo-1619904678
namespace: demo