Skip to content

Commit

Permalink
Fix the mirror PV handling
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbosdo committed Dec 9, 2024
1 parent 3c5a820 commit 712886b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
8 changes: 7 additions & 1 deletion mgradm/shared/kubernetes/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
27 changes: 9 additions & 18 deletions shared/kubernetes/pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,34 @@ 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,
volumeName string,
) 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"))
}
Expand Down
1 change: 1 addition & 0 deletions uyuni-tools.changes.cbosdo.k8s-mirror-fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix mirror pv check when the pv isn't claimed

0 comments on commit 712886b

Please sign in to comment.