Skip to content

Commit

Permalink
Merge pull request #1 from heyselbi/rhods-main
Browse files Browse the repository at this point in the history
Manual sync of odh/release branch into rhods/main
  • Loading branch information
Jooho authored Oct 19, 2023
2 parents d30616e + b2a83e3 commit ad5de24
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 108 deletions.
46 changes: 0 additions & 46 deletions .github/workflows/build-and-push.yml

This file was deleted.

69 changes: 69 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Build

on:
pull_request:
branches:
- main
- "release-[0-9].[0-9]+"
paths-ignore:
- "**.md"
push:
branches:
- main
- "release-[0-9].[0-9]+"
tags:
- "v*"
paths-ignore:
- "**.md"

env:
IMAGE_NAME: "kserve/rest-proxy"

jobs:
build:
runs-on: ubuntu-latest
env:
CI: true
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup QEMU
uses: docker/setup-qemu-action@v2

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to DockerHub
if: github.event_name == 'push'
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

- name: Export docker build args
run: |
# Strip git ref prefix from version
VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
# Generate PR tag from github.ref == "refs/pull/123/merge"
[ "$VERSION" == "merge" ] && VERSION=$(echo "${{ github.ref }}" | sed -e 's,refs/pull/\(.*\)/merge,pr-\1,')
# Use Docker `latest` tag convention
[ "$VERSION" == "main" ] && VERSION=latest
echo "IMAGE_TAG=$VERSION" >> $GITHUB_ENV
# print env vars for debugging
cat "$GITHUB_ENV"
- name: Build and push runtime image
uses: docker/build-push-action@v4
with:
platforms: linux/amd64,linux/arm64,linux/ppc64le,linux/s390x
context: .
target: runtime
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
cache-from: type=gha
cache-to: type=gha,mode=max
31 changes: 31 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Test

on:
pull_request:
branches:
- main
- 'release-[0-9].[0-9]+'
paths-ignore:
- '**.md'

jobs:
test:
runs-on: ubuntu-latest
env:
CI: true
DOCKER_BUILDKIT: 1
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build dev image
run: make build.develop

- name: Run lint
run: ./scripts/develop.sh make fmt

- name: Run unit test
run: ./scripts/develop.sh make test
19 changes: 0 additions & 19 deletions .github/workflows/unit-test.yml

This file was deleted.

121 changes: 82 additions & 39 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,86 +13,129 @@
# limitations under the License.

###############################################################################
# Stage 1: Create the develop, test, and build environment
# Stage 1: Create the developer image for the BUILDPLATFORM only
###############################################################################
FROM registry.access.redhat.com/ubi8/ubi-minimal:8.7 AS develop
ARG GOLANG_VERSION=1.18
FROM --platform=$BUILDPLATFORM registry.access.redhat.com/ubi8/go-toolset:$GOLANG_VERSION AS develop

ARG GOLANG_VERSION=1.18.9
ARG PROTOC_VERSION=21.12

USER root
ENV HOME=/root

# Install build and dev tools
RUN microdnf install \
gcc \
gcc-c++ \
make \
vim \
findutils \
diffutils \
git \
wget \
tar \
unzip \
python3 \
nodejs && \
RUN --mount=type=cache,target=/root/.cache/dnf:rw \
dnf install --setopt=cachedir=/root/.cache/dnf -y --nodocs \
python3 \
python3-pip \
nodejs \
&& true

# Install pre-commit
ENV PIP_CACHE_DIR=/root/.cache/pip
RUN --mount=type=cache,target=/root/.cache/pip \
pip3 install pre-commit

# Install go
ENV PATH /usr/local/go/bin:$PATH
RUN set -eux; \
wget -qO go.tgz "https://golang.org/dl/go${GOLANG_VERSION}.linux-amd64.tar.gz"; \
sha256sum *go.tgz; \
tar -C /usr/local -xzf go.tgz; \
go version
# When using the BuildKit backend, Docker predefines a set of ARG variables with
# information on the platform of the node performing the build (build platform)
# These arguments are defined in the global scope but are not automatically available
# inside build stages. We need to expose the BUILDOS and BUILDARCH inside the build
# stage and redefine it without a value
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
ARG BUILDOS
ARG BUILDARCH

# Install protoc
# The protoc download files use a different variation of architecture identifiers
# from the Docker BUILDARCH forms amd64, arm64, ppc64le, s390x
# protoc-22.2-linux-aarch_64.zip <- arm64
# protoc-22.2-linux-ppcle_64.zip <- ppc64le
# protoc-22.2-linux-s390_64.zip <- s390x
# protoc-22.2-linux-x86_64.zip <- amd64
# so we need to map the arch identifiers before downloading the protoc.zip using
# shell parameter expansion: with the first character of a parameter being an
# exclamation point (!) it introduces a level of indirection where the value
# of the parameter is used as the name of another variable and the value of that
# other variable is the result of the expansion, e.g. the echo statement in the
# following three lines of shell script print "x86_64"
# BUILDARCH=amd64
# amd64=x86_64
# echo ${!BUILDARCH}
RUN set -eux; \
wget -qO protoc.zip "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-x86_64.zip"; \
sha256sum protoc.zip; \
unzip protoc.zip -x readme.txt -d /usr/local; \
protoc --version
amd64=x86_64; \
arm64=aarch_64; \
ppc64le=ppcle_64; \
s390x=s390_64; \
wget -qO protoc.zip "https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-${BUILDOS}-${!BUILDARCH}.zip" \
&& sha256sum protoc.zip \
&& unzip protoc.zip -x readme.txt -d /usr/local \
&& protoc --version \
&& true

