blob: 10ac76a786f0073f868ba41708c2d964b0001afe [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
Alanny Lopezaef0a492017-12-28 16:03:14 -060018# 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
Alanny Lopez7b730932017-04-24 13:12:20 -050023# system to refer to PV/PVC
Alanny Lopezaef0a492017-12-28 16:03:14 -060024# mode = ReadWriteOnce|ReadOnlyMany|ReadWriteMany
Alanny Lopez7b730932017-04-24 13:12:20 -050025# Access Mode used by NFS normally uses ReadWriteMany
Alanny Lopezaef0a492017-12-28 16:03:14 -060026# reclaim = recycle|delete|retain
Alanny Lopez7b730932017-04-24 13:12:20 -050027# 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
Alanny Lopezaef0a492017-12-28 16:03:14 -060035ns=${ns:-openbmc}
36nfsip=${nfsip:-NFS-Server}
37nfspath=${nfspath:-/san/dir}
38size=${size:-10Gi}
39name=${name:-placeholder}
40mode=${mode:-ReadWriteMany}
41reclaim=${reclaim:-Retain}
Alanny Lopez7b730932017-04-24 13:12:20 -050042
43# Generate the PV
44pv=$(cat << EOF
45apiVersion: v1
46kind: PersistentVolume
47metadata:
48 labels:
Alanny Lopezaef0a492017-12-28 16:03:14 -060049 app: ${name}
50 name: ${name}
51 namespace: ${ns}
Alanny Lopez7b730932017-04-24 13:12:20 -050052spec:
53 accessModes:
Alanny Lopezaef0a492017-12-28 16:03:14 -060054 - ${mode}
Alanny Lopez7b730932017-04-24 13:12:20 -050055 capacity:
Alanny Lopezaef0a492017-12-28 16:03:14 -060056 storage: ${size}
Alanny Lopez7b730932017-04-24 13:12:20 -050057 nfs:
Alanny Lopezaef0a492017-12-28 16:03:14 -060058 path: ${nfspath}
59 server: ${nfsip}
60 persistentVolumeReclaimPolicy: ${reclaim}
Alanny Lopez7b730932017-04-24 13:12:20 -050061EOF
62)
63
64# create the volume
Alanny Lopezaef0a492017-12-28 16:03:14 -060065if [ -z $(kubectl get pv --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
66 echo "Creating Persistent Volume ${name}"
Alanny Lopez7b730932017-04-24 13:12:20 -050067 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:
Alanny Lopezaef0a492017-12-28 16:03:14 -060078 name: ${name}
79 namespace: ${ns}
Alanny Lopez7b730932017-04-24 13:12:20 -050080spec:
81 accessModes:
Alanny Lopezaef0a492017-12-28 16:03:14 -060082 - ${mode}
Alanny Lopez7b730932017-04-24 13:12:20 -050083 resources:
84 requests:
Alanny Lopezaef0a492017-12-28 16:03:14 -060085 storage: ${size}
Alanny Lopez7b730932017-04-24 13:12:20 -050086 selector:
87 matchLabels:
Alanny Lopezaef0a492017-12-28 16:03:14 -060088 app: ${name}
Alanny Lopez7b730932017-04-24 13:12:20 -050089EOF
90)
91
92# create PVC's to bind the PV's
Alanny Lopezaef0a492017-12-28 16:03:14 -060093if [ -z $(kubectl get pvc --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
94 echo "Creating Persistent Volume Claim ${name}"
Alanny Lopez7b730932017-04-24 13:12:20 -050095 kubectl create -f - <<< "${pvc}"
96else
97 echo "Persistent volume claim already exists."
98fi