Skip to content

Commit

Permalink
Add CI config
Browse files Browse the repository at this point in the history
  • Loading branch information
munnerz committed Apr 5, 2017
1 parent 07403d3 commit 5d433b7
Show file tree
Hide file tree
Showing 10 changed files with 265 additions and 18 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.kcpBuildImage
.init
.pkg/
bin/
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
language: generic
sudo: required
services:
- docker
script:
- make verify build test images
deploy:
provider: script
script: scripts/deploy.sh
on:
repo: munnerz/keepalived-cloud-provider
branch: master
5 changes: 0 additions & 5 deletions Dockerfile

This file was deleted.

2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) [year] [fullname]
Copyright (c) 2017 James Munnelly

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
153 changes: 153 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
# Modified for keepalived-cloud-provider
#
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

all: build test verify

# Some env vars that devs might find useful:
# GOFLAGS : extra "go build" flags to use - e.g. -v (for verbose)
# NO_DOCKER=1 : execute each step natively, not in a Docker container
# TEST_DIRS= : only run the unit tests from the specified dirs
# UNIT_TESTS= : only run the unit tests matching the specified regexp

# Define some constants
#######################
ROOT = $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
BINDIR ?= bin
BUILD_DIR ?= build
KCP_PKG = github.com/munnerz/keepalived-cloud-provider
TOP_SRC_DIRS = keepalivedcp
SRC_DIRS = $(shell sh -c "find $(TOP_SRC_DIRS) -name \\*.go \
-exec dirname {} \\; | sort | uniq")
TEST_DIRS ?= $(shell sh -c "find $(TOP_SRC_DIRS) -name \\*_test.go \
-exec dirname {} \\; | sort | uniq")
VERSION ?= $(shell git describe --always --abbrev=7 --dirty)
ifeq ($(shell uname -s),Darwin)
STAT = stat -f '%c %N'
else
STAT = stat -c '%Y %n'
endif
NEWEST_GO_FILE = $(shell find $(SRC_DIRS) -name \*.go -exec $(STAT) {} \; \
| sort -r | head -n 1 | sed "s/.* //")
GO_VERSION = 1.8.0

PLATFORM?=linux
ARCH?=amd64

GO_BUILD = env GOOS=$(PLATFORM) GOARCH=$(ARCH) go build -i $(GOFLAGS) \
-ldflags "-X $(KCP_PKG)/pkg.VERSION=$(VERSION)"
BASE_PATH = $(ROOT:/src/github.com/munnerz/keepalived-cloud-provider/=)
export GOPATH = $(BASE_PATH):$(ROOT)/vendor

MUTABLE_TAG ?= canary
CONTROLLER_IMAGE = $(REGISTRY)keepalived-cloud-provider:$(VERSION)
CONTROLLER_MUTABLE_IMAGE = $(REGISTRY)keepalived-cloud-provider:$(MUTABLE_TAG)

ifdef UNIT_TESTS
UNIT_TEST_FLAGS=-run $(UNIT_TESTS) -v
endif

ifdef NO_DOCKER
DOCKER_CMD =
kcpBuildImageTarget =
else
# Mount .pkg as pkg so that we save our cached "go build" output files
DOCKER_CMD = docker run --rm -v $(PWD):/go/src/$(KCP_PKG) \
-v $(PWD)/.pkg:/go/pkg kcpbuildimage
kcpBuildImageTarget = .kcpBuildImage
endif

NON_VENDOR_DIRS = $(shell $(DOCKER_CMD) go list $(KCP_PKG)/... | grep -v /vendor/)

# Some prereq stuff
###################
.init: $(kcpBuildImageTarget)

.kcpBuildImage: build/build-image/Dockerfile
sed "s/GO_VERSION/$(GO_VERSION)/g" < build/build-image/Dockerfile | \
docker build -t kcpbuildimage -
touch $@

# This section builds the output binaries.
#########################################################################
build: .init \
$(BINDIR)/keepalived-cloud-provider

keepalived-cloud-provider: $(BINDIR)/keepalived-cloud-provider
$(BINDIR)/keepalived-cloud-provider: .init
$(DOCKER_CMD) $(GO_BUILD) -o $@ $(KCP_PKG)


