Skip to content

Commit 5623b68

Browse files
simonpasquiergrobie
authored andcommitted
Support Go modules (prometheus#40)
Signed-off-by: Simon Pasquier <[email protected]>
1 parent 36a29c5 commit 5623b68

39 files changed

+2454
-240
lines changed

.circleci/config.yml

+24-29
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,30 @@
11
---
22
version: 2.1
33

4-
jobs:
5-
test:
4+
executors:
5+
# Whenever the Go version is updated here, .promu.yml should also be updated.
6+
golang:
7+
docker:
8+
- image: circleci/golang:1.11
9+
golang_memcached:
610
docker:
7-
- image: circleci/golang:1.10
11+
- image: circleci/golang:1.11
812
- image: memcached
9-
working_directory: /go/src/github.com/prometheus/memcached_exporter
13+
14+
jobs:
15+
test:
16+
executor: golang_memcached
1017

1118
steps:
1219
- checkout
1320
- setup_remote_docker
1421
- run: make promu
1522
- run: make
1623
- run: rm -f memcached_exporter
24+
- run: git diff --exit-code
1725

1826
build:
1927
machine: true
20-
working_directory: /home/circleci/.go_workspace/src/github.com/prometheus/memcached_exporter
2128

2229
steps:
2330
- checkout
@@ -29,36 +36,24 @@ jobs:
2936
- .build
3037

3138
docker_hub_master:
32-
docker:
33-
- image: circleci/golang:1.10
34-
working_directory: /go/src/github.com/prometheus/memcached_exporter
35-
36-
environment:
37-
DOCKER_IMAGE_NAME: prom/memcached-exporter
38-
QUAY_IMAGE_NAME: quay.io/prometheus/memcached-exporter
39+
executor: golang
3940

4041
steps:
4142
- checkout
4243
- setup_remote_docker
4344
- attach_workspace:
4445
at: .
4546
- run: ln -s .build/linux-amd64/memcached_exporter memcached_exporter
46-
- run: make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME
47-
- run: make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME
47+
- run: make docker
48+
- run: make docker DOCKER_REPO=quay.io/prometheus
4849
- run: docker images
4950
- run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
5051
- run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
51-
- run: docker push $DOCKER_IMAGE_NAME
52-
- run: docker push $QUAY_IMAGE_NAME
52+
- run: make docker-publish
53+
- run: make docker-publish DOCKER_REPO=quay.io/prometheus
5354

5455
docker_hub_release_tags:
55-
docker:
56-
- image: circleci/golang:1.10
57-
working_directory: /go/src/github.com/prometheus/memcached_exporter
58-
59-
environment:
60-
DOCKER_IMAGE_NAME: prom/memcached-exporter
61-
QUAY_IMAGE_NAME: quay.io/prometheus/memcached-exporter
56+
executor: golang
6257

6358
steps:
6459
- checkout
@@ -76,17 +71,17 @@ jobs:
7671
path: .tarballs
7772
destination: releases
7873
- run: ln -s .build/linux-amd64/memcached_exporter memcached_exporter
79-
- run: make docker DOCKER_IMAGE_NAME=$DOCKER_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
80-
- run: make docker DOCKER_IMAGE_NAME=$QUAY_IMAGE_NAME DOCKER_IMAGE_TAG=$CIRCLE_TAG
74+
- run: make docker DOCKER_IMAGE_TAG=$CIRCLE_TAG
75+
- run: make docker DOCKER_IMAGE_TAG=$CIRCLE_TAG DOCKER_REPO=quay.io/prometheus
8176
- run: docker login -u $DOCKER_LOGIN -p $DOCKER_PASSWORD
8277
- run: docker login -u $QUAY_LOGIN -p $QUAY_PASSWORD quay.io
8378
- run: |
8479
if [[ "$CIRCLE_TAG" =~ ^v[0-9]+(\.[0-9]+){2}$ ]]; then
85-
docker tag "$DOCKER_IMAGE_NAME:$CIRCLE_TAG" "$DOCKER_IMAGE_NAME:latest"
86-
docker tag "$QUAY_IMAGE_NAME:$CIRCLE_TAG" "$QUAY_IMAGE_NAME:latest"
80+
make docker-tag-latest DOCKER_IMAGE_TAG="$CIRCLE_TAG"
81+
make docker-tag-latest DOCKER_IMAGE_TAG="$CIRCLE_TAG" DOCKER_REPO=quay.io/prometheus
8782
fi
88-
- run: docker push $DOCKER_IMAGE_NAME
89-
- run: docker push $QUAY_IMAGE_NAME
83+
- run: make docker-publish
84+
- run: make docker-publish DOCKER_REPO=quay.io/prometheus
9085

9186
workflows:
9287
version: 2

.promu.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
go:
2+
# Whenever the Go version is updated here, .circle/config.yml should also
3+
# be updated.
4+
version: 1.11
15
repository:
26
path: github.com/prometheus/memcached_exporter
37
build:

Makefile.common

+153-30
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# !!! Open PRs only against the prometheus/prometheus/Makefile.common repository!
1717

1818
# Example usage :
19-
# Create the main Makefile in the root project directory.
19+
# Create the main Makefile in the root project directory.
2020
# include Makefile.common
2121
# customTarget:
2222
# @echo ">> Running customTarget"
@@ -28,22 +28,71 @@ unexport GOBIN
2828
GO ?= go
2929
GOFMT ?= $(GO)fmt
3030
FIRST_GOPATH := $(firstword $(subst :, ,$(shell $(GO) env GOPATH)))
31+
GOOPTS ?=
32+
33+
GO_VERSION ?= $(shell $(GO) version)
34+
GO_VERSION_NUMBER ?= $(word 3, $(GO_VERSION))
35+
PRE_GO_111 ?= $(shell echo $(GO_VERSION_NUMBER) | grep -E 'go1\.(10|[0-9])\.')
36+
37+
unexport GOVENDOR
38+
ifeq (, $(PRE_GO_111))
39+
ifneq (,$(wildcard go.mod))
40+
# Enforce Go modules support just in case the directory is inside GOPATH (and for Travis CI).
41+
GO111MODULE := on
42+
43+
ifneq (,$(wildcard vendor))
44+
# Always use the local vendor/ directory to satisfy the dependencies.
45+
GOOPTS := $(GOOPTS) -mod=vendor
46+
endif
47+
endif
48+
else
49+
ifneq (,$(wildcard go.mod))
50+
ifneq (,$(wildcard vendor))
51+
$(warning This repository requires Go >= 1.11 because of Go modules)
52+
$(warning Some recipes may not work as expected as the current Go runtime is '$(GO_VERSION_NUMBER)')
53+
endif
54+
else
55+
# This repository isn't using Go modules (yet).
56+
GOVENDOR := $(FIRST_GOPATH)/bin/govendor
57+
endif
58+
59+
unexport GO111MODULE
60+
endif
3161
PROMU := $(FIRST_GOPATH)/bin/promu
3262
STATICCHECK := $(FIRST_GOPATH)/bin/staticcheck
33-
GOVENDOR := $(FIRST_GOPATH)/bin/govendor
3463
pkgs = ./...
3564

65+
GO_VERSION ?= $(shell $(GO) version)
66+
GO_BUILD_PLATFORM ?= $(subst /,-,$(lastword $(GO_VERSION)))
67+
68+
PROMU_VERSION ?= 0.2.0
69+
PROMU_URL := https://github.com/prometheus/promu/releases/download/v$(PROMU_VERSION)/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM).tar.gz
70+
3671
PREFIX ?= $(shell pwd)
3772
BIN_DIR ?= $(shell pwd)
3873
DOCKER_IMAGE_TAG ?= $(subst /,-,$(shell git rev-parse --abbrev-ref HEAD))
74+
DOCKER_REPO ?= prom
3975

40-
all: style staticcheck unused build test
76+
.PHONY: all
77+
all: precheck style staticcheck unused build test
4178

42-
style:
43-
@echo ">> checking code style"
44-
! $(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print) | grep '^'
79+
# This rule is used to forward a target like "build" to "common-build". This
80+
# allows a new "build" target to be defined in a Makefile which includes this
81+
# one and override "common-build" without override warnings.
82+
%: common-% ;
4583

46-
check_license:
84+
.PHONY: common-style
85+
common-style:
86+
@echo ">> checking code style"
87+
@fmtRes=$$($(GOFMT) -d $$(find . -path ./vendor -prune -o -name '*.go' -print)); \
88+
if [ -n "$${fmtRes}" ]; then \
89+
echo "gofmt checking failed!"; echo "$${fmtRes}"; echo; \
90+
echo "Please ensure you are using $$($(GO) version) for formatting code."; \
91+
exit 1; \
92+
fi
93+
94+
.PHONY: common-check_license
95+
common-check_license:
4796
@echo ">> checking license header"
4897
@licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \
4998
awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \
@@ -53,48 +102,122 @@ check_license:
53102
exit 1; \
54103
fi
55104

56-
test-short:
105+
.PHONY: common-test-short
106+
common-test-short:
57107
@echo ">> running short tests"
58-
$(GO) test -short $(pkgs)
108+
GO111MODULE=$(GO111MODULE) $(GO) test -short $(GOOPTS) $(pkgs)
59109

60-
test:
110+
.PHONY: common-test
111+
common-test:
61112
@echo ">> running all tests"
62-
$(GO) test -race $(pkgs)
113+
GO111MODULE=$(GO111MODULE) $(GO) test -race $(GOOPTS) $(pkgs)
63114

64-
format:
115+
.PHONY: common-format
116+
common-format:
65117
@echo ">> formatting code"
66-
$(GO) fmt $(pkgs)
118+
GO111MODULE=$(GO111MODULE) $(GO) fmt $(GOOPTS) $(pkgs)
67119

68-
vet:
120+
.PHONY: common-vet
121+
common-vet:
69122
@echo ">> vetting code"
70-
$(GO) vet $(pkgs)
123+
GO111MODULE=$(GO111MODULE) $(GO) vet $(GOOPTS) $(pkgs)
71124

72-
staticcheck: $(STATICCHECK)
125+
.PHONY: common-staticcheck
126+
common-staticcheck: $(STATICCHECK)
73127
@echo ">> running staticcheck"
128+
ifdef GO111MODULE
129+
GO111MODULE=$(GO111MODULE) $(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" -checks "SA*" $(pkgs)
130+
else
74131
$(STATICCHECK) -ignore "$(STATICCHECK_IGNORE)" $(pkgs)
132+
endif
75133

76-
unused: $(GOVENDOR)
134+
.PHONY: common-unused
135+
common-unused: $(GOVENDOR)
136+
ifdef GOVENDOR
77137
@echo ">> running check for unused packages"
78138
@$(GOVENDOR) list +unused | grep . && exit 1 || echo 'No unused packages'
79-
80-
build: promu
139+
else
140+
ifdef GO111MODULE
141+
@echo ">> running check for unused/missing packages in go.mod"
142+
GO111MODULE=$(GO111MODULE) $(GO) mod tidy
143+
@git diff --exit-code -- go.sum go.mod
144+
ifneq (,$(wildcard vendor))
145+
@echo ">> running check for unused packages in vendor/"
146+
GO111MODULE=$(GO111MODULE) $(GO) mod vendor
147+
@git diff --exit-code -- go.sum go.mod vendor/
148+
endif
149+
endif
150+
endif
151+
152+
.PHONY: common-build
153+
common-build: promu
81154
@echo ">> building binaries"
82-
$(PROMU) build --prefix $(PREFIX)
155+
GO111MODULE=$(GO111MODULE) $(PROMU) build --prefix $(PREFIX)
83156

84-
tarball: promu
157+
.PHONY: common-tarball
158+
common-tarball: promu
85159
@echo ">> building release tarball"
86160
$(PROMU) tarball --prefix $(PREFIX) $(BIN_DIR)
87161

88-
docker:
89-
docker build -t "$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
162+
.PHONY: common-docker
163+
common-docker:
164+
docker build -t "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" .
165+
166+
.PHONY: common-docker-publish
167+
common-docker-publish:
168+
docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)"
169+
170+
.PHONY: common-docker-tag-latest
171+
common-docker-tag-latest:
172+
docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME):latest"
173+
174+
.PHONY: promu
175+
promu: $(PROMU)
176+
177+
$(PROMU):
178+
curl -s -L $(PROMU_URL) | tar -xvz -C /tmp
179+
mkdir -v -p $(FIRST_GOPATH)/bin
180+
cp -v /tmp/promu-$(PROMU_VERSION).$(GO_BUILD_PLATFORM)/promu $(PROMU)
181+
182+
.PHONY: proto
183+
proto:
184+
@echo ">> generating code from proto files"
185+
@./scripts/genproto.sh
186+
187+
.PHONY: $(STATICCHECK)
188+
$(STATICCHECK):
189+
ifdef GO111MODULE
190+
# Get staticcheck from a temporary directory to avoid modifying the local go.{mod,sum}.
191+
# See https://github.com/golang/go/issues/27643.
192+
# For now, we are using the next branch of staticcheck because master isn't compatible yet with Go modules.
193+
tmpModule=$$(mktemp -d 2>&1) && \
194+
mkdir -p $${tmpModule}/staticcheck && \
195+
cd "$${tmpModule}"/staticcheck && \
196+
GO111MODULE=on $(GO) mod init example.com/staticcheck && \
197+
GO111MODULE=on GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck@next && \
198+
rm -rf $${tmpModule};
199+
else
200+
GOOS= GOARCH= GO111MODULE=off $(GO) get -u honnef.co/go/tools/cmd/staticcheck
201+
endif
202+
203+
ifdef GOVENDOR
204+
.PHONY: $(GOVENDOR)
205+
$(GOVENDOR):
206+
GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor
207+
endif
90208

