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

chore(*) scripts for build, publish and fetch Envoy binaries #3110

Merged
merged 11 commits into from
Nov 17, 2021
Merged
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ include mk/k3d.mk
include mk/e2e.mk
include mk/e2e.new.mk
include mk/docs.mk
include mk/envoy.mk
2 changes: 1 addition & 1 deletion mk/build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ COREDNS_TMP_DIRECTORY ?= $(BUILD_DIR)/coredns
COREDNS_PLUGIN_CFG_PATH ?= $(TOP)/tools/builds/coredns/templates/plugin.cfg

# List of binaries that we have release build rules for.
BUILD_RELEASE_BINARIES := kuma-cp kuma-dp kumactl kuma-prometheus-sd coredns
BUILD_RELEASE_BINARIES := kuma-cp kuma-dp kumactl kuma-prometheus-sd coredns envoy

# List of binaries that we have test build roles for.
BUILD_TEST_BINARIES := test-server
Expand Down
2 changes: 1 addition & 1 deletion mk/docker.mk
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ docker/build/kuma-universal: build/artifacts-linux-amd64/kuma-cp/kuma-cp build/a
image/kuma-cp: build/kuma-cp/linux-amd64 docker/build/kuma-cp ## Dev: Rebuild `kuma-cp` Docker image

.PHONY: image/kuma-dp
image/kuma-dp: build/kuma-dp/linux-amd64 build/coredns/linux-amd64 docker/build/kuma-dp ## Dev: Rebuild `kuma-dp` Docker image
image/kuma-dp: build/kuma-dp/linux-amd64 build/coredns/linux-amd64 build/artifacts-linux-amd64/envoy docker/build/kuma-dp ## Dev: Rebuild `kuma-dp` Docker image

.PHONY: image/kumactl
image/kumactl: build/kumactl/linux-amd64 docker/build/kumactl ## Dev: Rebuild `kumactl` Docker image
Expand Down
26 changes: 26 additions & 0 deletions mk/envoy.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
ENVOY_TAG ?= v1.18.4
ENVOY_COMMIT_HASH ?=

SOURCE_DIR ?= ${TMPDIR}envoy-sources
ifndef TMPDIR
SOURCE_DIR ?= /tmp/envoy-sources
endif

BUILD_ENVOY_FROM_SOURCES ?= false

.PHONY: build/envoy
build/envoy:
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
$(MAKE) build/artifacts-${GOOS}-${GOARCH}/envoy

build/artifacts-%-amd64/envoy:
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
ifeq ($(BUILD_ENVOY_FROM_SOURCES),true)
ENVOY_TAG=${ENVOY_TAG} \
ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH} \
SOURCE_DIR=${SOURCE_DIR} \
BINARY_PATH=$@ ./tools/envoy/build_$*.sh
else
ENVOY_TAG=${ENVOY_TAG} \
ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH} \
BINARY_PATH=$@ \
ENVOY_DISTRO=$* ./tools/envoy/fetch.sh
endif
1 change: 1 addition & 0 deletions test/dockerfiles/Dockerfile.universal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ COPY --from=envoy /usr/local/bin/envoy /usr/local/bin/envoy

ADD $KUMA_ROOT/build/artifacts-linux-amd64/kuma-cp/kuma-cp /usr/bin
ADD $KUMA_ROOT/build/artifacts-linux-amd64/kuma-dp/kuma-dp /usr/bin
ADD $KUMA_ROOT/build/artifacts-linux-amd64/envoy /usr/bin
ADD $KUMA_ROOT/build/artifacts-linux-amd64/coredns/coredns /usr/bin
ADD $KUMA_ROOT/build/artifacts-linux-amd64/kumactl/kumactl /usr/bin
ADD $KUMA_ROOT/build/artifacts-linux-amd64/test-server/test-server /usr/bin
Expand Down
1 change: 1 addition & 0 deletions test/dockerfiles/Dockerfile.universal.dockerignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*
!build/artifacts-linux-amd64/kuma-cp/kuma-cp
!build/artifacts-linux-amd64/kuma-dp/kuma-dp
!build/artifacts-linux-amd64/envoy
!build/artifacts-linux-amd64/kumactl/kumactl
!build/artifacts-linux-amd64/coredns/coredns
!build/artifacts-linux-amd64/test-server/test-server
Expand Down
30 changes: 30 additions & 0 deletions tools/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env bash

function msg_green() {
builtin echo -en "\033[1;32m"
echo "$@"
builtin echo -en "\033[0m"
}

function msg_red() {
builtin echo -en "\033[1;31m" >&2
echo "$@" >&2
builtin echo -en "\033[0m" >&2
}

