Compare commits
No commits in common. "55f3dc8dff7dcca0986867517f77f0888315d317" and "6c4d1a749c2585b024e225bf42a741bbc97e7c26" have entirely different histories.
55f3dc8dff
...
6c4d1a749c
@ -23,13 +23,6 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
RESTIC_REPOSITORY = "RESTIC_REPOSITORY"
|
||||
RESTIC_PASSWORD = "RESTIC_PASSWORD"
|
||||
AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID"
|
||||
AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY"
|
||||
)
|
||||
|
||||
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
|
||||
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
|
||||
type S3 struct {
|
||||
@ -90,13 +83,13 @@ func (repo *Repo) GetResticEnv(backupConf BackupConfiguration) []corev1.EnvVar {
|
||||
strings.ToUpper(backupConf.Namespace),
|
||||
strings.ToLower(backupConf.Name))
|
||||
env = append(env, corev1.EnvVar{
|
||||
Name: RESTIC_REPOSITORY,
|
||||
Name: "RESTIC_REPOSITORY",
|
||||
Value: url,
|
||||
})
|
||||
for _, key := range []string{
|
||||
AWS_ACCESS_KEY_ID,
|
||||
AWS_SECRET_ACCESS_KEY,
|
||||
RESTIC_PASSWORD,
|
||||
"AWS_ACCESS_KEY_ID",
|
||||
"AWS_SECRET_ACCESS_KEY",
|
||||
"RESTIC_PASSWORD",
|
||||
} {
|
||||
env = append(env, corev1.EnvVar{
|
||||
Name: key,
|
||||
|
||||
@ -93,20 +93,11 @@ func (r *BackupConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.
|
||||
backupConf.Status.ActiveCronJob = true
|
||||
}
|
||||
|
||||
for _, target := range backupConf.Spec.Targets {
|
||||
switch target.BackupType {
|
||||
case formolv1alpha1.OnlineKind:
|
||||
// TODO: add a sidecar to the pod with the target.Containers[].Paths mounted
|
||||
if err := r.AddOnlineSidecar(backupConf, target); err != nil {
|
||||
r.Log.Error(err, "unable to add online sidecar")
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
backupConf.Status.ActiveSidecar = true
|
||||
case formolv1alpha1.JobKind:
|
||||
// TODO: add a sidecar to the pod with a shared
|
||||
case formolv1alpha1.SnapshotKind:
|
||||
// TOD: add a sidecar to run the steps
|
||||
}
|
||||
if err := r.AddSidecar(backupConf); err != nil {
|
||||
r.Log.Error(err, "unable to add sidecar container")
|
||||
return ctrl.Result{}, err
|
||||
} else {
|
||||
backupConf.Status.ActiveSidecar = true
|
||||
}
|
||||
|
||||
if err := r.Status().Update(ctx, &backupConf); err != nil {
|
||||
|
||||
@ -178,7 +178,11 @@ func (r *BackupConfigurationReconciler) DeleteSidecar(backupConf formolv1alpha1.
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *BackupConfigurationReconciler) AddOnlineSidecar(backupConf formolv1alpha1.BackupConfiguration, target formolv1alpha1.Target) error {
|
||||
func (r *BackupConfigurationReconciler) AddSidecar(backupConf formolv1alpha1.BackupConfiguration) error {
|
||||
// Go through all the 'targets'
|
||||
// the backupType: Online needs a sidecar container for every single listed 'container'
|
||||
// if the backupType is something else than Online, the 'container' will still need a sidecar
|
||||
// if it has 'steps'
|
||||
addTags := func(sideCar *corev1.Container, podSpec *corev1.PodSpec, target formolv1alpha1.Target) bool {
|
||||
for i, container := range podSpec.Containers {
|
||||
if container.Name == formolv1alpha1.SIDECARCONTAINER_NAME {
|
||||
@ -213,60 +217,73 @@ func (r *BackupConfigurationReconciler) AddOnlineSidecar(backupConf formolv1alph
|
||||
return true
|
||||
}
|
||||
|
||||
repo := formolv1alpha1.Repo{}
|
||||
if err := r.Get(r.Context, client.ObjectKey{
|
||||
Namespace: backupConf.Namespace,
|
||||
Name: backupConf.Spec.Repository,
|
||||
}, &repo); err != nil {
|
||||
r.Log.Error(err, "unable to get Repo")
|
||||
return err
|
||||
}
|
||||
r.Log.V(1).Info("Got Repository", "repo", repo)
|
||||
env := repo.GetResticEnv(backupConf)
|
||||
sideCar := corev1.Container{
|
||||
Name: formolv1alpha1.SIDECARCONTAINER_NAME,
|
||||
Image: backupConf.Spec.Image,
|
||||
Args: []string{"backupsession", "server"},
|
||||
Env: append(env,
|
||||
corev1.EnvVar{
|
||||
Name: formolv1alpha1.TARGET_NAME,
|
||||
Value: target.TargetName,
|
||||
},
|
||||
corev1.EnvVar{
|
||||
Name: formolv1alpha1.POD_NAMESPACE,
|
||||
ValueFrom: &corev1.EnvVarSource{
|
||||
FieldRef: &corev1.ObjectFieldSelector{
|
||||
FieldPath: "metadata.namespace",
|
||||
},
|
||||
},
|
||||
}),
|
||||
VolumeMounts: []corev1.VolumeMount{},
|
||||
}
|
||||
switch target.TargetKind {
|
||||
case formolv1alpha1.Deployment:
|
||||
deployment := &appsv1.Deployment{}
|
||||
if err := r.Get(r.Context, client.ObjectKey{
|
||||
Namespace: backupConf.Namespace,
|
||||
Name: target.TargetName,
|
||||
}, deployment); err != nil {
|
||||
r.Log.Error(err, "cannot get deployment", "Deployment", target.TargetName)
|
||||
return err
|
||||
for _, target := range backupConf.Spec.Targets {
|
||||
addSidecar := false
|
||||
for _, targetContainer := range target.Containers {
|
||||
if len(targetContainer.Steps) > 0 {
|
||||
addSidecar = true
|
||||
}
|
||||
}
|
||||
if addTags(&sideCar, &deployment.Spec.Template.Spec, target) {
|
||||
if err := r.createRBACSidecar(corev1.ServiceAccount{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: deployment.Namespace,
|
||||
Name: deployment.Spec.Template.Spec.ServiceAccountName,
|
||||
},
|
||||
}); err != nil {
|
||||
r.Log.Error(err, "unable to create RBAC for the sidecar container")
|
||||
if target.BackupType == formolv1alpha1.OnlineKind {
|
||||
addSidecar = true
|
||||
}
|
||||
if addSidecar {
|
||||
repo := formolv1alpha1.Repo{}
|
||||
if err := r.Get(r.Context, client.ObjectKey{
|
||||
Namespace: backupConf.Namespace,
|
||||
Name: backupConf.Spec.Repository,
|
||||
}, &repo); err != nil {
|
||||
r.Log.Error(err, "unable to get Repo")
|
||||
return err
|
||||
}
|
||||
deployment.Spec.Template.Spec.Containers = append(deployment.Spec.Template.Spec.Containers, sideCar)
|
||||
r.Log.V(1).Info("Updating deployment", "deployment", deployment, "containers", deployment.Spec.Template.Spec.Containers)
|
||||
if err := r.Update(r.Context, deployment); err != nil {
|
||||
r.Log.Error(err, "cannot update deployment", "Deployment", deployment)
|
||||
return err
|
||||
r.Log.V(1).Info("Got Repository", "repo", repo)
|
||||
env := repo.GetResticEnv(backupConf)
|
||||
sideCar := corev1.Container{
|
||||
Name: formolv1alpha1.SIDECARCONTAINER_NAME,
|
||||
Image: backupConf.Spec.Image,
|
||||
Args: []string{"backupsession", "server"},
|
||||
Env: append(env,
|
||||
corev1.EnvVar{
|
||||
Name: formolv1alpha1.TARGET_NAME,
|
||||
Value: target.TargetName,
|
||||
},
|
||||
corev1.EnvVar{
|
||||
Name: formolv1alpha1.POD_NAMESPACE,
|
||||
ValueFrom: &corev1.EnvVarSource{
|
||||
FieldRef: &corev1.ObjectFieldSelector{
|
||||
FieldPath: "metadata.namespace",
|
||||
},
|
||||
},
|
||||
}),
|
||||
VolumeMounts: []corev1.VolumeMount{},
|
||||
}
|
||||
switch target.TargetKind {
|
||||
case formolv1alpha1.Deployment:
|
||||
deployment := &appsv1.Deployment{}
|
||||
if err := r.Get(r.Context, client.ObjectKey{
|
||||
Namespace: backupConf.Namespace,
|
||||
Name: target.TargetName,
|
||||
}, deployment); err != nil {
|
||||
r.Log.Error(err, "cannot get deployment", "Deployment", target.TargetName)
|
||||
return err
|
||||
}
|
||||
if addTags(&sideCar, &deployment.Spec.Template.Spec, target) {
|
||||
if err := r.createRBACSidecar(corev1.ServiceAccount{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Namespace: deployment.Namespace,
|
||||
Name: deployment.Spec.Template.Spec.ServiceAccountName,
|
||||
},
|
||||
}); err != nil {
|
||||
r.Log.Error(err, "unable to create RBAC for the sidecar container")
|
||||
return err
|
||||
}
|
||||
deployment.Spec.Template.Spec.Containers = append(deployment.Spec.Template.Spec.Containers, sideCar)
|
||||
r.Log.V(1).Info("Updating deployment", "deployment", deployment, "containers", deployment.Spec.Template.Spec.Containers)
|
||||
if err := r.Update(r.Context, deployment); err != nil {
|
||||
r.Log.Error(err, "cannot update deployment", "Deployment", deployment)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user