Skip to content

Commit

Permalink
Merge pull request #181 from caseydavenport/merge-master
Browse files Browse the repository at this point in the history
Pull in changes from master
  • Loading branch information
caseydavenport authored May 25, 2018
2 parents f6ddbe0 + 30b2451 commit bc9dade
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FROM alpine
MAINTAINER Tom Denham <[email protected]>
ADD dist/libnetwork-plugin /libnetwork-plugin
ADD dist/amd64/libnetwork-plugin /libnetwork-plugin
ENTRYPOINT ["/libnetwork-plugin"]

4 changes: 4 additions & 0 deletions Dockerfile-ppc64le
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM ppc64le/alpine
MAINTAINER Tom Denham <[email protected]>
ADD dist/ppc64le/libnetwork-plugin /libnetwork-plugin
ENTRYPOINT ["/libnetwork-plugin"]
97 changes: 63 additions & 34 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
##############################################################################
# The build architecture is select by setting the ARCH variable.
# For example: When building on ppc64le you could use ARCH=ppc64le make <....>.
# When ARCH is undefined it defaults to amd64.
ARCH?=amd64
ifeq ($(ARCH),amd64)
ARCHTAG:=
GO_BUILD_VER?=v0.12
BUSYBOX_IMAGE?=busybox:latest
DIND_IMAGE?=docker:17.12.0-dind
endif

ifeq ($(ARCH),ppc64le)
ARCHTAG:=-ppc64le
GO_BUILD_VER?=latest
BUSYBOX_IMAGE?=ppc64le/busybox:latest
DIND_IMAGE?=ppc64le/docker:dind
endif

# Disable make's implicit rules, which are not useful for golang, and slow down the build
# considerably.
.SUFFIXES:

GO_BUILD_VER?=v0.9

SRC_FILES=$(shell find . -type f -name '*.go')

# These variables can be overridden by setting an environment variable.
LOCAL_IP_ENV?=$(shell ip route get 8.8.8.8 | head -1 | awk '{print $$7}')

# Can choose different docker versions see list here - https://hub.docker.com/_/docker/
DOCKER_VERSION?=17.12.0-dind
HOST_CHECKOUT_DIR?=$(CURDIR)
CONTAINER_NAME?=calico/libnetwork-plugin
GO_BUILD_CONTAINER?=calico/go-build:$(GO_BUILD_VER)
PLUGIN_LOCATION?=$(CURDIR)/dist/libnetwork-plugin
DOCKER_BINARY_CONTAINER?=docker-binary-container
CONTAINER_NAME?=calico/libnetwork-plugin$(ARCHTAG)
GO_BUILD_CONTAINER?=calico/go-build$(ARCHTAG):$(GO_BUILD_VER)
DIST=dist/$(ARCH)
PLUGIN_LOCATION?=$(CURDIR)/$(DIST)/libnetwork-plugin
DOCKER_BINARY_CONTAINER?=docker-binary-container$(ARCHTAG)

# To run with non-native docker (e.g. on Windows or OSX) you might need to overide this variable
LOCAL_USER_ID?=$(shell id -u $$USER)
Expand All @@ -39,39 +56,41 @@ install:
CGO_ENABLED=0 go install github.com/projectcalico/libnetwork-plugin

# Run the build in a container. Useful for CI
dist/libnetwork-plugin: vendor
-mkdir -p dist
$(DIST)/libnetwork-plugin: vendor
-mkdir -p $(DIST)
-mkdir -p .go-pkg-cache
docker run --rm \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin:ro \
-v $(CURDIR)/dist:/go/src/github.com/projectcalico/libnetwork-plugin/dist \
-v $(CURDIR)/$(DIST):/go/src/github.com/projectcalico/libnetwork-plugin/$(DIST) \
-v $(CURDIR)/.go-pkg-cache:/go/pkg/:rw \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-e ARCH=$(ARCH) \
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
make build'

build: $(SRC_FILES) vendor
CGO_ENABLED=0 go build -v -i -o dist/libnetwork-plugin -ldflags "-X main.VERSION=$(shell git describe --tags --dirty) -s -w" main.go
CGO_ENABLED=0 go build -v -i -o $(DIST)/libnetwork-plugin -ldflags "-X main.VERSION=$(shell git describe --tags --dirty) -s -w" main.go

$(CONTAINER_NAME): dist/libnetwork-plugin
docker build -t $(CONTAINER_NAME) .

