Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Makefile #445

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,15 @@ _testmain.go
*.test
*.prof

*.DS_Store
.idea/
dist/
.vscode/
.markdownlint.json
coverage.txt
apiserver.local.config/
/bin
/.go

/.idea
/.markdownlint.json
/.vscode
/apiserver.local.config
/coverage.txt
/dist
/hack/configs/.env
/test/e2e/junit.xml
/test/e2e/report.xml
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ language: go
go:
- 1.x

cache:
directories:
- $HOME/.cache/go-build
- $GOPATH/pkg/mod

install: true

script:
Expand Down
217 changes: 217 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
SHELL=/bin/bash -o pipefail

# The binary to build (just the basename).
BIN := kubedb
COMPRESS ?= no

# This version-strategy uses git tags to set the version string
git_branch := $(shell git rev-parse --abbrev-ref HEAD)
git_tag := $(shell git describe --exact-match --abbrev=0 2>/dev/null || echo "")
commit_hash := $(shell git rev-parse --verify HEAD)
commit_timestamp := $(shell date --date="@$$(git show -s --format=%ct)" --utc +%FT%T)

VERSION := $(shell git describe --tags --always --dirty)
version_strategy := commit_hash
ifdef git_tag
VERSION := $(git_tag)
version_strategy := tag
else
ifeq (,$(findstring $(git_branch),master HEAD))
ifneq (,$(patsubst release-%,,$(git_branch)))
VERSION := $(git_branch)
version_strategy := branch
endif
endif
endif

###
### These variables should not need tweaking.
###

SRC_DIRS := cmd pkg hack/gendocs # directories which hold app source (not vendored)

DOCKER_PLATFORMS := linux/amd64 linux/arm linux/arm64
BIN_PLATFORMS := $(DOCKER_PLATFORMS) windows/amd64 darwin/amd64

# Used internally. Users should pass GOOS and/or GOARCH.
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))

GO_VERSION ?= 1.12.5
BUILD_IMAGE ?= appscode/golang-dev:$(GO_VERSION)-stretch

OUTBIN = bin/$(OS)_$(ARCH)/$(BIN)
ifeq ($(OS),windows)
OUTBIN = bin/$(OS)_$(ARCH)/$(BIN).exe
endif

# Directories that we need created to build/test.
BUILD_DIRS := bin/$(OS)_$(ARCH) \
.go/bin/$(OS)_$(ARCH) \
.go/cache

# If you want to build all binaries, see the 'all-build' rule.
# If you want to build all containers, see the 'all-container' rule.
# If you want to build AND push all containers, see the 'all-push' rule.
all: fmt build

# For the following OS/ARCH expansions, we transform OS/ARCH into OS_ARCH
# because make pattern rules don't match with embedded '/' characters.

build-%:
@$(MAKE) build \
--no-print-directory \
GOOS=$(firstword $(subst _, ,$*)) \
GOARCH=$(lastword $(subst _, ,$*))

all-build: $(addprefix build-, $(subst /,_, $(BIN_PLATFORMS)))

version:
@echo version=$(VERSION)
@echo version_strategy=$(version_strategy)
@echo git_tag=$(git_tag)
@echo git_branch=$(git_branch)
@echo commit_hash=$(commit_hash)
@echo commit_timestamp=$(commit_timestamp)

gen:
@true

fmt: $(BUILD_DIRS)
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
./hack/fmt.sh $(SRC_DIRS)

build: $(OUTBIN)

# The following structure defeats Go's (intentional) behavior to always touch
# result files, even if they have not changed. This will still run `go` but
# will not trigger further work if nothing has actually changed.

$(OUTBIN): .go/$(OUTBIN).stamp
@true

# This will build the binary under ./.go and update the real binary iff needed.
.PHONY: .go/$(OUTBIN).stamp
.go/$(OUTBIN).stamp: $(BUILD_DIRS)
@echo "making $(OUTBIN)"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/bash -c " \
ARCH=$(ARCH) \
OS=$(OS) \
VERSION=$(VERSION) \
version_strategy=$(version_strategy) \
git_branch=$(git_branch) \
git_tag=$(git_tag) \
commit_hash=$(commit_hash) \
commit_timestamp=$(commit_timestamp) \
./hack/build.sh \
"
@if [ $(COMPRESS) = yes ] && [ $(OS) != windows ]; then \
echo "compressing $(OUTBIN)"; \
upx --brute .go/$(OUTBIN); \
fi
@if ! cmp -s .go/$(OUTBIN) $(OUTBIN); then \
mv .go/$(OUTBIN) $(OUTBIN); \
date >$@; \
fi
@echo

test: $(BUILD_DIRS)
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
$(BUILD_IMAGE) \
/bin/bash -c " \
ARCH=$(ARCH) \
OS=$(OS) \
VERSION=$(VERSION) \
./hack/test.sh $(SRC_DIRS) \
"

ADDTL_LINTERS := goconst,gofmt,goimports,unparam

