This allows the main container to run without write access to the host filesystem, except for two specific areas, thus trimming the attack surface. We lose the behaviour that the CNI plugin is only installed if Weave Net gets up and running at least once, so the error reports from kubelet will be slightly different in the case that it never manages to run.
62 lines
2.0 KiB
Bash
Executable File
62 lines
2.0 KiB
Bash
Executable File
#!/bin/sh
|
|
# Initialisation of Weave Net pod: check Linux settings and install CNI plugin
|
|
|
|
set -e
|
|
|
|
modprobe_safe() {
|
|
modprobe $1 || echo "Ignore the error if \"$1\" is built-in in the kernel" >&2
|
|
}
|
|
|
|
# Check whether xt_set actually exists
|
|
xt_set_exists() {
|
|
# Clean everything up in advance, in case there's leftovers
|
|
iptables -w -F WEAVE-KUBE-TEST 2>/dev/null || true
|
|
iptables -w -X WEAVE-KUBE-TEST 2>/dev/null || true
|
|
ipset destroy weave-kube-test 2>/dev/null || true
|
|
|
|
ipset create weave-kube-test hash:ip
|
|
iptables -w -t filter -N WEAVE-KUBE-TEST
|
|
if ! iptables -w -A WEAVE-KUBE-TEST -m set --match-set weave-kube-test src -j DROP; then
|
|
NOT_EXIST=1
|
|
fi
|
|
iptables -w -F WEAVE-KUBE-TEST
|
|
iptables -w -X WEAVE-KUBE-TEST
|
|
ipset destroy weave-kube-test
|
|
[ -z "$NOT_EXIST" ] || (echo "\"xt_set\" does not exist" >&2 && return 1)
|
|
}
|
|
|
|
# Default for network policy
|
|
EXPECT_NPC=${EXPECT_NPC:-1}
|
|
|
|
# Ensure we have the required modules for NPC
|
|
if [ "${EXPECT_NPC}" != "0" ]; then
|
|
modprobe_safe br_netfilter
|
|
modprobe_safe xt_set
|
|
xt_set_exists
|
|
fi
|
|
|
|
# kube-proxy requires that bridged traffic passes through netfilter
|
|
if ! BRIDGE_NF_ENABLED=$(cat /proc/sys/net/bridge/bridge-nf-call-iptables); then
|
|
echo "Cannot detect bridge-nf support - network policy and iptables mode kubeproxy may not work reliably" >&2
|
|
else
|
|
if [ "$BRIDGE_NF_ENABLED" != "1" ]; then
|
|
echo 1 > /proc/sys/net/bridge/bridge-nf-call-iptables
|
|
fi
|
|
fi
|
|
|
|
# This is where we expect the manifest to map host directories
|
|
HOST_ROOT=${HOST_ROOT:-/host}
|
|
|
|
# Install CNI plugin binary to typical CNI bin location
|
|
# with fall-back to CNI directory used by kube-up on GCI OS
|
|
if ! mkdir -p $HOST_ROOT/opt/cni/bin ; then
|
|
if mkdir -p $HOST_ROOT/home/kubernetes/bin ; then
|
|
export WEAVE_CNI_PLUGIN_DIR=$HOST_ROOT/home/kubernetes/bin
|
|
else
|
|
echo "Failed to install the Weave CNI plugin" >&2
|
|
exit 1
|
|
fi
|
|
fi
|
|
mkdir -p $HOST_ROOT/etc/cni/net.d
|
|
export HOST_ROOT
|
|
/home/weave/weave --local setup-cni |