diff --git a/.gitignore b/.gitignore index 4c098a9aee7..ef7ad72a843 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,6 @@ tests/validation/.idea all.yaml kustomization.yaml kontainer-engine +k3s-images.txt +k3s-airgap-images.tar +data.json diff --git a/Makefile b/Makefile index 276c1f94cd6..028617947be 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ TARGETS := $(shell ls scripts) +DEV_TARGETS := $(shell ls dev-scripts) .dapper: @echo Downloading dapper @@ -16,4 +17,7 @@ $(TARGETS): .dapper .DEFAULT_GOAL := ci -.PHONY: $(TARGETS) +$(DEV_TARGETS): + ./dev-scripts/$@ + +.PHONY: $(TARGETS) $(DEV_TARGETS) diff --git a/dev-scripts/build-local.sh b/dev-scripts/build-local.sh deleted file mode 100755 index ecbf75fe1a4..00000000000 --- a/dev-scripts/build-local.sh +++ /dev/null @@ -1,65 +0,0 @@ -#!/usr/bin/env bash - -# Script to build a Rancher local image with magic for cross-building for different platforms. -# Can be improved to build a multiarch image instead. - -set -eo pipefail -set -x - -SCRIPT_DIR="$(cd "$(dirname "$0")"; pwd)" -source "$SCRIPT_DIR/../scripts/export-config" - -TARGET_OS="${TARGET_OS:-linux}" -GO_BINARY="${GO_BINARY:-$(which go)}" - -if [ -z "${TARGET_REPO}" ]; then - echo "please specify the variable TARGET_REPO" - exit 1 -fi - -# If not defined assume it's being built for the current arch. -# Currently just supporting arm64 and amd64 but could add more in the future. -if [ -z "${TARGET_ARCH}" ]; then - case "$(uname -m)" in - arm64|aarch64) - TARGET_ARCH="arm64" - ;; - *) - TARGET_ARCH="amd64" - ;; - esac -fi - -set -u - -RKE_VERSION="$(grep -m1 'github.com/rancher/rke' "${SCRIPT_DIR}/../go.mod" | awk '{print $2}')" -DEFAULT_VALUES="{\"rke-version\":\"${RKE_VERSION}\"}" - -RANCHER_BINARY="${SCRIPT_DIR}/../bin/rancher" -GOOS="${TARGET_OS}" GOARCH="${TARGET_ARCH}" CGO_ENABLED=0 "${GO_BINARY}" build -tags k8s \ - -ldflags "-X github.com/rancher/rancher/pkg/version.Version=dev -X github.com/rancher/rancher/pkg/version.GitCommit=dev -X github.com/rancher/rancher/pkg/settings.InjectDefaults=$DEFAULT_VALUES -extldflags -static -s" \ - -o "${RANCHER_BINARY}" - -DATA_JSON_FILE="${SCRIPT_DIR}/../bin/data.json" -if [ ! -f "${DATA_JSON_FILE}" ]; then - curl -sLf https://releases.rancher.com/kontainer-driver-metadata/release-v2.9/data.json > "${DATA_JSON_FILE}" -fi - -K3S_AIRGAP_IMAGES_TARBALL="${SCRIPT_DIR}/../bin/k3s-airgap-images.tar" -if [ ! -f "${K3S_AIRGAP_IMAGES_TARBALL}" ]; then - touch "${K3S_AIRGAP_IMAGES_TARBALL}" -fi - -PACKAGE_FOLDER="${SCRIPT_DIR}/../package/" -cp "${RANCHER_BINARY}" "${PACKAGE_FOLDER}" -cp "${DATA_JSON_FILE}" "${PACKAGE_FOLDER}" -cp "${K3S_AIRGAP_IMAGES_TARBALL}" "${PACKAGE_FOLDER}" - -DOCKERFILE="${SCRIPT_DIR}/../package/Dockerfile" -# Always use buildx to make sure the image & the binary architectures match -docker buildx build -t "${TARGET_REPO}" -f "${DOCKERFILE}" \ - --build-arg CATTLE_RANCHER_WEBHOOK_VERSION="${CATTLE_RANCHER_WEBHOOK_VERSION}" \ - --build-arg CATTLE_CSP_ADAPTER_MIN_VERSION="${CATTLE_CSP_ADAPTER_MIN_VERSION}" \ - --build-arg CATTLE_FLEET_VERSION="${CATTLE_FLEET_VERSION}" \ - --build-arg ARCH="${TARGET_ARCH}" \ - "${PACKAGE_FOLDER}" --platform="${TARGET_OS}/${TARGET_ARCH}" diff --git a/dev-scripts/quick b/dev-scripts/quick new file mode 100755 index 00000000000..0fc42de1f6c --- /dev/null +++ b/dev-scripts/quick @@ -0,0 +1,58 @@ +#!/usr/bin/env bash + +## This script builds the Rancher server image exclusively, sans Dapper + +set -eo pipefail +set -x + +# variables +COMMIT=$(git rev-parse --short HEAD) +TAG="${TAG:-$(yq '.env.TAG | sub("-.*", "")' < .github/workflows/pull-request.yml)-${COMMIT}}" +OS="${OS:-linux}" +ARCH="${ARCH:-amd64}" +CATTLE_K3S_VERSION=$(yq '.env.CATTLE_K3S_VERSION' < .github/workflows/pull-request.yml) +CATTLE_KDM_BRANCH=$(yq '.env.CATTLE_KDM_BRANCH' < .github/workflows/pull-request.yml) +RKE_VERSION=$(grep -m1 'github.com/rancher/rke' go.mod | awk '{print $2}') +CATTLE_RANCHER_WEBHOOK_VERSION=$(yq '.webhookVersion' < build.yaml) +CATTLE_CSP_ADAPTER_MIN_VERSION=$(yq '.cspAdapterMinVersion' < build.yaml) +CATTLE_FLEET_VERSION=$(yq '.fleetVersion' < build.yaml) + +# download airgap images and export it to a tarball +if [ ! -f ./k3s-images.txt ]; then + curl -Lf https://github.com/rancher/k3s/releases/download/${CATTLE_K3S_VERSION}/k3s-images.txt -o ./k3s-images.txt +fi +if [ ! -f ./k3s-airgap-images.tar ]; then + AIRGAP_IMAGES=$(grep -e 'docker.io/rancher/mirrored-pause' -e 'docker.io/rancher/mirrored-coredns-coredns' ./k3s-images.txt) + xargs -n1 docker pull <<< "${AIRGAP_IMAGES}" + xargs -n2 docker save -o ./k3s-airgap-images.tar <<< "${AIRGAP_IMAGES}" +fi + +# download kontainer driver metadata +if [ ! -f ./data.json ]; then + curl -sLf https://releases.rancher.com/kontainer-driver-metadata/${CATTLE_KDM_BRANCH}/data.json > ./data.json +fi + +# start the builds +docker buildx build \ + --build-arg VERSION="${TAG}" \ + --build-arg ARCH=${ARCH} \ + --build-arg COMMIT="${COMMIT}" \ + --build-arg RKE_VERSION=${RKE_VERSION} \ + --build-arg CATTLE_RANCHER_WEBHOOK_VERSION=${CATTLE_RANCHER_WEBHOOK_VERSION} \ + --build-arg CATTLE_CSP_ADAPTER_MIN_VERSION=${CATTLE_CSP_ADAPTER_MIN_VERSION} \ + --build-arg CATTLE_FLEET_VERSION=${CATTLE_FLEET_VERSION} \ + --tag rancher/rancher:${TAG} \ + --platform="${OS}/${ARCH}" \ + --file ./package/Dockerfile . + +docker buildx build \ + --build-arg VERSION="${TAG}" \ + --build-arg ARCH=${ARCH} \ + --build-arg RANCHER_TAG=${TAG} \ + --build-arg RANCHER_IMAGE=rancher/rancher:${TAG}-${ARCH} \ + --build-arg COMMIT="${COMMIT}" \ + --build-arg RKE_VERSION=${RKE_VERSION} \ + --build-arg CATTLE_RANCHER_WEBHOOK_VERSION=${CATTLE_RANCHER_WEBHOOK_VERSION} \ + --tag rancher/rancher-agent:${TAG} \ + --platform="${OS}/${ARCH}" \ + --file ./package/Dockerfile.agent . diff --git a/docs/build.md b/docs/build.md index eb3c964b8be..15588599892 100644 --- a/docs/build.md +++ b/docs/build.md @@ -57,7 +57,7 @@ The following are examples of files that often refer to newly added configuratio - [build-server](../scripts/build-server) - [build-agent](../scripts/build-agent) -- [build-local.sh](../dev-scripts/build-local.sh) +- [quick](../dev-scripts/quick) - [Dockerfile](../package/Dockerfile) - [Dockerfile.agent](../package/Dockerfile.agent) - [pkg/settings/setting.go](../pkg/settings/setting.go) @@ -66,4 +66,4 @@ The following are examples of files that often refer to newly added configuratio It's better to follow the standard Kubernetes convention of preferring camelCase keys in the YAML file. -The exported resulting environment variables should be like standard ENV_VARS. \ No newline at end of file +The exported resulting environment variables should be like standard ENV_VARS. diff --git a/docs/development.md b/docs/development.md index 301937b2947..53bb9641ac2 100644 --- a/docs/development.md +++ b/docs/development.md @@ -3,29 +3,20 @@ ## Generate a local container image If you need to test some changes in a container image before they make it to a -pull request, you can use the handy [build-local.sh](../dev-scripts/build-local.sh) -script. +pull request, you can use the handy `make quick` script. This script uses `docker buildx` in order to enable cross-building of different architecture images. To build an image for your current OS and architecture, run from the Rancher project root: ```shell -TARGET_REPO="localhost:5000/my-test-repo/image:tag" dev-scripts/build-local.sh +TAG="localhost:5000/my-test-repo/image:tag" make quick ``` -If you wish to cross-build for a different OS or architecture, set the variables -`TARGET_ARCH` and/or `TARGET_OS`: +If you wish to cross-build for a different architecture, set the variable `ARCH`: ```shell -TARGET_REPO="localhost:5000/my-test-repo/image:tag" \ - TARGET_ARCH="amd64" \ - TARGET_OS="linux" \ - dev-scripts/build-local.sh -``` - -To specify a `go` binary other than the one present in your `PATH`, use the -`GO_BINARY` environment variable: -```shell -GO_BINARY="/opt/go1.18/bin/go" dev-scripts/build-local.sh +TAG="localhost:5000/my-test-repo/image:tag" \ + ARCH="amd64" \ + make quick ``` ## Deploy your custom image via Helm @@ -37,4 +28,4 @@ helm upgrade --install rancher/rancher \ --create-namespace \ --set rancherImage="my-test-repo/image" \ --set rancherImageTag="dev-tag" -``` \ No newline at end of file +``` diff --git a/scripts/quick b/scripts/quick deleted file mode 100755 index 05fb0a60353..00000000000 --- a/scripts/quick +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -set -e - -cd $(dirname $0) - -./build -./package