91-
promu:
92-
GOOS= GOARCH= $(GO) get -u github.com/prometheus/promu
209+
.PHONY: precheck
210+
precheck::
93211

94-
$(FIRST_GOPATH)/bin/staticcheck:
95-
GOOS= GOARCH= $(GO) get -u honnef.co/go/tools/cmd/staticcheck
212+
define PRECHECK_COMMAND_template =
213+
precheck:: $(1)_precheck
96214

97-
$(FIRST_GOPATH)/bin/govendor:
98-
GOOS= GOARCH= $(GO) get -u github.com/kardianos/govendor
99215

100-
.PHONY: all style check_license format build test vet assets tarball docker promu staticcheck $(FIRST_GOPATH)/bin/staticcheck govendor $(FIRST_GOPATH)/bin/govendor
216+
PRECHECK_COMMAND_$(1) ?= $(1) $$(strip $$(PRECHECK_OPTIONS_$(1)))
217+
.PHONY: $(1)_precheck
218+
$(1)_precheck:
219+
@if ! $$(PRECHECK_COMMAND_$(1)) 1>/dev/null 2>&1; then \
220+
echo "Execution of '$$(PRECHECK_COMMAND_$(1))' command failed. Is $(1) installed?"; \
221+
exit 1; \
222+
fi
223+
endef

go.mod

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module github.com/prometheus/memcached_exporter
2+
3+
require (
4+
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc // indirect
5+
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
6+
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973 // indirect
7+
github.com/gogo/protobuf v1.1.1 // indirect
8+
github.com/golang/protobuf v0.0.0-20181005181728-ddf22928ea3c // indirect
9+
github.com/grobie/gomemcache v0.0.0-20180201122607-1f779c573665
10+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect
11+
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
12+
github.com/prometheus/client_golang v0.9.0
13+
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910 // indirect
14+
github.com/prometheus/common v0.0.0-20181015124227-bcb74de08d37
15+
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d // indirect
16+
github.com/sirupsen/logrus v0.0.0-20181016112244-680f584d621d // indirect
17+
golang.org/x/crypto v0.0.0-20181015023909-0c41d7ab0a0e // indirect
18+
golang.org/x/sys v0.0.0-20181011152604-fa43e7bc11ba // indirect
19+
gopkg.in/alecthomas/kingpin.v2 v2.2.6
20+
)

0 commit comments

Comments
 (0)