Compare commits
2 Commits
9f40d2eb6c
...
86417391d7
| Author | SHA1 | Date | |
|---|---|---|---|
| 86417391d7 | |||
| 322c712a37 |
@ -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")
|
||||||
backupsession.CreateBackupSession(corev1.ObjectReference{
|
session.CreateBackupSession(corev1.ObjectReference{
|
||||||
Namespace: namespace,
|
Namespace: namespace,
|
||||||
Name: name,
|
Name: name,
|
||||||
})
|
})
|
||||||
|
|||||||
@ -4,7 +4,9 @@ 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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -24,8 +26,96 @@ func (r *RestoreSessionReconciler) Reconcile(ctx context.Context, req ctrl.Reque
|
|||||||
}
|
}
|
||||||
return ctrl.Result{}, err
|
return ctrl.Result{}, err
|
||||||
}
|
}
|
||||||
switch restoreSession.Status.SessionState {
|
if len(restoreSession.Status.Targets) == 0 {
|
||||||
|
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 3486ad2efe7bcf40990212299a87aaa70f88965f
|
Subproject commit 7e007bfd44cc0041a74760c82b752aa2a93242fb
|
||||||
41
session/create.go
Normal file
41
session/create.go
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,13 +1,10 @@
|
|||||||
package backupsession
|
package session
|
||||||
|
|
||||||
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"
|
||||||
@ -16,9 +13,6 @@ 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 (
|
||||||
@ -51,32 +45,3 @@ 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user