function msg_yellow() {
builtin echo -en "\033[1;33m"
echo "$@"
builtin echo -en "\033[0m"
}

function msg() {
builtin echo -en "\033[1m"
echo "$@"
builtin echo -en "\033[0m"
}

function msg_err() {
msg_red $@
exit 1
}
8 changes: 8 additions & 0 deletions tools/envoy/Dockerfile.build-centos7
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG ENVOY_BUILD_IMAGE
FROM $ENVOY_BUILD_IMAGE

ARG BUILD_CMD

RUN mkdir /build /source
COPY . /envoy-sources/
RUN bash -c "pushd /envoy-sources && $BUILD_CMD"
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
13 changes: 13 additions & 0 deletions tools/envoy/Dockerfile.build-ubuntu
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG ENVOY_BUILD_IMAGE
FROM $ENVOY_BUILD_IMAGE

ARG BUILD_CMD

RUN groupadd --gid $(id -g) -f envoygroup \
&& useradd -o --uid $(id -u) --gid $(id -g) --no-create-home --home-dir /build envoybuild \
&& usermod -a -G pcap envoybuild \
&& mkdir /build /source \
&& chown envoybuild:envoygroup /build /source

COPY . /envoy-sources/
RUN sudo -EHs -u envoybuild bash -c "pushd /envoy-sources && $BUILD_CMD"
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
Empty file.
40 changes: 40 additions & 0 deletions tools/envoy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Tools for Envoy

The current directory contains tools for building, publishing and fetching Envoy binaries.

There is a new Makefile target `build/envoy` that places an `envoy` binary in `build/artifacts-$GOOS-$GOARCH/` directory.
The default behaviour of that target – fetching binaries from [download.konghq.com](download.konghq.com) since it makes more sense for
overwhelming majority of users. However, there is a variable `BUILD_ENVOY_FROM_SOURCES` that allows to build Envoy from
source code.

### Usage

Download the latest supported Envoy binary for your host OS:
```shell
$ make build/envoy
```

Download the latest supported Envoy binary for specified system:
```shell
$ GOOS=linux make build/envoy # supported OS: linux, centos7 and darwin
```

Download the specific Envoy tag:
```shell
$ ENVOY_TAG=v1.18.4 make build/envoy
```

Download the specific Envoy commit hash (if it exists in [download.konghq.com](download.konghq.com)):
```shell
$ ENVOY_COMMIT_HASH=bef18019d8fc33a4ed6aca3679aff2100241ac5e make build/envoy
```

If desired commit hash doesn't exist, it could be built from sources:
```shell
$ ENVOY_COMMIT_HASH=bef18019d8fc33a4ed6aca3679aff2100241ac5e BUILD_ENVOY_FROM_SOURCES=true make build/envoy
```

When building from sources its still possible to specify OS:
```shell
$ GOOS=linux ENVOY_COMMIT_HASH=bef18019d8fc33a4ed6aca3679aff2100241ac5e BUILD_ENVOY_FROM_SOURCES=true make build/envoy
```
27 changes: 27 additions & 0 deletions tools/envoy/build_centos7.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

echo "Building Envoy for CentOS 7"

mkdir -p "$(dirname ${BINARY_PATH})"

SOURCE_DIR="${SOURCE_DIR}" ./tools/envoy/fetch_sources.sh

BUILD_CMD=${BUILD_CMD:-"BAZEL_BUILD_EXTRA_OPTIONS=\"${BAZEL_BUILD_EXTRA_OPTIONS:-}\" ./ci/do_ci.sh bazel.release.server_only"}

ENVOY_BUILD_SHA=$(curl --silent https://raw.githubusercontent.com/envoyproxy/envoy/"${ENVOY_TAG}"/.bazelrc | grep envoyproxy/envoy-build-ubuntu | sed -e 's#.*envoyproxy/envoy-build-ubuntu:\(.*\)#\1#'| uniq)
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
ENVOY_BUILD_IMAGE="envoyproxy/envoy-build-centos:${ENVOY_BUILD_SHA}"
LOCAL_BUILD_IMAGE="envoy-builder:${ENVOY_TAG}"

docker build -t "${LOCAL_BUILD_IMAGE}" --progress=plain \
--build-arg ENVOY_BUILD_IMAGE="${ENVOY_BUILD_IMAGE}" \
--build-arg BUILD_CMD="${BUILD_CMD}" \
-f tools/envoy/Dockerfile.build-centos7 "${SOURCE_DIR}"

# copy out the binary
id=$(docker create "${LOCAL_BUILD_IMAGE}")
docker cp "$id":/envoy-sources/linux/amd64/build_envoy_release_stripped/envoy "${BINARY_PATH}"
docker rm -v "$id"
29 changes: 29 additions & 0 deletions tools/envoy/build_darwin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

echo "Building Envoy for Darwin"

mkdir -p "$(dirname ${BINARY_PATH})"

SOURCE_DIR="${SOURCE_DIR}" ./tools/envoy/fetch_sources.sh

pushd "${SOURCE_DIR}"

BAZEL_BUILD_EXTRA_OPTIONS=${BAZEL_BUILD_EXTRA_OPTIONS:-""}
read -ra BAZEL_BUILD_EXTRA_OPTIONS <<< "${BAZEL_BUILD_EXTRA_OPTIONS}"
BAZEL_BUILD_OPTIONS=(
"--curses=no"
--show_task_finish
--verbose_failures
"--action_env=PATH=/usr/local/bin:/opt/local/bin:/usr/bin:/bin"
"--define" "wasm=disabled"
"${BAZEL_BUILD_EXTRA_OPTIONS[@]+"${BAZEL_BUILD_EXTRA_OPTIONS[@]}"}")
bazel build "${BAZEL_BUILD_OPTIONS[@]}" -c opt //source/exe:envoy-static

popd

cp ${SOURCE_DIR}/bazel-bin/source/exe/envoy-static ${BINARY_PATH}

29 changes: 29 additions & 0 deletions tools/envoy/build_linux.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

set -o errexit
set -o pipefail
set -o nounset

echo "Building Envoy for Linux"

mkdir -p "$(dirname ${BINARY_PATH})"

SOURCE_DIR="${SOURCE_DIR}" ./tools/envoy/fetch_sources.sh

BUILD_CMD=${BUILD_CMD:-"BAZEL_BUILD_EXTRA_OPTIONS=\"${BAZEL_BUILD_EXTRA_OPTIONS:-}\" ./ci/do_ci.sh bazel.release.server_only"}

ENVOY_BUILD_SHA=$(curl --silent https://raw.githubusercontent.com/envoyproxy/envoy/"${ENVOY_TAG}"/.bazelrc | grep envoyproxy/envoy-build-ubuntu | sed -e 's#.*envoyproxy/envoy-build-ubuntu:\(.*\)#\1#'| uniq)
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved
ENVOY_BUILD_IMAGE="envoyproxy/envoy-build-ubuntu:${ENVOY_BUILD_SHA}"
LOCAL_BUILD_IMAGE="envoy-builder:${ENVOY_TAG}"

echo "BUILD_CMD=${BUILD_CMD}"

docker build -t "${LOCAL_BUILD_IMAGE}" --progress=plain \
--build-arg ENVOY_BUILD_IMAGE="${ENVOY_BUILD_IMAGE}" \
--build-arg BUILD_CMD="${BUILD_CMD}" \
-f tools/envoy/Dockerfile.build-ubuntu "${SOURCE_DIR}"

# copy out the binary
id=$(docker create "${LOCAL_BUILD_IMAGE}")
docker cp "$id":/envoy-sources/linux/amd64/build_envoy_release_stripped/envoy "${BINARY_PATH}"
docker rm -v "$id"
68 changes: 68 additions & 0 deletions tools/envoy/fetch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash

# This script fetches Envoy binary from download.konghq.com
#
# Requires:
# - $BINARY_PATH, path where binary will be fetched, for example 'out/envoy'
# - $ENVOY_DISTRO, name of the distributive (i.e darwin, linux)
jpeach marked this conversation as resolved.
Show resolved Hide resolved
#
# Optional:
# - $ENVOY_TAG, git tag to reference specific revision
# - $ENVOY_COMMIT_HASH, hash of the git commit. If specified, then $ENVOY_TAG will be ignored
#
# at least one of $ENVOY_TAG or $ENVOY_COMMIT_HASH should be specified

set -o errexit
set -o pipefail
set -o nounset

source "$(dirname "$(dirname "$0")")/common.sh" # relative path to ../common.sh

function download_envoy() {
local binary_name=$1
echo "Downloading ${binary_name}"

if [ ! -d "$(dirname "${BINARY_PATH}")" ]; then
mkdir -p "$(dirname "${BINARY_PATH}")"
fi

local status=$(curl -# --location --output "${BINARY_PATH}" --write-out %{http_code} \
"https://download.konghq.com/mesh-alpine/${binary_name}")

[ -f "${BINARY_PATH}" ] && chmod +x "${BINARY_PATH}"
[ "$status" -ne "200" ] && msg_err "Error: failed downloading Envoy" || true
}

ENVOY_TAG=${ENVOY_TAG:-}
ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH:-}
[[ -z "${ENVOY_TAG}" ]] && [[ -z "${ENVOY_COMMIT_HASH}" ]] && msg_err "Error: either ENVOY_TAG or ENVOY_COMMIT_HASH should be specified"

if [ "${ENVOY_DISTRO}" == "linux" ]; then
ENVOY_DISTRO="alpine"
fi

if [ "${ENVOY_DISTRO}" == "centos7" ]; then
ENVOY_DISTRO="centos"
fi

if [[ -n "${ENVOY_COMMIT_HASH}" ]]; then
ENVOY_SHORT_HASH=${ENVOY_COMMIT_HASH:0:8}

BINARY_NAME=$(curl --silent https://download.konghq.com/mesh-alpine/ \
| { grep "${ENVOY_SHORT_HASH}" || true; } \
| { grep "${ENVOY_DISTRO}" || true; } \
| sed -e 's#.*<li><a href=".*">\(.*\)</a></li>#\1#')

[[ -z "${BINARY_NAME}" ]] && msg_err "failed to resolve binary name by ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH}"

download_envoy "${BINARY_NAME}"
exit 0
fi

if [[ -n "${ENVOY_TAG}" ]]; then
ENVOY_VERSION=${ENVOY_TAG:1}
BINARY_NAME="envoy-${ENVOY_VERSION}-${ENVOY_DISTRO}"

download_envoy "${BINARY_NAME}"
exit 0
fi
45 changes: 45 additions & 0 deletions tools/envoy/fetch_sources.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/bin/bash

# This script fetches Envoy source code to $SOURCE_DIR
#
# Requires:
# - $SOURCE_DIR, a directory where sources will be placed
jpeach marked this conversation as resolved.
Show resolved Hide resolved
#
# Optional:
# - $ENVOY_TAG, git tag to reference specific revision
# - $ENVOY_COMMIT_HASH, hash of the git commit. If specified, then $ENVOY_TAG will be ignored
#
# at least one of $ENVOY_TAG or $ENVOY_COMMIT_HASH should be specified

set -o errexit
set -o pipefail
set -o nounset

source "$(dirname "$(dirname "$0")")/common.sh" # relative path to ../common.sh

ENVOY_TAG=${ENVOY_TAG:-}
ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH:-}
[[ -z "${ENVOY_TAG}" ]] && [[ -z "${ENVOY_COMMIT_HASH}" ]] && msg_err "Error: either ENVOY_TAG or ENVOY_COMMIT_HASH should be specified"
lobkovilya marked this conversation as resolved.
Show resolved Hide resolved

