Compare commits
No commits in common. "86417391d79cec0204f2becc8cbf961b055f90f5" and "9f40d2eb6c64d211b3329f6b53eb15a9a7f18de3" have entirely different histories.
86417391d7
...
9f40d2eb6c
@ -1,10 +1,13 @@
|
|||||||
package session
|
package backupsession
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/apimachinery/pkg/types"
|
||||||
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
@ -13,6 +16,9 @@ import (
|
|||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -45,3 +51,32 @@ func init() {
|
|||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateBackupSession(ref corev1.ObjectReference) {
|
||||||
|
log := logger.WithName("CreateBackupSession")
|
||||||
|
log.V(0).Info("CreateBackupSession called")
|
||||||
|
backupConf := formolv1alpha1.BackupConfiguration{}
|
||||||
|
if err := cl.Get(ctx, types.NamespacedName{
|
||||||
|
Namespace: ref.Namespace,
|
||||||
|
Name: ref.Name,
|
||||||
|
}, &backupConf); err != nil {
|
||||||
|
log.Error(err, "unable to get backupconf")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
log.V(0).Info("got backupConf", "backupConf", backupConf)
|
||||||
|
|
||||||
|
backupSession := &formolv1alpha1.BackupSession{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: strings.Join([]string{"backupsession", ref.Name, strconv.FormatInt(time.Now().Unix(), 10)}, "-"),
|
||||||
|
Namespace: ref.Namespace,
|
||||||
|
},
|
||||||
|
Spec: formolv1alpha1.BackupSessionSpec{
|
||||||
|
Ref: ref,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
log.V(1).Info("create backupsession", "backupSession", backupSession)
|
||||||
|
if err := cl.Create(ctx, backupSession); err != nil {
|
||||||
|
log.Error(err, "unable to create backupsession")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,8 +5,8 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/desmo999r/formolcli/backupsession"
|
||||||
"github.com/desmo999r/formolcli/controllers"
|
"github.com/desmo999r/formolcli/controllers"
|
||||||
"github.com/desmo999r/formolcli/session"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
corev1 "k8s.io/api/core/v1"
|
corev1 "k8s.io/api/core/v1"
|
||||||
"os"
|
"os"
|
||||||
@ -19,7 +19,7 @@ var createBackupSessionCmd = &cobra.Command{
|
|||||||
name, _ := cmd.Flags().GetString("name")
|
name, _ := cmd.Flags().GetString("name")
|
||||||
namespace, _ := cmd.Flags().GetString("namespace")
|
namespace, _ := cmd.Flags().GetString("namespace")
|
||||||
fmt.Println("create backupsession called")
|
fmt.Println("create backupsession called")
|
||||||
session.CreateBackupSession(corev1.ObjectReference{
|
backupsession.CreateBackupSession(corev1.ObjectReference{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Name: name,
|
Name: name,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -4,9 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
"os"
|
|
||||||
ctrl "sigs.k8s.io/controller-runtime"
|
ctrl "sigs.k8s.io/controller-runtime"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
||||||
"sigs.k8s.io/controller-runtime/pkg/log"
|
"sigs.k8s.io/controller-runtime/pkg/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -26,96 +24,8 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
|
|||||||
}
|
}
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
if len(restoreSession.Status.Targets) == 0 {
|
switch restoreSession.Status.SessionState {
|
||||||
r.Log.V(0).Info("RestoreSession still being initialized by the main controller. Wait for the next update...")
|
|
||||||
return ctrl.Result{}, nil
|
|
||||||
}
|
|
||||||
// We need the BackupConfiguration to get information about our restore target
|
|
||||||
backupSession := formolv1alpha1.BackupSession{
|
|
||||||
Spec: restoreSession.Spec.BackupSessionRef.Spec,
|
|
||||||
Status: restoreSession.Spec.BackupSessionRef.Status,
|
|
||||||
}
|
|
||||||
backupConf := formolv1alpha1.BackupConfiguration{}
|
|
||||||
err = r.Get(r.Context, client.ObjectKey{
|
|
||||||
Namespace: backupSession.Spec.Ref.Namespace,
|
|
||||||
Name: backupSession.Spec.Ref.Name,
|
|
||||||
}, &backupConf)
|
|
||||||
if err != nil {
|
|
||||||
if errors.IsNotFound(err) {
|
|
||||||
return ctrl.Result{}, nil
|
|
||||||
}
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
|
||||||
r.Namespace = backupConf.Namespace
|
|
||||||
|
|
||||||
// we don't want a copy because we will modify and update it.
|
|
||||||
var target formolv1alpha1.Target
|
|
||||||
var targetStatus *formolv1alpha1.TargetStatus
|
|
||||||
targetName := os.Getenv(formolv1alpha1.TARGET_NAME)
|
|
||||||
|
|
||||||
for i, t := range backupConf.Spec.Targets {
|
|
||||||
if t.TargetName == targetName {
|
|
||||||
target = t
|
|
||||||
targetStatus = &(restoreSession.Status.Targets[i])
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do preliminary checks with the repository
|
|
||||||
if err = r.setResticEnv(backupConf); err != nil {
|
|
||||||
r.Log.Error(err, "unable to set restic env")
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
|
||||||
|
|
||||||
var newSessionState formolv1alpha1.SessionState
|
|
||||||
switch targetStatus.SessionState {
|
|
||||||
case formolv1alpha1.New:
|
case formolv1alpha1.New:
|
||||||
// New session move to Initializing
|
|
||||||
r.Log.V(0).Info("New session. Move to Initializing state")
|
|
||||||
newSessionState = formolv1alpha1.Initializing
|
|
||||||
case formolv1alpha1.Initializing:
|
|
||||||
// Run the initializing Steps and then move to Initialized or Failure
|
|
||||||
r.Log.V(0).Info("Start to run the backup initializing steps is any")
|
|
||||||
// Runs the Steps functions in chroot env
|
|
||||||
if err := r.runInitializeSteps(target); err != nil {
|
|
||||||
r.Log.Error(err, "unable to run the initialization steps")
|
|
||||||
newSessionState = formolv1alpha1.Failure
|
|
||||||
} else {
|
|
||||||
r.Log.V(0).Info("Done with the initializing Steps. Move to Initialized state")
|
|
||||||
newSessionState = formolv1alpha1.Initialized
|
|
||||||
}
|
|
||||||
case formolv1alpha1.Running:
|
|
||||||
// Do the restore and move to Waiting once it is done.
|
|
||||||
// The restore is different if the Backup was an OnlineKind or a JobKind
|
|
||||||
switch target.BackupType {
|
|
||||||
case formolv1alpha1.JobKind:
|
|
||||||
case formolv1alpha1.OnlineKind:
|
|
||||||
// The restore has to be done by an initContainer since the data is mounted RO
|
|
||||||
// We create the initContainer here
|
|
||||||
// Once the the container has rebooted and the initContainer has done its job, it will change the targetStatus to Waiting.
|
|
||||||
targetObject, targetPodSpec := formolv1alpha1.GetTargetObjects(target.TargetKind)
|
|
||||||
if err := r.Get(r.Context, client.ObjectKey{
|
|
||||||
Namespace: backupConf.Namespace,
|
|
||||||
Name: target.TargetName,
|
|
||||||
}, targetObject); err != nil {
|
|
||||||
r.Log.Error(err, "unable to get target objects", "target", target.TargetName)
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
|
||||||
initContainer := corev1.Container {}
|
|
||||||
targetPodSpec.InitContainers = append(targetPodSpec.InitContainers, initContainer)
|
|
||||||
if err := r.Update(r.Context, targetObject); err != nil {
|
|
||||||
r.Log.Error(err, "unable to add the restore init container", "targetObject", targetObject)
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if newSessionState != "" {
|
|
||||||
targetStatus.SessionState = newSessionState
|
|
||||||
err := r.Status().Update(ctx, &restoreSession)
|
|
||||||
if err != nil {
|
|
||||||
r.Log.Error(err, "unable to update RestoreSession status")
|
|
||||||
}
|
|
||||||
return ctrl.Result{}, err
|
|
||||||
}
|
}
|
||||||
return ctrl.Result{}, nil
|
return ctrl.Result{}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
2
formol
2
formol
@ -1 +1 @@
|
|||||||
Subproject commit 7e007bfd44cc0041a74760c82b752aa2a93242fb
|
Subproject commit 3486ad2efe7bcf40990212299a87aaa70f88965f
|
||||||
@ -1,41 +0,0 @@
|
|||||||
package session
|
|
||||||
|
|
||||||
import (
|
|
||||||
formolv1alpha1 "github.com/desmo999r/formol/api/v1alpha1"
|
|
||||||
corev1 "k8s.io/api/core/v1"
|
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
||||||
"k8s.io/apimachinery/pkg/types"
|
|
||||||
"os"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func CreateBackupSession(ref corev1.ObjectReference) {
|
|
||||||
log := logger.WithName("CreateBackupSession")
|
|
||||||
log.V(0).Info("CreateBackupSession called")
|
|
||||||
backupConf := formolv1alpha1.BackupConfiguration{}
|
|
||||||
if err := cl.Get(ctx, types.NamespacedName{
|
|
||||||
Namespace: ref.Namespace,
|
|
||||||
Name: ref.Name,
|
|
||||||
}, &backupConf); err != nil {
|
|
||||||
log.Error(err, "unable to get backupconf")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
log.V(0).Info("got backupConf", "backupConf", backupConf)
|
|
||||||
|
|
||||||
backupSession := &formolv1alpha1.BackupSession{
|
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
|
||||||
Name: strings.Join([]string{"backupsession", ref.Name, strconv.FormatInt(time.Now().Unix(), 10)}, "-"),
|
|
||||||
Namespace: ref.Namespace,
|
|
||||||
},
|
|
||||||
Spec: formolv1alpha1.BackupSessionSpec{
|
|
||||||
Ref: ref,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
log.V(1).Info("create backupsession", "backupSession", backupSession)
|
|
||||||
if err := cl.Create(ctx, backupSession); err != nil {
|
|
||||||
log.Error(err, "unable to create backupsession")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user