From 07c38b44823f62879575b7281b9665a41abf88a3 Mon Sep 17 00:00:00 2001 From: Casey Davenport Date: Tue, 2 Aug 2016 17:00:30 -0700 Subject: [PATCH] Tweak Dockerfile to install bins on k8s --- Dockerfile | 11 ++++++++++ Makefile | 5 +++-- scripts/conf_writer.py | 50 ++++++++++++++++++++++++++++++++++++++++++ scripts/install-cni.sh | 31 ++++++++++++++++++++++++++ 4 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 scripts/conf_writer.py create mode 100755 scripts/install-cni.sh diff --git a/Dockerfile b/Dockerfile index dde3f034f..5bca42f2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,20 @@ FROM debian:jessie MAINTAINER projectcalico +# Add python +RUN apt-get update +RUN apt-get install -y python + +# Add binaries to image. RUN mkdir -p /opt/cni/bin ADD ./dist/calico /opt/cni/bin ADD ./dist/calico-ipam /opt/cni/bin + +# Add CNI install script. +ADD ./scripts/install-cni.sh /install-cni.sh +ADD ./scripts/conf_writer.py /conf_writer.py + +# Set volume, environment, and working directory. VOLUME /opt/cni ENV PATH=$PATH:/opt/cni/bin WORKDIR /opt/cni/bin diff --git a/Makefile b/Makefile index 7aebb7ba2..806555800 100644 --- a/Makefile +++ b/Makefile @@ -34,8 +34,9 @@ dist/calico-ipam: $(SRCFILES) # Makes a docker image dist/docker: - docker build -t calico/cni . - docker tag calico/cni calico/cni:$(CALICO_VERSION) + docker build -t caseydavenport/cni:latest . + #docker build -t calico/cni . + #docker tag calico/cni calico/cni:$(CALICO_VERSION) # Updates the version information in version.py update-version: diff --git a/scripts/conf_writer.py b/scripts/conf_writer.py new file mode 100644 index 000000000..6bc5b2bf9 --- /dev/null +++ b/scripts/conf_writer.py @@ -0,0 +1,50 @@ +# Writes a Calico CNI network config to disk using +# the provided environment. +import os +import json + +# Standard network config file. Will be populated based on +# the environment. +conf_dict = { + "type": "calico", + "ipam": { + "type": "calico-ipam" + }, +} + +# Check if a kubeconfig was given. +if os.environ.get("KUBECONFIG"): + conf_dict["kubernetes"] = {} + conf_dict["kubernetes"]["kubeconfig"] = os.environ.get("KUBECONFIG") + +# Check for policy type. +if os.environ.get("POLICY_TYPE"): + p = conf_dict.setdefault("policy", {}) + p["type"] = os.environ.get("POLICY_TYPE") + + # Check for a k8s API root. + if os.environ.get("K8S_API"): + p["k8s_api_root"] = os.environ.get("K8S_API") + + # Check for certificates. + if os.environ.get("K8S_CLIENT_CERT"): + p["k8s_client_certificate"] = os.environ.get("K8S_CLIENT_CERT") + if os.environ.get("K8S_CLIENT_KEY"): + p["k8s_client_key"] = os.environ.get("K8S_CLIENT_KEY") + if os.environ.get("K8S_CERT_AUTHORITY"): + p["k8s_certificate_authority"] = os.environ.get("K8S_CERT_AUTHORITY") + + # Check for token. + if os.environ.get("K8S_TOKEN"): + p["k8s_auth_token"] = os.environ.get("K8S_TOKEN") + +# Set log level. +conf_dict["log_level"] = os.environ.get("LOG_LEVEL", "info").lower() + +# Set the etcd endpoints. +conf_dict["etcd_endpoints"] = os.environ.get("ETCD_ENDPOINTS", + "http://127.0.0.1:2379") + +# Print the generated network config. +conf = json.dumps(conf_dict, indent=2) +print conf diff --git a/scripts/install-cni.sh b/scripts/install-cni.sh new file mode 100755 index 000000000..382fc121b --- /dev/null +++ b/scripts/install-cni.sh @@ -0,0 +1,31 @@ +#!/bin/bash + +# Script to install Calico CNI on a Kubernetes host. +# Expects the CNI binary path to be mounted in to the +# `calico/cni` container at /host/opt/cni/bin. +# Expects the CNI network config path to be mounted in +# to the `calico/cni` container at /host/etc/cni/net.d. + +# Clean up any existing binaries. +rm -f /host/opt/cni/bin/calico /host/opt/cni/bin/calico-ipam + +# Place the new binaries. +cp /opt/cni/bin/calico /host/opt/cni/bin/calico +cp /opt/cni/bin/calico-ipam /host/opt/cni/bin/calico-ipam +echo "Wrote Calico CNI binaries to /host/opt/cni/bin/" +echo "Version: $(/host/opt/cni/bin/calico -v)" + +# Make the network configuration file. +cat >/host/etc/cni/net.d/10-calico.conf <