From 549da378059dddf4d73c5cdd4eacb6bbb620dfc8 Mon Sep 17 00:00:00 2001 From: Monis Khan Date: Mon, 20 Jul 2020 00:21:47 -0400 Subject: [PATCH] Encode git version info into binary and user agent Signed-off-by: Monis Khan --- Dockerfile | 8 +- cmd/placeholder-name/main.go | 5 + deploy/deployment.yaml | 2 +- go.mod | 1 + hack/get-ldflags.sh | 10 ++ hack/lib/version.sh | 182 +++++++++++++++++++++++++++++++++++ hack/update.sh | 2 +- hack/verify.sh | 2 +- 8 files changed, 205 insertions(+), 7 deletions(-) create mode 100755 hack/get-ldflags.sh create mode 100644 hack/lib/version.sh diff --git a/Dockerfile b/Dockerfile index 9624812f..6649b5dd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ ARG ACCESS_TOKEN_USR="nothing" ARG ACCESS_TOKEN_PWD="nothing" # git is required to fetch go dependencies -RUN apk add --no-cache ca-certificates git +RUN apk add --no-cache ca-certificates git bash # Create a netrc file using the credentials specified using --build-arg RUN printf "machine github.com\n\ @@ -28,15 +28,15 @@ RUN go mod download # Copy the source code COPY . . # Build the executable binary -RUN GOOS=linux GOARCH=amd64 go build -o out ./... +RUN GOOS=linux GOARCH=amd64 go build -ldflags "$(hack/get-ldflags.sh)" -o out ./... FROM alpine:latest # Install CA certs and some tools for debugging RUN apk --update --no-cache add ca-certificates bash curl WORKDIR /root/ # Copy the binary from the build-env stage -COPY --from=build-env /work/out/placeholder-name app +COPY --from=build-env /work/out/placeholder-name placeholder-name # Document the port EXPOSE 8080 # Set the command -CMD ["./app"] +CMD ["./placeholder-name"] diff --git a/cmd/placeholder-name/main.go b/cmd/placeholder-name/main.go index 6bd514c4..69219924 100644 --- a/cmd/placeholder-name/main.go +++ b/cmd/placeholder-name/main.go @@ -8,10 +8,15 @@ package main import ( "os" + "k8s.io/client-go/pkg/version" + "k8s.io/client-go/rest" + "k8s.io/klog/v2" + "github.com/suzerain-io/placeholder-name/cmd/placeholder-name/app" ) func main() { + klog.Infof("Running %s at %#v", rest.DefaultKubernetesUserAgent(), version.Get()) if err := app.New(os.Args[1:], os.Stdout, os.Stderr).Run(); err != nil { os.Exit(1) } diff --git a/deploy/deployment.yaml b/deploy/deployment.yaml index 71713551..4523c996 100644 --- a/deploy/deployment.yaml +++ b/deploy/deployment.yaml @@ -55,7 +55,7 @@ spec: #@ end imagePullPolicy: IfNotPresent command: - - ./app + - ./placeholder-name args: - --config=/etc/config/placeholder-config.yaml - --downward-api-path=/etc/podinfo diff --git a/go.mod b/go.mod index b5dfabd0..cc14f8f0 100644 --- a/go.mod +++ b/go.mod @@ -14,6 +14,7 @@ require ( k8s.io/apimachinery v0.19.0-rc.0 k8s.io/apiserver v0.19.0-rc.0 k8s.io/client-go v0.19.0-rc.0 + k8s.io/klog/v2 v2.2.0 k8s.io/kube-aggregator v0.19.0-rc.0 k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19 sigs.k8s.io/yaml v1.2.0 diff --git a/hack/get-ldflags.sh b/hack/get-ldflags.sh new file mode 100755 index 00000000..9f3fe07b --- /dev/null +++ b/hack/get-ldflags.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -o errexit +set -o nounset +set -o pipefail + +KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +source "${KUBE_ROOT}/hack/lib/version.sh" + +kube::version::ldflags diff --git a/hack/lib/version.sh b/hack/lib/version.sh new file mode 100644 index 00000000..15180fb3 --- /dev/null +++ b/hack/lib/version.sh @@ -0,0 +1,182 @@ +#!/usr/bin/env bash + +# Copyright 2014 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. + +# ----------------------------------------------------------------------------- +# Version management helpers. These functions help to set, save and load the +# following variables: +# +# KUBE_GIT_COMMIT - The git commit id corresponding to this +# source code. +# KUBE_GIT_TREE_STATE - "clean" indicates no changes since the git commit id +# "dirty" indicates source code changes after the git commit id +# "archive" indicates the tree was produced by 'git archive' +# KUBE_GIT_VERSION - "vX.Y" used to indicate the last release version. +# KUBE_GIT_MAJOR - The major part of the version +# KUBE_GIT_MINOR - The minor component of the version + +# Grovels through git to set a set of env variables. +# +# If KUBE_GIT_VERSION_FILE, this function will load from that file instead of +# querying git. +kube::version::get_version_vars() { + if [[ -n ${KUBE_GIT_VERSION_FILE-} ]]; then + kube::version::load_version_vars "${KUBE_GIT_VERSION_FILE}" + return + fi + + # If the kubernetes source was exported through git archive, then + # we likely don't have a git tree, but these magic values may be filled in. + # shellcheck disable=SC2016,SC2050 + # Disabled as we're not expanding these at runtime, but rather expecting + # that another tool may have expanded these and rewritten the source (!) + if [[ '$Format:%%$' == "%" ]]; then + KUBE_GIT_COMMIT='$Format:%H$' + KUBE_GIT_TREE_STATE="archive" + # When a 'git archive' is exported, the '$Format:%D$' below will look + # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: ' + # can be extracted from it. + if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then + KUBE_GIT_VERSION="${BASH_REMATCH[1]}" + fi + fi + + local git=(git --work-tree "${KUBE_ROOT}") + + if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then + if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then + # Check if the tree is dirty. default to dirty + if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then + KUBE_GIT_TREE_STATE="clean" + else + KUBE_GIT_TREE_STATE="dirty" + fi + fi + + # Use git describe to find the version based on tags. + if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --match='v*' --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then + # This translates the "git describe" to an actual semver.org + # compatible semantic version that looks something like this: + # v1.1.0-alpha.0.6+84c76d1142ea4d + # + # TODO: We continue calling this "git version" because so many + # downstream consumers are expecting it there. + # + # These regexes are painful enough in sed... + # We don't want to do them in pure shell, so disable SC2001 + # shellcheck disable=SC2001 + DASHES_IN_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/[^-]//g") + if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then + # shellcheck disable=SC2001 + # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) + KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/") + elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then + # shellcheck disable=SC2001 + # We have distance to base tag (v1.1.0-1-gCommitHash) + KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/") + fi + if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then + # git describe --dirty only considers changes to existing files, but + # that is problematic since new untracked .go files affect the build, + # so use our idea of "dirty" from git status instead. + KUBE_GIT_VERSION+="-dirty" + fi + + + # Try to match the "git describe" output to a regex to try to extract + # the "major" and "minor" versions and whether this is the exact tagged + # version or whether the tree is between two tagged versions. + if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then + KUBE_GIT_MAJOR=${BASH_REMATCH[1]} + KUBE_GIT_MINOR=${BASH_REMATCH[2]} + if [[ -n "${BASH_REMATCH[4]}" ]]; then + KUBE_GIT_MINOR+="+" + fi + fi + + # If KUBE_GIT_VERSION is not a valid Semantic Version, then refuse to build. + if ! [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then + echo "KUBE_GIT_VERSION should be a valid Semantic Version. Current value: ${KUBE_GIT_VERSION}" + echo "Please see more details here: https://semver.org" + exit 1 + fi + fi + fi +} + +# Saves the environment flags to $1 +kube::version::save_version_vars() { + local version_file=${1-} + [[ -n ${version_file} ]] || { + echo "!!! Internal error. No file specified in kube::version::save_version_vars" + return 1 + } + + cat <"${version_file}" +KUBE_GIT_COMMIT='${KUBE_GIT_COMMIT-}' +KUBE_GIT_TREE_STATE='${KUBE_GIT_TREE_STATE-}' +KUBE_GIT_VERSION='${KUBE_GIT_VERSION-}' +KUBE_GIT_MAJOR='${KUBE_GIT_MAJOR-}' +KUBE_GIT_MINOR='${KUBE_GIT_MINOR-}' +EOF +} + +# Loads up the version variables from file $1 +kube::version::load_version_vars() { + local version_file=${1-} + [[ -n ${version_file} ]] || { + echo "!!! Internal error. No file specified in kube::version::load_version_vars" + return 1 + } + + source "${version_file}" +} + +# Prints the value that needs to be passed to the -ldflags parameter of go build +# in order to set the Kubernetes based on the git tree status. +# IMPORTANT: if you update any of these, also update the lists in +# pkg/version/def.bzl and hack/print-workspace-status.sh. +kube::version::ldflags() { + kube::version::get_version_vars + + local -a ldflags + function add_ldflag() { + local key=${1} + local val=${2} + # If you update these, also update the list component-base/version/def.bzl. + ldflags+=( + "-X 'k8s.io/client-go/pkg/version.${key}=${val}'" + "-X 'k8s.io/component-base/version.${key}=${val}'" + ) + } + + add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')" + if [[ -n ${KUBE_GIT_COMMIT-} ]]; then + add_ldflag "gitCommit" "${KUBE_GIT_COMMIT}" + add_ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}" + fi + + if [[ -n ${KUBE_GIT_VERSION-} ]]; then + add_ldflag "gitVersion" "${KUBE_GIT_VERSION}" + fi + + if [[ -n ${KUBE_GIT_MAJOR-} && -n ${KUBE_GIT_MINOR-} ]]; then + add_ldflag "gitMajor" "${KUBE_GIT_MAJOR}" + add_ldflag "gitMinor" "${KUBE_GIT_MINOR}" + fi + + # The -ldflags parameter takes a single string, so join the output. + echo "${ldflags[*]-}" +} diff --git a/hack/update.sh b/hack/update.sh index 9f67d0ec..4ca42d28 100755 --- a/hack/update.sh +++ b/hack/update.sh @@ -2,4 +2,4 @@ set -e go mod tidy -go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./... --fix +go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./... --fix --timeout=10m diff --git a/hack/verify.sh b/hack/verify.sh index 0b7aa8c0..f1f82036 100755 --- a/hack/verify.sh +++ b/hack/verify.sh @@ -1,4 +1,4 @@ #!/bin/bash set -e -go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./... --modules-download-mode=readonly +go run github.com/golangci/golangci-lint/cmd/golangci-lint run ./... --modules-download-mode=readonly --timeout=10m