<commit_before>#!/bin/bash

set -e

# Backwards compatibility
if [ -n "${CILIUM_FLANNEL_MASTER_DEVICE}" ]; then
	CILIUM_CNI_CHAINING_MODE="flannel"
fi

HOST_PREFIX=${HOST_PREFIX:-/host}

case "$CILIUM_CNI_CHAINING_MODE" in
"flannel")
	until ip link show "${CILIUM_FLANNEL_MASTER_DEVICE}" &>/dev/null ; do
		echo "Waiting for ${CILIUM_FLANNEL_MASTER_DEVICE} to be initialized"
		sleep 1s
	done
	CNI_CONF_NAME=${CNI_CONF_NAME:-04-flannel-cilium-cni.conflist}
	;;
"generic-veth")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
"portmap")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
"aws-cni")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
*)
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conf}
	;;
esac

ENABLE_DEBUG=false
CNI_EXCLUSIVE=true
while test $# -gt 0; do
  case "$1" in
    --enable-debug*)
      # shellcheck disable=SC2001
      ENABLE_DEBUG=$(echo "$1" | sed -e 's/^[^=]*=//g')
      shift
      ;;
    --cni-exclusive*)
      # shellcheck disable=SC2001
      CNI_EXCLUSIVE=$(echo "$1" | sed -e 's/^[^=]*=//g')
      shift
      ;;
    *)
      break
      ;;
  esac
done

BIN_NAME=cilium-cni
CNI_DIR=${CNI_DIR:-${HOST_PREFIX}/opt/cni}
CILIUM_CNI_CONF=${CILIUM_CNI_CONF:-${HOST_PREFIX}/etc/cni/net.d/${CNI_CONF_NAME}}
CNI_CONF_DIR="$(dirname "$CILIUM_CNI_CONF")"

if [ ! -d "${CNI_DIR}/bin" ]; then
	mkdir -p "${CNI_DIR}/bin"
fi

# Install the CNI loopback driver if not installed already
if [ ! -f "${CNI_DIR}/bin/loopback" ]; then
	echo "Installing loopback driver..."

	# Don't fail hard if this fails as it is usually not required
	cp /cni/loopback "${CNI_DIR}/bin/" || true
fi

echo "Installing ${BIN_NAME} to ${CNI_DIR}/bin/ ..."

# Move an eventual old existing binary out of the way, we can't delete it
# as it might be in use right now.
if [ -f "${CNI_DIR}/bin/${BIN_NAME}" ]; then
	rm -f "${CNI_DIR}/bin/${BIN_NAME}.old" || true
	mv "${CNI_DIR}/bin/${BIN_NAME}" "${CNI_DIR}/bin/${BIN_NAME}.old"
fi

cp "/opt/cni/bin/${BIN_NAME}" "${CNI_DIR}/bin/"

# Remove any active Cilium CNI configurations left over from previous installs
# to make sure the one we're installing later will take effect.
# Ignore the file specified by CNI_CONF_NAME. The agent will use this
# filename to write a user-specified CNI config and races against this script.
echo "Removing active Cilium CNI configurations from ${CNI_CONF_DIR}})..."
find "${CNI_CONF_DIR}" -maxdepth 1 -type f \
  -name '*cilium*' -and \( \
    -name '*.conf' -or \
    -name '*.conflist' \
  \) \
  -not -name "${CNI_CONF_NAME}" \
  -delete

# Rename all remaining CNI configurations to *.cilium_bak. This ensures only
# Cilium's CNI plugin will remain active. This makes sure Pods are not
# scheduled by another CNI when the Cilium agent is down during upgrades
# or restarts. See GH-14128 and related issues for more context.
if [ "${CNI_EXCLUSIVE}" != "false" ]; then
  find "$(dirname "${CILIUM_CNI_CONF}")" \
     -maxdepth 1 \
     -type f \
     \( -name '*.conf' \
     -or -name '*.conflist' \
     -or -name '*.json' \
     \) \
     -not \( -name '*.cilium_bak' \
     -or -name "${CNI_CONF_NAME}" \) \
     -exec mv {} {}.cilium_bak \;
fi

if [ "${CILIUM_CUSTOM_CNI_CONF}" = "true" ]; then
	echo "Using custom ${CILIUM_CNI_CONF}..."
	exit 0
fi

echo "Installing new ${CILIUM_CNI_CONF}..."
case "$CILIUM_CNI_CHAINING_MODE" in
"flannel")
	cat > "${CNI_CONF_NAME}" <<EOF
{
  "cniVersion": "0.3.1",
  "name": "cbr0",
  "plugins": [
    {
      "type": "flannel",
      "delegate": {
         "hairpinMode": true,
         "isDefaultGateway": true
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    },<commit_msg>plugins/cilium-cni: cni-install - document CILIUM_CUSTOM_CNI_CONF purpose<commit_after>#!/bin/bash

set -e

# Backwards compatibility
if [ -n "${CILIUM_FLANNEL_MASTER_DEVICE}" ]; then
	CILIUM_CNI_CHAINING_MODE="flannel"
fi

HOST_PREFIX=${HOST_PREFIX:-/host}

case "$CILIUM_CNI_CHAINING_MODE" in
"flannel")
	until ip link show "${CILIUM_FLANNEL_MASTER_DEVICE}" &>/dev/null ; do
		echo "Waiting for ${CILIUM_FLANNEL_MASTER_DEVICE} to be initialized"
		sleep 1s
	done
	CNI_CONF_NAME=${CNI_CONF_NAME:-04-flannel-cilium-cni.conflist}
	;;
"generic-veth")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
"portmap")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
"aws-cni")
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conflist}
	;;
