GetVolumeMounts was not working

This commit is contained in:
Jean-Marc ANDRE 2023-04-20 09:21:16 +02:00
parent ae6b682034
commit 1b31b497a0

View File

@ -3,7 +3,9 @@ package v1alpha1
import ( import (
"fmt" "fmt"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
"os"
"path/filepath" "path/filepath"
"strings"
) )
const ( const (
@ -57,16 +59,21 @@ func GetVolumeMounts(container corev1.Container, targetContainer TargetContainer
vm := corev1.VolumeMount{ReadOnly: true} vm := corev1.VolumeMount{ReadOnly: true}
var longest int = 0 var longest int = 0
var sidecarPath string var sidecarPath string
path = filepath.Clean(path)
splitPath := strings.Split(path, string(os.PathSeparator))
for _, volumeMount := range container.VolumeMounts { for _, volumeMount := range container.VolumeMounts {
// if strings.HasPrefix(path, volumeMount.MountPath) && len(volumeMount.MountPath) > longest { splitMountPath := strings.Split(volumeMount.MountPath, string(os.PathSeparator))
if rel, err := filepath.Rel(volumeMount.MountPath, path); err == nil && len(volumeMount.MountPath) > longest { for j, pathItem := range splitMountPath {
longest = len(volumeMount.MountPath) if j < len(splitPath) && pathItem == splitPath[j] && j > longest {
longest = j
vm.Name = volumeMount.Name vm.Name = volumeMount.Name
vm.MountPath = fmt.Sprintf("/%s%d", BACKUP_PREFIX_PATH, i) vm.MountPath = fmt.Sprintf("/%s%d", BACKUP_PREFIX_PATH, i)
vm.SubPath = volumeMount.SubPath vm.SubPath = volumeMount.SubPath
rel, _ := filepath.Rel(volumeMount.MountPath, path)
sidecarPath = filepath.Join(vm.MountPath, rel) sidecarPath = filepath.Join(vm.MountPath, rel)
} }
} }
}
vms = append(vms, vm) vms = append(vms, vm)
sidecarPaths = append(sidecarPaths, sidecarPath) sidecarPaths = append(sidecarPaths, sidecarPath)
} }