Skip to content

Commit

Permalink
Merge pull request #196 from JimmyYang20/optimize-platform
Browse files Browse the repository at this point in the history
Sedna control plane supports amd64/arm64
  • Loading branch information
kubeedge-bot authored Sep 28, 2021
2 parents 3d88d0d + d8766bd commit 1d7bd48
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/image-publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ jobs:
# ${GITHUB_REF} => refs/tags/v0.0.1
# IMAGE_REPO=kubeedge IMAGE_TAG=v0.0.1
# see https://docs.github.com/en/actions/reference/environment-variables#default-environment-variables
make push-all IMAGE_REPO=${GITHUB_REPOSITORY%/*} IMAGE_TAG=${GITHUB_REF#refs/*/}
make push-all IMAGE_REPO=$(echo ${GITHUB_REPOSITORY%/*} | tr A-Z a-z) IMAGE_TAG=${GITHUB_REF#refs/*/}
working-directory: ./src/github.com/${{ github.repository }}
21 changes: 21 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,24 @@ jobs:
fetch-depth: 0

- run: make images

docker_cross_build:
runs-on: ubuntu-latest
name: docker cross build images for gm/lc/kb
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.14.x

- uses: actions/cache@v2
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}

- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 0

- run: make docker-cross-build
27 changes: 20 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ GOPATH ?= $(shell go env GOPATH)

OUT_DIR ?= _output
OUT_BINPATH := $(OUT_DIR)/bin
OUT_IMAGESPATH := $(OUT_DIR)/images

IMAGE_REPO ?= kubeedge

# the env PLATFORMS defines to generate linux images for amd 64-bit, arm 64-bit and armv7 architectures
# the full list of PLATFORMS is linux/amd64,linux/arm64,linux/arm/v7
PLATFORMS ?= linux/amd64,linux/arm64
COMPONENTS ?= gm lc kb

IMAGE_TAG ?= v0.3.0
GO_LDFLAGS ?=''
GO_LDFLAGS ?= ""

# set allowDangerousTypes for allowing float
CRD_OPTIONS ?= "crd:crdVersions=v1,allowDangerousTypes=true"
Expand Down Expand Up @@ -50,7 +57,7 @@ define BUILD_HELP_INFO

endef

.PHONY: build all
.PHONY: build docker-cross-build all
ifeq ($(HELP),y)
build all:
@echo "$${BUILD_HELP_INFO//TARGET/$@}"
Expand All @@ -59,6 +66,9 @@ else
# default target
build:
hack/make-rules/build.sh $(WHAT)
# build multi-platform images and results will be saved in tar packages.
docker-cross-build:
bash hack/make-rules/cross-build.sh

all: verify build

Expand Down Expand Up @@ -132,18 +142,21 @@ gmimage lcimage kbimage:
docker build --build-arg GO_LDFLAGS=${GO_LDFLAGS} -t ${IMAGE_REPO}/sedna-${@:image=}:${IMAGE_TAG} -f build/${@:image=}/Dockerfile .


.PHONY: push push-examples push-all
push-all: push push-examples
.PHONY: push push-examples push-all push-multi-platforms
push-all: push-multi-platform-images push-examples

# push target pushes sedna-built images
push: images
docker push ${IMAGE_REPO}/sedna-gm:${IMAGE_TAG}
docker push ${IMAGE_REPO}/sedna-lc:${IMAGE_TAG}
docker push ${IMAGE_REPO}/sedna-kb:${IMAGE_TAG}
for target in $(COMPONENTS); do \
docker push ${IMAGE_REPO}/sedna-$$target:${IMAGE_TAG}
done
bash scripts/storage-initializer/push_image.sh

push-examples:
bash examples/push_image.sh
# push multi-platform images
push-multi-platform-images:
bash hack/make-rules/push.sh

.PHONY: e2e
e2e:
Expand Down
90 changes: 90 additions & 0 deletions hack/lib/buildx.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/usr/bin/env bash

# Copyright 2021 The KubeEdge Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

