Skip to content

Commit

Permalink
Running of containerized commands refactored (#2377)
Browse files Browse the repository at this point in the history
* running of build & docs containers refactored

* A `run` rule fixed in Makefile

* build/run_container.sh + minor fixes in Makefile

Signed-off-by: Sergey Aksenov <sergey.aksenov@veeam.com>

* trivial review fixes

Signed-off-by: Sergey Aksenov <sergey.aksenov@veeam.com>

* review comments fixed

Signed-off-by: Sergey Aksenov <sergey.aksenov@veeam.com>

---------

Signed-off-by: Sergey Aksenov <sergey.aksenov@veeam.com>
  • Loading branch information
k0taperk0t committed Oct 6, 2023
1 parent 4a26467 commit 4301258
Show file tree
Hide file tree
Showing 2 changed files with 168 additions and 83 deletions.
110 changes: 27 additions & 83 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ REGISTRY ?= kanisterio
# Which architecture to build - see $(ALL_ARCH) for options.
ARCH ?= amd64

# Which platform to build.
PLATFORM ?= linux/$(ARCH)

# This version-strategy uses git tags to set the version string
VERSION := $(shell git describe --tags --always --dirty)
#
Expand All @@ -44,8 +41,6 @@ PWD := $$(pwd)
# Whether to build inside a containerized build environment
DOCKER_BUILD ?= "true"

DOCKER_CONFIG ?= "$(HOME)/.docker"

# Mention the vm-driver that should be used to install OpenShift
vm-driver ?= "kvm"

Expand All @@ -65,11 +60,6 @@ IMAGE_NAME := $(BIN)

IMAGE := $(REGISTRY)/$(IMAGE_NAME)

BUILD_IMAGE ?= ghcr.io/kanisterio/build:v0.0.24

# tag 0.1.0 is, 0.0.1 (latest) + gh + aws + helm binary
DOCS_BUILD_IMAGE ?= ghcr.io/kanisterio/docker-sphinx:0.2.0

DOCS_RELEASE_BUCKET ?= s3://docs.kanister.io

GITHUB_TOKEN ?= ""
Expand Down Expand Up @@ -103,40 +93,25 @@ all-push: $(addprefix push-, $(ALL_ARCH))
build: bin/$(ARCH)/$(BIN)

build-controller:
@$(MAKE) run CMD='-c " \
@$(MAKE) run CMD=" \
goreleaser build --id $(BIN) --rm-dist --debug --snapshot \
&& cp dist/$(BIN)_linux_$(ARCH)_*/$(BIN) bin/$(ARCH)/$(BIN) \
"'
"

bin/$(ARCH)/$(BIN):
@echo "building: $@"
@$(MAKE) run CMD='-c " \
@$(MAKE) run CMD=" \
GOARCH=$(ARCH) \
VERSION=$(VERSION) \
PKG=$(PKG) \
BIN=$(BIN) \
GOBORING=$(GOBORING) \
./build/build.sh \
"'
"
# Example: make shell CMD="-c 'date > datefile'"
shell: build-dirs
@echo "launching a shell in the containerized build environment"
@docker run \
--platform $(PLATFORM) \
-ti \
--rm \
--privileged \
--net host \
-v "$(PWD)/.go/pkg:/go/pkg" \
-v "$(PWD)/.go/cache:/go/.cache" \
-v "${HOME}/.kube:/root/.kube" \
-v "$(PWD):/go/src/$(PKG)" \
-v "$(PWD)/bin/$(ARCH):/go/bin" \
-v "$(DOCKER_CONFIG):/root/.docker" \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/sh
@PWD=$(PWD) ARCH=$(ARCH) PKG=$(PKG) GITHUB_TOKEN=$(GITHUB_TOKEN) CMD="/bin/bash $(CMD)" /bin/bash ./build/run_container.sh shell

DOTFILE_IMAGE = $(subst :,_,$(subst /,_,$(IMAGE))-$(VERSION))

Expand Down Expand Up @@ -180,39 +155,31 @@ deploy: release-controller .deploy-$(DOTFILE_IMAGE)
@kubectl apply -f .deploy-$(DOTFILE_IMAGE)

test: build-dirs
@$(MAKE) run CMD='-c "./build/test.sh $(SRC_DIRS)"'
@$(MAKE) run CMD="./build/test.sh $(SRC_DIRS)"

helm-test: build-dirs
@$(MAKE) run CMD='-c "./build/helm-test.sh $(SRC_DIRS)"'
@$(MAKE) run CMD="./build/helm-test.sh $(SRC_DIRS)"

integration-test: build-dirs
@$(MAKE) run CMD='-c "./build/integration-test.sh short"'
@$(MAKE) run CMD="./build/integration-test.sh short"

openshift-test:
@/bin/bash ./build/integration-test.sh openshift $(ocp_version)

golint:
@$(MAKE) run CMD='-c "./build/golint.sh"'
@$(MAKE) run CMD="./build/golint.sh"

codegen:
@$(MAKE) run CMD='-c "./build/codegen.sh"'
@$(MAKE) run CMD="./build/codegen.sh"

DOCS_CMD = "cd docs && make clean && \
doc8 --max-line-length 90 --ignore D000 . && \
make spelling && make html \
"
make spelling && make html"

docs:
ifeq ($(DOCKER_BUILD),"true")
@echo "running DOCS_CMD in the containerized build environment"
@docker run \
--platform $(PLATFORM) \
--entrypoint '' \
--rm \
-v "$(PWD):/repo" \
-w /repo \
$(DOCS_BUILD_IMAGE) \
/bin/bash -c $(DOCS_CMD)
@PWD=$(PWD) ARCH=$(ARCH) CMD=$(DOCS_CMD) /bin/bash ./build/run_container.sh docs
else
@/bin/bash -c $(DOCS_CMD)
endif
Expand All @@ -221,20 +188,12 @@ API_DOCS_CMD = "gen-crd-api-reference-docs \
-config docs/api_docs/config.json \
-api-dir ./pkg/apis/cr/v1alpha1 \
-template-dir docs/api_docs/template \
-out-file API.md \
"
-out-file API.md"

crd_docs:
ifeq ($(DOCKER_BUILD),"true")
@echo "running API_DOCS_CMD in the containerized build environment"
@docker run \
--platform $(PLATFORM) \
--entrypoint '' \
--rm \
-v "$(PWD):/repo" \
-w /repo \
$(BUILD_IMAGE) \
/bin/bash -c $(API_DOCS_CMD)
@PWD=$(PWD) ARCH=$(ARCH) CMD=$(API_DOCS_CMD) /bin/bash ./build/run_container.sh crd_docs
else
@/bin/bash -c $(API_DOCS_CMD)
endif
Expand All @@ -246,22 +205,7 @@ build-dirs:
run: build-dirs
ifeq ($(DOCKER_BUILD),"true")
@echo "running CMD in the containerized build environment"
@docker run \
--platform $(PLATFORM) \
--rm \
--net host \
-e GITHUB_TOKEN=$(GITHUB_TOKEN) \
-v "${HOME}/.kube:/root/.kube" \
-v "$(PWD)/.go/pkg:/go/pkg" \
-v "$(PWD)/.go/cache:/go/.cache" \
-v "$(PWD):/go/src/$(PKG)" \
-v "$(PWD)/bin/$(ARCH):/go/bin" \
-v "$(PWD)/.go/std/$(ARCH):/usr/local/go/pkg/linux_$(ARCH)" \
-v "$(DOCKER_CONFIG):/root/.docker" \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /go/src/$(PKG) \
$(BUILD_IMAGE) \
/bin/bash $(CMD)
@PWD=$(PWD) ARCH=$(ARCH) PKG=$(PKG) GITHUB_TOKEN=$(GITHUB_TOKEN) CMD="$(CMD)" /bin/bash ./build/run_container.sh run
else
@/bin/bash $(CMD)
endif
Expand Down Expand Up @@ -292,28 +236,28 @@ release-helm:
@/bin/bash ./build/release_helm.sh $(VERSION)

gorelease:
@$(MAKE) run CMD='-c "./build/gorelease.sh"'
@$(MAKE) run CMD="./build/gorelease.sh"

release-snapshot:
@$(MAKE) run CMD='-c "GORELEASER_CURRENT_TAG=v9.99.9-dev goreleaser --debug release --rm-dist --snapshot --timeout=60m0s"'
@$(MAKE) run CMD="GORELEASER_CURRENT_TAG=v9.99.9-dev goreleaser --debug release --rm-dist --snapshot --timeout=60m0s"

go-mod-download:
@$(MAKE) run CMD='-c "go mod download"'
@$(MAKE) run CMD="go mod download"

start-kind:
@$(MAKE) run CMD='-c "./build/local_kubernetes.sh start_localkube"'
@$(MAKE) run CMD="./build/local_kubernetes.sh start_localkube"

tiller:
@/bin/bash ./build/init_tiller.sh

install-minio:
@$(MAKE) run CMD='-c "./build/minio.sh install_minio"'
@$(MAKE) run CMD="./build/minio.sh install_minio"

install-csi-hostpath-driver:
@$(MAKE) run CMD='-c "./build/local_kubernetes.sh install_csi_hostpath_driver"'
@$(MAKE) run CMD="./build/local_kubernetes.sh install_csi_hostpath_driver"

uninstall-minio:
@$(MAKE) run CMD='-c "./build/minio.sh uninstall_minio"'
@$(MAKE) run CMD="./build/minio.sh uninstall_minio"

start-minishift:
@/bin/bash ./build/minishift.sh start_minishift $(vm-driver)
Expand All @@ -322,21 +266,21 @@ stop-minishift:
@/bin/bash ./build/minishift.sh stop_minishift

stop-kind:
@$(MAKE) run CMD='-c "./build/local_kubernetes.sh stop_localkube"'
@$(MAKE) run CMD="./build/local_kubernetes.sh stop_localkube"

check:
@./build/check.sh

go-mod-tidy:
@$(MAKE) run CMD='-c "./build/gomodtidy.sh"'
@$(MAKE) run CMD="./build/gomodtidy.sh"


install-crds: ## Install CRDs into the K8s cluster specified in ~/.kube/config.
@$(MAKE) run CMD='-c "kubectl apply -f pkg/customresource/"'
@$(MAKE) run CMD="kubectl apply -f pkg/customresource/"

uninstall-crds: ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
@$(MAKE) run CMD='-c "kubectl delete -f pkg/customresource/"'
@$(MAKE) run CMD="kubectl delete -f pkg/customresource/"

manifests: ## Generates CustomResourceDefinition objects.
@$(MAKE) run CMD='-c "./build/generate_crds.sh ${CONTROLLER_TOOLS_VERSION}"'
@$(MAKE) run CMD="./build/generate_crds.sh ${CONTROLLER_TOOLS_VERSION}"

141 changes: 141 additions & 0 deletions build/run_container.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#!/bin/bash

# Copyright 2019 The Kanister Authors.
#
# Copyright 2016 The Kubernetes 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

PWD="${PWD:-$(pwd)}"

DOCS_BUILD_IMAGE="${DOCS_BUILD_IMAGE:-ghcr.io/kanisterio/docker-sphinx:0.2.0}"
BUILD_IMAGE="${BUILD_IMAGE:-ghcr.io/kanisterio/build:v0.0.24}"
PKG="${PKG:-github.com/kanisterio/kanister}"

ARCH="${ARCH:-amd64}"
PLATFORM="linux/${ARCH}"

check_param() {
local arg_name=${1}
local value_of_arg=""
eval value_of_arg=\$$arg_name
if [ -z "${value_of_arg}" ]; then
echo "$arg_name must be set"
exit 1
fi
}


run_build_container() {
local github_token="${GITHUB_TOKEN:-}"
local extra_params="${EXTRA_PARAMS:-}"

local cmd=(/bin/bash -c "$CMD")
if [ -z "${CMD}" ]; then
cmd=(/bin/bash)
fi

docker run \
--platform ${PLATFORM} \
${extra_params} \
--rm \
--net host \
-e GITHUB_TOKEN="${github_token}" \
-v "${HOME}/.kube:/root/.kube" \
-v "${PWD}/.go/pkg:/go/pkg" \
-v "${PWD}/.go/cache:/go/.cache" \
-v "${PWD}:/go/src/${PKG}" \
-v "${PWD}/bin/${ARCH}:/go/bin" \
-v "${PWD}/.go/std/${ARCH}:/usr/local/go/pkg/linux_${ARCH}" \
-v "${HOME}/.docker:/root/.docker" \
-v /var/run/docker.sock:/var/run/docker.sock \
-w /go/src/${PKG} \
${BUILD_IMAGE} \
"${cmd[@]}"
}

run_docs_container() {
check_param "IMAGE"
check_param "CMD"

docker run \
--platform ${PLATFORM} \
--entrypoint '' \
--rm \
-v "${PWD}:/repo" \
-w /repo \
${IMAGE} \
/bin/bash -c "${CMD}"
}

run() {
check_param "CMD"

echo "Running command within build container..."
run_build_container
}

shell() {
echo "Running build container in interactive shell mode..."
EXTRA_PARAMS="-ti" run_build_container
}

docs() {
check_param "CMD"

echo "Running docs container..."
IMAGE=${DOCS_BUILD_IMAGE} run_docs_container
}

crd_docs() {
check_param "CMD"

echo "Running crd docs container..."
IMAGE=${BUILD_IMAGE} run_docs_container
}

usage() {
cat <<EOM
Usage: ${0} <operation>
Where operation is one of the following:
build: run some command within a build container
crd_docs: build API docs within a container
docs: build docs within a container
shell: run build container in interactive shell mode
EOM
exit 1
}

[ ${#@} -gt 0 ] || usage
case "${1}" in
# Alphabetically sorted
crd_docs)
time -p crd_docs
;;
docs)
time -p docs
;;
run)
time -p run
;;
shell)
time -p shell
;;
*)
usage
exit 1
esac

0 comments on commit 4301258

Please sign in to comment.