-
Notifications
You must be signed in to change notification settings - Fork 16
/
Makefile
123 lines (106 loc) · 4.55 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
# Copyright The HTNN Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
SHELL = /bin/bash
TARGET_SO = libgolang.so
PROJECT_NAME = mosn.io/htnn/dev_your_plugin
DOCKER_MIRROR = m.daocloud.io/
# Both images use glibc 2.31. Ensure libc in the images match each other.
BUILD_IMAGE ?= $(DOCKER_MIRROR)docker.io/library/golang:1.21-bullseye
# We don't use istio/proxyv2 because it is not designed to be run separately (need to work around permission issue).
PROXY_IMAGE ?= $(DOCKER_MIRROR)docker.io/envoyproxy/envoy:contrib-v1.29.5
# We may need to use timestamp if we need to update the image in one PR
DEV_TOOLS_IMAGE ?= $(DOCKER_MIRROR)ghcr.io/mosn/htnn-dev-tools:2024-03-05
PROTOC = protoc
# Define a recursive wildcard function
rwildcard=$(foreach d,$(wildcard $(addsuffix *,$(1))),$(call rwildcard,$d/,$(2))$(filter $(subst *,%,$(2)),$d))
PROTO_FILES = $(call rwildcard,./,*.proto)
GO_TARGETS = $(patsubst %.proto,%.pb.go,$(PROTO_FILES))
MOUNT_GOMOD_CACHE = -v $(shell go env GOPATH):/go
ifeq ($(IN_CI), true)
# Mount go mod cache in the CI environment will cause 'Permission denied' error
# when accessing files on host in later phase because the mounted directory will
# have files which is created by the root user in Docker.
# Run as low privilege user in the Docker doesn't
# work because we also need root to create /.cache in the Docker.
MOUNT_GOMOD_CACHE =
endif
.PHONY: dev-tools
dev-tools:
@if ! docker images ${DEV_TOOLS_IMAGE} | grep dev-tools > /dev/null; then \
docker pull ${DEV_TOOLS_IMAGE}; \
fi
.PHONY: gen-proto
gen-proto: dev-tools $(GO_TARGETS)
%.pb.go: %.proto
docker run --rm -v $(PWD):/go/src/${PROJECT_NAME} --user $(shell id -u) -w /go/src/${PROJECT_NAME} \
${DEV_TOOLS_IMAGE} \
protoc --proto_path=. --go_opt="paths=source_relative" --go_out=. --validate_out="lang=go,paths=source_relative:." \
-I /go/src/protoc-gen-validate $<
.PHONY: build-so-local
build-so-local:
CGO_ENABLED=1 go build -tags so \
--buildmode=c-shared \
-v -o ${TARGET_SO} \
${PROJECT_NAME}/cmd/libgolang
# As the tasks below mount the GOPATH into the docker container, please make sure you don't have Go binary put into the GOPATH
# which will override the one provides by the docker image.
.PHONY: build-so
build-so:
docker run --rm $(MOUNT_GOMOD_CACHE) -v $(PWD):/go/src/${PROJECT_NAME} -w /go/src/${PROJECT_NAME} \
-e GOPROXY \
${BUILD_IMAGE} \
make build-so-local
.PHONY: unit-test-local
unit-test-local:
go test -v ./plugins/...
.PHONY: unit-test
unit-test:
docker run --rm $(MOUNT_GOMOD_CACHE) -v $(PWD):/go/src/${PROJECT_NAME} -w /go/src/${PROJECT_NAME} \
-e GOPROXY \
${BUILD_IMAGE} \
make unit-test-local
.PHONY: build-test-so-local
build-test-so-local:
CGO_ENABLED=1 go build -tags so \
--buildmode=c-shared \
-cover -covermode=atomic -coverpkg=${PROJECT_NAME}/... \
-v -o ./tests/integration/${TARGET_SO} \
${PROJECT_NAME}/cmd/libgolang
.PHONY: build-test-so
build-test-so:
docker run --rm $(MOUNT_GOMOD_CACHE) -v $(PWD):/go/src/${PROJECT_NAME} -w /go/src/${PROJECT_NAME} \
-e GOPROXY \
${BUILD_IMAGE} \
make build-test-so-local
# This task can only be run in the CI, because we have `go.work` in the root directory and this module
# is not in it. We don't add this module to the `go.work` to ensure the `go.mod` and `go.sum` files in
# this module are always up-to-date.
# In the CI environment, we remove the `go.work`. If you want to run the integration test locally,
# you can remove the `go.work` and run `make integration-test`, or copy this module to a directory
# which doesn't have `go.work` in its ancestors.
.PHONY: integration-test
# The data plane image used in the integration test can be controlled via env var PROXY_IMAGE
integration-test: build-test-so
test -d /tmp/htnn_coverage && rm -rf /tmp/htnn_coverage || true
go test -v ./tests/integration/...
.PHONY: run-plugin
run-plugin:
docker run --name dev_your_plugin --rm -d -v $(PWD)/etc/demo.yaml:/etc/demo.yaml \
-v $(PWD)/libgolang.so:/etc/libgolang.so \
-p 10000:10000 \
${PROXY_IMAGE} \
envoy -c /etc/demo.yaml
.PHONY: stop-plugin
stop-plugin:
docker stop dev_your_plugin