|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -eux |
| 4 | + |
| 5 | +# Set KUBECONFIG path - use dynamic IP-based path |
| 6 | +KUBECONFIG=${KUBECONFIG:-/var/lib/microshift/resources/kubeadmin/kubeconfig} |
| 7 | + |
| 8 | +ns="test-lvms" |
| 9 | +appLabel="app-lvms" |
| 10 | + |
| 11 | +echo "INFO:" "Create Namespace, PVC and Deployment resources......" |
| 12 | +oc --kubeconfig "${KUBECONFIG}" create ns ${ns} |
| 13 | + |
| 14 | +cat <<EOF | oc --kubeconfig "${KUBECONFIG}" -n ${ns} apply -f - |
| 15 | +kind: PersistentVolumeClaim |
| 16 | +apiVersion: v1 |
| 17 | +metadata: |
| 18 | + name: mypvc-lvms |
| 19 | +spec: |
| 20 | + accessModes: |
| 21 | + - ReadWriteOnce |
| 22 | + storageClassName: topolvm-provisioner |
| 23 | + volumeMode: Filesystem |
| 24 | + resources: |
| 25 | + requests: |
| 26 | + storage: 1Mi |
| 27 | +--- |
| 28 | +kind: Deployment |
| 29 | +apiVersion: apps/v1 |
| 30 | +metadata: |
| 31 | + name: mydep-lvms |
| 32 | +spec: |
| 33 | + replicas: 1 |
| 34 | + selector: |
| 35 | + matchLabels: |
| 36 | + app: ${appLabel} |
| 37 | + template: |
| 38 | + metadata: |
| 39 | + labels: |
| 40 | + app: ${appLabel} |
| 41 | + spec: |
| 42 | + containers: |
| 43 | + - name: http-server |
| 44 | + image: quay.io/openshifttest/hello-openshift@sha256:b1aabe8c8272f750ce757b6c4263a2712796297511e0c6df79144ee188933623 |
| 45 | + ports: |
| 46 | + - name: httpd |
| 47 | + containerPort: 80 |
| 48 | + volumeMounts: |
| 49 | + - name: local |
| 50 | + mountPath: /mnt/storage |
| 51 | + volumes: |
| 52 | + - name: local |
| 53 | + persistentVolumeClaim: |
| 54 | + claimName: mypvc-lvms |
| 55 | +EOF |
| 56 | + |
| 57 | +echo "INFO:" "Check if deployment Pod is 'Running'......." |
| 58 | +iter=48 |
| 59 | +period=5 |
| 60 | +podName="" |
| 61 | +set +e |
| 62 | +while [[ "${podName=}" == "" && ${iter} -gt 0 ]]; do |
| 63 | + sleep ${period} |
| 64 | + podName=$(oc --kubeconfig "${KUBECONFIG}" get pod -n ${ns} -l app=${appLabel} --no-headers | awk '{print $1}') |
| 65 | + (( iter -- )) |
| 66 | +done |
| 67 | +if [ "${podName}" == "" ]; then |
| 68 | + echo "ERROR:" "Deployment Pod not found." |
| 69 | + exit 1 |
| 70 | +fi |
| 71 | +echo "INFO:" "Waiting for Pod to become ready(max 4-minutes)....." |
| 72 | +iter=48 |
| 73 | +result="" |
| 74 | +while [[ "${result}" != "Running" && ${iter} -gt 0 ]]; do |
| 75 | + #shellcheck disable=SC1083 |
| 76 | + result=$(oc --kubeconfig "${KUBECONFIG}" get pod "${podName}" -n ${ns} -o=jsonpath={.status.phase}) |
| 77 | + (( iter -- )) |
| 78 | + sleep ${period} |
| 79 | +done |
| 80 | +set -e |
| 81 | +if [ "${result}" == "Running" ]; then |
| 82 | + echo "INFO:" "Deployment Pod is Running." |
| 83 | +else |
| 84 | + echo "ERROR:" "Deployment Pod creation Failed." |
| 85 | + oc --kubeconfig "${KUBECONFIG}" -n ${ns} describe pod "${podName}" |
| 86 | + exit 1 |
| 87 | +fi |
| 88 | + |
| 89 | +echo "INFO:" "Check if data can be read/write into Pod mounted volume......." |
| 90 | +#shellcheck disable=SC2016 |
| 91 | +oc --kubeconfig "${KUBECONFIG}" exec -n ${ns} "${podName}" -- /bin/sh -c 'echo Storage_Test $(date) > /mnt/storage/testfile' |
| 92 | +data=$(oc --kubeconfig "${KUBECONFIG}" exec -n ${ns} "${podName}" -- /bin/sh -c 'cat /mnt/storage/testfile') |
| 93 | +if [[ ${data} =~ "Storage_Test" ]]; then |
| 94 | + echo "SUCCESS:" "Data successfully written into Pod" |
| 95 | +else |
| 96 | + echo "ERROR:" "Failed to write data into the Pod" |
| 97 | + exit 1 |
| 98 | +fi |
0 commit comments