Commit e57acf73 authored by Max Goltzsche's avatar Max Goltzsche Committed by Derek Su
Browse files

Provide helper Pod params as env vars.



For backward compatibility the CLI options are still provided.

Closes #175

Signed-off-by: default avatarMax Goltzsche <max.goltzsche@gmail.com>
parent ff864e18
......@@ -119,7 +119,7 @@ Now you've verified that the provisioner works as expected.
### Customize the ConfigMap
The configuration of the provisioner is a json file `config.json` and two bash scripts `setup` and `teardown`, stored in the a config map, e.g.:
The configuration of the provisioner is a json file `config.json`, a Pod template `helperPod.yaml` and two bash scripts `setup` and `teardown`, stored in a config map, e.g.:
```
kind: ConfigMap
apiVersion: v1
......@@ -146,40 +146,12 @@ data:
}
setup: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
mkdir -m 0777 -p ${absolutePath}
set -eu
mkdir -m 0777 -p "$VOL_DIR"
teardown: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
rm -rf ${absolutePath}
set -eu
rm -rf "$VOL_DIR"
helperPod.yaml: |-
apiVersion: v1
kind: Pod
......@@ -209,16 +181,19 @@ The configuration must obey following rules:
3. No duplicate paths allowed for one node.
4. No duplicate node allowed.
#### Scripts `setup` and `teardown` and `helperPod.yaml`
#### Scripts `setup` and `teardown` and the `helperPod.yaml` template
The script `setup` will be executed before the volume is created, to prepare the directory on the node for the volume.
* The `setup` script is run before the volume is created, to prepare the volume directory on the node.
* The `teardown` script is run after the volume is deleted, to cleanup the volume directory on the node.
* The `helperPod.yaml` template is used to create a helper Pod that runs the `setup` or `teardown` script.
The script `teardown` will be executed after the volume is deleted, to cleanup the directory on the node for the volume.
The scripts receive their input as environment variables:
The yaml file `helperPod.yaml` will be created by local-path-storage to execute `setup` or `teardown` script with three parameters `-p <path> -s <size> -m <mode>` :
* path: the absolute path provisioned on the node
- size: pvc.Spec.resources.requests.storage in bytes
* mode: pvc.Spec.VolumeMode
| Environment variable | Description |
| -------------------- | ----------- |
| `VOL_DIR` | Volume directory that should be created or removed. |
| `VOL_MODE` | The PersistentVolume mode (`Block` or `Filesystem`). |
| `VOL_SIZE_BYTES` | Requested volume size in bytes. |
#### Reloading
......
......@@ -93,40 +93,12 @@ configmap:
# specify the custom script for setup and teardown
setup: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
mkdir -m 0777 -p ${absolutePath}
set -eu
mkdir -m 0777 -p "$VOL_DIR"
teardown: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
rm -rf ${absolutePath}
set -eu
rm -rf "$VOL_DIR"
# specify the custom helper pod yaml
helperPod: |-
apiVersion: v1
......
......@@ -23,40 +23,12 @@ data:
}
setup: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
mkdir -m 0777 -p ${absolutePath}
set -eu
mkdir -m 0777 -p "$VOL_DIR"
teardown: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
rm -rf ${absolutePath}
set -eu
rm -rf "$VOL_DIR"
helperPod.yaml: |-
apiVersion: v1
kind: Pod
......
......@@ -110,40 +110,12 @@ data:
}
setup: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
mkdir -m 0777 -p ${absolutePath}
set -eu
mkdir -m 0777 -p "$VOL_DIR"
teardown: |-
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
rm -rf ${absolutePath}
set -eu
rm -rf "$VOL_DIR"
helperPod.yaml: |-
apiVersion: v1
kind: Pod
......
......@@ -3,7 +3,7 @@ this is an example to enable quota for xfs
# Usage
> 1. build a helper image using the sample dockerfile to replace helper image xxx/storage-xfs-quota:v0.1 at configmap(helperPod.yaml) of debug.yaml.
> 2. use the sample setup and teardown script at configmap of debug.yaml
> 2. use the sample setup and teardown scripts contained within the kustomization.
Notice:
> 1. make sure the path at nodePathMap is the mountpoint of xfs which enables pquota
......@@ -13,6 +13,7 @@ Notice:
> git clone https://github.com/rancher/local-path-provisioner.git
> cd local-path-provisioner
> go build
> kubectl apply -f debug.yaml
> kubectl apply -k examples/quota
> kubectl delete -n local-path-storage deployment local-path-provisioner
> ./local-path-provisioner --debug start --namespace=local-path-storage
```
apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
resources:
- ../../deploy
configMapGenerator:
- name: local-path-config
namespace: local-path-storage
behavior: merge
files:
- helperPod.yaml
- setup
- teardown
generatorOptions:
disableNameSuffixHash: true
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
xfsPath=$(dirname "$absolutePath")
pvcName=$(basename "$absolutePath")
mkdir -p ${absolutePath}
xfsPath=$(dirname "$VOL_DIR")
pvcName=$(basename "$VOL_DIR")
mkdir -p "$VOL_DIR"
# support xfs quota
type=`stat -f -c %T ${xfsPath}`
......@@ -34,10 +20,10 @@ if [ ${type} == 'xfs' ]; then
id=$[${id}+1]
fi
echo "${id}:${absolutePath}" >> /etc/projects
echo "${id}:${VOL_DIR}" >> /etc/projects
echo "${pvcName}:${id}" >> /etc/projid
xfs_quota -x -c "project -s ${pvcName}"
xfs_quota -x -c "limit -p bhard=${sizeInBytes} ${pvcName}" ${xfsPath}
xfs_quota -x -c "limit -p bhard=${VOL_SIZE_BYTES} ${pvcName}" ${xfsPath}
xfs_quota -x -c "report -pbih" ${xfsPath}
fi
\ No newline at end of file
fi
#!/bin/sh
while getopts "m:s:p:" opt
do
case $opt in
p)
absolutePath=$OPTARG
;;
s)
sizeInBytes=$OPTARG
;;
m)
volMode=$OPTARG
;;
esac
done
xfsPath=$(dirname "$absolutePath")
pvcName=$(basename "$absolutePath")
xfsPath=$(dirname "$VOL_DIR")
pvcName=$(basename "$VOL_DIR")
# support xfs quota
type=`stat -f -c %T ${xfsPath}`
......@@ -26,10 +11,10 @@ if [ ${type} == 'xfs' ]; then
xfs_quota -x -c "limit -p bhard=0 ${pvcName}" ${xfsPath}
fi
rm -rf ${absolutePath}
rm -rf "$VOL_DIR"
if [ ${type} == 'xfs' ]; then
echo "$(sed "/${pvcName}/d" /etc/projects)" > /etc/projects
echo "$(sed "/${pvcName}/d" /etc/projid)" > /etc/projid
xfs_quota -x -c "report -pbih" ${xfsPath}
fi
......@@ -35,6 +35,10 @@ const (
helperScriptDir = "/script"
helperDataVolName = "data"
helperScriptVolName = "script"
envVolDir = "VOL_DIR"
envVolMode = "VOL_MODE"
envVolSize = "VOL_SIZE_BYTES"
)
var (
......@@ -403,6 +407,11 @@ func (p *LocalPathProvisioner) createHelperPod(action ActionType, cmd []string,
// it covers the `/` case
return fmt.Errorf("invalid path %v for %v: cannot find parent dir or volume dir or parent dir is relative", action, o.Path)
}
env := []v1.EnvVar{
{Name: envVolDir, Value: filepath.Join(parentDir, volumeDir)},
{Name: envVolMode, Value: string(o.Mode)},
{Name: envVolSize, Value: strconv.FormatInt(o.SizeInBytes, 10)},
}
// use different name for helper pods
// https://github.com/rancher/local-path-provisioner/issues/154
......@@ -417,6 +426,7 @@ func (p *LocalPathProvisioner) createHelperPod(action ActionType, cmd []string,
helperPod.Spec.Tolerations = append(helperPod.Spec.Tolerations, lpvTolerations...)
helperPod.Spec.Volumes = append(helperPod.Spec.Volumes, lpvVolumes...)
helperPod.Spec.Containers[0].Command = cmd
helperPod.Spec.Containers[0].Env = append(helperPod.Spec.Containers[0].Env, env...)
helperPod.Spec.Containers[0].Args = []string{"-p", filepath.Join(parentDir, volumeDir),
"-s", strconv.FormatInt(o.SizeInBytes, 10),
"-m", string(o.Mode)}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment