-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
Makefile
209 lines (170 loc) · 6.77 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
TARGETS ?= linux/amd64 darwin/amd64 windows/amd64 linux/386 linux/ppc64le linux/s390x linux/arm linux/arm64
PROJECT_NAME := s3-proxy
PKG := github.com/oxyno-zeta/$(PROJECT_NAME)
# go option
GO ?= go
TAGS :=
LDFLAGS := -w -s
GOFLAGS :=
BINDIR := $(CURDIR)/bin
DISTDIR := dist
CURRENT_DIR = $(CURDIR)
# Required for globs to work correctly
SHELL=/usr/bin/env bash
# Version
GIT_COMMIT = $(shell git rev-parse HEAD)
GIT_SHA = $(shell git rev-parse --short HEAD)
GIT_TAG = $(shell git describe --tags --abbrev=0 --exact-match 2>/dev/null)
DATE = $(shell date +%F_%T%Z)
BINARY_VERSION = ${GIT_SHA}
LDFLAGS += -X ${PKG}/pkg/${PROJECT_NAME}/version.Version=${BINARY_VERSION}
LDFLAGS += -X ${PKG}/pkg/${PROJECT_NAME}/version.GitCommit=${GIT_COMMIT}
LDFLAGS += -X ${PKG}/pkg/${PROJECT_NAME}/version.BuildDate=${DATE}
HAS_GORELEASER := $(shell command -v goreleaser;)
HAS_GIT := $(shell command -v git;)
HAS_GOLANGCI_LINT := $(shell command -v golangci-lint;)
HAS_CURL:=$(shell command -v curl;)
HAS_MOCKGEN:=$(shell command -v mockgen;)
HAS_GOTESTSUM:=$(shell command -v gotestsum;)
HAS_FIELDALIGNMENT:=$(shell command -v fieldalignment;)
#
## Tool versions
#
# ? Note: Go install versions are inline because renovate can manage them like that.
# renovate: datasource=github-tags depName=golangci/golangci-lint
GOLANGCI_LINT_VERSION := "v1.61.0"
.DEFAULT_GOAL := code/lint
#############
# Build #
#############
.PHONY: code/generate
code/generate:
$(GO) generate ./...
.PHONY: code/lint
code/lint: setup/dep/install
golangci-lint run ./...
.PHONY: code/fieldalignment
code/fieldalignment: setup/dep/install
fieldalignment -fix -test=false ./...
.PHONY: code/build
code/build: code/clean setup/dep/install
$(GO) build -o $(BINDIR)/$(PROJECT_NAME) $(GOFLAGS) -tags '$(TAGS)' -ldflags '$(LDFLAGS)' $(PKG)/cmd/${PROJECT_NAME}
.PHONY: code/build-cross
code/build-cross: code/clean setup/dep/install
ifdef HAS_GORELEASER
goreleaser -p 2 --snapshot
endif
ifndef HAS_GORELEASER
curl -sL https://git.io/goreleaser | bash -s -- -p 2 --snapshot
endif
.PHONY: code/clean
code/clean:
@rm -rf $(BINDIR) $(DISTDIR)
.PHONY: code/docs
code/docs: setup/docs
docker run --rm -it --user 1000:1000 -v ${PWD}:/docs -p 8000:8000 mkdocs serve -a 0.0.0.0:8000
.PHONY: code/build/docs
code/build/docs: setup/docs
docker run --rm -it --user 1000:1000 -v ${PWD}:/docs -p 8000:8000 mkdocs build
#############
# Release #
#############
.PHONY: release/all
release/all: code/clean setup/dep/install
ifdef HAS_GORELEASER
goreleaser -p 2
endif
ifndef HAS_GORELEASER
curl -sL https://git.io/goreleaser | bash -s -- -p 2
endif
#############
# Tests #
#############
.PHONY: test/all
test/all: setup/dep/install
gotestsum --junitfile junit.xml --format testname --format-hide-empty-pkg -- --tags=unit,integration -coverpkg=./pkg/... -covermode=count -coverprofile=c.out.tmp ./pkg/...
.PHONY: test/all/original
test/all/original: setup/dep/install
$(GO) test --tags=unit,integration -v -coverpkg=./pkg/... -covermode=count -coverprofile=c.out.tmp ./pkg/...
.PHONY: test/unit
test/unit: setup/dep/install
$(GO) test --tags=unit -v -coverpkg=./pkg/... -covermode=count -coverprofile=c.out.tmp ./pkg/...
.PHONY: test/integration
test/integration: setup/dep/install
$(GO) test --tags=integration -v -coverpkg=./pkg/... -covermode=count -coverprofile=c.out.tmp ./pkg/...
.PHONY: test/coverage
test/coverage:
cat c.out.tmp | grep -v "mock_" > c.out
$(GO) tool cover -html=c.out -o coverage.html
$(GO) tool cover -func c.out
#############
# Setup #
#############
.PHONY: down/services
down/services:
docker rm -f opa || true
docker rm -f keycloak || true
.PHONY: down/tracing-services
down/tracing-services:
docker rm -f jaeger || true
.PHONY: down/metrics-services
down/metrics-services:
docker rm -f prometheus || true
docker rm -f grafana || true
.PHONY: down/oauth2-proxy-services
down/oauth2-proxy-services:
docker rm -f oauth2-proxy || true
.PHONY: setup/metrics-services
setup/metrics-services:
docker run --rm -d --name prometheus -v $(CURRENT_DIR)/local-resources/prometheus/prometheus.yml:/prometheus/prometheus.yml --network=host prom/prometheus:v2.18.0 --web.listen-address=:9191
docker run --rm -d --name grafana --network=host grafana/grafana:7.0.3
.PHONY: setup/tracing-services
setup/tracing-services: down/tracing-services
@echo "Setup tracing services"
docker run --name jaeger -d -p 6831:6831/udp -p 16686:16686 jaegertracing/all-in-one:latest
.PHONY: setup/services
setup/services: down/services
tar czvf local-resources/opa/bundle.tar.gz --directory=local-resources/opa/bundle example/
docker run -d --rm --name opa -p 8181:8181 -v $(CURRENT_DIR)/local-resources/opa/bundle.tar.gz:/bundle.tar.gz openpolicyagent/opa run --server --log-level debug --log-format text --bundle /bundle.tar.gz
docker run -d --rm --name keycloak -p 8088:8080 -e KEYCLOAK_IMPORT=/tmp/realm-export.json -v $(CURRENT_DIR)/local-resources/keycloak/realm-export.json:/tmp/realm-export.json -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:11.0.3
.PHONY: setup/oauth2-proxy-services
setup/oauth2-proxy-services: down/oauth2-proxy-services
# --skip-jwt-bearer-tokens enable the feature of accepting Authorization headers with Bearer TOKEN inside and not just only cookies
# See code: https://github.com/oauth2-proxy/oauth2-proxy/blob/f6ae15e8c37b15c7cc29332c1f070a06bc503dc7/oauthproxy.go#L260
docker run -d --rm --network=host --name oauth2-proxy quay.io/oauth2-proxy/oauth2-proxy:v7.2.1 \
--cookie-secret=SJ3QEg6Q0levwH5XAZjKKQ== --client-id=client-without-secret --email-domain="*" --provider=oidc \
--oidc-issuer-url="http://localhost:8088/auth/realms/integration" --client-secret="fake" --cookie-secure=false --cookie-name="oidc" \
--upstream="http://localhost:8080" --pass-authorization-header --pass-host-header --skip-provider-button --skip-jwt-bearer-tokens \
--skip-auth-preflight
.PHONY: setup/docs
setup/docs:
docker build -t mkdocs -f Dockerfile.docs .
.PHONY: setup/dep/install
setup/dep/install:
ifndef HAS_GOLANGCI_LINT
@echo "=> Installing golangci-lint tool"
ifndef HAS_CURL
$(error You must install curl)
endif
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin $(GOLANGCI_LINT_VERSION)
endif
ifndef HAS_GIT
$(error You must install Git)
endif
ifndef HAS_MOCKGEN
@echo "=> Installing mockgen tool"
go install go.uber.org/mock/mockgen@v0.4.0
endif
ifndef HAS_GOTESTSUM
@echo "=> Installing gotestsum tool"
go install gotest.tools/gotestsum@v1.11.0
endif
ifndef HAS_FIELDALIGNMENT
@echo "=> Installing fieldalignment tool"
$(GO) install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@v0.23.0
endif
go mod download all
go mod tidy
.PHONY: setup/dep/update
setup/dep/update:
go get -u ./...