-
Notifications
You must be signed in to change notification settings - Fork 10
/
Makefile
169 lines (133 loc) · 5.19 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
###############################################################################
#
# Makefile for project lifecycle
#
###############################################################################
export TYK_VERSION := v5.3.0
ifeq ($(origin DOCKER_USER), undefined)
DOCKER_USER := 1000
endif
# Default task: sets up development environment
install: up build
### PROJECT ###################################################################
# Builds the Go plugin
build: go-build restart-gateway
# Builds production-ready plugin bundle
bundle: go-bundle restart-gateway
# Outputs the project logs
logs: docker-logs
# Outputs the gateway log with formatting to make it easier to read in local dev
log: docker-gateway-log
# Brings up the project - Pro
up: docker-up bootstrap docker-status
# Brings up the project - Pro w/ oTel
up-otel: docker-up-otel bootstrap docker-status
# Brings up the project - OSS
up-oss: docker-up-oss bootstrap-oss docker-status
# Brings up the project - OSS w/ oTel
up-oss-otel: docker-up-oss-otel bootstrap-oss docker-status
# Brings down the project
down: docker-down docker-status
# Cleans the project
clean: docker-clean go-clean
# Gets the status of the docker containers
status: docker-status
### DOCKER ####################################################################
# Gets the status of the running containers
.PHONY: docker-status
docker-status:
docker compose ps
# Gets the container logs
.PHONY: docker-logs
docker-logs:
docker compose logs -t --tail="all"
# Gets the container log for gateway and applies formatting for easier reading in local dev
.PHONY: docker-gateway-log
docker-gateway-log:
docker compose logs tyk-gateway -t -f | perl -ne 'if (/time="([^"]+)" level=(\w+) msg="((?:\\"|[^"])*)"(\s*prefix=([^\s]+))?/) { print "$$1 ".sprintf("%-20s", "[$$2]".($$5 ? "[".substr($$5,0,10)."] " : (" " x 12)))."$$3\n" }'
# Bring docker containers up
.PHONY: docker-up
docker-up:
docker compose up -d --remove-orphans tyk-dashboard tyk-gateway
# Bring docker containers up /w oTel
.PHONY: docker-up-otel
docker-up-otel:
docker compose -f docker compose.yml -f deployments/otel/docker compose.yml up -d --remove-orphans tyk-dashboard tyk-gateway
# Bring docker containers up in OSS
.PHONY: docker-up-oss
docker-up-oss:
docker compose -f docker compose-oss.yml up -d --remove-orphans tyk-gateway
# Bring docker containers up in OSS /w oTel
.PHONY: docker-up-oss-otel
docker-up-oss-otel:
docker compose -f docker compose-oss.yml -f deployments/otel/docker compose.yml up -d --remove-orphans tyk-gateway
# Bootstrap dashboard
.PHONY: bootstrap
bootstrap:
$(shell ./tyk/scripts/bootstrap.sh)
# Bring docker containers down
.PHONY: docker-down
docker-down:
docker compose down --remove-orphans
# Clean docker containers volumes
.PHONY: docker-clean
docker-clean:
docker compose down --volumes --remove-orphans
### Tyk Go Plugin ########################################################################
go/src/go.mod:
cd ./go/src ; \
go mod init tyk-plugin ; \
go get -d github.com/TykTechnologies/tyk@`git ls-remote https://github.com/TykTechnologies/tyk.git refs/tags/${TYK_VERSION} | awk '{print $$1;}'` ; \
go mod tidy ; \
go mod vendor
# Builds Go plugin and moves it into local Tyk instance
.PHONY: go-build
go-build: go/src/go.mod
/bin/sh -c "cd ./go/src && go mod tidy && go mod vendor"
docker compose run --rm tyk-plugin-compiler CustomGoPlugin.so _$$(date +%s)
mv -f ./go/src/CustomGoPlugin*.so ./tyk/middleware/
# Runs Go Linter
lint:
/bin/sh -c "docker run --rm -v ${PWD}/go/src:/app -v ~/.cache/golangci-lint/v1.53.2:/root/.cache -w /app golangci/golangci-lint:v1.53.2 golangci-lint run"
# Runs Go unit tests
test:
/bin/sh -c "cd ./go/src && go test"
# Run Go test coverage
coverage:
mkdir -p /tmp/test-results ; \
cd ./go/src ; \
go test ./... -coverprofile coverage.out -covermode count ; \
grep -v tyk-plugin/tyk_util.go coverage.out > coverage.out.tmp ; \
mv coverage.out.tmp coverage.out ; \
go tool cover -func coverage.out ; \
go tool cover -html=coverage.out -o coverage.html ; \
mv coverage.out coverage.html /tmp/test-results ; \
totalCoverage=`go tool cover -func=/tmp/test-results/coverage.out | grep total | grep -Eo '[0-9]+\.[0-9]+'` ; \
echo "Total Coverage: $$totalCoverage %" ; \
rm -rf /tmp/test-results
# Builds production-ready Go plugin bundle as non-root user, using Tyk Bundler tool
.PHONY: go-bundle
go-bundle: go-build
sed "s/replace_version/$(TYK_VERSION)/g" tyk/bundle/manifest-template.json | \
sed "s/replace_platform/amd64/g" > tyk/bundle/manifest.json
cp tyk/middleware/CustomGoPlugin*.so tyk/bundle/
docker compose run --rm --user=$(DOCKER_USER) -w /opt/tyk-gateway/bundle tyk-gateway bundle build -y
rm tyk/bundle/CustomGoPlugin*.so
# Cleans application files
.PHONY: go-clean
go-clean:
-rm -rf ./go/src/vendor
-rm -rf ./go/src/go.mod
-rm -rf ./go/src/go.sum
-rm -f ./tyk/middleware/CustomGoPlugin*.so
-rm -f ./tyk/bundle/CustomGoPlugin*.so
-rm -f ./tyk/bundle/manifest.so
-rm -f ./tyk/bundle/bundle.zip
# Restarts the Tyk Gateway to instantly load new iterations of the Go plugin
.PHONY: restart-gateway
restart-gateway:
docker compose restart tyk-gateway
# Bootstrap dashboard
.PHONY: bootstrap-oss
bootstrap-oss:
$(shell ./tyk/scripts/bootstrap-oss.sh)