rearranged a bit

This commit is contained in:
Jean-Marc ANDRE 2023-04-02 22:04:43 +02:00
parent e54d8abdb9
commit b67d300db2

View File

@ -265,16 +265,29 @@ func (r *BackupConfigurationReconciler) addSidecar(backupConf formolv1alpha1.Bac
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
} }
for i, container := range targetPodSpec.Containers {
for _, targetContainer := range target.Containers {
if targetContainer.Name == container.Name {
// Found a target container. Tag it.
targetPodSpec.Containers[i].Env = append(container.Env, corev1.EnvVar{
Name: formolv1alpha1.TARGETCONTAINER_TAG,
Value: container.Name,
})
switch target.BackupType { switch target.BackupType {
case formolv1alpha1.OnlineKind: case formolv1alpha1.OnlineKind:
sidecarPaths, vms := addOnlineSidecarTags(targetPodSpec, target) sidecarPaths, vms := addOnlineSidecarTags(container, targetContainer)
sidecar.Env = append(sidecar.Env, corev1.EnvVar{ sidecar.Env = append(sidecar.Env, corev1.EnvVar{
Name: formolv1alpha1.BACKUP_PATHS, Name: formolv1alpha1.BACKUP_PATHS,
Value: strings.Join(sidecarPaths, string(os.PathListSeparator)), Value: strings.Join(sidecarPaths, string(os.PathListSeparator)),
}) })
sidecar.VolumeMounts = vms sidecar.VolumeMounts = vms
case formolv1alpha1.JobKind: case formolv1alpha1.JobKind:
sidecar.VolumeMounts = addJobSidecarTags(targetPodSpec, target) sidecar.VolumeMounts = addJobSidecarTags(targetPodSpec, i, targetContainer)
case formolv1alpha1.SnapshotKind:
}
}
}
} }
if repo.Spec.Backend.Local != nil { if repo.Spec.Backend.Local != nil {
sidecar.VolumeMounts = append(sidecar.VolumeMounts, corev1.VolumeMount{ sidecar.VolumeMounts = append(sidecar.VolumeMounts, corev1.VolumeMount{
@ -414,15 +427,7 @@ func (r *BackupConfigurationReconciler) createRBACSidecar(sa corev1.ServiceAccou
return nil return nil
} }
func addJobSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (vms []corev1.VolumeMount) { func addJobSidecarTags(podSpec *corev1.PodSpec, index int, targetContainer formolv1alpha1.TargetContainer) (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,
})
// Create a shared mount between the target and sidecar container // Create a shared mount between the target and sidecar container
// the output of the Job will be saved in the shared volume // the output of the Job will be saved in the shared volume
// and restic will then backup the content of the volume // and restic will then backup the content of the volume
@ -439,7 +444,7 @@ func addJobSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (v
VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}, VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}},
}) })
} }
podSpec.Containers[i].VolumeMounts = append(podSpec.Containers[i].VolumeMounts, corev1.VolumeMount{ podSpec.Containers[index].VolumeMounts = append(podSpec.Containers[index].VolumeMounts, corev1.VolumeMount{
Name: formolv1alpha1.FORMOL_SHARED_VOLUME, Name: formolv1alpha1.FORMOL_SHARED_VOLUME,
MountPath: targetContainer.SharePath, MountPath: targetContainer.SharePath,
}) })
@ -447,21 +452,10 @@ func addJobSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (v
Name: formolv1alpha1.FORMOL_SHARED_VOLUME, Name: formolv1alpha1.FORMOL_SHARED_VOLUME,
MountPath: targetContainer.SharePath, MountPath: targetContainer.SharePath,
}) })
}
}
}
return return
} }
func addOnlineSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target) (sidecarPaths []string, vms []corev1.VolumeMount) { func addOnlineSidecarTags(container corev1.Container, targetContainer formolv1alpha1.TargetContainer) (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 // targetContainer.Paths are the paths to backup
// We have to find what volumes are mounted under those paths // We have to find what volumes are mounted under those paths
// and mount them under a path that exists in the sidecar container // and mount them under a path that exists in the sidecar container
@ -482,8 +476,5 @@ func addOnlineSidecarTags(podSpec *corev1.PodSpec, target formolv1alpha1.Target)
vms = append(vms, vm) vms = append(vms, vm)
sidecarPaths = append(sidecarPaths, sidecarPath) sidecarPaths = append(sidecarPaths, sidecarPath)
} }
}
}
}
return return
} }