.PHONY: lint
lint: $(BUILD_DIRS)
@echo "running linter"
@docker run \
-i \
--rm \
-u $$(id -u):$$(id -g) \
-v $$(pwd):/src \
-w /src \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin \
-v $$(pwd)/.go/bin/$(OS)_$(ARCH):/go/bin/$(OS)_$(ARCH) \
-v $$(pwd)/.go/cache:/.cache \
--env HTTP_PROXY=$(HTTP_PROXY) \
--env HTTPS_PROXY=$(HTTPS_PROXY) \
--env GO111MODULE=on \
--env GOFLAGS="-mod=vendor" \
$(BUILD_IMAGE) \
golangci-lint run --enable $(ADDTL_LINTERS)

$(BUILD_DIRS):
@mkdir -p $@

.PHONY: dev
dev: gen fmt build

.PHONY: ci
ci: lint test build #cover

.PHONY: qa
qa: docker-manifest
@if [ "$$APPSCODE_ENV" = "prod" ]; then \
echo "Nothing to do in prod env. Are you trying to 'release' binaries to prod?"; \
exit 1; \
fi
@if [ "$(version_strategy)" = "git_tag" ]; then \
echo "Are you trying to 'release' binaries to prod?"; \
exit 1; \
fi
@$(MAKE) clean all-build --no-print-directory

.PHONY: release
release: docker-manifest
@if [ "$$APPSCODE_ENV" != "prod" ]; then \
echo "'release' only works in PROD env."; \
exit 1; \
fi
@if [ "$(version_strategy)" != "git_tag" ]; then \
echo "'apply_tag' to release binaries and/or docker images."; \
exit 1; \
fi
@$(MAKE) clean all-build --no-print-directory

.PHONY: clean
clean:
rm -rf .go bin
30 changes: 13 additions & 17 deletions cmd/kubedb/version.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
package main

import v "github.com/appscode/go/version"
import (
v "github.com/appscode/go/version"
)

var (
Version string
VersionStrategy string
Os string
Arch string
CommitHash string
GitBranch string
GitTag string
GitBranch string
CommitHash string
CommitTimestamp string
BuildTimestamp string
BuildHost string
BuildHostOs string
BuildHostArch string
GoVersion string
Compiler string
Platform string
)

func init() {
v.Version.Version = Version
v.Version.VersionStrategy = VersionStrategy
v.Version.Os = Os
v.Version.Arch = Arch
v.Version.CommitHash = CommitHash
v.Version.GitBranch = GitBranch
v.Version.GitTag = GitTag
v.Version.GitBranch = GitBranch
v.Version.CommitHash = CommitHash
v.Version.CommitTimestamp = CommitTimestamp
v.Version.BuildTimestamp = BuildTimestamp
v.Version.BuildHost = BuildHost
v.Version.BuildHostOs = BuildHostOs
v.Version.BuildHostArch = BuildHostArch
v.Version.GoVersion = GoVersion
v.Version.Compiler = Compiler
v.Version.Platform = Platform
}
12 changes: 5 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@ go 1.12
require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/MakeNowJust/heredoc v0.0.0-20171113091838-e9091a26100e // indirect
github.com/appscode/go v0.0.0-20190424183524-60025f1135c9
github.com/appscode/go v0.0.0-20190523031839-1468ee3a76e8
github.com/chai2010/gettext-go v0.0.0-20160711120539-c6fed771bfd5 // indirect
github.com/cpuguy83/go-md2man v1.0.10 // indirect
github.com/docker/docker v0.7.3-0.20190327010347-be7ac8be2ae0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/camelcase v0.0.0-20160318181535-f6a740d52f96 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/kubedb/apimachinery v0.0.0-20190508221312-5ba915343400
github.com/kubedb/apimachinery v0.0.0-20190526014453-48e4bab67179
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/spf13/cobra v0.0.3
github.com/spf13/cobra v0.0.4
github.com/xlab/handysort v0.0.0-20150421192137-fb3537ed64a1 // indirect
golang.org/x/sys v0.0.0-20190508220229-2d0786266e9c // indirect
k8s.io/api v0.0.0-20190503110853-61630f889b3c
Expand All @@ -26,9 +25,8 @@ require (
k8s.io/client-go v11.0.0+incompatible
k8s.io/cloud-provider v0.0.0-20190508104637-039924654234 // indirect
k8s.io/component-base v0.0.0-20190424053038-9fe063da3132
k8s.io/kubernetes v1.14.1
kmodules.xyz/client-go v0.0.0-20190508091620-0d215c04352f
kmodules.xyz/custom-resources v0.0.0-20190508103408-464e8324c3ec // indirect
k8s.io/kubernetes v1.14.2
kmodules.xyz/client-go v0.0.0-20190524133821-9c8a87771aea
kmodules.xyz/monitoring-agent-api v0.0.0-20190508125842-489150794b9b
kmodules.xyz/objectstore-api v0.0.0-20190506085934-94c81c8acca9
vbom.ml/util v0.0.0-20160121211510-db5cfe13f5cc // indirect
Expand Down
Loading