*)
	CNI_CONF_NAME=${CNI_CONF_NAME:-05-cilium.conf}
	;;
esac

ENABLE_DEBUG=false
CNI_EXCLUSIVE=true
while test $# -gt 0; do
  case "$1" in
    --enable-debug*)
      # shellcheck disable=SC2001
      ENABLE_DEBUG=$(echo "$1" | sed -e 's/^[^=]*=//g')
      shift
      ;;
    --cni-exclusive*)
      # shellcheck disable=SC2001
      CNI_EXCLUSIVE=$(echo "$1" | sed -e 's/^[^=]*=//g')
      shift
      ;;
    *)
      break
      ;;
  esac
done

BIN_NAME=cilium-cni
CNI_DIR=${CNI_DIR:-${HOST_PREFIX}/opt/cni}
CILIUM_CNI_CONF=${CILIUM_CNI_CONF:-${HOST_PREFIX}/etc/cni/net.d/${CNI_CONF_NAME}}
CNI_CONF_DIR="$(dirname "$CILIUM_CNI_CONF")"

if [ ! -d "${CNI_DIR}/bin" ]; then
	mkdir -p "${CNI_DIR}/bin"
fi

# Install the CNI loopback driver if not installed already
if [ ! -f "${CNI_DIR}/bin/loopback" ]; then
	echo "Installing loopback driver..."

	# Don't fail hard if this fails as it is usually not required
	cp /cni/loopback "${CNI_DIR}/bin/" || true
fi

echo "Installing ${BIN_NAME} to ${CNI_DIR}/bin/ ..."

# Move an eventual old existing binary out of the way, we can't delete it
# as it might be in use right now.
if [ -f "${CNI_DIR}/bin/${BIN_NAME}" ]; then
	rm -f "${CNI_DIR}/bin/${BIN_NAME}.old" || true
	mv "${CNI_DIR}/bin/${BIN_NAME}" "${CNI_DIR}/bin/${BIN_NAME}.old"
fi

cp "/opt/cni/bin/${BIN_NAME}" "${CNI_DIR}/bin/"

# Remove any active Cilium CNI configurations left over from previous installs
# to make sure the one we're installing later will take effect.
# Ignore the file specified by CNI_CONF_NAME. The agent will use this
# filename to write a user-specified CNI config and races against this script.
echo "Removing active Cilium CNI configurations from ${CNI_CONF_DIR}})..."
find "${CNI_CONF_DIR}" -maxdepth 1 -type f \
  -name '*cilium*' -and \( \
    -name '*.conf' -or \
    -name '*.conflist' \
  \) \
  -not -name "${CNI_CONF_NAME}" \
  -delete

# Rename all remaining CNI configurations to *.cilium_bak. This ensures only
# Cilium's CNI plugin will remain active. This makes sure Pods are not
# scheduled by another CNI when the Cilium agent is down during upgrades
# or restarts. See GH-14128 and related issues for more context.
if [ "${CNI_EXCLUSIVE}" != "false" ]; then
  find "$(dirname "${CILIUM_CNI_CONF}")" \
     -maxdepth 1 \
     -type f \
     \( -name '*.conf' \
     -or -name '*.conflist' \
     -or -name '*.json' \
     \) \
     -not \( -name '*.cilium_bak' \
     -or -name "${CNI_CONF_NAME}" \) \
     -exec mv {} {}.cilium_bak \;
fi

# The CILIUM_CUSTOM_CNI_CONF env is set if the user specifies a custom
# CNI config in a ConfigMap and if the cni.customConf Helm option is set.
# Skip the remainder of the script, since this custom config is written
# to disk by the Cilium agent itself after initialization.
if [ "${CILIUM_CUSTOM_CNI_CONF}" = "true" ]; then
	echo "Using custom ${CILIUM_CNI_CONF}, exiting..."
	exit 0
fi

echo "Installing new ${CILIUM_CNI_CONF}..."
case "$CILIUM_CNI_CHAINING_MODE" in
"flannel")
	cat > "${CNI_CONF_NAME}" <<EOF
{
  "cniVersion": "0.3.1",
  "name": "cbr0",
  "plugins": [
    {
      "type": "flannel",
      "delegate": {
         "hairpinMode": true,
         "isDefaultGateway": true
      }
    },
    {
      "type": "portmap",
      "capabilities": {
        "portMappings": true
      }
    },
{'fraction_non_alphanumeric': 0.15971951694585118, 'fraction_numerical': 0.009089728606674457, 'mean_word_length': 3.353872244205766, 'pattern_counts': {'":': 20, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5695803', 'n_tokens_mistral': 3060, 'n_tokens_neox': 2967, 'n_words': 740}