$(CONTAINER_NAME): $(DIST)/libnetwork-plugin
docker build -t $(CONTAINER_NAME) -f Dockerfile$(ARCHTAG) .

# Perform static checks on the code. The golint checks are allowed to fail, the others must pass.
.PHONY: static-checks
static-checks: vendor
docker run --rm \
-e LOCAL_USER_ID=$(LOCAL_USER_ID) \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin \
calico/go-build sh -c '\
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
gometalinter --deadline=30s --disable-all --enable=goimports --enable=vet --enable=errcheck --enable=varcheck --enable=unused --enable=dupl $$(glide nv)'

run-etcd:
@-docker rm -f calico-etcd
docker run --detach \
--net=host \
--name calico-etcd quay.io/coreos/etcd \
--name calico-etcd quay.io/coreos/etcd:v3.2.5$(ARCHTAG) \
etcd \
--advertise-client-urls "http://$(LOCAL_IP_ENV):2379,http://127.0.0.1:2379" \
--listen-client-urls "http://0.0.0.0:2379"
Expand All @@ -85,28 +104,28 @@ endif
# Check that the version output appears on a line of its own (the -x option to grep).
# Tests that the "git tag" makes it into the binary. Main point is to catch "-dirty" builds
@echo "Checking if the tag made it into the binary"
docker run --rm calico/libnetwork-plugin -v | grep -x $(VERSION) || (echo "Reported version:" `docker run --rm calico/libnetwork-plugin -v` "\nExpected version: $(VERSION)" && exit 1)
docker tag calico/libnetwork-plugin calico/libnetwork-plugin:$(VERSION)
docker tag calico/libnetwork-plugin quay.io/calico/libnetwork-plugin:$(VERSION)
docker tag calico/libnetwork-plugin quay.io/calico/libnetwork-plugin:latest
docker run --rm calico/libnetwork-plugin$(ARCHTAG) -v | grep -x $(VERSION) || (echo "Reported version:" `docker run --rm calico/libnetwork-plugin$(ARCHTAG) -v` "\nExpected version: $(VERSION)" && exit 1)
docker tag calico/libnetwork-plugin$(ARCHTAG) calico/libnetwork-plugin$(ARCHTAG):$(VERSION)
docker tag calico/libnetwork-plugin$(ARCHTAG) quay.io/calico/libnetwork-plugin$(ARCHTAG):$(VERSION)
docker tag calico/libnetwork-plugin$(ARCHTAG) quay.io/calico/libnetwork-plugin$(ARCHTAG):latest

@echo "Now push the tag and images. Then create a release on Github and attach the dist/libnetwork-plugin binary"
@echo "Now push the tag and images. Then create a release on Github and attach the $(DIST)/libnetwork-plugin binary"
@echo "git push origin $(VERSION)"
@echo "docker push calico/libnetwork-plugin:$(VERSION)"
@echo "docker push quay.io/calico/libnetwork-plugin:$(VERSION)"
@echo "docker push calico/libnetwork-plugin:latest"
@echo "docker push quay.io/calico/libnetwork-plugin:latest"
@echo "docker push calico/libnetwork-plugin$(ARCHTAG):$(VERSION)"
@echo "docker push quay.io/calico/libnetwork-plugin$(ARCHTAG):$(VERSION)"
@echo "docker push calico/libnetwork-plugin$(ARCHTAG):latest"
@echo "docker push quay.io/calico/libnetwork-plugin$(ARCHTAG):latest"

clean:
rm -rf dist *.tar vendor docker .go-pkg-cache
rm -rf $(DIST) bin *.tar vendor .go-pkg-cache

