blob: fa5cc46ae5fb8a6ec51b316bd22f856e5dbc01d6 [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 Lopez1347ea62018-02-25 01:07:59 -060018# mode ReadWriteOnce|ReadOnlyMany|ReadWriteMany
19# Access Mode used by NFS normally uses ReadWriteMany
20# Default: ReadWriteMany
21# name The name of the PV and PVC that will be used by the Kubernetes
22# system to refer to PV/PVC
23# Default: "placeholder"
24# nfs_ip Server IP for NFS server that will be used
25# Default: "NFS-Server"
26# nfs_path Path of the directory that will be mounted from NFS server
27# Default: "/san/dir"
28# ns Namespace under which to create the mounts on the cluster
29# Default: "openbmc"
30# reclaim recycle|delete|retain
31# The policy, defines what occurs when claim on PV is released, can
32# be either: recycle, delete, or retain.
33# Default: "Retain"
34# size The size of the volume, numeric value followed by Gi or Mi
35# Default: "10Gi"
Alanny Lopez7b730932017-04-24 13:12:20 -050036#
37# Note: Kubernetes Systems permissions vary by implementation
38# some will require permissions to create PV's or PVC's
39#
40###############################################################################
41
Alanny Lopezaef0a492017-12-28 16:03:14 -060042mode=${mode:-ReadWriteMany}
Alanny Lopez1347ea62018-02-25 01:07:59 -060043name=${name:-placeholder}
44nfs_ip=${nfs_ip:-NFS-Server}
45nfs_path=${nfs_path:-/san/dir}
46ns=${ns:-openbmc}
Alanny Lopezaef0a492017-12-28 16:03:14 -060047reclaim=${reclaim:-Retain}
Alanny Lopez1347ea62018-02-25 01:07:59 -060048size=${size:-10Gi}
Alanny Lopez7b730932017-04-24 13:12:20 -050049
50# Generate the PV
51pv=$(cat << EOF
52apiVersion: v1
53kind: PersistentVolume
54metadata:
55 labels:
Alanny Lopezaef0a492017-12-28 16:03:14 -060056 app: ${name}
57 name: ${name}
58 namespace: ${ns}
Alanny Lopez7b730932017-04-24 13:12:20 -050059spec:
60 accessModes:
Alanny Lopezaef0a492017-12-28 16:03:14 -060061 - ${mode}
Alanny Lopez7b730932017-04-24 13:12:20 -050062 capacity:
Alanny Lopezaef0a492017-12-28 16:03:14 -060063 storage: ${size}
Alanny Lopez7b730932017-04-24 13:12:20 -050064 nfs:
Alanny Lopez1347ea62018-02-25 01:07:59 -060065 path: ${nfs_path}
66 server: ${nfs_ip}
Alanny Lopezaef0a492017-12-28 16:03:14 -060067 persistentVolumeReclaimPolicy: ${reclaim}
Alanny Lopez7b730932017-04-24 13:12:20 -050068EOF
69)
70
71# create the volume
Alanny Lopezaef0a492017-12-28 16:03:14 -060072if [ -z $(kubectl get pv --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
73 echo "Creating Persistent Volume ${name}"
Alanny Lopez7b730932017-04-24 13:12:20 -050074 kubectl create -f - <<< "${pv}"
75else
76 echo "Persistent Volume already Exists"
77fi
78
79
80# Generate the PVC
81pvc=$(cat << EOF
82apiVersion: v1
83kind: PersistentVolumeClaim
84metadata:
Alanny Lopezaef0a492017-12-28 16:03:14 -060085 name: ${name}
86 namespace: ${ns}
Alanny Lopez7b730932017-04-24 13:12:20 -050087spec:
88 accessModes:
Alanny Lopezaef0a492017-12-28 16:03:14 -060089 - ${mode}
Alanny Lopez7b730932017-04-24 13:12:20 -050090 resources:
91 requests:
Alanny Lopezaef0a492017-12-28 16:03:14 -060092 storage: ${size}
Alanny Lopez7b730932017-04-24 13:12:20 -050093 selector:
94 matchLabels:
Alanny Lopezaef0a492017-12-28 16:03:14 -060095 app: ${name}
Alanny Lopez7b730932017-04-24 13:12:20 -050096EOF
97)
98
99# create PVC's to bind the PV's
Alanny Lopezaef0a492017-12-28 16:03:14 -0600100if [ -z $(kubectl get pvc --namespace=${ns} | grep '^'${name}' ' | cut -d " " -f1) ];then
101 echo "Creating Persistent Volume Claim ${name}"
Alanny Lopez7b730932017-04-24 13:12:20 -0500102 kubectl create -f - <<< "${pvc}"
103else
104 echo "Persistent volume claim already exists."
105fi