Skip to content

Commit

Permalink
Merge pull request #5 from renyunkang/master
Browse files Browse the repository at this point in the history
update image build way
  • Loading branch information
ks-ci-bot authored Mar 6, 2024
2 parents 6749aae + 10d7c26 commit f0099b2
Show file tree
Hide file tree
Showing 23 changed files with 1,536 additions and 34 deletions.
38 changes: 38 additions & 0 deletions .github/workflows/docker-build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: BuildContainerImage

on:
push:
branches:
- 'master'
- 'release-*'
tags:
- 'v*'

jobs:
build:
name: Build
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: all

- name: Set up Docker buildx
uses: docker/setup-buildx-action@v1

- name: Build and push docker images
env:
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
if: github.event_name == 'push'
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
REPO=kubesphere TAG="${GITHUB_REF#refs/*/}" make container-cross-push
29 changes: 26 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,34 @@
rootfs/kube-keepalived-vip
rootfs/keepalived.tar.gz

chart/kube-keepalived-vip-*.tgz
chart/kube-keepalived-vip/Chart.yaml
chart/kube-keepalived-vip/values.yaml
# Ignore backup files
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
bin

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Kubernetes Generated files - skip generated files, except for vendored files

!vendor/**/zz_generated.*

# editor and IDE paraphernalia
.idea
*.swp
*.swo
*~
.vscode
*.bak
.DS_Store
.idea/
*.coverprofile
# log
*.porterlog

63 changes: 35 additions & 28 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
all: push
all: container-cross-push

# 0.0 shouldn't clobber any release builds
REPO ?= kubesphere
TAG ?= latest
HAPROXY_TAG = 0.1
# Helm uses SemVer2 versioning
CHART_VERSION = 1.0.0
BUILD_IMAGE = build-keepalived
PKG = github.com/openelb/kube-keepalived-vip
KeepalivedVersion = 2.2.8

GO_LIST_FILES=$(shell go list ${PKG}/... | grep -v vendor)

controller: clean
CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -ldflags '-s -w' -trimpath -o rootfs/kube-keepalived-vip ./cmd
binary:
hack/gobuild.sh cmd/kube-keepalived-vip

container: controller keepalived
docker build -t $(REPO)/kube-keepalived-vip:$(TAG) -o type=docker rootfs
# build kube-keepalived-vip in docker
container: ;$(info $(M)...Begin to build the docker image.) @ ## Build the docker image.
DRY_RUN=true hack/docker_build.sh

container-push: ;$(info $(M)...Begin to build and push.) @ ## Build and Push.
hack/docker_build.sh

container-cross: ; $(info $(M)...Begin to build container images for multiple platforms.) @ ## Build container images for multiple platforms. Currently, only linux/amd64,linux/arm64 are supported.
DRY_RUN=true hack/docker_build_multiarch.sh

container-cross-push: ; $(info $(M)...Begin to build and push.) @ ## Build and Push.
hack/docker_build_multiarch.sh


# only build/compile keepalived binary
# https://github.com/acassen/keepalived/archive/refs/tags/v${VERSION}.tar.gz
keepalived:
docker build --build-arg VERSION=${KeepalivedVersion} -t $(BUILD_IMAGE):$(TAG) -o type=docker build
docker create --name $(BUILD_IMAGE) $(BUILD_IMAGE):$(TAG) true
# docker cp semantics changed between 1.7 and 1.8, so we cp the file to cwd and rename it.
docker cp $(BUILD_IMAGE):/keepalived.tar.gz rootfs
docker rm -f $(BUILD_IMAGE)
DRY_RUN=true COMPILE_ONLY=true hack/docker_build.sh

keepalived-push:
COMPILE_ONLY=true hack/docker_build.sh

keepalived-cross: ; $(info $(M)...Begin to build container images for multiple platforms.) @ ## Build container images for multiple platforms. Currently, only linux/amd64,linux/arm64 are supported.
DRY_RUN=true COMPILE_ONLY=true hack/docker_build_multiarch.sh

