Skip to content

Commit

Permalink
Provide helper Pod params as env vars.
Browse files Browse the repository at this point in the history
For backward compatibility the CLI options are still provided.

Closes #175

Signed-off-by: Max Goltzsche <max.goltzsche@gmail.com>
  • Loading branch information
mgoltzsche committed Mar 17, 2022
1 parent 8658846 commit 5a0f570
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 178 deletions.
55 changes: 15 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
36 changes: 4 additions & 32 deletions deploy/chart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 4 additions & 32 deletions deploy/example-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 4 additions & 32 deletions deploy/local-path-storage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions examples/quota/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
> kustomize build examples/quota | kubectl apply -f -
> kubectl delete -n local-path-storage deployment local-path-provisioner
> ./local-path-provisioner --debug start --namespace=local-path-storage
```
File renamed without changes.
17 changes: 17 additions & 0 deletions examples/quota/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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
28 changes: 7 additions & 21 deletions examples/quota/setup
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
#!/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}`
Expand All @@ -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
fi
23 changes: 4 additions & 19 deletions examples/quota/teardown
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
#!/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}`
Expand All @@ -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

10 changes: 10 additions & 0 deletions provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ const (
helperScriptDir = "/script"
helperDataVolName = "data"
helperScriptVolName = "script"

envVolDir = "VOL_DIR"
envVolMode = "VOL_MODE"
envVolSize = "VOL_SIZE_BYTES"
)

var (
Expand Down Expand Up @@ -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
Expand All @@ -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)}
Expand Down

0 comments on commit 5a0f570

Please sign in to comment.