# Util targets
##############
.PHONY: verify
verify: .init
@echo Running gofmt:
@$(DOCKER_CMD) gofmt -l -s $(TOP_SRC_DIRS) > .out 2>&1 || true
@bash -c '[ "`cat .out`" == "" ] || \
(echo -e "\n*** Please 'gofmt' the following:" ; cat .out ; echo ; false)'
@rm .out
@#
@echo Running golint and go vet:
@# Exclude the generated (zz) files for now, as well as defaults.go (it
@# observes conventions from upstream that will not pass lint checks).
@$(DOCKER_CMD) sh -c \
'for i in $$(find $(TOP_SRC_DIRS) -name *.go); \
do \
golint --set_exit_status $$i || exit 1; \
done'
@#
$(DOCKER_CMD) go vet $(NON_VENDOR_DIRS)

format: .init
$(DOCKER_CMD) gofmt -w -s $(TOP_SRC_DIRS)

test: .init build test-unit

test-unit: .init build
@echo Running tests:
$(DOCKER_CMD) go test $(UNIT_TEST_FLAGS) \
$(addprefix $(KCP_PKG)/,$(TEST_DIRS))

clean: clean-bin clean-deps clean-build-image

clean-bin:
rm -rf $(BINDIR)

clean-deps:
rm -f .init

clean-build-image:
rm -f .kcpBuildImage
docker rmi -f kcpbuildimage > /dev/null 2>&1 || true

# Building Docker Images for our executables
############################################
images: keepalived-cloud-provider-image

keepalived-cloud-provider-image: $(BINDIR)/keepalived-cloud-provider
mkdir -p build/keepalived-cloud-provider/tmp
cp $(BINDIR)/keepalived-cloud-provider build/keepalived-cloud-provider/tmp
docker build -t $(CONTROLLER_IMAGE) build/keepalived-cloud-provider
docker tag $(CONTROLLER_IMAGE) $(CONTROLLER_MUTABLE_IMAGE)
rm -rf build/keepalived-cloud-provider/tmp

# Push our Docker Images to a registry
######################################
push: keepalived-cloud-provider-push

keepalived-cloud-provider-push: keepalived-cloud-provider-image
docker push $(CONTROLLER_IMAGE)
docker push $(CONTROLLER_MUTABLE_IMAGE)
22 changes: 22 additions & 0 deletions build/build-image/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Modified for keepalived-cloud-provider
#
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

FROM golang:GO_VERSION

RUN go get -u github.com/golang/lint/golint

RUN mkdir -p /go/src/github.com/munnerz/keepalived-cloud-provider
WORKDIR /go/src/github.com/munnerz/keepalived-cloud-provider
10 changes: 10 additions & 0 deletions build/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
set -o errexit
set -o nounset
set -o pipefail

export REGISTRY=quay.io/munnerz/

docker login -e="${QUAY_EMAIL}" -u "${QUAY_USERNAME}" -p "${QUAY_PASSWORD}" quay.io

echo "Pushing images with sha tag."
make push
5 changes: 5 additions & 0 deletions build/keepalived-cloud-provider/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FROM alpine:3.5

ADD tmp/keepalived-cloud-provider /usr/local/bin/keepalived-cloud-provider

CMD ["keepalived-cloud-provider"]
12 changes: 0 additions & 12 deletions cloudbuild.yaml

This file was deleted.

58 changes: 58 additions & 0 deletions keepalivedcp/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package keepalivedcp

import "testing"

func TestAllocateIP(t *testing.T) {
type testDef struct {
name string
config config
cidr string
expectedIP string
err bool
}

tests := []testDef{
{
name: "allocate ip address in empty pool",
config: config{
Services: []serviceConfig{},
},
cidr: "10.0.0.0/8",
expectedIP: "10.0.0.1",
},
{
name: "allocate ip address in pool with one address",
config: config{
Services: []serviceConfig{
{
UID: "a",
IP: "10.0.0.1",
},
},
},
cidr: "10.0.0.0/8",
expectedIP: "10.0.0.2",
},
}

for _, test := range tests {
t.Run(test.name, func(test testDef) func(*testing.T) {
return func(t *testing.T) {
ip, err := test.config.allocateIP(test.cidr)

if err != nil {
if test.err {
return
}

t.Errorf("got error: %s", err.Error())
return
}

if ip != test.expectedIP {
t.Errorf("expected IP '%s' but got '%s'", test.expectedIP, ip)
}
}
}(test))
}
}

0 comments on commit 5d433b7

Please sign in to comment.