keepalived-cross-push: ; $(info $(M)...Begin to build and push.) @ ## Build and Push.
COMPILE_ONLY=true hack/docker_build_multiarch.sh

push: container
docker push $(REPO)/kube-keepalived-vip:$(TAG)

.PHONY: chart
chart: chart/kube-keepalived-vip-$(CHART_VERSION).tgz
Expand All @@ -43,20 +54,20 @@ chart/kube-keepalived-vip-$(CHART_VERSION).tgz: chart-subst $(shell which helm)
helm lint --strict chart/kube-keepalived-vip
helm package --version '$(CHART_VERSION)' -d chart chart/kube-keepalived-vip

clean:
rm -f kube-keepalived-vip
clean-up:
./hack/cleanup.sh

.PHONY: fmt
fmt:
@go list -f '{{if len .TestGoFiles}}"gofmt -s -l {{.Dir}}"{{end}}' ${GO_LIST_FILES} | xargs -L 1 sh -c
go fmt ./pkg/... ./cmd/...

.PHONY: lint
lint:
@go list -f '{{if len .TestGoFiles}}"golint -min_confidence=0.85 {{.Dir}}/..."{{end}}' ${GO_LIST_FILES} | xargs -L 1 sh -c
@hack/verify-golangci-lint.sh

.PHONY: test
test:
@go test -v -race -tags "$(BUILDTAGS) cgo" ${GO_LIST_FILES}
@go test ./pkg/... ./cmd/... -covermode=atomic -coverprofile=coverage.txt

.PHONY: cover
cover:
Expand All @@ -66,11 +77,7 @@ cover:

.PHONY: vet
vet:
@go vet ${GO_LIST_FILES}
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go vet ./pkg/... ./cmd/...


.PHONY: dep-ensure
dep-ensure:
GO111MODULE=on go mod tidy -v
find vendor -name '*_test.go' -delete
GO111MODULE=on go mod vendor

2 changes: 1 addition & 1 deletion build/Dockerfile → build/keepalived/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

FROM debian:10.13

COPY build.sh /build.sh
COPY build/keepalived/build.sh /build.sh
ARG VERSION

ENV VERSION $VERSION
Expand Down
File renamed without changes.
23 changes: 21 additions & 2 deletions rootfs/Dockerfile → build/kube-keepalived/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,29 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Build
FROM golang:1.21 as build_context

ENV OUTDIR=/out
RUN mkdir -p ${OUTDIR}/usr/local/bin/

WORKDIR /workspace
ADD . /workspace/

RUN make binary
RUN mv /workspace/bin/cmd/kube-keepalived-vip ${OUTDIR}/kube-keepalived-vip



##############
# Final image
#############
FROM rykren/build-keepalived:2.2.8 as keepalived
FROM debian:10.13

ADD keepalived.tar.gz /
COPY . /
COPY --from=build_context /out/kube-keepalived-vip /
COPY --from=keepalived /keepalived/ /keepalived/
COPY build/kube-keepalived /

RUN apt update && apt install -y \
libssl1.1 \
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions hack/cleanup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
docker image prune -f
docker image ls | grep keepalived | grep -v infra | awk '{print $1":"$2}' | xargs docker rmi
54 changes: 54 additions & 0 deletions hack/docker_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

set -ex
set -o pipefail

OPENELB_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${OPENELB_ROOT}/hack/lib/init.sh"

# push to kubesphere with default latest tag
REPO=${REPO:-kubesphere}
VERSION=${VERSION:-2.2.8}
COMPILE_ONLY=${COMPILE_ONLY:-}
if [[ -n "${COMPILE_ONLY:-}" ]]; then
TAG=${TAG:-${VERSION}}
else
TAG=${TAG:-latest}
fi

# If set, just building, no pushing
DRY_RUN=${DRY_RUN:-}

# support other container tools. e.g. podman
CONTAINER_CLI=${CONTAINER_CLI:-docker}
CONTAINER_BUILDER=${CONTAINER_BUILDER:-build}

