Skip to content

Commit

Permalink
added build and makefile scripts
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Wilkerson <[email protected]>
  • Loading branch information
wilkermichael committed Feb 20, 2024
1 parent 04fce6a commit 29d3813
Show file tree
Hide file tree
Showing 12 changed files with 742 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions .go-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.21.6
108 changes: 100 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,18 +1,110 @@
FROM --platform=$BUILDPLATFORM golang:1.21.6 as builder
# This Dockerfile contains multiple targets.
# Use 'docker build --target=<name> .' to build one.
#
# Every target has a BIN_NAME argument that must be provided via --build-arg=BIN_NAME=<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 <[email protected]>" \
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 <[email protected]>" \
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/
# ===================================
#
# Set default target to 'dev'.
#
# ===================================
FROM dev
14 changes: 14 additions & 0 deletions build-scripts/version.sh
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions build-support/functions/00-vars.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Whether to colorize shell output
COLORIZE=${COLORIZE-1}
152 changes: 152 additions & 0 deletions build-support/functions/10-util.sh
Original file line number Diff line number Diff line change
@@ -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 <var>` 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 $?
}
Loading

0 comments on commit 29d3813

Please sign in to comment.