sedna::buildx::prepare_env() {
# Check whether buildx exists.
if ! docker buildx >/dev/null 2>&1; then
echo "ERROR: docker buildx not available. Docker 19.03 or higher is required with experimental features enabled" >&2
exit 1
fi

# Use tonistiigi/binfmt that is to enable an execution of different multi-architecture containers
docker run --privileged --rm tonistiigi/binfmt --install all

# Create a new builder which gives access to the new multi-architecture features.
builder_instance="sedna-buildx"
if ! docker buildx inspect $builder_instance >/dev/null 2>&1; then
docker buildx create --use --name $builder_instance --driver docker-container
fi
docker buildx use $builder_instance
}

sedna::buildx:generate-dockerfile() {
dockerfile=${1}
sed "/AS builder/s/FROM/FROM --platform=\$BUILDPLATFORM/g" ${dockerfile}
}

sedna::buildx::push-multi-platform-images() {
sedna::buildx::prepare_env

bash scripts/storage-initializer/push_image.sh

for component in ${COMPONENTS[@]}; do
echo "pushing ${PLATFORMS} image for $component"

temp_dockerfile=build/${component}/buildx_dockerfile
sedna::buildx:generate-dockerfile build/${component}/Dockerfile > ${temp_dockerfile}

docker buildx build --push \
--build-arg GO_LDFLAGS=${GO_LDFLAGS} \
--platform ${PLATFORMS} \
-t ${IMAGE_REPO}/sedna-${component}:${IMAGE_TAG} \
-f ${temp_dockerfile} .

rm ${temp_dockerfile}
done
}

sedna::buildx::build-multi-platform-images() {
sedna::buildx::prepare_env

mkdir -p ${OUT_IMAGESPATH}
arch_array=(${PLATFORMS//,/ })

temp_dockerfile=${OUT_IMAGESPATH}/buildx_dockerfile
for component in ${COMPONENTS[@]}; do
echo "building ${PLATFORMS} image for ${component}"

sedna::buildx:generate-dockerfile build/${component}/Dockerfile > ${temp_dockerfile}

for arch in ${arch_array[@]}; do
dest_tar=${OUT_IMAGESPATH}/${component}-${IMAGE_TAG}-${arch////-}.tar
echo "building ${arch} image for ${component} and the image will be saved in ${dest_tar}"

docker buildx build -o type=docker,dest=${dest_tar} \
--build-arg GO_LDFLAGS=${GO_LDFLAGS} \
--platform ${arch} \
-t ${IMAGE_REPO}/sedna-${component}:${IMAGE_TAG} \
-f ${temp_dockerfile} .
done
done

rm ${temp_dockerfile}
}
2 changes: 2 additions & 0 deletions hack/lib/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ set -o pipefail
SEDNA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"

SEDNA_OUT_BINPATH="${SEDNA_ROOT}/${OUT_BINPATH:-_output/bin}"
SEDNA_OUT_IMAGEPATH="${SEDNA_ROOT}/${OUT_IMAGESPATH:-_output/images}"

readonly SEDNA_GO_PACKAGE="github.com/kubeedge/sedna"

source "${SEDNA_ROOT}/hack/lib/golang.sh"
source "${SEDNA_ROOT}/hack/lib/util.sh"
source "${SEDNA_ROOT}/hack/lib/buildx.sh"
24 changes: 24 additions & 0 deletions hack/make-rules/cross-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# Copyright 2021 The KubeEdge Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

SEDNA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"

source "${SEDNA_ROOT}/hack/lib/init.sh"
sedna::buildx::build-multi-platform-images
24 changes: 24 additions & 0 deletions hack/make-rules/push.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

# Copyright 2021 The KubeEdge Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

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

SEDNA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd -P)"

source "${SEDNA_ROOT}/hack/lib/init.sh"
sedna::buildx::push-multi-platform-images
7 changes: 6 additions & 1 deletion scripts/storage-initializer/push_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,9 @@
cd "$(dirname "${BASH_SOURCE[0]}")"

source build_image.sh
docker push $IMAGE

if [ -z "${PLATFORMS:-}" ]; then
docker push $IMAGE
else
docker buildx build --push --platform $PLATFORMS -t $IMAGE --label sedna=scripts .
fi

0 comments on commit 1d7bd48

Please sign in to comment.