Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the mirror PV handling #514

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading