Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: setups for container images #18

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/commit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,6 @@ jobs:
key: unittest-${{ hashFiles('**/go.mod', '**/go.sum', '**/Makefile') }}
- name: Run unit tests
run: make test

docker_builds:
uses: ./.github/workflows/docker_builds_template.yaml
63 changes: 63 additions & 0 deletions .github/workflows/docker_builds_template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
name: Docker Builds Template
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: make this as a template since it will be used "release" workflow in the future


on:
workflow_call:

jobs:
docker_builds:
name: Build and/or Push Docker Images
runs-on: ubuntu-latest
strategy:
matrix:
target:
- command_name: "controller"
- command_name: "extproc"
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
cache: false
go-version-file: go.mod
- uses: actions/cache@v4
with:
path: |
~/.cache/go-build
~/.cache/golangci-lint
~/go/pkg/mod
~/go/bin
key: build-container-${{ hashFiles('**/go.mod', '**/go.sum', '**/Makefile') }}

- uses: docker/setup-buildx-action@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@49b3bc8e6bdd4a60e6116a5414239cba5943d3cf # v3.2.0

- name: Set up Docker buildx
id: buildx
uses: docker/setup-buildx-action@988b5a0280414f521da01fcc63a27aeeb4b104db # v3.6.1

- name: Login into GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

# Do not build and push images for pull requests. Just build them to ensure the build is successful.
- name: Build Image
if: github.event_name != 'push'
run: |
make docker-build.${{ matrix.target.command_name }}

# Push images for the push events, e.g. when a new tag is pushed as well as PR merges.
# * Only use the tag if the event is a tag event, otherwise use "latest".
# * Build for both amd64 and arm64 platforms.
- name: Build and Push Image
if: github.event_name == 'push'
run: |
if [[ "$GITHUB_REF" == refs/tags/* ]]; then
TAG="${GITHUB_REF#refs/tags/}"
else
TAG="latest"
fi
make docker-build.${{ matrix.target.command_name }} ENABLE_MULTI_PLATFORMS=true TAG=$TAG BUILDX_ARGS="--push"
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM gcr.io/distroless/static-debian11:nonroot
ARG COMMAND_NAME
ARG TARGETOS
ARG TARGETARCH

COPY ./out/${COMMAND_NAME}-${TARGETOS}-${TARGETARCH} /${COMMAND_NAME}

USER nonroot:nonroot
ENTRYPOINT ["/${COMMAND_NAME}"]
85 changes: 85 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
# The Go-based tools are defined in Makefile.tools.mk.
include Makefile.tools.mk

# The list of commands that can be built.
COMMANDS := controller extproc

# This is the package that contains the version information for the build.
GIT_COMMIT:=$(shell git rev-parse HEAD)
VERSION_PACKAGE := github.com/envoyproxy/ai-gateway/internal/version
GO_LDFLAGS += -X $(VERSION_PACKAGE).Version=$(GIT_COMMIT)

# This is the directory where the built artifacts will be placed.
OUTPUT_DIR ?= out

# Arguments for docker builds.
OCI_REGISTRY ?= ghcr.io/envoyproxy/ai-gateway
TAG ?= latest
ENABLE_MULTI_PLATFORMS ?= false

# This runs the linter, formatter, and tidy on the codebase.
.PHONY: lint
lint: golangci-lint
Expand Down Expand Up @@ -48,3 +64,72 @@ check:
test:
@echo "test => ./..."
@go test -v $(shell go list ./... | grep -v e2e)

# This builds a binary for the given command under the internal/cmd directory.
#
# Example:
# - `make build.controler`: will build the internal/cmd/controller directory.
# - `make build.extproc`: will build the internal/cmd/extproc directory.
#
# By default, this will build for the current GOOS and GOARCH.
# To build for multiple platforms, set the GOOS_LIST and GOARCH_LIST variables.
#
# Example:
# - `make build.controler GOOS_LIST="linux darwin" GOARCH_LIST="amd64 arm64"`
GOOS_LIST ?= $(shell go env GOOS)
GOARCH_LIST ?= $(shell go env GOARCH)
.PHONY: build.%
build.%:
$(eval COMMAND_NAME := $(subst build.,,$@))
@mkdir -p $(OUTPUT_DIR)
@for goos in $(GOOS_LIST); do \
for goarch in $(GOARCH_LIST); do \
echo "-> Building $(COMMAND_NAME) for $$goos/$$goarch"; \
CGO_ENABLED=0 GOOS=$$goos GOARCH=$$goarch go build -ldflags "$(GO_LDFLAGS)" \
-o $(OUTPUT_DIR)/$(COMMAND_NAME)-$$goos-$$goarch ./internal/cmd/$(COMMAND_NAME); \
echo "<- Built $(OUTPUT_DIR)/$(COMMAND_NAME)-$$goos-$$goarch"; \
done; \
done

# This builds a docker image for a given command.
#
# Example:
# - `make docker-build.controller`: will build the controller command.
# - `make docker-build.extproc`: will build the extproc command.
#
# By default, this will build for the current GOARCH and linux.
# To build for multiple platforms, set the ENABLE_MULTI_PLATFORMS variable to true.
#
# Example:
# - `make docker-build.controller ENABLE_MULTI_PLATFORMS=true`
#
# Also, DOCKER_BUILD_ARGS can be set to pass additional arguments to the docker build command.
#
# Example:
# - `make docker-build.controller ENABLE_MULTI_PLATFORMS=true DOCKER_BUILD_ARGS="--push"` to push the image to the registry.
# - `make docker-build.controller ENABLE_MULTI_PLATFORMS=true DOCKER_BUILD_ARGS="--load"` to load the image after building.
#
# By default, the image tag is set to `latest`. `TAG` can be set to a different value.
#
# Example:
# - `make docker-build.controller TAG=v1.2.3`
.PHONY: docker-build.%
docker-build.%:
$(eval COMMAND_NAME := $(subst docker-build.,,$@))
@if [ "$(ENABLE_MULTI_PLATFORMS)" = "true" ]; then \
GOARCH_LIST="amd64 arm64"; PLATFORMS="--platform linux/amd64,linux/arm64"; \
else \
GOARCH_LIST="$(shell go env GOARCH)"; PLATFORMS=""; \
fi
@$(MAKE) build.$(COMMAND_NAME) GOOS_LIST="linux"
docker buildx build . -t $(OCI_REGISTRY)/$(COMMAND_NAME):$(TAG) --build-arg COMMAND_NAME=$(COMMAND_NAME) $(PLATFORMS) $(DOCKER_BUILD_ARGS)

# This builds docker images for all commands. All options for `docker-build.%` apply.
#
# Example:
# - `make docker-build`
# - `make docker-build ENABLE_MULTI_PLATFORMS=true DOCKER_BUILD_ARGS="--load"`
# - `make docker-build ENABLE_MULTI_PLATFORMS=true DOCKER_BUILD_ARGS="--push" TAG=v1.2.3`
.PHONE: docker-build
docker-build:
@$(foreach COMMAND_NAME,$(COMMANDS),$(MAKE) docker-build.$(COMMAND_NAME);)
4 changes: 3 additions & 1 deletion internal/cmd/controller/main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package main

import "github.com/envoyproxy/ai-gateway/internal/version"

func main() {
println("hello")
println(version.Version)
}
7 changes: 7 additions & 0 deletions internal/cmd/extproc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/envoyproxy/ai-gateway/internal/version"

func main() {
println(version.Version)
}
4 changes: 4 additions & 0 deletions internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package version

// Version is the current version of build. This is populated by the Go linker.
var Version = "dev"
Loading