Skip to content
This repository has been archived by the owner on Nov 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #57 from pusher/prow-migration
Browse files Browse the repository at this point in the history
Migrate CI tasks to Makefile ready for Prow
  • Loading branch information
JoelSpeed committed Apr 25, 2019
2 parents a472745 + 0a7bfaf commit b46cd36
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 91 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,7 @@ vendor/
# Kubernetes config
kubeconfig
credentials/

# Local environmental config
.env
k8s-spot-rescheduler
8 changes: 5 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ Download the dependencies using [`glide`](https://github.com/Masterminds/glide).

```bash
cd $GOPATH/src/github.com # Create this directory if it doesn't exist
git clone git@github.com:<YOUR_FORK>/spot-rescheduler pusher/spot-rescheduler
glide install -v # Installs dependencies to vendor folder.
git clone git@github.com:<YOUR_FORK>/k8s-spot-rescheduler pusher/k8s-spot-rescheduler
cd $GOPATH/src/github.com/pusher/k8s-spot-recheduler
./configure # Configure local tooling - install anything reported as missing
make vendor # Clone required project dependencies
```

The main package is within `rescheduler.go` and an overview of it's operating logic is described in the [Readme](README.md/#operating-logic).
Expand All @@ -25,4 +27,4 @@ Please mention the open bug issue number within your PR if applicable.
### Tests
Unit tests are covering the decision making parts of this code and can be run using the built in Go test suite.

To run the tests: `go test $(glide novendor)`
To run the tests: `make test`
33 changes: 25 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
FROM golang:1.10 AS builder
ARG VERSION=undefined

FROM golang:1.12 AS builder
ARG VERSION

RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

WORKDIR /go/src/github.com/pusher/k8s-spot-rescheduler
COPY . .
RUN go get -u github.com/golang/dep/cmd/dep \
&& dep ensure -v \
&& env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o rescheduler

FROM scratch
COPY --from=builder /go/src/github.com/pusher/k8s-spot-rescheduler/rescheduler /bin/rescheduler
COPY Gopkg.lock Gopkg.lock
COPY Gopkg.toml Gopkg.toml

RUN dep ensure --vendor-only

COPY *.go ./
COPY deploy deploy/
COPY metrics metrics/
COPY nodes nodes/
COPY scaler scaler/

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-X main.VERSION=${VERSION}" -a -o k8s-spot-rescheduler github.com/pusher/k8s-spot-rescheduler

FROM alpine:3.9
RUN apk --no-cache add ca-certificates
WORKDIR /bin
COPY --from=builder /go/src/github.com/pusher/k8s-spot-rescheduler/k8s-spot-rescheduler .

ENTRYPOINT ["/bin/rescheduler"]
ENTRYPOINT ["/bin/k8s-spot-rescheduler"]
91 changes: 91 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
include .env
BINARY := k8s-spot-rescheduler
VERSION := $(shell git describe --always --dirty --tags 2>/dev/null || echo "undefined")

RED := \033[31m
GREEN := \033[32m
NC := \033[0m

IMG ?= quay.io/pusher/k8s-spot-rescheduler

.NOTPARALLEL:

.PHONY: all
all: distclean test build

.PHONY: build
build: clean $(BINARY)

.PHONY: clean
clean:
rm -f $(BINARY)

.PHONY: distclean
distclean: clean
rm -rf vendor
rm -rf release

.PHONY: fmt
fmt:
$(GO) fmt ./...

.PHONY: vet
vet: vendor
$(GO) vet ./...

.PHONY: lint
lint:
@ echo "$(GREEN)Linting code$(NC)"
$(LINTER) run --disable-all \
--exclude-use-default=false \
--enable=govet \
--enable=ineffassign \
--enable=deadcode \
--enable=golint \
--enable=goconst \
--enable=gofmt \
--enable=goimports \
--skip-dirs=pkg/client/ \
--deadline=120s \
--tests ./...
@ echo

vendor:
@ echo "$(GREEN)Pulling dependencies$(NC)"
$(DEP) ensure --vendor-only
@ echo

.PHONY: test
test: vendor
@ echo "$(GREEN)Running test suite$(NC)"
$(GO) test ./...
@ echo

.PHONY: check
check: fmt lint vet test

.PHONY: build
build: clean $(BINARY)

$(BINARY): fmt vet
CGO_ENABLED=0 $(GO) build -o $(BINARY) -ldflags="-X main.VERSION=${VERSION}" github.com/pusher/k8s-spot-rescheduler

.PHONY: docker-build
docker-build: check
docker build --build-arg VERSION=${VERSION} . -t ${IMG}:${VERSION}
@echo "$(GREEN)Built $(IMG):$(VERSION)$(NC)"

TAGS ?= latest
.PHONY: docker-tag
docker-tag: docker-build
@IFS=","; tags=${TAGS}; for tag in $${tags}; do docker tag ${IMG}:${VERSION} ${IMG}:$${tag}; echo "$(GREEN)Tagged $(IMG):$(VERSION) as $${tag}$(NC)"; done

PUSH_TAGS ?= ${VERSION}, latest
.PHONY: docker-push
docker-push: docker-build docker-tag
@IFS=","; tags=${PUSH_TAGS}; for tag in $${tags}; do docker push ${IMG}:$${tag}; echo "$(GREEN)Pushed $(IMG):$${tag}$(NC)"; done

TAGS ?= latest
.PHONY: docker-clean
docker-clean:
@IFS=","; tags=${TAGS}; for tag in $${tags}; do docker rmi -f ${IMG}:${VERSION} ${IMG}:$${tag}; done
107 changes: 107 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#!/usr/bin/env bash

if (( ${BASH_VERSION:0:1} < 4 )); then
echo "This configure script requires bash 4"
exit 1
fi

RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
NC='\033[0m'

declare -A tools=()

vercomp () {
if [[ $1 == $2 ]]
then
return 0
fi
local IFS=.
local i ver1=($1) ver2=($2)
# fill empty fields in ver1 with zeros
for ((i=${#ver1[@]}; i<${#ver2[@]}; i++))
do
ver1[i]=0
done
for ((i=0; i<${#ver1[@]}; i++))
do
if [[ -z ${ver2[i]} ]]
then
# fill empty fields in ver2 with zeros
ver2[i]=0
fi
if ((10#${ver1[i]} > 10#${ver2[i]}))
then
return 1
fi
if ((10#${ver1[i]} < 10#${ver2[i]}))
then
return 2
fi
done
return 0
}

check_for() {
echo -n "Checking for $1... "
TOOL_PATH=$(command -v $1)
if ! [ -x "$TOOL_PATH" -a -f "$TOOL_PATH" ]; then
printf "${RED}not found${NC}\n"
cd - > /dev/null
exit 1
else
printf "${GREEN}found${NC}\n"
tools[$1]=$TOOL_PATH
fi
}

check_go_env() {
echo -n "Checking \$GOPATH... "
if [ -z "$GOPATH" ]; then
printf "${RED}invalid${NC} - GOPATH not set\n"
exit 1
fi
printf "${GREEN}valid${NC} - $GOPATH\n"
}

check_go_version() {
echo -n "Checking go version... "
GO_VERSION=$(${tools[go]} version | ${tools[awk]} '{where = match($0, /[0-9]\.[0-9]+[\.0-9]*/); if (where != 0) print substr($0, RSTART, RLENGTH)}')
vercomp $GO_VERSION 1.10
case $? in
0) ;&
1)
printf "${GREEN}"
echo $GO_VERSION
printf "${NC}"
;;
2)
printf "${RED}"
echo "$GO_VERSION < 1.10"
exit 1
;;
esac
}

cd ${0%/*}

check_for make
check_for awk
check_for go
check_for dep
check_for golangci-lint
check_for shasum
check_go_env
check_go_version

cat <<- EOF > .env
MAKE := ${tools[make]}
SHASUM := ${tools[shasum]}
GO := ${tools[go]}
GOVERSION := $GO_VERSION
DEP := ${tools[dep]}
LINTER := ${tools[golangci-lint]}
EOF

echo "Environment configuration written to .env"
7 changes: 7 additions & 0 deletions rescheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ var (
`Address to listen on for serving prometheus metrics`)

deleteNonReplicatedPods = flags.Bool("delete-non-replicated-pods", false, `Delete non-replicated pods running on on-demand instance. Note that some non-replicated pods will not be rescheduled.`)

showVersion = flags.Bool("version", false, "Show version information and exit.")
)

func main() {
Expand All @@ -103,6 +105,11 @@ func main() {

flags.Parse(os.Args)

if *showVersion {
fmt.Printf("k8s-spot-rescheduler %s\n", VERSION)
os.Exit(0)
}

glog.Infof("Running Rescheduler")

// Register metrics from metrics.go
Expand Down
4 changes: 4 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package main

// VERSION contains injected version information
var VERSION = "undefined"
80 changes: 0 additions & 80 deletions wercker.yml

This file was deleted.

0 comments on commit b46cd36

Please sign in to comment.