Compare commits
No commits in common. "775e42259754defa5b3fcc9417c89a70ebac6120" and "1e44ea838d091c63bf25e97db0679c0db407301d" have entirely different histories.
775e422597
...
1e44ea838d
@ -49,20 +49,17 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BackupSessionUpdateStatus(state formolv1alpha1.SessionState, snapshotId string, duration time.Duration) error {
|
func BackupSessionUpdateStatus(state formolv1alpha1.BackupState, snapshotId string, duration time.Duration) error {
|
||||||
log := logger.WithName("BackupSessionUpdateStatus")
|
log := logger.WithName("BackupSessionUpdateStatus")
|
||||||
targetName := os.Getenv("TARGET_NAME")
|
targetName := os.Getenv("TARGET_NAME")
|
||||||
backupSession := &formolv1alpha1.BackupSession{}
|
backupSession := &formolv1alpha1.BackupSession{}
|
||||||
if err := cl.Get(context.Background(), client.ObjectKey{
|
cl.Get(context.Background(), client.ObjectKey{
|
||||||
Namespace: os.Getenv("BACKUPSESSION_NAMESPACE"),
|
Namespace: os.Getenv("BACKUPSESSION_NAMESPACE"),
|
||||||
Name: os.Getenv("BACKUPSESSION_NAME"),
|
Name: os.Getenv("BACKUPSESSION_NAME"),
|
||||||
}, backupSession); err != nil {
|
}, backupSession)
|
||||||
log.Error(err, "unable to get backupsession", "BACKUPSESSION_NAME", os.Getenv("BACKUPSESSION_NAME"), "BACKUPSESSION_NAMESPACE", os.Getenv("BACKUPSESSION_NAMESPACE"))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
for i, target := range backupSession.Status.Targets {
|
for i, target := range backupSession.Status.Targets {
|
||||||
if target.Name == targetName {
|
if target.Name == targetName {
|
||||||
backupSession.Status.Targets[i].SessionState = state
|
backupSession.Status.Targets[i].BackupState = state
|
||||||
backupSession.Status.Targets[i].SnapshotId = snapshotId
|
backupSession.Status.Targets[i].SnapshotId = snapshotId
|
||||||
backupSession.Status.Targets[i].Duration = &metav1.Duration{Duration: duration}
|
backupSession.Status.Targets[i].Duration = &metav1.Duration{Duration: duration}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,9 +5,11 @@ import (
|
|||||||
"github.com/go-logr/logr"
|
"github.com/go-logr/logr"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
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/event"
|
"sigs.k8s.io/controller-runtime/pkg/event"
|
||||||
|
"sigs.k8s.io/controller-runtime/pkg/log/zap"
|
||||||
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,8 +17,59 @@ import (
|
|||||||
"github.com/desmo999r/formolcli/pkg/backup"
|
"github.com/desmo999r/formolcli/pkg/backup"
|
||||||
formolcliutils "github.com/desmo999r/formolcli/pkg/utils"
|
formolcliutils "github.com/desmo999r/formolcli/pkg/utils"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
"k8s.io/client-go/rest"
|
||||||
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
deploymentName = ""
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
log := zap.New(zap.UseDevMode(true)).WithName("init")
|
||||||
|
|
||||||
|
namespace := os.Getenv("POD_NAMESPACE")
|
||||||
|
if namespace == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
config, err := rest.InClusterConfig()
|
||||||
|
if err != nil {
|
||||||
|
config, err = clientcmd.BuildConfigFromFlags("", filepath.Join(os.Getenv("HOME"), ".kube", "config"))
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err, "unable to get config")
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
clientset, err := kubernetes.NewForConfig(config)
|
||||||
|
if err != nil {
|
||||||
|
panic("unable to get clientset")
|
||||||
|
}
|
||||||
|
|
||||||
|
hostname := os.Getenv("POD_NAME")
|
||||||
|
if hostname == "" {
|
||||||
|
panic("unable to get hostname")
|
||||||
|
}
|
||||||
|
|
||||||
|
pod, err := clientset.CoreV1().Pods(namespace).Get(context.Background(), hostname, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
log.Error(err, "unable to get pod")
|
||||||
|
panic("unable to get pod")
|
||||||
|
}
|
||||||
|
|
||||||
|
podOwner := metav1.GetControllerOf(pod)
|
||||||
|
replicasetList, err := clientset.AppsV1().ReplicaSets(namespace).List(context.Background(), metav1.ListOptions{
|
||||||
|
FieldSelector: "metadata.name=" + string(podOwner.Name),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
panic("unable to get replicaset" + err.Error())
|
||||||
|
}
|
||||||
|
for _, replicaset := range replicasetList.Items {
|
||||||
|
replicasetOwner := metav1.GetControllerOf(&replicaset)
|
||||||
|
deploymentName = replicasetOwner.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// BackupSessionReconciler reconciles a BackupSession object
|
// BackupSessionReconciler reconciles a BackupSession object
|
||||||
type BackupSessionReconciler struct {
|
type BackupSessionReconciler struct {
|
||||||
client.Client
|
client.Client
|
||||||
@ -43,7 +96,6 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
|
|||||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
deploymentName := os.Getenv("POD_DEPLOYMENT")
|
|
||||||
for _, target := range backupConf.Spec.Targets {
|
for _, target := range backupConf.Spec.Targets {
|
||||||
switch target.Kind {
|
switch target.Kind {
|
||||||
case "Deployment":
|
case "Deployment":
|
||||||
@ -51,7 +103,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
|
|||||||
for i, status := range backupSession.Status.Targets {
|
for i, status := range backupSession.Status.Targets {
|
||||||
if status.Name == target.Name {
|
if status.Name == target.Name {
|
||||||
log.V(0).Info("It's for us!", "target", target)
|
log.V(0).Info("It's for us!", "target", target)
|
||||||
switch status.SessionState {
|
switch status.BackupState {
|
||||||
case formolv1alpha1.New:
|
case formolv1alpha1.New:
|
||||||
// TODO: Run beforeBackup
|
// TODO: Run beforeBackup
|
||||||
log.V(0).Info("New session, run the beforeBackup hooks if any")
|
log.V(0).Info("New session, run the beforeBackup hooks if any")
|
||||||
@ -59,7 +111,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
|
|||||||
if err := formolcliutils.RunBeforeBackup(target); err != nil {
|
if err := formolcliutils.RunBeforeBackup(target); err != nil {
|
||||||
result = formolv1alpha1.Failure
|
result = formolv1alpha1.Failure
|
||||||
}
|
}
|
||||||
backupSession.Status.Targets[i].SessionState = result
|
backupSession.Status.Targets[i].BackupState = result
|
||||||
log.V(1).Info("current backupSession status", "status", backupSession.Status)
|
log.V(1).Info("current backupSession status", "status", backupSession.Status)
|
||||||
if err := r.Status().Update(ctx, backupSession); err != nil {
|
if err := r.Status().Update(ctx, backupSession); err != nil {
|
||||||
log.Error(err, "unable to update backupsession status")
|
log.Error(err, "unable to update backupsession status")
|
||||||
@ -78,7 +130,7 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
|
|||||||
backupSession.Status.Targets[i].SnapshotId = snapshotId
|
backupSession.Status.Targets[i].SnapshotId = snapshotId
|
||||||
backupSession.Status.Targets[i].Duration = &metav1.Duration{Duration: duration}
|
backupSession.Status.Targets[i].Duration = &metav1.Duration{Duration: duration}
|
||||||
}
|
}
|
||||||
backupSession.Status.Targets[i].SessionState = result
|
backupSession.Status.Targets[i].BackupState = result
|
||||||
log.V(1).Info("current backupSession status", "status", backupSession.Status)
|
log.V(1).Info("current backupSession status", "status", backupSession.Status)
|
||||||
if err := r.Status().Update(ctx, backupSession); err != nil {
|
if err := r.Status().Update(ctx, backupSession); err != nil {
|
||||||
log.Error(err, "unable to update backupsession status")
|
log.Error(err, "unable to update backupsession status")
|
||||||
@ -100,9 +152,10 @@ func (r *BackupSessionReconciler) Reconcile(req ctrl.Request) (ctrl.Result, erro
|
|||||||
func (r *BackupSessionReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
func (r *BackupSessionReconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||||
return ctrl.NewControllerManagedBy(mgr).
|
return ctrl.NewControllerManagedBy(mgr).
|
||||||
For(&formolv1alpha1.BackupSession{}).
|
For(&formolv1alpha1.BackupSession{}).
|
||||||
|
//WithEventFilter(predicate.GenerationChangedPredicate{}). // Don't reconcile when status gets updated
|
||||||
WithEventFilter(predicate.Funcs{
|
WithEventFilter(predicate.Funcs{
|
||||||
CreateFunc: func(e event.CreateEvent) bool { return false },
|
CreateFunc: func(e event.CreateEvent) bool { return false },
|
||||||
DeleteFunc: func(e event.DeleteEvent) bool { return false },
|
DeleteFunc: func(e event.DeleteEvent) bool { return false },
|
||||||
}).
|
}). // Don't reconcile when status gets updated
|
||||||
Complete(r)
|
Complete(r)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user