diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 707f325..56a9c8a 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,6 +38,7 @@ jobs: dist/metadata.yaml dist/cluster-template*.yaml dist/clusterclass-*.yaml + dist/kini-* generate_release_notes: true draft: ${{ contains(github.ref_name, 'rc') }} prerelease: ${{ contains(github.ref_name, 'rc') }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 750fa40..0ce1e61 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -23,3 +23,8 @@ jobs: - name: make test run: make test + + - name: make kini + run: | + make kini + ./bin/kini --help diff --git a/Makefile b/Makefile index 0bafa98..b6bb0f4 100644 --- a/Makefile +++ b/Makefile @@ -206,10 +206,11 @@ release: manifests generate kustomize ## Generate a consolidated YAML with CRDs sed -i 's,volumes: \[\],volumes: $${CAPN_VOLUMES:=[]},' dist/infrastructure-components.yaml .PHONY: dist -dist: release ## Generate release assets. +dist: release kini-release ## Generate release assets. cp templates/clusterclass*.yaml dist/ cp templates/cluster-template*.yaml dist/ cp metadata.yaml dist/ + cp $(LOCALBIN)/kini-* dist/ ##@ Kini @@ -217,10 +218,15 @@ KINI ?= $(LOCALBIN)/kini .PHONY: kini kini: $(LOCALBIN) ## Build kini for development - go build -o $(KINI) ./cmd/exp/kini + ./hack/scripts/kini/build.sh $(KINI) $(TAG) + ln -sf kini $(LOCALBIN)/docker ln -sf kini $(LOCALBIN)/kind +.PHONY: kini-cross +kini-release: $(LOCALBIN) ## Build kini release binaries + ./hack/scripts/kini/build-cross.sh $(LOCALBIN) $(TAG) + ##@ Deployment ifndef ignore-not-found diff --git a/cmd/exp/kini/kini/kini_version.go b/cmd/exp/kini/kini/kini_version.go new file mode 100644 index 0000000..a9d8748 --- /dev/null +++ b/cmd/exp/kini/kini/kini_version.go @@ -0,0 +1,26 @@ +package kini + +import ( + "fmt" + "runtime" + + "github.com/spf13/cobra" + "sigs.k8s.io/kind/pkg/apis/config/defaults" + kindversion "sigs.k8s.io/kind/pkg/cmd/kind/version" +) + +// Set with -X cmd/exp/kini/kini/kini_version.Version=v0.8.2 +var version = "dev" + +func newKiniVersionCmd() *cobra.Command { + return &cobra.Command{ + Use: "version", + Short: "print kini version", + RunE: func(cmd *cobra.Command, args []string) error { + fmt.Printf("kini version %s (%s %s/%s)\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) + fmt.Printf("kind version v%s (default image %s)\n", kindversion.Version(), defaults.Image) + + return nil + }, + } +} diff --git a/cmd/exp/kini/kini/root.go b/cmd/exp/kini/kini/root.go index 6e73247..158d624 100644 --- a/cmd/exp/kini/kini/root.go +++ b/cmd/exp/kini/kini/root.go @@ -75,5 +75,7 @@ kini can also be used with an existing kind binary. You can do this as follows: docker.NewCmd(), ) + cmd.AddCommand(newKiniVersionCmd()) + return cmd } diff --git a/docs/book/src/get-kini.sh b/docs/book/src/get-kini.sh new file mode 120000 index 0000000..e8fb62c --- /dev/null +++ b/docs/book/src/get-kini.sh @@ -0,0 +1 @@ +../../../hack/scripts/kini/get-kini.sh \ No newline at end of file diff --git a/hack/scripts/kini/build-cross.sh b/hack/scripts/kini/build-cross.sh new file mode 100755 index 0000000..bfa4662 --- /dev/null +++ b/hack/scripts/kini/build-cross.sh @@ -0,0 +1,11 @@ +#!/bin/bash -e + +# Usage: $0 + +DIR="$(dirname "$(realpath "$0")")" +OUT="$(realpath "${1}")" + +# build for each platform +GOOS=linux GOARCH=amd64 "${DIR}/build.sh" "${OUT}/kini-linux-amd64" "${2}" +GOOS=linux GOARCH=arm64 "${DIR}/build.sh" "${OUT}/kini-linux-arm64" "${2}" +GOOS=darwin GOARCH=arm64 "${DIR}/build.sh" "${OUT}/kini-darwin-arm64" "${2}" diff --git a/hack/scripts/kini/build.sh b/hack/scripts/kini/build.sh new file mode 100755 index 0000000..3d4ba60 --- /dev/null +++ b/hack/scripts/kini/build.sh @@ -0,0 +1,16 @@ +#!/bin/bash -e + +# Usage: $0 + +DIR="$(dirname "$(realpath "$0")")" + +( + cd "${DIR}/../../../" + export CGO_ENABLED=0 + + (set -x; go build -o "${1}" \ + -ldflags "-s -w -X=github.com/lxc/cluster-api-provider-incus/cmd/exp/kini/kini.version=$2" \ + ./cmd/exp/kini) + + (cd "$(dirname "${1}")"; sha256sum "$(basename "${1}")" > "${1}.sha256sum") +) diff --git a/hack/scripts/kini/get-kini.sh b/hack/scripts/kini/get-kini.sh new file mode 100755 index 0000000..b906e8d --- /dev/null +++ b/hack/scripts/kini/get-kini.sh @@ -0,0 +1,55 @@ +#!/bin/bash -e + +# Usage: +# +# 1). Install latest version +# $ curl https://capn.linuxcontainers.org/get-kini.sh | bash -x +# +# 2). Install in local directory (without root) +# $ curl https://capn.linuxcontainers.org/get-kini.sh | SUDO= KINI_INSTALL_DIR=~/.local/bin bash +# +# 3). Install specific version +# $ curl https://capn.linuxcontainers.org/get-kini.sh | KINI_DOWNLOAD_URL=https://github.com/lxc/cluster-api-provider/releases/v0.8.2/download bash + +set -eu + +KINI_INSTALL_DIR="${KINI_INSTALL_DIR:-/usr/local/bin}" +KINI_DOWNLOAD_URL="${KINI_DOWNLOAD_URL:-https://github.com/lxc/cluster-api-provider/releases/latest/download}" + +# infer SUDO, if not running as root +SUDO="${SUDO:-sudo}" +if [ "$(id -u)" == "0" ]; then SUDO=; fi + +# infer OS (linux, darwin) +case "$(uname)" in + Linux) OS=linux ;; + Darwin) OS=darwin ;; + *) echo "Unsupported OS $(uname)"; exit 1 ;; +esac + +# infer ARCH (amd64, arm64) +case "$(uname -m)" in + x86_64|amd64) ARCH=amd64 ;; + aarch64|arm64) ARCH=arm64 ;; + *) echo "Unsupported ARCH $(uname -m)"; exit 1 ;; +esac + +echo "Downloading ${KINI_DOWNLOAD_URL}/kini-${OS}-${ARCH}" + +# download kini binary +( + set -x + mkdir -p "/tmp/kini$$" + curl "${KINI_DOWNLOAD_URL}/kini-${OS}-${ARCH}" -f -o "/tmp/kini$$/kini" + + chmod +x "/tmp/kini$$/kini" + + "/tmp/kini$$/kini" version + + $SUDO mkdir -p "${KINI_INSTALL_DIR}" + $SUDO mv "/tmp/kini$$/kini" "${KINI_INSTALL_DIR}/kini" + rm -rf "/tmp/kini$$" +) + +kini version +echo "Successfully downloaded kini at ${KINI_INSTALL_DIR}" diff --git a/internal/exp/kini/setup.go b/internal/exp/kini/setup.go index 3efb7af..a4db0e4 100644 --- a/internal/exp/kini/setup.go +++ b/internal/exp/kini/setup.go @@ -29,7 +29,7 @@ func SetupEnvironment(ctx context.Context, docker bool, kind bool) (string, func return nil } - self, err := filepath.Abs(os.Args[0]) + self, err := os.Executable() if err != nil { _ = cleanup() return "", nil, fmt.Errorf("failed to identity absolute path to %q: %w", os.Args[0], err)