diff --git a/.github/workflows/build_and_test.yaml b/.github/workflows/build_and_test.yaml index e05331829f..80aefb54e9 100644 --- a/.github/workflows/build_and_test.yaml +++ b/.github/workflows/build_and_test.yaml @@ -20,31 +20,25 @@ jobs: build-and-test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - name: Check out code + uses: actions/checkout@v3 - name: Run yamllint run: | - MAKE_IN_DOCKER=1 make lint-yamllint - - name: Run Codespell - uses: codespell-project/actions-codespell@master - with: - # can't reuse tools/codespell/.codespell.skip, shell is not supported - skip: .git,*.png,*.woff,*.woff2,*.eot,*.ttf,*.jpg,*.ico,*.svg,go.mod,go.sum - ignore_words_file: 'tools/codespell/.codespell.ignorewords' - check_filenames: true - check_hidden: true - - uses: actions/setup-go@v3 + MAKE_IN_DOCKER=1 make lint.yamllint + - name: Run codespell + run: | + MAKE_IN_DOCKER=1 make lint.codespell + - name: Run golangci-lint + run: | + MAKE_IN_DOCKER=1 make lint.golint + - name: Set up Go + uses: actions/setup-go@v3 with: go-version: ${{ env.GO_VERSION }} - cache: true - - name: Run golangci-lint - uses: golangci/golangci-lint-action@v2 - with: - version: v1.46.2 - args: --build-tags=e2e - - name: Build - run: make build-all + - name: Build Binaries + run: make build.multiarch - name: Test and report coverage - run: go test ./... -race -coverprofile=coverage.xml -covermode=atomic + run: make go.test.coverage - name: Upload coverage to Codecov uses: codecov/codecov-action@v2 with: @@ -54,10 +48,10 @@ jobs: verbose: true - name: Set up Docker Buildx uses: docker/setup-buildx-action@v2 - - name: Build + - name: Build Docker Image uses: docker/build-push-action@v3 with: - file: ./Dockerfile + file: ./tools/docker/envoy-gateway/Dockerfile context: bin platforms: linux/amd64 tags: ${{ env.ENVOY_GATEWAY_DEV_IMAGE }}:${{ github.sha }} @@ -73,7 +67,7 @@ jobs: if: github.event_name == 'push' && github.ref == 'refs/heads/main' uses: docker/build-push-action@v3 with: - file: ./Dockerfile + file: ./tools/docker/envoy-gateway/Dockerfile context: bin platforms: linux/amd64,linux/arm64 push: true diff --git a/Makefile b/Makefile index b4043a8df2..a75634481d 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,23 @@ +# All make targets should be defined in Makefile.targets.mk +# All make targets should be implemented in tools/make/*.mk +# ==================================================================================================== +# Supported Targets: (Run `make help` to see more information) +# ==================================================================================================== +## build: Build envoy-gateway for host platform. See Option PLATFORM and BINS. +## image: Build docker images for host platform. See Option PLATFORM and BINS. +## push: Push docker images to registry. +## build.multiarch: Build envoy-gateway for multiple platforms. See Option PLATFORMS and IMAGES. +## image.multiarch: Build docker images for multiple platforms. See Option PLATFORMS and IMAGES. +## push.multiarch: Push docker images for multiple platforms to registry. +## lint: Run all linter of code sources, including golint, yamllint and codespell. +## test: Run all Go test of code sources. +## format: Format codes style with mod tidy, gofmt and goimports. +## clean: Remove all files that are created by building. +# ==================================================================================================== + # This is a wrapper around `make` so it can run # directly on the host or inside a container # -# All make targets must be defined in Makefile.targets.mk - # Set MAKE_IN_DOCKER=1 as an environment variable to run `make` inside # a Docker container with preinstalled tools. @@ -18,7 +33,7 @@ DOCKER_RUN_CMD ?= docker run \ %: ifeq ($(MAKE_IN_DOCKER), 1) # Build builder image - @$(DOCKER_BUILD_CMD) -t $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) - < tools/docker/Dockerfile + @$(DOCKER_BUILD_CMD) -t $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) - < tools/docker/builder/Dockerfile # Run make inside the builder image # Run with MAKE_IN_DOCKER=0 to eliminate an infinite loop @$(DOCKER_RUN_CMD) $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) MAKE_IN_DOCKER=0 $@ diff --git a/Makefile.targets.mk b/Makefile.targets.mk index 8328ecbfcf..27aaacfe46 100644 --- a/Makefile.targets.mk +++ b/Makefile.targets.mk @@ -1,153 +1,123 @@ -# Golang variables -GOOS ?= $(shell go env GOOS) -GOARCH ?= $(shell go env GOARCH) - -# Docker variables -# REGISTRY is the image registry to use for build and push image targets. -REGISTRY ?= docker.io/envoyproxy -# IMAGE is the image URL for build and push image targets. -IMAGE ?= ${REGISTRY}/gateway-dev -# REV is the short git sha of latest commit. -REV=$(shell git rev-parse --short HEAD) -# Tag is the tag to use for build and push image targets. -TAG ?= $(REV) -# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary. -ENVTEST_K8S_VERSION = 1.23 - -# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) -ifeq (,$(shell go env GOBIN)) -GOBIN=$(shell go env GOPATH)/bin -else -GOBIN=$(shell go env GOBIN) -endif - -# Setting SHELL to bash allows bash commands to be executed by recipes. -# This is a requirement for 'setup-envtest.sh' in the test target. -# Options are set to exit when a recipe line exits non-zero or a piped command fails. -SHELL = /usr/bin/env bash -o pipefail -.SHELLFLAGS = -ec - -##@ General - -# The help target prints out all targets with their descriptions organized -# beneath their categories. The categories are represented by '##@' and the -# target descriptions by '##'. The awk commands is responsible for reading the -# entire set of makefiles included in this invocation, looking for lines of the -# file as xyz: ## something, and then pretty-format the target and help. Then, -# if there's a line with ##@ something, that gets pretty-printed as a category. -# More info on the usage of ANSI control characters for terminal formatting: -# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters -# More info on the awk command: -# http://linuxcommand.org/lc3_adv_awk.php - -.PHONY: help -help: ## Display this help - @echo Envoy Gateway is an open source project for managing Envoy Proxy as a standalone or Kubernetes-based application gateway - @echo - @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) - -##@ Build - +# ==================================================================================================== +# ROOT Options: +# ==================================================================================================== + +ROOT_PACKAGE=github.com/envoyproxy/gateway + +# ==================================================================================================== +# Includes: +# ==================================================================================================== +include tools/make/common.mk +include tools/make/golang.mk +include tools/make/image.mk +include tools/make/lint.mk + +# ==================================================================================================== +# Targets: +# ==================================================================================================== + +# ==================================================================================================== +## build: Build envoy-gateway for host platform. See Option PLATFORM and BINS. +# ==================================================================================================== .PHONY: build -build: ## Build the envoy-gateway binary - @CGO_ENABLED=0 go build -a -o ./bin/${GOOS}/${GOARCH}/ github.com/envoyproxy/gateway/cmd/envoy-gateway - -build-linux-amd64: - @GOOS=linux GOARCH=amd64 $(MAKE) build - -build-linux-arm64: - @GOOS=linux GOARCH=arm64 $(MAKE) build - -build-all: build-linux-amd64 build-linux-arm64 +build: + @$(MAKE) go.build + +# ==================================================================================================== +## image: Build docker images for host platform. See Option PLATFORM and BINS. +# ==================================================================================================== +.PHONY: image +image: + @$(MAKE) image.build + +# ==================================================================================================== +## push: Push docker images to registry. +# ==================================================================================================== +.PHONY: push +push: + @$(MAKE) image.push + +# ==================================================================================================== +## build.multiarch: Build envoy-gateway for multiple platforms. See Option PLATFORMS and IMAGES. +# ==================================================================================================== +.PHONY: build.multiarch +build.multiarch: + @$(MAKE) go.build.multiarch + +# ==================================================================================================== +## image.multiarch: Build docker images for multiple platforms. See Option PLATFORMS and IMAGES. +# ==================================================================================================== +.PHONY: image.multiarch +image.multiarch: + @$(MAKE) image.build.multiarch + +# ==================================================================================================== +## push.multiarch: Push docker images for multiple platforms to registry. +# ==================================================================================================== +.PHONY: push.multiarch +push.multiarch: + @$(MAKE) image.push.multiarch + +# ==================================================================================================== +## lint: Run all linter of code sources, including golint, yamllint and codespell. +# ==================================================================================================== +.PHONY: lint +lint: + @$(MAKE) lint.golint lint.yamllint lint.codespell +# ==================================================================================================== +## test: Run all Go test of code sources. +# ==================================================================================================== .PHONY: test -test: - @go test ./... - -.PHONY: docker-build -docker-build: build-all ## Build the envoy-gateway docker image. - @DOCKER_BUILDKIT=1 docker build -t $(IMAGE):$(TAG) -f Dockerfile bin - -.PHONY: docker-push -docker-push: ## Push the docker image for envoy-gateway. - @docker push $(IMAGE):$(TAG) - -.PHONY: lint -lint: ## Run lint checks -lint: lint-golint lint-yamllint lint-codespell - -.PHONY: lint-golint -lint-golint: - @echo Running Go linter ... - @golangci-lint run --build-tags=e2e --config=tools/golangci-lint/.golangci.yml - -.PHONY: lint-yamllint -lint-yamllint: - @echo Running YAML linter ... - ## TODO(lianghao208): add other directories later - @yamllint --config-file=tools/yamllint/.yamllint changelogs/ - -.PHONY: lint-codespell -lint-codespell: CODESPELL_SKIP := $(shell cat tools/codespell/.codespell.skip | tr \\n ',') -lint-codespell: - @codespell --skip $(CODESPELL_SKIP) --ignore-words tools/codespell/.codespell.ignorewords --check-filenames --check-hidden -q2 - -##@ Development - -.PHONY: manifests -manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. - $(CONTROLLER_GEN) rbac:roleName=envoy-gateway-role crd webhook paths="./..." output:crd:artifacts:config=pkg/provider/kubernetes/config/crd/bases - -.PHONY: generate -generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations. - $(CONTROLLER_GEN) object paths="./..." - -.PHONY: kube-test -kube-test: manifests generate lint envtest ## Run Kubernetes provider tests. - KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out - -##@ Deployment - -ifndef ignore-not-found - ignore-not-found = false -endif - -.PHONY: kube-install -kube-install: manifests kustomize ## Install CRDs into the Kubernetes cluster specified in ~/.kube/config. - $(KUSTOMIZE) build pkg/provider/kubernetes/config/crd | kubectl apply -f - - -.PHONY: kube-uninstall -kube-uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion. - $(KUSTOMIZE) build pkg/provider/kubernetes/config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f - - -##@ Build Dependencies - -## Location to install dependencies to -LOCALBIN ?= $(shell pwd)/bin -$(LOCALBIN): - mkdir -p $(LOCALBIN) - -## Tool Binaries -KUSTOMIZE ?= $(LOCALBIN)/kustomize -CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen -ENVTEST ?= $(LOCALBIN)/setup-envtest - -## Tool Versions -KUSTOMIZE_VERSION ?= v3.8.7 -CONTROLLER_TOOLS_VERSION ?= v0.8.0 - -KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" -.PHONY: kustomize -kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. -$(KUSTOMIZE): $(LOCALBIN) - curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN) - -.PHONY: controller-gen -controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. -$(CONTROLLER_GEN): $(LOCALBIN) - GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION) - -.PHONY: envtest -envtest: $(ENVTEST) ## Download envtest-setup locally if necessary. -$(ENVTEST): $(LOCALBIN) - GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest +test: + @$(MAKE) go.test.unit + +# ==================================================================================================== +## format: Format codes style with mod tidy, gofmt and goimports. +# ==================================================================================================== +.PHONY: format +format: + @$(MAKE) go.format + +# ==================================================================================================== +## clean: Remove all files that are created by building. +# ==================================================================================================== +.PHONY: clean +clean: + @$(MAKE) go.clean + +# ==================================================================================================== +# Usage +# ==================================================================================================== + +define USAGE_OPTIONS + +Options: + BINS The binaries to build. Default is all of cmd. + This option is available when using: make build/build.multiarch + Example: make build BINS="envoy-gateway" + IMAGES Backend images to make. Default is all of cmds. + This option is available when using: make image/image.multiarch/push/push.multiarch + Example: make image.multiarch IMAGES="envoy-gateway" + PLATFORM The specified platform to build. + This option is available when using: make build/image + Example: make build BINS="envoy-gateway" PLATFORM="linux_amd64" + Supported Platforms: linux_amd64 linux_arm64 darwin_amd64 darwin_arm64 + PLATFORMS The multiple platforms to build. + This option is available when using: make build.multiarch + Example: make image.multiarch IMAGES="envoy-gateway" PLATFORMS="linux_amd64 linux_arm64" + Default is "linux_amd64 linux_arm64 darwin_amd64 darwin_arm64". +endef +export USAGE_OPTIONS + +# ==================================================================================================== +# Help +# ==================================================================================================== + +## help: Show this help info. +.PHONY: help +help: Makefile + @echo "Envoy Gateway is an open source project for managing Envoy Proxy as a standalone or Kubernetes-based application gateway\n" + @echo "Usage: make ...\n\nTargets:" + @sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /' + @echo "$$USAGE_OPTIONS" diff --git a/go.sum b/go.sum index 03c2945c72..9f28c89b5d 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,6 @@ github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -21,6 +20,7 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7 github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= +github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI= github.com/getkin/kin-openapi v0.76.0/go.mod h1:660oXbgy5JFMKreazJaQTw7o+X00qeSyhcnluiMv+Xg= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= @@ -51,6 +51,7 @@ github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5a github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/gofuzz v1.1.0 h1:Hsa8mG0dQ46ij8Sl2AYJDUv1oA9/d6Vk+3LG99Oe02g= @@ -70,6 +71,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= @@ -83,15 +85,19 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20120707110453-a547fc61f48d/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f/go.mod h1:ZdcZmHo+o7JKHSa8/e818NopupXU1YMK5fe1lsApnBw= +github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e h1:fD57ERR4JtEqsWbfPhv4DMiApHyliiK5xCTNVSPiaAs= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A= +github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE= github.com/onsi/ginkgo v0.0.0-20170829012221-11459a886d9c/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= +github.com/onsi/ginkgo v1.16.5 h1:8xi0RTUf59SOSfEtZMvwTvXYMzG4gV23XVHOZiXNtnE= github.com/onsi/gomega v0.0.0-20170829124025-dcabb60a477c/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= +github.com/onsi/gomega v1.18.1 h1:M1GfJqGRrBrrGGsbxzV5dqM2U2ApXefZCQpkukxYRLE= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= @@ -158,6 +164,7 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220209214540-3681064d5158 h1:rm+CHSpPEEW2IsXUib1ThaHIjuBVZjxNgSKmBLFfD4c= golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= @@ -179,6 +186,7 @@ golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= @@ -201,14 +209,15 @@ google.golang.org/protobuf v1.24.0/go.mod h1:r/3tXBNzIEhYS9I1OUVjXDlt8tc493IdKGj google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU= gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/inf.v0 v0.9.1 h1:73M5CoZyi3ZLMOyDlQh031Cx6N9NDJ2Vvfl76EDAgDc= gopkg.in/inf.v0 v0.9.1/go.mod h1:cWUDdTG/fYaXco+Dcufb5Vnc6Gp2YChqWtbxRZE0mXw= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= @@ -217,13 +226,13 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +k8s.io/api v0.24.2 h1:g518dPU/L7VRLxWfcadQn2OnsiGWVOadTLpdnqgY2OI= k8s.io/apimachinery v0.24.2 h1:5QlH9SL2C8KMcrNJPor+LbXVTaZRReml7svPEh4OKDM= k8s.io/apimachinery v0.24.2/go.mod h1:82Bi4sCzVBdpYjyI4jY6aHX+YCUchUIrZrXKedjd2UM= k8s.io/gengo v0.0.0-20210813121822-485abfe95c7c/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E= @@ -243,3 +252,4 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.2/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK sigs.k8s.io/structured-merge-diff/v4 v4.2.1 h1:bKCqE9GvQ5tiVHn5rfn1r+yao3aLQEaLzkkmAkf+A6Y= sigs.k8s.io/structured-merge-diff/v4 v4.2.1/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4= sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc= +sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo= diff --git a/tools/docker/Dockerfile b/tools/docker/builder/Dockerfile similarity index 100% rename from tools/docker/Dockerfile rename to tools/docker/builder/Dockerfile diff --git a/Dockerfile b/tools/docker/envoy-gateway/Dockerfile similarity index 100% rename from Dockerfile rename to tools/docker/envoy-gateway/Dockerfile diff --git a/tools/codespell/.codespell.ignorewords b/tools/linter/codespell/.codespell.ignorewords similarity index 100% rename from tools/codespell/.codespell.ignorewords rename to tools/linter/codespell/.codespell.ignorewords diff --git a/tools/codespell/.codespell.skip b/tools/linter/codespell/.codespell.skip similarity index 100% rename from tools/codespell/.codespell.skip rename to tools/linter/codespell/.codespell.skip diff --git a/tools/golangci-lint/.golangci.yml b/tools/linter/golangci-lint/.golangci.yml similarity index 100% rename from tools/golangci-lint/.golangci.yml rename to tools/linter/golangci-lint/.golangci.yml diff --git a/tools/yamllint/.yamllint b/tools/linter/yamllint/.yamllint similarity index 100% rename from tools/yamllint/.yamllint rename to tools/linter/yamllint/.yamllint diff --git a/tools/make/common.mk b/tools/make/common.mk new file mode 100644 index 0000000000..ba430c73e7 --- /dev/null +++ b/tools/make/common.mk @@ -0,0 +1,53 @@ +# This is a wrapper to set common variables +# +# All make targets related to common variables are defined in this file. + +# Set Root Directory Path +ifeq ($(origin ROOT_DIR),undefined) +ROOT_DIR := $(abspath $(shell pwd -P)) +endif + +# Set Output Directory Path +ifeq ($(origin OUTPUT_DIR),undefined) +OUTPUT_DIR := $(ROOT_DIR)/bin +endif + +# Set the version number. you should not need to do this +# for the majority of scenarios. +ifeq ($(origin VERSION), undefined) +VERSION := $(shell git describe --abbrev=0 --dirty --always --tags | sed 's/-/./g') +endif + +# REV is the short git sha of latest commit. +REV=$(shell git rev-parse --short HEAD) + +# Supported Platforms for building multiarch binaries. +PLATFORMS ?= darwin_amd64 darwin_arm64 linux_amd64 linux_arm64 + +# Set a specific PLATFORM +ifeq ($(origin PLATFORM), undefined) + ifeq ($(origin GOOS), undefined) + GOOS := $(shell go env GOOS) + endif + ifeq ($(origin GOARCH), undefined) + GOARCH := $(shell go env GOARCH) + endif + PLATFORM := $(GOOS)_$(GOARCH) + # Use linux as the default OS when building images + IMAGE_PLAT := linux_$(GOARCH) +else + GOOS := $(word 1, $(subst _, ,$(PLATFORM))) + GOARCH := $(word 2, $(subst _, ,$(PLATFORM))) + IMAGE_PLAT := $(PLATFORM) +endif + +# List commands in cmd directory for building targets +COMMANDS ?= $(wildcard ${ROOT_DIR}/cmd/*) +BINS ?= $(foreach cmd,${COMMANDS},$(notdir ${cmd})) + +ifeq (${COMMANDS},) + $(error Could not determine COMMANDS, set ROOT_DIR or run in source dir) +endif +ifeq (${BINS},) + $(error Could not determine BINS, set ROOT_DIR or run in source dir) +endif diff --git a/tools/make/golang.mk b/tools/make/golang.mk new file mode 100644 index 0000000000..0a49d54415 --- /dev/null +++ b/tools/make/golang.mk @@ -0,0 +1,58 @@ +# This is a wrapper to build and push golang binaries +# +# All make targets related to golang are defined in this file. + +GOPATH := $(shell go env GOPATH) +ifeq ($(origin GOBIN), undefined) + GOBIN := $(GOPATH)/bin +endif + +# Build the target binary in target platform. +# The pattern of build.% is `build.{Platform}.{Command}`. +# If we want to build envoy-gateway in linux amd64 platform, +# just execute make build.linux_amd64.envoy-gateway. +.PHONY: go.build.% +go.build.%: + $(eval COMMAND := $(word 2,$(subst ., ,$*))) + $(eval PLATFORM := $(word 1,$(subst ., ,$*))) + $(eval OS := $(word 1,$(subst _, ,$(PLATFORM)))) + $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM)))) + @echo "===========> Building binary $(COMMAND) with commit $(REV) in version $(VERSION) for $(OS) $(ARCH)" + @CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go build -o $(OUTPUT_DIR)/$(OS)/$(ARCH)/$(COMMAND) $(ROOT_PACKAGE)/cmd/$(COMMAND) + +# Build the envoy-gateway binaries in the hosted platforms. +.PHONY: go.build +go.build: $(addprefix go.build., $(addprefix $(PLATFORM)., $(BINS))) + +# Build the envoy-gateway binaries in multi platforms +# It will build the linux/amd64, linux/arm64, darwin/amd64, darwin/arm64 binaries out. +.PHONY: go.build.multiarch +go.build.multiarch: $(foreach p,$(PLATFORMS),$(addprefix go.build., $(addprefix $(p)., $(BINS)))) + +.PHONY: go.test.unit +go.test.unit: ## Run go unit tests + @go test ./... + +.PHONY: go.test.coverage +go.test.coverage: ## Run go unit tests in GitHub Actions + @go test ./... -race -coverprofile=coverage.xml -covermode=atomic + +.PHONY: go.clean +go.clean: ## Clean the building output files + @echo "===========> Cleaning all build output" + @rm -rf $(OUTPUT_DIR) + +.PHONY: go.format.verify +go.format.verify: +ifeq ($(shell which goimports), ) + @echo "===========> Installing missing goimports" + @go get -v golang.org/x/tools/cmd/goimports + @go install golang.org/x/tools/cmd/goimports +endif + +.PHONY: go.format +go.format: go.format.verify + @echo "===========> Running go codes format" + @gofmt -s -w . + @goimports -w -local $(ROOT_PACKAGE) . + @go mod tidy diff --git a/tools/make/image.mk b/tools/make/image.mk new file mode 100644 index 0000000000..f13aa4d9a1 --- /dev/null +++ b/tools/make/image.mk @@ -0,0 +1,66 @@ +# This is a wrapper to build and push docker image +# +# All make targets related to docker image are defined in this file. + +# Docker variables +# REGISTRY is the image registry to use for build and push image targets. +REGISTRY ?= docker.io/envoyproxy +# IMAGE is the image URL for build and push image targets. +IMAGE ?= ${REGISTRY}/gateway-dev +# Tag is the tag to use for build and push image targets. +TAG ?= $(REV) + +DOCKER := docker +DOCKER_SUPPORTED_API_VERSION ?= 1.32 + +# Determine image files by looking into build/docker/*/Dockerfile +IMAGES_DIR ?= $(wildcard ${ROOT_DIR}tools/docker/*) +# Determine images names by stripping out the dir names +IMAGES ?= envoy-gateway +IMAGE_PLATFORMS ?= linux_amd64 linux_arm64 + +ifeq (${IMAGES},) + $(error Could not determine IMAGES, set ROOT_DIR or run in source dir) +endif + +.PHONY: image.verify +image.verify: + $(eval API_VERSION := $(shell $(DOCKER) version | grep -E 'API version: {1,6}[0-9]' | head -n1 | awk '{print $$3} END { if (NR==0) print 0}' )) + $(eval PASS := $(shell echo "$(API_VERSION) > $(DOCKER_SUPPORTED_API_VERSION)" | bc)) + @if [ $(PASS) -ne 1 ]; then \ + $(DOCKER) -v ;\ + echo "Unsupported docker version. Docker API version should be greater than $(DOCKER_SUPPORTED_API_VERSION)"; \ + exit 1; \ + fi + +.PHONY: image.build +image.build: image.verify $(addprefix image.build., $(addprefix $(IMAGE_PLAT)., $(IMAGES))) + +.PHONY: image.build.multiarch +image.build.multiarch: image.verify $(foreach p,$(IMAGE_PLATFORMS),$(addprefix image.build., $(addprefix $(p)., $(IMAGES)))) + +.PHONY: image.build.% +image.build.%: go.build.% + $(eval IMAGES := $(COMMAND)) + $(eval IMAGE_PLAT := $(subst _,/,$(PLATFORM))) + @echo "===========> Building image $(IMAGES) in tag $(TAG) for $(IMAGE_PLAT)" + $(eval BUILD_SUFFIX := --pull -t $(IMAGE):$(TAG) -f $(ROOT_DIR)/tools/docker/$(IMAGES)/Dockerfile bin) + @echo "===========> Creating image tag $(REGISTRY)/$(IMAGES):$(TAG) for $(ARCH)"; \ + $(DOCKER) build --platform $(IMAGE_PLAT) $(BUILD_SUFFIX) + +.PHONY: image.push +image.push: image.verify $(addprefix image.push., $(addprefix $(IMAGE_PLAT)., $(IMAGES))) + +.PHONY: image.push.multiarch +image.push.multiarch: image.verify $(foreach p,$(IMAGE_PLATFORMS),$(addprefix image.push., $(addprefix $(p)., $(IMAGES)))) + +.PHONY: image.push.% +image.push.%: + $(eval COMMAND := $(word 2,$(subst ., ,$*))) + $(eval IMAGES := $(COMMAND)) + $(eval PLATFORM := $(word 1,$(subst ., ,$*))) + $(eval ARCH := $(word 2,$(subst _, ,$(PLATFORM)))) + $(eval IMAGE_PLAT := $(subst _,/,$(PLATFORM))) + @echo "===========> Pushing image $(IMAGES) $(TAG) to $(REGISTRY)" + @echo "===========> Pushing docker image tag $(IMAGE):$(TAG) for $(ARCH)"; \ + $(DOCKER) push $(IMAGE):$(TAG); \ diff --git a/tools/make/lint.mk b/tools/make/lint.mk new file mode 100644 index 0000000000..4d1cabbebc --- /dev/null +++ b/tools/make/lint.mk @@ -0,0 +1,18 @@ +# This is a wrapper to do lint checks +# +# All make targets related to lint are defined in this file. + +.PHONY: lint.golint +lint.golint: + @echo Running Go linter ... + @golangci-lint run --build-tags=e2e --config=tools/linter/golangci-lint/.golangci.yml + +.PHONY: lint.yamllint +lint.yamllint: + @echo Running YAML linter ... + @yamllint --config-file=tools/linter/yamllint/.yamllint changelogs/ + +.PHONY: lint.codespell +lint.codespell: CODESPELL_SKIP := $(shell cat tools/linter/codespell/.codespell.skip | tr \\n ',') +lint.codespell: + @codespell --skip $(CODESPELL_SKIP) --ignore-words tools/linter/codespell/.codespell.ignorewords --check-filenames --check-hidden -q2