fixed conflict

This commit is contained in:
Jean-Marc ANDRE 2023-02-23 22:44:46 +01:00
parent b330e2cfdd
commit 6b0add3fdf
3 changed files with 54 additions and 57 deletions

View File

@ -47,9 +47,11 @@ type Step struct {
} }
type TargetContainer struct { type TargetContainer struct {
Name string `json:"name"` Name string `json:"name"`
// +optional
Paths []string `json:"paths,omitempty"` Paths []string `json:"paths,omitempty"`
Steps []Step `json:"steps,omitempty"` // +optional
Steps []Step `json:"steps,omitempty"`
} }
type Target struct { type Target struct {

View File

@ -95,18 +95,11 @@ func (r *BackupConfigurationReconciler) Reconcile(ctx context.Context, req ctrl.
} }
for _, target := range backupConf.Spec.Targets { for _, target := range backupConf.Spec.Targets {
switch target.BackupType { if err := r.addSidecar(backupConf, target); err != nil {
case formolv1alpha1.OnlineKind: r.Log.Error(err, "unable to add online sidecar")
if err := r.addOnlineSidecar(backupConf, target); err != nil { return ctrl.Result{}, err
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 volume
case formolv1alpha1.SnapshotKind:
// TODO: add a sidecar to run the steps
} }
backupConf.Status.ActiveSidecar = true
} }
if err := r.Status().Update(ctx, &backupConf); err != nil { if err := r.Status().Update(ctx, &backupConf); err != nil {

View File

@ -213,42 +213,7 @@ func hasSidecar(podSpec *corev1.PodSpec) bool {
return false return false
} }
func (r *BackupConfigurationReconciler) addOnlineSidecar(backupConf formolv1alpha1.BackupConfiguration, target formolv1alpha1.Target) (err error) { func (r *BackupConfigurationReconciler) addSidecar(backupConf formolv1alpha1.BackupConfiguration, target formolv1alpha1.Target) (err error) {
addTags := func(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (sidecarPaths []string, vms []corev1.VolumeMount) {
for i, container := range podSpec.Containers {
for _, targetContainer := range target.Containers {
if targetContainer.Name == container.Name {
// Found a target container. Tag it.
podSpec.Containers[i].Env = append(container.Env, corev1.EnvVar{
Name: formolv1alpha1.TARGETCONTAINER_TAG,
Value: container.Name,
})
// targetContainer.Paths are the paths to backup
// We have to find what volumes are mounted under those paths
// and mount them under a path that exists in the sidecar container
for i, path := range targetContainer.Paths {
vm := corev1.VolumeMount{ReadOnly: true}
var longest int = 0
var sidecarPath string
for _, volumeMount := range container.VolumeMounts {
// if strings.HasPrefix(path, volumeMount.MountPath) && len(volumeMount.MountPath) > longest {
if rel, err := filepath.Rel(volumeMount.MountPath, path); err == nil && len(volumeMount.MountPath) > longest {
longest = len(volumeMount.MountPath)
vm.Name = volumeMount.Name
vm.MountPath = fmt.Sprintf("/%s%d", formolv1alpha1.BACKUP_PREFIX_PATH, i)
vm.SubPath = volumeMount.SubPath
sidecarPath = filepath.Join(vm.MountPath, rel)
}
}
vms = append(vms, vm)
sidecarPaths = append(sidecarPaths, sidecarPath)
}
}
}
}
return
}
repo := formolv1alpha1.Repo{} repo := formolv1alpha1.Repo{}
if err = r.Get(r.Context, client.ObjectKey{ if err = r.Get(r.Context, client.ObjectKey{
Namespace: backupConf.Namespace, Namespace: backupConf.Namespace,
@ -303,13 +268,17 @@ func (r *BackupConfigurationReconciler) addOnlineSidecar(backupConf formolv1alph
r.Log.Error(err, "unable to create RBAC for the sidecar container") r.Log.Error(err, "unable to create RBAC for the sidecar container")
return return
} }
sidecarPaths, vms := addTags(targetPodSpec, target) switch target.BackupType {
sidecar.Env = append(sidecar.Env, corev1.EnvVar{ case formolv1alpha1.OnlineKind:
Name: formolv1alpha1.BACKUP_PATHS, sidecarPaths, vms := addOnlineSidecarTags(targetPodSpec, target)
Value: strings.Join(sidecarPaths, string(os.PathListSeparator)), sidecar.Env = append(sidecar.Env, corev1.EnvVar{
}) Name: formolv1alpha1.BACKUP_PATHS,
Value: strings.Join(sidecarPaths, string(os.PathListSeparator)),
})
sidecar.VolumeMounts = vms
}
if repo.Spec.Backend.Local != nil { if repo.Spec.Backend.Local != nil {
sidecar.VolumeMounts = append(vms, corev1.VolumeMount{ sidecar.VolumeMounts = append(sidecar.VolumeMounts, corev1.VolumeMount{
Name: formolv1alpha1.RESTIC_REPO_VOLUME, Name: formolv1alpha1.RESTIC_REPO_VOLUME,
MountPath: formolv1alpha1.RESTIC_REPO_PATH, MountPath: formolv1alpha1.RESTIC_REPO_PATH,
}) })
@ -317,8 +286,6 @@ func (r *BackupConfigurationReconciler) addOnlineSidecar(backupConf formolv1alph
Name: formolv1alpha1.RESTIC_REPO_VOLUME, Name: formolv1alpha1.RESTIC_REPO_VOLUME,
VolumeSource: repo.Spec.Backend.Local.VolumeSource, VolumeSource: repo.Spec.Backend.Local.VolumeSource,
}) })
} else {
sidecar.VolumeMounts = vms
} }
// The sidecar definition is complete. Add it to the targetObject // The sidecar definition is complete. Add it to the targetObject
@ -432,3 +399,38 @@ func (r *BackupConfigurationReconciler) createRBACSidecar(sa corev1.ServiceAccou
} }
return nil return nil
} }
func addOnlineSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (sidecarPaths []string, vms []corev1.VolumeMount) {
for i, container := range podSpec.Containers {
for _, targetContainer := range target.Containers {
if targetContainer.Name == container.Name {
// Found a target container. Tag it.
podSpec.Containers[i].Env = append(container.Env, corev1.EnvVar{
Name: formolv1alpha1.TARGETCONTAINER_TAG,
Value: container.Name,
})
// targetContainer.Paths are the paths to backup
// We have to find what volumes are mounted under those paths
// and mount them under a path that exists in the sidecar container
for i, path := range targetContainer.Paths {
vm := corev1.VolumeMount{ReadOnly: true}
var longest int = 0
var sidecarPath string
for _, volumeMount := range container.VolumeMounts {
// if strings.HasPrefix(path, volumeMount.MountPath) && len(volumeMount.MountPath) > longest {
if rel, err := filepath.Rel(volumeMount.MountPath, path); err == nil && len(volumeMount.MountPath) > longest {
longest = len(volumeMount.MountPath)
vm.Name = volumeMount.Name
vm.MountPath = fmt.Sprintf("/%s%d", formolv1alpha1.BACKUP_PREFIX_PATH, i)
vm.SubPath = volumeMount.SubPath
sidecarPath = filepath.Join(vm.MountPath, rel)
}
}
vms = append(vms, vm)
sidecarPaths = append(sidecarPaths, sidecarPath)
}
}
}
}
return
}