WORKDIR /opt/app

COPY go.mod go.sum ./

# Install go protoc plugins
ENV PATH /root/go/bin:$PATH
RUN go get google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc

WORKDIR /opt/app
ENV PATH $HOME/go/bin:$PATH
RUN true \
&& go get google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc \
&& go install google.golang.org/protobuf/cmd/protoc-gen-go \
google.golang.org/grpc/cmd/protoc-gen-go-grpc \
&& protoc-gen-go --version \
&& true

# Download and initialize the pre-commit environments before copying the source so they will be cached
COPY .pre-commit-config.yaml ./
RUN git init && \
pre-commit install-hooks && \
rm -rf .git

# Download dependiencies before copying the source so they will be cached
# Download dependencies before copying the source so they will be cached
RUN go mod download

# the ubi/go-toolset image doesn't define ENTRYPOINT or CMD, but we need it to run 'make develop'
CMD /bin/bash


###############################################################################
# Stage 2: Run the build
# Stage 2: Run the go build with BUILDPLATFORM's native go compiler
###############################################################################
FROM develop AS build
FROM --platform=$BUILDPLATFORM develop AS build

LABEL image="build"

# Copy the source
COPY . ./

# Build the binary
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o /go/bin/server ./proxy/
# https://docs.docker.com/engine/reference/builder/#automatic-platform-args-in-the-global-scope
# don't provide "default" values (e.g. 'ARG TARGETARCH=amd64') for non-buildx environments,
# see https://github.com/docker/buildx/issues/510
ARG TARGETOS
ARG TARGETARCH

# Build the binaries using native go compiler from BUILDPLATFORM but compiled output for TARGETPLATFORM
# https://www.docker.com/blog/faster-multi-platform-builds-dockerfile-cross-compilation-guide/
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
GOOS=${TARGETOS:-linux} \
GOARCH=${TARGETARCH:-amd64} \
CGO_ENABLED=0 \
GO111MODULE=on \
go build -a -o /go/bin/server ./proxy/


###############################################################################
# Stage 3: Copy binary to create the smallest final runtime image
# Stage 3: Copy binaries only to create the smallest final runtime image
###############################################################################
FROM scratch AS runtime
FROM registry.access.redhat.com/ubi8/ubi-micro:8.7 as runtime

ARG USER=2000

USER ${USER}

COPY version /etc/modelmesh-version
COPY --from=build /go/bin/server /go/bin/server

CMD ["/go/bin/server"]
39 changes: 39 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,55 @@

IMG_NAME ?= kserve/rest-proxy

# collect args from `make run` so that they don't run twice
ifeq (run,$(firstword $(MAKECMDGOALS)))
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
ifneq ("$(wildcard /.dockerenv)","")
$(error Inside docker container, run 'make $(RUN_ARGS)')
endif
endif

.PHONY: all
## Alias for `build`
all: build

.PHONY: build
## Build runtime Docker image
build:
docker build -t ${IMG_NAME}:latest --target runtime .

.PHONY: build.develop
## Build develop container image
build.develop:
docker build -t ${IMG_NAME}-develop:latest --target develop .

.PHONY: develop
## Run interactive shell inside developer container
develop: build.develop
./scripts/develop.sh

.PHONY: run
## Run make target inside developer container (e.g. `make run fmt`)
run: build.develop
./scripts/develop.sh make $(RUN_ARGS)

.PHONY: fmt
## Auto-format source code and report code-style violations (lint)
fmt:
./scripts/fmt.sh

.PHONY: test
## Run tests
test:
go test -coverprofile cover.out `go list ./...`

.DEFAULT_GOAL := help
.PHONY: help
## Print Makefile documentation
help:
@perl -0 -nle 'printf("\033[36m %-15s\033[0m %s\n", "$$2", "$$1") while m/^##\s*([^\r\n]+)\n^([\w.-]+):[^=]/gm' $(MAKEFILE_LIST) | sort

# Override targets if they are included in RUN_ARGs so it doesn't run them twice
# otherwise 'make run fmt' would be equivalent to calling './scripts/develop.sh make fmt'
# followed by 'make fmt'
$(eval $(RUN_ARGS):;@:)
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build](https://github.com/kserve/rest-proxy/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/kserve/rest-proxy/actions/workflows/build.yml)

# KServe V2 REST Proxy

This REST Proxy leverages [gRPC-Gateway](https://github.com/grpc-ecosystem/grpc-gateway) to create a reverse-proxy server which translates a RESTful HTTP API into gRPC. This allows sending inference requests using the [KServe V2 REST Predict Protocol](https://github.com/kserve/kserve/blob/master/docs/predict-api/v2/required_api.md#httprest) to platforms that expect the [gRPC V2 Predict Protocol](https://github.com/kserve/kserve/blob/master/docs/predict-api/v2/required_api.md#grpc).
Expand Down
Loading

0 comments on commit ad5de24

Please sign in to comment.