Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit 19cf38e

Browse files
authored
Merge pull request #16 from storageos/max-pids
Report effective max pid limit and optionally block if required minimum not met.
2 parents 18be8b5 + 069fe83 commit 19cf38e

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

daemonset.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ spec:
7373
valueFrom:
7474
fieldRef:
7575
fieldPath: metadata.namespace
76+
- name: MINIMUM_MAX_PIDS_LIMIT
77+
value: "1024"
78+
- name: RECOMMENDED_MAX_PIDS_LIMIT
79+
value: "4096"
7680
volumeMounts:
7781
- name: kernel-modules
7882
mountPath: /lib/modules

e2e.sh

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
set -Eeuxo pipefail
44

5+
cluster="init"
6+
57
prepare_host() {
68
sudo apt -y update
79
sudo apt -y install linux-modules-extra-$(uname -r)
@@ -18,12 +20,14 @@ run_kind() {
1820
curl -Lo kubectl https://storage.googleapis.com/kubernetes-release/release/"${K8S_VERSION}"/bin/linux/amd64/kubectl && chmod +x kubectl && sudo mv kubectl /usr/local/bin/
1921
echo
2022

21-
echo "Create Kubernetes cluster with kind..."
22-
# kind create cluster --image=kindest/node:"$K8S_VERSION"
23-
kind create cluster --image storageos/kind-node:"$K8S_VERSION" --name kind-1
23+
if [ $(kind get clusters | grep -c ^$cluster$) -eq 0 ]; then
24+
echo "Create Kubernetes cluster with kind..."
25+
# kind create cluster --image=kindest/node:"$K8S_VERSION"
26+
kind create cluster --image storageos/kind-node:"$K8S_VERSION" --name "$cluster"
27+
fi
2428

2529
echo "Export kubeconfig..."
26-
kind get kubeconfig --name="kind-1" > kubeconfig.yaml
30+
kind get kubeconfig --name="$cluster" > kubeconfig.yaml
2731
export KUBECONFIG="kubeconfig.yaml"
2832
echo
2933

@@ -50,7 +54,7 @@ main() {
5054
echo
5155

5256
# Copy the init container image into KinD.
53-
x=$(docker ps -f name=kind-1-control-plane -q)
57+
x=$(docker ps -f name=${cluster}-control-plane -q)
5458
docker save storageos/init:test > init.tar
5559
docker cp init.tar $x:/init.tar
5660

@@ -82,6 +86,7 @@ main() {
8286

8387
echo "Checking init container exit code"
8488
exitCode=$(kubectl get pod $stospod --no-headers -o go-template='{{(index .status.initContainerStatuses 0).state.terminated.exitCode}}')
89+
kubectl delete -f daemonset.yaml
8590
if [ "$exitCode" == "0" ]; then
8691
echo "init successful!"
8792
exit 0

scripts/02-limits/limits.sh

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
# For a directory containeing the cgroup slice information, return the value of
6+
# pids.max, or 0 if set to "max". Return -1 exit code if the file doesn't exist.
7+
function read_max_pids() {
8+
if [ ! -f ${1}/pids.max ]; then
9+
return -1
10+
fi
11+
local max_pids=$(<${1}/pids.max)
12+
if [ $max_pids == "max" ]; then
13+
echo 0
14+
return
15+
fi
16+
echo $max_pids
17+
}
18+
19+
default_max_pids_limit=999999999
20+
max_pids_limit=$default_max_pids_limit
21+
dirprefix="/sys/fs/cgroup/pids"
22+
23+
for cg in $(grep :pids: /proc/self/cgroup); do
24+
# Parse out the slice field from the cgroup output.
25+
# <cgroup_id>:<subystem>:<slice>
26+
dirsuffix=$(echo "$cg" | awk -F\: '{print $3}')
27+
28+
# The slice field can have a prefix that is not part of the directory path.
29+
# This must be stripped iteratively until we find the valid slice directory.
30+
while [ ! -d "${dirprefix}/${dirsuffix}" ]; do
31+
dirsuffix=${dirsuffix#*/}
32+
done
33+
dir="${dirprefix}/${dirsuffix}"
34+
35+
# Start at the current cgroup and traverse up the directory hierarchy
36+
# reading max.pids in each. The lowest value will be the effective max.pids
37+
# value.
38+
while [ -f "${dir}/pids.max" ]; do
39+
max_pids=$(read_max_pids "${dir}")
40+
if [[ $max_pids -gt 0 && $max_pids -lt $max_pids_limit ]]; then
41+
max_pids_limit=$max_pids
42+
fi
43+
dir="${dir}/.."
44+
done
45+
done
46+
47+
# TBC: Don't fail if we can't determine limit.
48+
if [ $max_pids_limit -eq $default_max_pids_limit ]; then
49+
echo "WARNING: Unable to determine effective max.pids limit"
50+
exit 0
51+
fi
52+
53+
# Fail if MINIMUM_MAX_PIDS_LIMIT is set and is greater than current limit.
54+
if [[ -n "${MINIMUM_MAX_PIDS_LIMIT}" && $MINIMUM_MAX_PIDS_LIMIT -gt $max_pids_limit ]]; then
55+
echo "ERROR: Effective max.pids limit ($max_pids_limit) less than MINIMUM_MAX_PIDS_LIMIT ($MINIMUM_MAX_PIDS_LIMIT)"
56+
exit 1
57+
fi
58+
59+
if [ -n "${RECOMMENDED_MAX_PIDS_LIMIT}" ]; then
60+
if [ $RECOMMENDED_MAX_PIDS_LIMIT -gt $max_pids_limit ]; then
61+
echo "WARNING: Effective max.pids limit ($max_pids_limit) less than RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)"
62+
else
63+
echo "OK: Effective max.pids limit ($max_pids_limit) at least RECOMMENDED_MAX_PIDS_LIMIT ($RECOMMENDED_MAX_PIDS_LIMIT)"
64+
fi
65+
exit 0
66+
fi
67+
68+
# No requirements set, just output current limit.
69+
echo "Effective max.pids limit: $max_pids_limit"

0 commit comments

Comments
 (0)