diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index affb357..b8fc5a9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -52,7 +52,7 @@ jobs: - name: Build creation run: | - make build + CGO_ENABLED=0 GOOS=linux GOARCH=$(TARGETARCH) go build -v -o rollouts-plugin-trafficrouter-consul ./ go-mod-tidy: name: Go mod tidy diff --git a/.go-version b/.go-version new file mode 100644 index 0000000..6e83421 --- /dev/null +++ b/.go-version @@ -0,0 +1 @@ +1.21.6 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index dd81e6a..457a021 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,18 +1,110 @@ -FROM --platform=$BUILDPLATFORM golang:1.21.6 as builder +# This Dockerfile contains multiple targets. +# Use 'docker build --target= .' to build one. +# +# Every target has a BIN_NAME argument that must be provided via --build-arg=BIN_NAME= +# when building. -ENV GO111MODULE=on + +# =================================== +# +# Non-release images. +# +# =================================== + +# go-discover builds the discover binary (which we don't currently publish +# either). +FROM golang:1.19.2-alpine as go-discover +RUN CGO_ENABLED=0 go install github.com/hashicorp/go-discover/cmd/discover@49f60c093101c9c5f6b04d5b1c80164251a761a6 + +# dev copies the binary from a local build +# ----------------------------------- +# BIN_NAME is a requirement in the hashicorp docker github action +FROM alpine:3.17 AS dev + +# NAME and VERSION are the name of the software in releases.hashicorp.com +# and the version to download. Example: NAME=consul VERSION=1.2.3. +ARG BIN_NAME=rollouts-plugin-trafficrouter-consul +ARG VERSION ARG TARGETARCH +ARG TARGETOS + +LABEL name=${BIN_NAME} \ + maintainer="Team Consul Kubernetes " \ + vendor="HashiCorp" \ + version=${VERSION} \ + release=${VERSION} \ + summary="rollouts-plugin-trafficrouter-consul is a plugin for Argo Rollouts." \ + description="rollouts-plugin-trafficrouter-consul is a plugin for Argo Rollouts." + +# Set ARGs as ENV so that they can be used in ENTRYPOINT/CMD +ENV BIN_NAME=${BIN_NAME} +ENV VERSION=${VERSION} + +RUN apk add --no-cache ca-certificates libcap openssl su-exec iputils libc6-compat iptables + +# Create a non-root user to run the software. +RUN addgroup ${BIN_NAME} && \ + adduser -S -G ${BIN_NAME} 100 -WORKDIR /app +COPY --from=go-discover /go/bin/discover /bin/ +COPY pkg/bin/linux_${TARGETARCH}/${BIN_NAME} /bin -COPY . . +USER 100 +CMD /bin/${BIN_NAME} -RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o rollouts-plugin-trafficrouter-consul . -FROM alpine:3.19.0 +# =================================== +# +# Release images. +# +# =================================== + +# default release image +# ----------------------------------- +# This Dockerfile creates a production release image for the project. This +# downloads the release from releases.hashicorp.com and therefore requires that +# the release is published before building the Docker image. +# +# We don't rebuild the software because we want the exact checksums and +# binary signatures to match the software and our builds aren't fully +# reproducible currently. +FROM alpine:3.17 AS release-default + +ARG BIN_NAME=rollouts-plugin-trafficrouter-consul +ARG PRODUCT_VERSION + +LABEL name=${BIN_NAME} \ + maintainer="Team Consul Kubernetes " \ + vendor="HashiCorp" \ + version=${PRODUCT_VERSION} \ + release=${PRODUCT_VERSION} \ + summary="rollouts-plugin-trafficrouter-consul is a plugin for Argo Rollouts" \ + description="rollouts-plugin-trafficrouter-consul is a plugin for Argo Rollouts." + +# Set ARGs as ENV so that they can be used in ENTRYPOINT/CMD +ENV BIN_NAME=${BIN_NAME} +ENV VERSION=${PRODUCT_VERSION} + +RUN apk add --no-cache ca-certificates libcap openssl su-exec iputils libc6-compat iptables + +# TARGETOS and TARGETARCH are set automatically when --platform is provided. +ARG TARGETOS ARG TARGETARCH -USER 999 +# Create a non-root user to run the software. +RUN addgroup ${BIN_NAME} && \ + adduser -S -G ${BIN_NAME} 100 + +COPY --from=go-discover /go/bin/discover /bin/ +COPY dist/${TARGETOS}/${TARGETARCH}/${BIN_NAME} /bin/ + +USER 100 +CMD /bin/${BIN_NAME} -COPY --from=builder /app/rollouts-plugin-trafficrouter-consul /bin/ \ No newline at end of file +# =================================== +# +# Set default target to 'dev'. +# +# =================================== +FROM dev \ No newline at end of file diff --git a/build-scripts/version.sh b/build-scripts/version.sh new file mode 100755 index 0000000..6affd15 --- /dev/null +++ b/build-scripts/version.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + + +version_file=$1 +version=$(awk '$1 == "Version" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}") +prerelease=$(awk '$1 == "VersionPrerelease" && $2 == "=" { gsub(/"/, "", $3); print $3 }' < "${version_file}") + +if [ -n "$prerelease" ]; then + echo "${version}-${prerelease}" +else + echo "${version}" +fi diff --git a/build-support/functions/00-vars.sh b/build-support/functions/00-vars.sh new file mode 100644 index 0000000..ab74803 --- /dev/null +++ b/build-support/functions/00-vars.sh @@ -0,0 +1,2 @@ +# Whether to colorize shell output +COLORIZE=${COLORIZE-1} \ No newline at end of file diff --git a/build-support/functions/10-util.sh b/build-support/functions/10-util.sh new file mode 100644 index 0000000..baf67b9 --- /dev/null +++ b/build-support/functions/10-util.sh @@ -0,0 +1,152 @@ +function status { + if test "${COLORIZE}" -eq 1; then + tput bold + tput setaf 4 + fi + + echo "$@" + + if test "${COLORIZE}" -eq 1; then + tput sgr0 + fi +} + +function status_stage { + if test "${COLORIZE}" -eq 1; then + tput bold + tput setaf 2 + fi + + echo "$@" + + if test "${COLORIZE}" -eq 1; then + tput sgr0 + fi +} + +function is_set { + # Arguments: + # $1 - string value to check its truthiness + # + # Return: + # 0 - is truthy (backwards I know but allows syntax like `if is_set ` to work) + # 1 - is not truthy + + local val=$(tr '[:upper:]' '[:lower:]' <<<"$1") + case $val in + 1 | t | true | y | yes) + return 0 + ;; + *) + return 1 + ;; + esac +} + +function sed_i { + if test "$(uname)" == "Darwin"; then + sed -i '' "$@" + return $? + else + sed -i "$@" + return $? + fi +} + +function prepare_dev { + # Arguments: + # $1 - Path to top level Consul K8s source + # $2 - The version of the release + # + # Returns: + # 0 - success + # * - error + + local curDir=$1 + local nextReleaseVersion=$2 + + echo "prepare_dev: dir:${curDir} plugin:${nextReleaseVersion} mode:dev" + set_version "${curDir}" "${nextReleaseVersion}" "dev" + + return 0 +} + +function prepare_release { + # Arguments: + # $1 - Path to top level Consul K8s source + # $2 - The version of the release + # $3 - The pre-release version + # + # + # Returns: + # 0 - success + # * - error + + local curDir=$1 + local version=$2 + local prereleaseVersion=$3 + + echo "prepare_release: dir:${curDir} plugin:${version} prerelease_version (can be empty):${prereleaseVersion}" + set_version "${curDir}" "${version}" "${prereleaseVersion}" +} + +function set_version { + # Arguments: + # $1 - Path to top level Consul K8s source + # $2 - The version of the release + # $3 - The pre-release version + # + # + # Returns: + # 0 - success + # * - error + + if ! test -d "$1"; then + err "ERROR: '$1' is not a directory. prepare_release must be called with the path to a git repo as the first argument" + return 1 + fi + + if test -z "$2"; then + err "ERROR: The version specified was empty" + return 1 + fi + + local sdir="$1" + local vers="$2" + local prevers="$3" + + status_stage "==> Updating "${sdir}/pkg/version/version.go" with version info: ${vers} ${prevers}" + if ! update_version "${sdir}/pkg/version/version.go" "${vers}" "${prevers}"; then + return 1 + fi + + return 0 +} + +function update_version { + # Arguments: + # $1 - Path to the version file + # $2 - Version string + # $3 - PreRelease version (if unset will become an empty string) + # + # Returns: + # 0 - success + # * - error + + if ! test -f "$1"; then + err "ERROR: '$1' is not a regular file. update_version must be called with the path to a go version file" + return 1 + fi + + if test -z "$2"; then + err "ERROR: The version specified was empty" + return 1 + fi + + local vfile="$1" + local version="$2" + local prerelease="$3" + + sed_i ${SED_EXT} -e "s/\(Version[[:space:]]*=[[:space:]]*\)\"[^\"]*\"/\1\"${version}\"/g" -e "s/\(VersionPrerelease[[:space:]]*=[[:space:]]*\)\"[^\"]*\"/\1\"${prerelease}\"/g" "${vfile}" + return $? +} \ No newline at end of file diff --git a/build-support/functions/20-build.sh b/build-support/functions/20-build.sh new file mode 100644 index 0000000..83e6d04 --- /dev/null +++ b/build-support/functions/20-build.sh @@ -0,0 +1,206 @@ +function build_post { + # Arguments + # $1 - Path to the top level source + # $2 - Subdirectory under pkg/bin (Optional) + # + # Returns: + # 0 - success + # * - error + # + # Notes: + # pkg/bin is where to place binary packages + # pkg.bin.new is where the just built binaries are located + # bin is where to place the local systems versions + + if ! test -d "$1" + then + err "ERROR: '$1' is not a directory. build_post must be called with the path to the top level source as the first argument'" + return 1 + fi + + local sdir="$1" + + local extra_dir_name="$2" + local extra_dir="" + + if test -n "${extra_dir_name}" + then + extra_dir="${extra_dir_name}/" + fi + + pushd "${sdir}" > /dev/null + + # recreate the pkg dir + rm -r pkg/bin/${extra_dir}* 2> /dev/null + mkdir -p pkg/bin/${extra_dir} 2> /dev/null + + # move all files in pkg.new into pkg + cp -r pkg.bin.new/${extra_dir}* pkg/bin/${extra_dir} + rm -r pkg.bin.new + + DEV_PLATFORM="./pkg/bin/${extra_dir}$(go env GOOS)_$(go env GOARCH)" + for F in $(find ${DEV_PLATFORM} -mindepth 1 -maxdepth 1 -type f 2>/dev/null) + do + # recreate the bin dir + rm -r bin/* 2> /dev/null + mkdir -p bin 2> /dev/null + + cp ${F} bin/ + cp ${F} ${MAIN_GOPATH}/bin + done + + popd > /dev/null + + return 0 +} + +function build_local { + # Arguments: + # $1 - Path to the top level source + # $2 - Space separated string of OSes to build. If empty will use env vars for determination. + # $3 - Space separated string of architectures to build. If empty will use env vars for determination. + # $4 - Output binary name + # $5 - Subdirectory to put binaries in under pkg/bin (optional) + # + # Returns: + # 0 - success + # * - error + # + # Note: + # The GOLDFLAGS, GOEXPERIMENT, and GOTAGS environment variables will be used if set + # If the CONSUL_DEV environment var is truthy only the local platform/architecture is built. + # If the XC_OS or the XC_ARCH environment vars are present then only those platforms/architectures + # will be built. Otherwise all supported platform/architectures are built + # The NOGOX environment variable will be used if present. This will prevent using gox and instead + # build with go install. + # The GOXPARALLEL environment variable is used if set + + if [ "${GOTAGS:-}" == "fips" ]; then + CGO_ENABLED=1 + else + CGO_ENABLED=0 + fi + + echo "GOEXPERIMENT: $GOEXPERIMENT, GOTAGS: $GOTAGS CGO_ENABLED: $CGO_ENABLED" >> ~/debug.txt + + if ! test -d "$1" + then + err "ERROR: '$1' is not a directory. build must be called with the path to the top level source as the first argument'" + return 1 + fi + + local sdir="$1" + local build_os="$2" + local build_arch="$3" + local bin_name="$4" + local extra_dir_name="$5" + local extra_dir="" + + if test -n "${extra_dir_name}" + then + extra_dir="${extra_dir_name}/" + fi + + pushd ${sdir} > /dev/null + if is_set "${CONSUL_DEV}" + then + if test -z "${XC_OS}" + then + XC_OS=$(go env GOOS) + fi + + if test -z "${XC_ARCH}" + then + XC_ARCH=$(go env GOARCH) + fi + fi + XC_OS=${XC_OS:-"solaris darwin freebsd linux windows"} + XC_ARCH=${XC_ARCH:-"386 amd64 arm arm64"} + + if test -z "${build_os}" + then + build_os="${XC_OS}" + fi + + if test -z "${build_arch}" + then + build_arch="${XC_ARCH}" + fi + + local use_gox=1 + is_set "${NOGOX}" && use_gox=0 + which gox > /dev/null || use_gox=0 + + status_stage "==> Building ${bin_name} - OSes: ${build_os}, Architectures: ${build_arch}" + mkdir pkg.bin.new 2> /dev/null + if is_set "${use_gox}" + then + status "Using gox for concurrent compilation" + + CGO_ENABLED=${CGO_ENABLED} GOEXPERIMENT=${GOEXPERIMENT} gox \ + -os="${build_os}" \ + -arch="${build_arch}" \ + -ldflags="${GOLDFLAGS}" \ + -parallel="${GOXPARALLEL:-"-1"}" \ + -output "pkg.bin.new/${extra_dir}{{.OS}}_{{.Arch}}/${bin_name}" \ + -tags="${GOTAGS}" \ + . + + if test $? -ne 0 + then + err "ERROR: Failed to build plugin" + rm -r pkg.bin.new + return 1 + fi + else + status "Building sequentially with go build" + for os in ${build_os} + do + for arch in ${build_arch} + do + outdir="pkg.bin.new/${extra_dir}${os}_${arch}" + osarch="${os}/${arch}" + if test "${osarch}" == "darwin/arm" -o "${osarch}" == "freebsd/arm64" -o "${osarch}" == "windows/arm" -o "${osarch}" == "windows/arm64" + then + continue + fi + + if test "${os}" == "solaris" -a "${arch}" != "amd64" + then + continue + fi + + echo "---> ${osarch}" + + + mkdir -p "${outdir}" + GOBIN_EXTRA="" + if test "${os}" != "$(go env GOHOSTOS)" -o "${arch}" != "$(go env GOHOSTARCH)" + then + GOBIN_EXTRA="${os}_${arch}/" + fi + if test "${os}" == "windows" + then + OS_BIN_EXTENSION=".exe" + else + OS_BIN_EXTENSION="" + fi + CGO_ENABLED=${CGO_ENABLED} GOEXPERIMENT=${GOEXPERIMENT} GOOS=${os} GOARCH=${arch} go build -ldflags "${GOLDFLAGS}" -tags "${GOTAGS}" -o "${outdir}/${bin_name}" + if test $? -ne 0 + then + err "ERROR: Failed to build plugin for ${osarch}" + rm -r pkg.bin.new + return 1 + fi + done + done + fi + + build_post "${sdir}" "${extra_dir_name}" + if test $? -ne 0 + then + err "ERROR: Failed postprocessing plugin binaries" + return 1 + fi + return 0 +} \ No newline at end of file diff --git a/build-support/scripts/build-local.sh b/build-support/scripts/build-local.sh new file mode 100755 index 0000000..137653c --- /dev/null +++ b/build-support/scripts/build-local.sh @@ -0,0 +1,117 @@ +#!/bin/bash +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +SCRIPT_NAME="$(basename ${BASH_SOURCE[0]})" +pushd $(dirname ${BASH_SOURCE[0]}) > /dev/null +SCRIPT_DIR=$(pwd) +pushd ../.. > /dev/null +SOURCE_DIR=$(pwd) +popd > /dev/null +pushd ../functions > /dev/null +FN_DIR=$(pwd) +popd > /dev/null +popd > /dev/null + +source "${SCRIPT_DIR}/functions.sh" + +function usage { +cat <<-EOF +Usage: ${SCRIPT_NAME} [] + +Description: + This script will build the rollouts-plugin-trafficrouter-consul binary on the local system. + All the requisite tooling must be installed for this to be + successful. + +Options: + + -s | --source DIR Path to source to build. + Defaults to "${SOURCE_DIR}" + + -o | --os OSES Space separated string of OS + platforms to build. + + -a | --arch ARCH Space separated string of + architectures to build. + + --fips FIPS Whether to use FIPS cryptography. + + -h | --help Print this help text. +EOF +} + +function err_usage { + err "$1" + err "" + err "$(usage)" +} + +function main { + declare sdir="${SOURCE_DIR}" + declare build_os="" + declare build_arch="" + + + while test $# -gt 0 + do + case "$1" in + -h | --help ) + usage + return 0 + ;; + -s | --source ) + if test -z "$2" + then + err_usage "ERROR: option -s/--source requires an argument" + return 1 + fi + + if ! test -d "$2" + then + err_usage "ERROR: '$2' is not a directory and not suitable for the value of -s/--source" + return 1 + fi + + sdir="$2" + shift 2 + ;; + -o | --os ) + if test -z "$2" + then + err_usage "ERROR: option -o/--os requires an argument" + return 1 + fi + + build_os="$2" + shift 2 + ;; + -a | --arch ) + if test -z "$2" + then + err_usage "ERROR: option -a/--arch requires an argument" + return 1 + fi + + build_arch="$2" + shift 2 + ;; + --fips ) + GOTAGS="fips" + GOEXPERIMENT="boringcrypto" + shift 1 + ;; + * ) + err_usage "ERROR: Unknown argument: '$1'" + return 1 + ;; + esac + done + + build_local "${sdir}" "${build_os}" "${build_arch}" "rollouts-plugin-trafficrouter-consul" || return 1 + + return 0 +} + +main "$@" +exit $? diff --git a/build-support/scripts/functions.sh b/build-support/scripts/functions.sh new file mode 100644 index 0000000..98a0a02 --- /dev/null +++ b/build-support/scripts/functions.sh @@ -0,0 +1,20 @@ +# Copyright (c) HashiCorp, Inc. +# SPDX-License-Identifier: MPL-2.0 + +# +# NOTE: This file is meant to be sourced from other bash scripts/shells +# +# It provides all the scripting around building the plugin and the release process + +pushd $(dirname ${BASH_SOURCE[0]}) > /dev/null +pushd ../functions > /dev/null +FUNC_DIR=$(pwd) +popd > /dev/null +popd > /dev/null + +func_sources=$(find ${FUNC_DIR} -mindepth 1 -maxdepth 1 -name "*.sh" -type f | sort -n) + +for src in $func_sources +do + source $src +done diff --git a/makefile b/makefile index 2cbac6a..8835d0e 100644 --- a/makefile +++ b/makefile @@ -1,12 +1,89 @@ +VERSION = $(shell ./build-scripts/version.sh pkg/version/version.go) +GOLANG_VERSION?=$(shell head -n 1 .go-version) +GOARCH?=$(shell go env GOARCH) +GIT_COMMIT?=$(shell git rev-parse --short HEAD) +GIT_DIRTY?=$(shell test -n "`git status --porcelain`" && echo "+CHANGES" || true) +GIT_DESCRIBE?=$(shell git describe --tags --always) +DEV_IMAGE?=rollouts-plugin-trafficrouter-consul-dev + +##@ CI + .PHONY: go_lint -go_lint: +go_lint: ## Run linter. golangci-lint run ./... .PHONY: build -build: - CGO_ENABLED=0 GOOS=linux GOARCH=$(TARGETARCH) go build -v -o rollouts-plugin-trafficrouter-consul ./ +build: ## Build the rollouts-plugin-trafficrouter-consul binary + @$(SHELL) $(CURDIR)/build-support/scripts/build-local.sh --os linux --arch $(GOARCH) + +.PHONY: docker +docker-dev: build ## Build rollouts-plugin-trafficrouter-consul dev Docker image. + docker build -t '$(DEV_IMAGE)' \ + --target=dev \ + --build-arg 'GOLANG_VERSION=$(GOLANG_VERSION)' \ + --build-arg 'TARGETARCH=$(GOARCH)' \ + --build-arg 'GIT_COMMIT=$(GIT_COMMIT)' \ + --build-arg 'GIT_DIRTY=$(GIT_DIRTY)' \ + --build-arg 'GIT_DESCRIBE=$(GIT_DESCRIBE)' \ + -f $(CURDIR)/Dockerfile ./ + +.PHONY: check-remote-dev-image-env +check-remote-dev-image-env: +ifndef REMOTE_DEV_IMAGE + $(error REMOTE_DEV_IMAGE is undefined: set this image to /:, e.g. hashicorp/consul-k8s-dev:latest) +endif + +.PHONY: docker-multi-arch +docker-multi-arch: check-remote-dev-image-env ## Build rollouts-plugin-trafficrouter-consul dev multi-arch Docker image. + @$(SHELL) $(CURDIR)/build-support/scripts/build-local.sh --os linux --arch "arm64 amd64" + @docker buildx create --use && docker buildx build -t '$(REMOTE_DEV_IMAGE)' \ + --platform linux/amd64,linux/arm64 \ + --target=dev \ + --build-arg 'GOLANG_VERSION=$(GOLANG_VERSION)' \ + --build-arg 'GIT_COMMIT=$(GIT_COMMIT)' \ + --build-arg 'GIT_DIRTY=$(GIT_DIRTY)' \ + --build-arg 'GIT_DESCRIBE=$(GIT_DESCRIBE)' \ + --push \ + -f $(CURDIR)/Dockerfile ./ + +.PHONY: version +version: ## display version + @echo $(VERSION) + +##@ Release +.PHONY: check-env +check-env: ## check env + @printenv | grep "ROLLOUT_PLUGIN" + +.PHONY: prepare-release-script +prepare-release-script: ## Sets the versions, updates changelog to prepare this repository to release +ifndef ROLLOUT_PLUGIN_RELEASE_VERSION + $(error ROLLOUT_PLUGIN_RELEASE_VERSION is required) +endif + @source $(CURDIR)/build-support/scripts/functions.sh; prepare_release $(CURDIR) $(ROLLOUT_PLUGIN_RELEASE_VERSION) $(ROLLOUT_PLUGIN_PRERELEASE_VERSION); \ + +.PHONY: prepare-release +prepare-release: prepare-release-script + +.PHONY: prepare-dev +prepare-dev: ## prepare main dev +ifndef ROLLOUT_PLUGIN_NEXT_RELEASE_VERSION + $(error ROLLOUT_PLUGIN_NEXT_RELEASE_VERSION is required) +endif + source $(CURDIR)/build-support/scripts/functions.sh; prepare_dev $(CURDIR) $(ROLLOUT_PLUGIN_NEXT_RELEASE_VERSION) + +##@ Help -docker: - docker build . -t rollouts-plugin-trafficrouter-consul - docker tag rollouts-plugin-trafficrouter-consul wilko1989/rollouts-plugin-trafficrouter-consul:latest - docker push wilko1989/rollouts-plugin-trafficrouter-consul:latest +# The help target prints out all targets with their descriptions organized +# beneath their categories. The categorises are represented by '##@' and the +# target descriptions by '##'. The awk commands is responsible for reading the +# entire set of makefiles included in this invocation, looking for lines of the +# file as xyz: ## something, and then pretty-format the target and help. Then, +# if there's a line with ##@ something, that gets pretty-printed as a category. +# More info on the usage of ANSI control characters for terminal formatting: +# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters +# More info on the awk command: +# http://linuxcommand.org/lc3_adv_awk.php +.PHONY: help +help: ## Display this help + @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) diff --git a/pkg/version/version.go b/pkg/version/version.go new file mode 100644 index 0000000..49766e0 --- /dev/null +++ b/pkg/version/version.go @@ -0,0 +1,43 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package version + +import ( + "fmt" + "strings" +) + +var ( + // The git commit that was compiled. These will be filled in by the + // compiler. + GitCommit string + + // The main version number that is being run at the moment. + // + // Version must conform to the format expected by github.com/hashicorp/go-version + // for tests to work. + Version = "0.0.1" + + // A pre-release marker for the version. If this is "" (empty string) + // then it means that it is a final release. Otherwise, this is a pre-release + // such as "dev" (in development), "beta", "rc1", etc. + VersionPrerelease = "dev" +) + +// GetHumanVersion composes the parts of the version in a way that's suitable +// for displaying to humans. +func GetHumanVersion() string { + version := Version + release := VersionPrerelease + + if release != "" { + if !strings.HasSuffix(version, "-"+release) { + // if we tagged a prerelease version then the release is in the version already + version += fmt.Sprintf("-%s", release) + } + } + + // Strip off any single quotes added by the git information. + return strings.ReplaceAll(version, "'", "") +} diff --git a/testing/makefile b/testing/makefile index aa91932..5b5d4dc 100644 --- a/testing/makefile +++ b/testing/makefile @@ -73,10 +73,10 @@ apply-crds: -f resources/canary_rollout.yaml create-kind-config: - ./scripts/create-kind-config.sh ./resources/kind_config.yaml $(PLUGIN_DIR) + ./scripts/create-kind-config.sh ./resources/kind_config.yaml $(PLUGIN_DIR)/testing build-plugin: - make -C $(PLUGIN_DIR) build + @cd $(PLUGIN_DIR); CGO_ENABLED=0 GOOS=linux GOARCH=$(TARGETARCH) go build -v -o testing/rollouts-plugin-trafficrouter-consul ./ ## EXTRAS install-required: