summaryrefslogtreecommitdiffstats
path: root/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py
diff options
context:
space:
mode:
authorNitin Goyal <nigoyal@redhat.com>2020-11-10 19:34:02 +0530
committerNitin Goyal <nigoyal@redhat.com>2020-12-08 11:41:03 +0530
commitd6eaf768798f7e0069350cb33ff71433958dd73c (patch)
tree268c47c352a38cfc917049f72de37825eb443759 /openshift-storage-libs/openshiftstoragelibs/openshift_ops.py
parentd0bc92df671f56b2ae15f68a71b5af1e2529f544 (diff)
[Lib] Add lib oc_create_offline_block_volume_expand_job
The lib will create a k8s JOB for expanding block PVC. Change-Id: I18b4f45b30c7541e90bfcb13bf819b1542560ba8 Signed-off-by: Nitin Goyal <nigoyal@redhat.com>
Diffstat (limited to 'openshift-storage-libs/openshiftstoragelibs/openshift_ops.py')
-rw-r--r--openshift-storage-libs/openshiftstoragelibs/openshift_ops.py85
1 files changed, 85 insertions, 0 deletions
diff --git a/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py b/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py
index b8ba40ef..b91b7fc1 100644
--- a/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py
+++ b/openshift-storage-libs/openshiftstoragelibs/openshift_ops.py
@@ -2039,3 +2039,88 @@ def match_pv_and_heketi_volumes(hostname, heketi_volumes, pvc_prefix):
"PV: {}, Heketi volumes {}, "
"Difference: {}".format(pv_volumes, heketi_volumes, vol_diff))
assert not vol_diff, err_msg
+
+
+def oc_create_offline_block_volume_expand_job(
+ hostname, pvc_name, job_name_prefix='block-expand-job',
+ mount_path='/mnt'):
+ """Create Block Volume Expand Job with block PVC mounted at /mnt
+
+ Args:
+ hostname (str): Hostname on which we want to run oc commands
+ pvc_name (str): Name of a block PVC to attach to block expand job
+ job_name_prefix (str): Job name prefix given by user at the time
+ of job creation
+ mount_path (str): Where PVC should be mounted
+
+ Returns:
+ string: Name of the created job
+ """
+
+ # Find MOUNTPOINT on host node wrt to the mount_path on pod and run
+ # xfs_growfs on host MOUNTPOINT
+ command = [
+ 'sh', '-c', 'echo -e "# df -Th {0}" && df -Th {0} && '
+ 'DEVICE=$(df --output=source {0} | sed -e /^Filesystem/d) && '
+ 'MOUNTPOINT=$($EXEC_ON_HOST lsblk $DEVICE -n -o MOUNTPOINT) && '
+ '$EXEC_ON_HOST xfs_growfs $MOUNTPOINT > /dev/null && '
+ 'echo -e "\n# df -Th {0}" && df -Th {0}'.format(mount_path)
+ ]
+
+ # This will be a privileged container be careful while playing with it
+ job_name = "%s-%s" % (job_name_prefix, utils.get_random_str())
+ job_data = json.dumps({
+ "apiVersion": "batch/v1",
+ "kind": "Job",
+ "metadata": {"name": job_name},
+ "spec": {
+ "completions": 1,
+ "template": {
+ "spec": {
+ "containers": [{
+ "image": "rhel7",
+ "env": [
+ {
+ "name": "HOST_ROOTFS",
+ "value": "/rootfs"
+ },
+ {
+ "name": "EXEC_ON_HOST",
+ "value": "nsenter --root=$(HOST_ROOTFS) "
+ "nsenter -t 1 -m"
+ }
+ ],
+ "command": command,
+ "name": "rhel7",
+ "volumeMounts": [
+ {"mountPath": mount_path, "name": "block-pvc"},
+ {"mountPath": "/dev", "name": "host-dev"},
+ {"mountPath": "/rootfs", "name": "host-rootfs"}
+ ],
+ "securityContext": {"privileged": True}
+ }],
+ "volumes": [
+ {
+ "name": "block-pvc", "persistentVolumeClaim": {
+ "claimName": pvc_name
+ }
+ },
+ {
+ "name": "host-dev", "hostPath": {
+ "path": "/dev"
+ }
+ },
+ {
+ "name": "host-rootfs", "hostPath": {
+ "path": "/"
+ }
+ }
+ ],
+ "restartPolicy": "Never"
+ }
+ }
+ }
+ })
+
+ oc_create(hostname, job_data, 'stdin')
+ return job_name