# use host os and arch as default target os and arch
TARGETOS=${TARGETOS:-$(kube::util::host_os)}
TARGETARCH=${TARGETARCH:-$(kube::util::host_arch)}


if [[ -n "${COMPILE_ONLY:-}" ]]; then
${CONTAINER_CLI} "${CONTAINER_BUILDER}" \
--build-arg TARGETARCH="${TARGETARCH}" \
--build-arg TARGETOS="${TARGETOS}" \
--build-arg VERSION="${VERSION}" \
--output type=docker \
-f build/keepalived/Dockerfile \
-t "${REPO}"/build-keepalived:"${TAG}" .
else
${CONTAINER_CLI} "${CONTAINER_BUILDER}" \
--build-arg "TARGETARCH=${TARGETARCH}" \
--build-arg "TARGETOS=${TARGETOS}" \
--output type=docker \
-f build/kube-keepalived/Dockerfile \
-t "${REPO}"/kube-keepalived-vip:"${TAG}" .
fi

if [[ -z "${DRY_RUN:-}" ]]; then
if [[ -n "${COMPILE_ONLY:-}" ]]; then
${CONTAINER_CLI} push "${REPO}"/build-keepalived:"${TAG}"
else
${CONTAINER_CLI} push "${REPO}"/kube-keepalived-vip:"${TAG}"
fi
fi
51 changes: 51 additions & 0 deletions hack/docker_build_multiarch.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env bash

set -ex
set -o pipefail

OPENELB_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${OPENELB_ROOT}/hack/lib/init.sh"

# push to kubesphere with default latest tag
REPO=${REPO:-kubesphere}
VERSION=${VERSION:-2.2.8}
PUSH=${PUSH:-}
COMPILE_ONLY=${COMPILE_ONLY:-}
if [[ -n "${COMPILE_ONLY:-}" ]]; then
TAG=${TAG:-${VERSION}}
else
TAG=${TAG:-latest}
fi

# support other container tools. e.g. podman
CONTAINER_CLI=${CONTAINER_CLI:-docker}
CONTAINER_BUILDER=${CONTAINER_BUILDER:-"buildx build"}

# If set, just building, no pushing
if [[ -z "${DRY_RUN:-}" ]]; then
PUSH="--push"
fi

# supported platforms
PLATFORMS=linux/amd64,linux/arm64


# shellcheck disable=SC2086 # inteneded splitting of CONTAINER_BUILDER
if [[ -n "${COMPILE_ONLY:-}" ]]; then
${CONTAINER_CLI} ${CONTAINER_BUILDER} \
--platform ${PLATFORMS} \
--build-arg VERSION="${VERSION}" \
${PUSH} \
-f build/keepalived/Dockerfile \
-t "${REPO}"/build-keepalived:"${TAG}" .

else
${CONTAINER_CLI} ${CONTAINER_BUILDER} \
--platform ${PLATFORMS} \
${PUSH} \
-f build/kube-keepalived/Dockerfile \
-t "${REPO}"/kube-keepalived-vip:"${TAG}" .

fi


29 changes: 29 additions & 0 deletions hack/gobuild.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

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

OPENELB_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${OPENELB_ROOT}/hack/lib/init.sh"

VERBOSE=${VERBOSE:-"0"}
if [[ "${VERBOSE}" == "1" ]];then
set -x
fi

OUTPUT_DIR=bin
BUILDPATH=./${1:?"path to build"}
OUT=${OUTPUT_DIR}/${1:?"output path"}


GOBINARY=${GOBINARY:-go}
BUILD_GOOS=${GOOS:-$(go env GOOS)}
BUILD_GOARCH=${GOARCH:-$(go env GOARCH)}
LDFLAGS=$(kube::version::ldflags)

# forgoing -i (incremental build) because it will be deprecated by tool chain.
GOOS=${BUILD_GOOS} CGO_ENABLED=0 GOARCH=${BUILD_GOARCH} ${GOBINARY} build \
-ldflags="${LDFLAGS}" \
-o "${OUT}" \
"${BUILDPATH}"
Loading

0 comments on commit f0099b2

Please sign in to comment.