Revert "Revert "oran-shell-release: release image for F""
[pti/rtp.git] / meta-starlingx / meta-stx-virt / recipes-containers / kubernetes / files / kubelet-cgroup-setup.sh
1 #!/bin/bash
2 #
3 # Copyright (c) 2019 Wind River Systems, Inc.
4 #
5 # SPDX-License-Identifier: Apache-2.0
6 #
7 # This script does minimal cgroup setup for kubelet. This creates k8s-infra
8 # cgroup for a minimal set of resource controllers, and configures cpuset
9 # attributes to span all online cpus and nodes. This will do nothing if
10 # the k8s-infra cgroup already exists (i.e., assume already configured).
11 # NOTE: The creation of directories under /sys/fs/cgroup is volatile, and
12 # does not persist reboots. The cpuset.mems and cpuset.cpus is later updated
13 # by puppet kubernetes.pp manifest.
14 #
15
16 # Define minimal path
17 PATH=/bin:/usr/bin:/usr/local/bin
18
19 # Log info message to /var/log/daemon.log
20 function LOG {
21     logger -p daemon.info "$0($$): $@"
22 }
23
24 # Log error message to /var/log/daemon.log
25 function ERROR {
26     logger -s -p daemon.error "$0($$): ERROR: $@"
27 }
28
29 # Create minimal cgroup directories and configure cpuset attributes
30 # pids should be first in the list, since it appears to get auto deleted
31 function create_cgroup {
32     local cg_name=$1
33     local cg_nodeset=$2
34     local cg_cpuset=$3
35
36     local CGROUP=/sys/fs/cgroup
37     local CONTROLLERS=("pids" "cpuset" "memory" "cpu,cpuacct" "systemd")
38     local cnt=''
39     local CGDIR=''
40     local RC=0
41
42     # Create the cgroup for required controllers
43     for cnt in ${CONTROLLERS[@]}; do
44         CGDIR=${CGROUP}/${cnt}/${cg_name}
45         if [ -d ${CGDIR} ]; then
46             LOG "Nothing to do, already configured: ${CGDIR}."
47             exit ${RC}
48         fi
49         LOG "Creating: ${CGDIR}"
50         mkdir -p ${CGDIR}
51         RC=$?
52         if [ ${RC} -ne 0 ]; then
53             ERROR "Creating: ${CGDIR}, rc=${RC}"
54             exit ${RC}
55         fi
56     done
57
58     # Customize cpuset attributes
59     LOG "Configuring cgroup: ${cg_name}, nodeset: ${cg_nodeset}, cpuset: ${cg_cpuset}"
60     CGDIR=${CGROUP}/cpuset/${cg_name}
61     local CGMEMS=${CGDIR}/cpuset.mems
62     local CGCPUS=${CGDIR}/cpuset.cpus
63     local CGTASKS=${CGDIR}/tasks
64
65     # Assign cgroup memory nodeset
66     LOG "Assign nodeset ${cg_nodeset} to ${CGMEMS}"
67     /bin/echo ${cg_nodeset} > ${CGMEMS}
68     RC=$?
69     if [ ${RC} -ne 0 ]; then
70         ERROR "Unable to write to: ${CGMEMS}, rc=${RC}"
71         exit ${RC}
72     fi
73
74     # Assign cgroup cpus
75     LOG "Assign cpuset ${cg_cpuset} to ${CGCPUS}"
76     /bin/echo ${cg_cpuset} > ${CGCPUS}
77     RC=$?
78     if [ ${RC} -ne 0 ]; then
79         ERROR "Assigning: ${cg_cpuset} to ${CGCPUS}, rc=${RC}"
80         exit ${RC}
81     fi
82
83     # Set file ownership
84     chown root:root ${CGMEMS} ${CGCPUS} ${CGTASKS}
85     RC=$?
86     if [ ${RC} -ne 0 ]; then
87         ERROR "Setting owner for: ${CGMEMS}, ${CGCPUS}, ${CGTASKS}, rc=${RC}"
88         exit ${RC}
89     fi
90
91     # Set file mode permissions
92     chmod 644 ${CGMEMS} ${CGCPUS} ${CGTASKS}
93     RC=$?
94     if [ ${RC} -ne 0 ]; then
95         ERROR "Setting mode for: ${CGMEMS}, ${CGCPUS}, ${CGTASKS}, rc=${RC}"
96         exit ${RC}
97     fi
98
99     return ${RC}
100 }
101
102 if [ $UID -ne 0 ]; then
103     ERROR "Require sudo/root."
104     exit 1
105 fi
106
107 # Configure default kubepods cpuset to span all online cpus and nodes.
108 ONLINE_NODESET=$(/bin/cat /sys/devices/system/node/online)
109 ONLINE_CPUSET=$(/bin/cat /sys/devices/system/cpu/online)
110
111 # Configure kubelet cgroup to match cgroupRoot.
112 create_cgroup 'k8s-infra' ${ONLINE_NODESET} ${ONLINE_CPUSET}
113
114 exit $?
115