# clone Envoy repo if not exists
if [[ ! -d "${SOURCE_DIR}" ]]; then
mkdir -p "${SOURCE_DIR}"
(
cd "${SOURCE_DIR}"
git init .
git remote add origin https://github.com/envoyproxy/envoy.git
)
else
echo "Envoy source directory already exists, just fetching"
pushd ${SOURCE_DIR} && git fetch --all && popd
fi

pushd ${SOURCE_DIR}

git fetch origin --depth=1 "${ENVOY_COMMIT_HASH:-${ENVOY_TAG}}"
git reset --hard FETCH_HEAD

echo "ENVOY_TAG=${ENVOY_TAG}"
echo "ENVOY_COMMIT_HASH=${ENVOY_COMMIT_HASH}"

popd
35 changes: 35 additions & 0 deletions tools/envoy/publish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

# This script publishes binary passed to $1
#
# Requirements
# * Docker

set -o errexit
set -o pipefail
set -o nounset

source "$(dirname "$(dirname "$0")")/common.sh" # relative path to ../common.sh

if [ $# -eq 0 ]; then
echo "Usage: ./publish.sh path_to_envoy"
echo "Example: ./publish.sh out/envoy-v1.20.0"
exit 1
fi

BINARY_PATH=$1

PULP_HOST="https://api.pulp.konnect-prod.konghq.com"
PULP_PACKAGE_TYPE="mesh"
PULP_DIST_NAME="alpine"

[ -z "$PULP_USERNAME" ] && msg_err "PULP_USERNAME required"
[ -z "$PULP_PASSWORD" ] && msg_err "PULP_PASSWORD required"

docker run --rm \
-e PULP_USERNAME=${PULP_USERNAME} \
-e PULP_PASSWORD=${PULP_PASSWORD} \
-e PULP_HOST=${PULP_HOST} \
-v "${PWD}":/files:ro -it kong/release-script \
--file /files/"${BINARY_PATH}" \
--package-type ${PULP_PACKAGE_TYPE} --dist-name ${PULP_DIST_NAME} --publish
Loading