From 712886b58e8a3430e8af8d6e2f46bafb8271f037 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Bosdonnat?= Date: Mon, 9 Dec 2024 13:40:57 +0100 Subject: [PATCH] Fix the mirror PV handling If the PV has no claimRef, the generated JSON fragment will miss a value and won't be valid. Output the whole PV as JSON and parse it using the Kubernetes API type. The volume mount for the mirror is also missing in the container. --- mgradm/shared/kubernetes/deployment.go | 8 ++++++- shared/kubernetes/pvc.go | 27 ++++++++--------------- uyuni-tools.changes.cbosdo.k8s-mirror-fix | 1 + 3 files changed, 17 insertions(+), 19 deletions(-) create mode 100644 uyuni-tools.changes.cbosdo.k8s-mirror-fix diff --git a/mgradm/shared/kubernetes/deployment.go b/mgradm/shared/kubernetes/deployment.go index a4bd4af8b..765e41239 100644 --- a/mgradm/shared/kubernetes/deployment.go +++ b/mgradm/shared/kubernetes/deployment.go @@ -80,12 +80,18 @@ func getServerDeployment( } if mirrorPvName != "" { - // Add a mount for the mirror + // Add a volume for the mirror mounts = append(mounts, types.VolumeMount{MountPath: "/mirror", Name: mirrorPvName}) // Add the environment variable for the deployment to use the mirror // This doesn't makes sense for migration as the setup script is not executed envs = append(envs, core.EnvVar{Name: "MIRROR_PATH", Value: "/mirror"}) + + // Add the volume mount now since we don't want it in the init container ones. + volumeMounts = append(volumeMounts, core.VolumeMount{ + Name: mirrorPvName, + MountPath: "/mirror", + }) } volumes := kubernetes.CreateVolumes(mounts) diff --git a/shared/kubernetes/pvc.go b/shared/kubernetes/pvc.go index b14736ccd..e86fefecf 100644 --- a/shared/kubernetes/pvc.go +++ b/shared/kubernetes/pvc.go @@ -53,17 +53,6 @@ func hasPersistentVolumeClaim(namespace string, name string) bool { return err == nil && strings.TrimSpace(string(out)) != "" } -// Contains the data extracted from the PV to create the linked PVC for it. -type pvData struct { - ClaimRef struct { - Name string - Namespace string - } - StorageClass string - AccessModes []core.PersistentVolumeAccessMode - Size string -} - // CreatePersistentVolumeClaimForVolume creates a PVC bound to a specific Volume. func CreatePersistentVolumeClaimForVolume( namespace string, @@ -71,25 +60,27 @@ func CreatePersistentVolumeClaimForVolume( ) error { // Get the PV Storage class and claimRef out, err := utils.RunCmdOutput(zerolog.DebugLevel, - "kubectl", "get", "pv", volumeName, "-n", namespace, - "-o", `jsonpath={"{\"claimRef\": "}{.spec.claimRef}, "storageClass": "{.spec.storageClassName}", `+ - `"accessModes": {.spec.accessModes}, "size": "{.spec.capacity.storage}{"\"}"}`, + "kubectl", "get", "pv", volumeName, "-n", namespace, "-o", "json", ) if err != nil { return err } - var pv pvData + var pv core.PersistentVolume if err := json.Unmarshal(out, &pv); err != nil { return utils.Errorf(err, L("failed to parse pv data")) } // Ensure the claimRef of the volume is for our PVC - if pv.ClaimRef.Name != volumeName && pv.ClaimRef.Namespace != namespace { - return fmt.Errorf(L("the %[1]s volume should reference the %[2]s claim in %[3]s namespace"), volumeName, namespace) + if pv.Spec.ClaimRef == nil || pv.Spec.ClaimRef.Name != volumeName && pv.Spec.ClaimRef.Namespace != namespace { + return fmt.Errorf(L("the %[1]s volume has to reference the %[1]s claim in %[2]s namespace"), volumeName, namespace) } // Create the PVC object - pvc := newPersistentVolumeClaim(namespace, volumeName, pv.StorageClass, pv.Size, pv.AccessModes, false) + pvc := newPersistentVolumeClaim( + namespace, volumeName, pv.Spec.StorageClassName, + pv.Spec.Capacity.Storage().String(), pv.Spec.AccessModes, false, + ) + pvc.Spec.VolumeName = volumeName return Apply([]runtime.Object{&pvc}, L("failed to run the persistent volume claims")) } diff --git a/uyuni-tools.changes.cbosdo.k8s-mirror-fix b/uyuni-tools.changes.cbosdo.k8s-mirror-fix new file mode 100644 index 000000000..f6049ef7a --- /dev/null +++ b/uyuni-tools.changes.cbosdo.k8s-mirror-fix @@ -0,0 +1 @@ +- Fix mirror pv check when the pv isn't claimed