blob: 0adffe6d2d8cea1d5a98dd461e3cb1f75e558452 [file] [log] [blame]
Alanny Lopez7b730932017-04-24 13:12:20 -05001#!/bin/bash
2###############################################################################
3#
4# This script creates an NFS Persistent Volumes(PV) and also claims that PV
5# with a PVC of the same size.
6# Note: PVs can be claimed by one PVC at a time
7#
8###############################################################################
9#
10# Requirements:
11# - NFS server with directory to use as path for mount
12# - Access to an existing Kubernetes Cluster
13# - Kubectl installed and configured on machine running script
14#
15###############################################################################
16#
17# The script expects a few variables which are needed to define PV's and PVC's
18# NS = Namespace under which to create the mounts on the cluster
19# NFSIP = Server IP for NFS server that will be used
20# NFSPATH = Path of the directory that will be mounted from NFS server
21# SIZE = The size of the volume, numeric value followed by Gi or Mi
22# NAME = The name of the PV and PVC that will be used by the Kubernetes
23# system to refer to PV/PVC
24# MODE = ReadWriteOnce|ReadOnlyMany|ReadWriteMany
25# Access Mode used by NFS normally uses ReadWriteMany
26# RECLAIM = recycle|delete|retain
27# The policy, defines what occurs when claim on PV is released, can
28# be either: recycle, delete, or retain.
29#
30# Note: Kubernetes Systems permissions vary by implementation
31# some will require permissions to create PV's or PVC's
32#
33###############################################################################
34
35NS=${NS:-openbmc}
36NFSIP=${NFSIP:-NFS-Server}
37NFSPATH=${NFSPATH:-/san/dir}
38SIZE=${SIZE:-10Gi}
39NAME=${NAME:-placeholder}
40MODE=${MODE:-ReadWriteMany}
41RECLAIM=${RECLAIM:-Retain}
42
43# Generate the PV
44pv=$(cat << EOF
45apiVersion: v1
46kind: PersistentVolume
47metadata:
48 labels:
49 app: ${NS}
50 name: ${NAME}
51 namespace: ${NS}
52spec:
53 accessModes:
54 - ${MODE}
55 capacity:
56 storage: ${SIZE}
57 nfs:
58 path: ${NFSPATH}
59 server: ${NFSIP}
60 persistentVolumeReclaimPolicy: ${RECLAIM}
61EOF
62)
63
64# create the volume
65if [ -z $(kubectl get pv --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then
66 echo "Creating Persistent Volume ${NAME}"
67 kubectl create -f - <<< "${pv}"
68else
69 echo "Persistent Volume already Exists"
70fi
71
72
73# Generate the PVC
74pvc=$(cat << EOF
75apiVersion: v1
76kind: PersistentVolumeClaim
77metadata:
78 name: ${NAME}
79 namespace: ${NS}
80spec:
81 accessModes:
82 - ${MODE}
83 resources:
84 requests:
85 storage: ${SIZE}
86 selector:
87 matchLabels:
88 app: ${NS}
89EOF
90)
91
92# create PVC's to bind the PV's
93if [ -z $(kubectl get pvc --namespace=${NS} | grep '^'${NAME}' ' | cut -d " " -f1) ];then
94 echo "Creating Persistent Volume Claim ${NAME}"
95 kubectl create -f - <<< "${pvc}"
96else
97 echo "Persistent volume claim already exists."
98fi