From d46b02c631e25a79be03a444f4409b139fc6c82d Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Wed, 8 Jun 2022 16:41:42 -0700 Subject: [PATCH 1/3] Run make inside a container with dev tools * Adds an option `MAKE_IN_DOCKER=1 make ` to allow the dev to run the make target inside a container with preinstalled dev tools * By default `MAKE_IN_DOCKER` is unset, so `make` would run locally on the host Relates to https://github.com/envoyproxy/gateway/issues/90 Signed-off-by: Arko Dasgupta --- DEVELOPER.md | 21 +++++++++++++++ Dockerfile.builder | 9 +++++++ Makefile | 66 +++++++++++++++++++-------------------------- Makefile.targets.mk | 38 ++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 DEVELOPER.md create mode 100644 Dockerfile.builder create mode 100644 Makefile.targets.mk diff --git a/DEVELOPER.md b/DEVELOPER.md new file mode 100644 index 0000000000..df0bf40ff9 --- /dev/null +++ b/DEVELOPER.md @@ -0,0 +1,21 @@ +# Developer documentation + +Envoy Gateway is built using a [make](https://www.gnu.org/software/make/)-based +build system. Our CI is based on [Github Actions](https://docs.github.com/en/actions) (see: [workflows](.github/workflows)) + +## Prerequisites + +* Go, currently we use 1.18.x. You can refer to https://go.dev/doc/install to +download and install Go on your system. +* [make](https://www.gnu.org/software/make/). +* [Docker](https://docs.docker.com/engine/install/), this is optional when you want to build the image. +* The project also uses the below tools for linting. If you do not have these tools installed on your machine, +you can alternatively run `MAKE_IN_DOCKER=1 make ` to run `make` inside a Docker container which has all the +preinstalled tools needed to support all the `make` targets. + * [TODO](https://github.com/envoyproxy/gateway/issues/73) + + +## Quick start + +[TODO](https://github.com/envoyproxy/gateway/issues/101) Run `make help` to see all the available targets to build, test +and run `envoy-gateway`. diff --git a/Dockerfile.builder b/Dockerfile.builder new file mode 100644 index 0000000000..c16e7d179c --- /dev/null +++ b/Dockerfile.builder @@ -0,0 +1,9 @@ +# Make sure versions are same as +# https://github.com/envoyproxy/gateway/blob/main/.github/workflows/build_and_test.yaml +FROM golang:1.18.2 as builder + +# Install Docker CLI +RUN curl -fsSL https://get.docker.com | VERSION=20.10.16 sh + +WORKDIR /workspace +ENTRYPOINT ["make"] diff --git a/Makefile b/Makefile index 30a87792a2..ec98612ca8 100644 --- a/Makefile +++ b/Makefile @@ -1,38 +1,28 @@ -# 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) - -.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 - -.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) - +# 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 envioronment variable to run `make` inside +# a Docker container with preinstalled tools. + +DOCKER_BUILDER_IMAGE ?= envoyproxy/gateway-dev-builder +DOCKER_BUILDER_TAG ?= latest +DOCKER_BUILD_CMD ?= DOCKER_BUILDKIT=1 docker build +DOCKER_RUN_CMD ?= docker run \ + --rm \ + -it \ + -v /var/run/docker.sock:/var/run/docker.sock \ + -v ${PWD}:/workspace + +%: +ifeq ($(MAKE_IN_DOCKER), 1) +# Build builder image + @$(DOCKER_BUILD_CMD) -t $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) - < Dockerfile.builder +# 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 $@ +else +# Run make locally + @$(MAKE) -f Makefile.targets.mk $@ +endif diff --git a/Makefile.targets.mk b/Makefile.targets.mk new file mode 100644 index 0000000000..75f76ca77e --- /dev/null +++ b/Makefile.targets.mk @@ -0,0 +1,38 @@ +# 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) + +.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 + +.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) + From 449db6b7d82291632bd0c64e1c05558982ac968f Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Thu, 9 Jun 2022 11:09:13 -0700 Subject: [PATCH 2/3] ensure same versioning across dockerfile, ci docs Signed-off-by: Arko Dasgupta --- DEVELOPER.md | 24 +++++++++++++++++------- Dockerfile.builder | 10 ++++++---- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/DEVELOPER.md b/DEVELOPER.md index df0bf40ff9..ae98f6ba6a 100644 --- a/DEVELOPER.md +++ b/DEVELOPER.md @@ -5,15 +5,25 @@ build system. Our CI is based on [Github Actions](https://docs.github.com/en/act ## Prerequisites -* Go, currently we use 1.18.x. You can refer to https://go.dev/doc/install to -download and install Go on your system. -* [make](https://www.gnu.org/software/make/). -* [Docker](https://docs.docker.com/engine/install/), this is optional when you want to build the image. -* The project also uses the below tools for linting. If you do not have these tools installed on your machine, +### go +* Version: 1.18.2 +* Installation Guide: https://go.dev/doc/install to + +### make +* Recommended Version: 4.3 +* Installation Guide: https://www.gnu.org/software/make/). + +### docker +* Optional when you want to build a Docker image or run make inside Docker. +* Recommened Version: 20.10.16 +* Installation Guide: https://docs.docker.com/engine/install/ + +### linters +* [TODO](https://github.com/envoyproxy/gateway/issues/73) + +* If you do not have these tools installed on your machine, you can alternatively run `MAKE_IN_DOCKER=1 make ` to run `make` inside a Docker container which has all the preinstalled tools needed to support all the `make` targets. - * [TODO](https://github.com/envoyproxy/gateway/issues/73) - ## Quick start diff --git a/Dockerfile.builder b/Dockerfile.builder index c16e7d179c..d58801f89e 100644 --- a/Dockerfile.builder +++ b/Dockerfile.builder @@ -1,8 +1,10 @@ -# Make sure versions are same as -# https://github.com/envoyproxy/gateway/blob/main/.github/workflows/build_and_test.yaml -FROM golang:1.18.2 as builder +# Make sure the tooling versions are same as defined in the +# CI https://github.com/envoyproxy/gateway/blob/main/.github/workflows/build_and_test.yaml +# as well as in the Developer docs - https://github.com/envoyproxy/gateway/blob/main/DEVELOPER.md#prerequisites -# Install Docker CLI +# go +FROM golang:1.18.2 as builder +# docker CLI RUN curl -fsSL https://get.docker.com | VERSION=20.10.16 sh WORKDIR /workspace From cd7379eb00f8cafb8f887e95d8534f4ef142d49d Mon Sep 17 00:00:00 2001 From: Arko Dasgupta Date: Thu, 9 Jun 2022 19:05:50 -0700 Subject: [PATCH 3/3] move Dockerfile.builder into tools/ Signed-off-by: Arko Dasgupta --- Makefile | 2 +- Dockerfile.builder => tools/Dockerfile.builder | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Dockerfile.builder => tools/Dockerfile.builder (100%) diff --git a/Makefile b/Makefile index ec98612ca8..1e23087f5e 100644 --- a/Makefile +++ b/Makefile @@ -18,7 +18,7 @@ DOCKER_RUN_CMD ?= docker run \ %: ifeq ($(MAKE_IN_DOCKER), 1) # Build builder image - @$(DOCKER_BUILD_CMD) -t $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) - < Dockerfile.builder + @$(DOCKER_BUILD_CMD) -t $(DOCKER_BUILDER_IMAGE):$(DOCKER_BUILDER_TAG) - < tools/Dockerfile.builder # 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/Dockerfile.builder b/tools/Dockerfile.builder similarity index 100% rename from Dockerfile.builder rename to tools/Dockerfile.builder