run-plugin: run-etcd dist/libnetwork-plugin
run-plugin: run-etcd $(DIST)/libnetwork-plugin
-docker rm -f dind
docker run -tid -h test --name dind --privileged $(ADDITIONAL_DIND_ARGS) \
-e ETCD_ENDPOINTS=http://$(LOCAL_IP_ENV):2379 \
-p 5375:2375 \
-v $(PLUGIN_LOCATION):/libnetwork-plugin \
docker:$(DOCKER_VERSION) --cluster-store=etcd://$(LOCAL_IP_ENV):2379
$(DIND_IMAGE) --cluster-store=etcd://$(LOCAL_IP_ENV):2379
# View the logs by running 'docker exec dind cat plugin.log'
docker exec -tid --privileged dind sh -c 'sysctl -w net.ipv6.conf.default.disable_ipv6=0'
docker exec -tid --privileged dind sh -c '/libnetwork-plugin 2>>/plugin.log'
Expand All @@ -118,18 +137,28 @@ run-plugin: run-etcd dist/libnetwork-plugin
test:
CGO_ENABLED=0 ginkgo -v tests/*

test-containerized: dist/libnetwork-plugin
# Target test-containerized needs the docker binary to be available in the go-build container.
# Obtaining it from the docker:dind images docker should provided the latest version. However,
# this assumes that the go_build container has the required dependencies or that docker is static.
# This may not be the case in all configurations. In this cases you should pre-populate ./bin
# with a docker binary compatible with the go-build image that is used.
bin/docker:
-docker rm -f $(DOCKER_BINARY_CONTAINER) 2>&1
docker create --name $(DOCKER_BINARY_CONTAINER) docker:$(DOCKER_VERSION)
docker cp $(DOCKER_BINARY_CONTAINER):/usr/local/bin/docker .
mkdir -p ./bin
docker create --name $(DOCKER_BINARY_CONTAINER) $(DIND_IMAGE)
docker cp $(DOCKER_BINARY_CONTAINER):/usr/local/bin/docker ./bin/docker
docker rm -f $(DOCKER_BINARY_CONTAINER)
docker run -ti --rm --net=host \

test-containerized: $(DIST)/libnetwork-plugin bin/docker
docker run -t --rm --net=host \
-v $(CURDIR):/go/src/github.com/projectcalico/libnetwork-plugin \
-v /var/run/docker.sock:/var/run/docker.sock \
-v $(CURDIR)/docker:/usr/bin/docker \
-e PLUGIN_LOCATION=$(CURDIR)/dist/libnetwork-plugin \
-v $(CURDIR)/bin/docker:/usr/bin/docker \
-e PLUGIN_LOCATION=$(CURDIR)/$(DIST)/libnetwork-plugin \
-e LOCAL_USER_ID=0 \
calico/go-build sh -c '\
-e ARCH=$(ARCH) \
-e BUSYBOX_IMAGE=$(BUSYBOX_IMAGE) \
$(GO_BUILD_CONTAINER) sh -c '\
cd /go/src/github.com/projectcalico/libnetwork-plugin && \
make test'

2 changes: 2 additions & 0 deletions driver/ipam_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,11 @@ func (i IpamDriver) RequestAddress(request *ipam.RequestAddressRequest) (*ipam.R
version = ipNet.Version()
if version == 4 {
poolV4 = []caliconet.IPNet{caliconet.IPNet{IPNet: pool.Metadata.CIDR.IPNet}}
numIPv4 = 1
log.Debugln("Using specific pool ", poolV4)
} else if version == 6 {
poolV6 = []caliconet.IPNet{caliconet.IPNet{IPNet: pool.Metadata.CIDR.IPNet}}
numIPv6 = 1
log.Debugln("Using specific pool ", poolV6)
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/custom_if_prefix/libnetwork_env_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package custom_if_prefix
import (
"fmt"
"math/rand"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -23,7 +24,7 @@ var _ = Describe("Running plugin with custom ENV", func() {
DockerString(fmt.Sprintf("docker network create %s -d calico --ipam-driver calico-ipam", name))

// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down
5 changes: 3 additions & 2 deletions tests/custom_wep_labelling/libnetwork_env_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"time"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -23,7 +24,7 @@ var _ = Describe("Running plugin with custom ENV", func() {
DockerString(fmt.Sprintf("docker network create %s -d calico --ipam-driver calico-ipam", name))

// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --label not=expected --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --label not=expected --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down Expand Up @@ -67,7 +68,7 @@ var _ = Describe("Running plugin with custom ENV", func() {
DockerString(fmt.Sprintf("docker network create %s -d calico --ipam-driver calico-ipam", name))

// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --label not=expected --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --label not=expected --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down
51 changes: 45 additions & 6 deletions tests/default_environment/libnetwork_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"math/rand"
"regexp"
"os"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -137,7 +138,7 @@ var _ = Describe("Libnetwork Tests", func() {

It("creates a container on a network and checks all assertions", func() {
// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down Expand Up @@ -182,7 +183,7 @@ var _ = Describe("Libnetwork Tests", func() {
It("creates a container with specific MAC", func() {
// Create a container that will just sit in the background
chosen_mac := "00:22:33:44:55:66"
DockerString(fmt.Sprintf("docker run --mac-address %s --net %s -tid --name %s busybox", chosen_mac, name, name))
DockerString(fmt.Sprintf("docker run --mac-address %s --net %s -tid --name %s %s", chosen_mac, name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down Expand Up @@ -219,7 +220,7 @@ var _ = Describe("Libnetwork Tests", func() {

PIt("creates a container with specific link local address", func() { // https://github.com/docker/docker/issues/28606
// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --link-local-ip 169.254.0.50 %s --net %s -tid --name %s busybox", name, name, name))
DockerString(fmt.Sprintf("docker run --link-local-ip 169.254.0.50 %s --net %s -tid --name %s %s", name, name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Delete container
DockerString(fmt.Sprintf("docker rm -f %s", name))
Expand All @@ -234,7 +235,7 @@ var _ = Describe("Libnetwork Tests", func() {
DockerString(fmt.Sprintf("docker network create %s --subnet 192.169.0.0/16 -d calico --ipam-driver calico-ipam", name_subnet))
// Create a container that will just sit in the background
chosen_ip := "192.169.50.51"
DockerString(fmt.Sprintf("docker run --ip %s --net %s -tid --name %s busybox", chosen_ip, name_subnet, name_subnet))
DockerString(fmt.Sprintf("docker run --ip %s --net %s -tid --name %s %s", chosen_ip, name_subnet, name_subnet, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name_subnet, name_subnet)
Expand Down Expand Up @@ -269,9 +270,47 @@ var _ = Describe("Libnetwork Tests", func() {
DockerString(fmt.Sprintf("docker network rm %s", name_subnet))
})

It("creates a container in network with a subnet", func() {
name_subnet := fmt.Sprintf("run%d", rand.Uint32())
DockerString(fmt.Sprintf("docker network create %s --subnet 192.169.0.0/16 -d calico --ipam-driver calico-ipam", name_subnet))
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s %s", name_subnet, name_subnet, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name_subnet, name_subnet)
ip := docker_endpoint.IPAddress
mac := docker_endpoint.MacAddress
endpoint_id := docker_endpoint.EndpointID
interface_name_subnet := "cali" + endpoint_id[:mathutils.MinInt(11, len(endpoint_id))]

Expect(ip).Should(HavePrefix("192.169."))

// Check that the endpoint is created in etcd
etcd_endpoint := GetEtcdString(fmt.Sprintf("/calico/v1/host/test/workload/libnetwork/libnetwork/endpoint/%s", endpoint_id))
Expect(etcd_endpoint).Should(MatchJSON(fmt.Sprintf(
`{"state":"active","name":"%s","active_instance_id":"","mac":"%s","profile_ids":["%s"],"ipv4_nets":["%s/32"],"ipv6_nets":[]}`,
interface_name_subnet, mac, name_subnet, ip)))

// Check the interface exists on the Host - it has an autoassigned
// mac and ip, so don't check anything!
DockerString(fmt.Sprintf("ip addr show %s", interface_name_subnet))

// Make sure the interface in the container exists and has the assigned ip and mac
container_interface_string := DockerString(fmt.Sprintf("docker exec -i %s ip addr", name_subnet))
Expect(container_interface_string).Should(ContainSubstring(ip))
Expect(container_interface_string).Should(ContainSubstring(mac))

// Make sure the container has the routes we expect
routes := DockerString(fmt.Sprintf("docker exec -i %s ip route", name_subnet))
Expect(routes).Should(Equal("default via 169.254.1.1 dev cali0 \n169.254.1.1 dev cali0 scope link"))

// Delete container and network
DockerString(fmt.Sprintf("docker rm -f %s", name_subnet))
DockerString(fmt.Sprintf("docker network rm %s", name_subnet))
})

It("creates a container with labels, but do not expect those in endpoint", func() {
// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --label org.projectcalico.label.foo=bar --label org.projectcalico.label.baz=quux --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down Expand Up @@ -304,7 +343,7 @@ var _ = Describe("Libnetwork Tests", func() {

It("creates a container on a network and checks all assertions", func() {
// Create a container that will just sit in the background
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s busybox", name, name))
DockerString(fmt.Sprintf("docker run --net %s -tid --name %s %s", name, name, os.Getenv("BUSYBOX_IMAGE") ))

// Gather information for assertions
docker_endpoint := GetDockerEndpoint(name, name)
Expand Down

0 comments on commit bc9dade

Please sign in to comment.