Skip to content

Commit

Permalink
Tweak Dockerfile to install bins on k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
caseydavenport committed Aug 4, 2016
1 parent ed9f217 commit 2628758
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
46 changes: 46 additions & 0 deletions scripts/conf_writer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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()

# Print the generated network config.
conf = json.dumps(conf_dict, indent=2)
print conf
31 changes: 31 additions & 0 deletions scripts/install-cni.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF
$(/usr/bin/python /conf_writer.py)
EOF

echo "Wrote CNI config to disk: $(cat /host/etc/cni/net.d/10-calico.conf)"

# Unless told otherwise, sleep forever.
# This prevents Kubernetes from restarting the pod repeatedly.
should_sleep=${SLEEP:-"true"}
echo "Done configuring CNI. Sleep=$should_sleep"
while [ "$should_sleep" == "true" ]; do
sleep 1;
done

0 comments on commit 2628758

Please sign in to comment.