diff --git a/Dockerfile b/Dockerfile index 91f66992a..38a4d3632 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,6 +12,11 @@ RUN yum install -y $INSTALL_PKGS && \ yum clean all && \ rm -rf /tmp/* +# Install jq : command-line JSON processor +RUN curl -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 > jq && \ + chmod +x jq && \ + mv jq /usr/local/bin + WORKDIR / LABEL io.k8s.display-name="Multus CNI" \ diff --git a/images/README.md b/images/README.md index e16f4106f..4f30212d4 100644 --- a/images/README.md +++ b/images/README.md @@ -31,10 +31,12 @@ You can get get help with the `--help` flag. ``` $ ./entrypoint.sh --help -This is an entrypoint script for Multus CNI to overlay its -binary and configuration into locations in a filesystem. -The configuration & binary file will be copied to the -corresponding configuration directory. +This is an entrypoint script for Multus CNI to overlay its binary and +configuration into locations in a filesystem. The configuration & binary file +will be copied to the corresponding configuration directory. When +`--multus-conf-file=auto` is used, 00-multus.conf will be automatically +generated from the CNI configuration file of the master plugin (the first file +in lexicographical order in cni-conf-dir). ./entrypoint.sh -h --help @@ -59,7 +61,7 @@ Note: You'll noticed that there's a `/host/...` directory from the root for the Example docker run command: ``` -$ docker run -it -v /opt/cni/bin/:/host/opt/cni/bin/ -v /etc/cni/net.d/:/host/etc/cni/net.d/ --entrypoint=/bin/bash dougbtv/multus +$ docker run -it -v /opt/cni/bin/:/host/opt/cni/bin/ -v /etc/cni/net.d/:/host/etc/cni/net.d/ --entrypoint=/bin/bash dougbtv/multus ``` -Originally inspired by and is a portmanteau of the [Flannel daemonset](https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml), the [Calico Daemonset](https://github.com/projectcalico/calico/blob/master/v2.0/getting-started/kubernetes/installation/hosted/k8s-backend-addon-manager/calico-daemonset.yaml), and the [Calico CNI install bash script](https://github.com/projectcalico/cni-plugin/blob/be4df4db2e47aa7378b1bdf6933724bac1f348d0/k8s-install/scripts/install-cni.sh#L104-L153). \ No newline at end of file +Originally inspired by and is a portmanteau of the [Flannel daemonset](https://github.com/coreos/flannel/blob/master/Documentation/kube-flannel.yml), the [Calico Daemonset](https://github.com/projectcalico/calico/blob/master/v2.0/getting-started/kubernetes/installation/hosted/k8s-backend-addon-manager/calico-daemonset.yaml), and the [Calico CNI install bash script](https://github.com/projectcalico/cni-plugin/blob/be4df4db2e47aa7378b1bdf6933724bac1f348d0/k8s-install/scripts/install-cni.sh#L104-L153). diff --git a/images/entrypoint.sh b/images/entrypoint.sh index 8b60bf032..10da37315 100755 --- a/images/entrypoint.sh +++ b/images/entrypoint.sh @@ -12,10 +12,12 @@ MULTUS_BIN_FILE="/usr/src/multus-cni/bin/multus" # Give help text for parameters. function usage() { - echo -e "This is an entrypoint script for Multus CNI to overlay its" - echo -e "binary and configuration into locations in a filesystem." - echo -e "The configuration & binary file will be copied to the " - echo -e "corresponding configuration directory." + echo -e "This is an entrypoint script for Multus CNI to overlay its binary and " + echo -e "configuration into locations in a filesystem. The configuration & binary file " + echo -e "will be copied to the corresponding configuration directory. When " + echo -e "`--multus-conf-file=auto` is used, 00-multus.conf will be automatically " + echo -e "generated from the CNI configuration file of the master plugin (the first file " + echo -e "in lexicographical order in cni-conf-dir)." echo -e "" echo -e "./entrypoint.sh" echo -e "\t-h --help" @@ -57,7 +59,11 @@ done # Create array of known locations -declare -a arr=($CNI_CONF_DIR $CNI_BIN_DIR $MULTUS_CONF_FILE $MULTUS_BIN_FILE) +declare -a arr=($CNI_CONF_DIR $CNI_BIN_DIR $MULTUS_BIN_FILE) +if [ "$MULTUS_CONF_FILE" != "auto" ]; then + arr+=($MULTUS_BIN_FILE) +fi + # Loop through and verify each location each. for i in "${arr[@]}" @@ -69,8 +75,10 @@ do done # Copy files into proper places. -cp -f $MULTUS_CONF_FILE $CNI_CONF_DIR cp -f $MULTUS_BIN_FILE $CNI_BIN_DIR +if [ "$MULTUS_CONF_FILE" != "auto" ]; then + cp -f $MULTUS_CONF_FILE $CNI_CONF_DIR +fi # Make a multus.d directory (for our kubeconfig) @@ -134,6 +142,35 @@ fi # ---------------------- end Generate a "kube-config". +# ------------------------------- Generate "00-multus.conf" + +if [ "$MULTUS_CONF_FILE" == "auto" ]; then + echo "Generating Multus configuration file ..." + MASTER_PLUGIN="$(ls $CNI_CONF_DIR | grep .conf | head -1)" + if [ "$MASTER_PLUGIN" == "" ]; then + echo "Error: Multus could not be configured: no master plugin was found." + exit 1; + elif [ "$MASTER_PLUGIN" == "00-multus.conf" ]; then + echo "Warning: Multus is already configured: auto configuration skipped." + else + MASTER_PLUGIN_JSON="$(cat $CNI_CONF_DIR/$MASTER_PLUGIN)" + CONF=$(cat <<-EOF + { + "name": "multus-cni-network", + "type": "multus", + "kubeconfig": "$MULTUS_KUBECONFIG", + "delegates": [ + $MASTER_PLUGIN_JSON + ] + } + EOF + ) + echo $CONF | jq . > $CNI_CONF_DIR/00-multus.conf + fi +fi + +# ---------------------- end Generate "00-multus.conf". + echo "Entering sleep... (success)" # Sleep forever.