Skip to content

Commit

Permalink
fix version stamping
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedanese committed Jul 22, 2021
1 parent d70bee4 commit 4d4bfa2
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 86 deletions.
1 change: 1 addition & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
startup --expand_configs_in_place

build --stamp
build --workspace_status_command tools/workspace-status.sh
build --define RELEASE=devel

Expand Down
1 change: 1 addition & 0 deletions cmd/gke-gcloud-auth-plugin/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ load("//defs:version.bzl", "version_x_defs")
go_binary(
name = "gke-gcloud-auth-plugin",
embed = [":go_default_library"],
x_defs = version_x_defs(),
)

go_library(
Expand Down
4 changes: 2 additions & 2 deletions defs/version.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
def version_x_defs():
# This should match the list of packages in kube::version::ldflag
stamp_pkgs = [
"k8s.io/kubernetes/vendor/k8s.io/component-base/version",
"k8s.io/kubernetes/vendor/k8s.io/client-go/pkg/version",
"k8s.io/cloud-provider-gcp/vendor/k8s.io/component-base/version",
"k8s.io/cloud-provider-gcp/vendor/k8s.io/client-go/pkg/version",
]

# This should match the list of vars in kube::version::ldflags
Expand Down
85 changes: 1 addition & 84 deletions tools/version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@
# If KUBE_GIT_VERSION_FILE, this function will load from that file instead of
# querying git.
get_version_vars() {
echo "Starting get_version_vars"
if [[ -n ${KUBE_GIT_VERSION_FILE-} ]]; then
echo "Exiting early"
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
Expand All @@ -53,9 +46,6 @@ get_version_vars() {
if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
KUBE_GIT_VERSION="${BASH_REMATCH[1]}"
fi
else
echo "Format failed, format = ?"
#echo "Format failed, format = '$Format:%%$'"
fi

local git=(git --work-tree "${KUBE_ROOT}")
Expand All @@ -68,8 +58,6 @@ get_version_vars() {
else
KUBE_GIT_TREE_STATE="dirty"
fi
else
echo "git tree state failed"
fi

# Use git describe to find the version based on tags.
Expand Down Expand Up @@ -100,7 +88,6 @@ get_version_vars() {
# 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"
echo "KUBE_GIT_VERSION set to ${KUBE_GIT_VERSION}"
fi


Expand All @@ -117,81 +104,11 @@ get_version_vars() {

# 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
else
echo "git version failed"
echo "KUBE_GIT_VERSION is ${KUBE_GIT_VERSION}"
echo "KUBE_GIT_COMMIT is ${KUBE_GIT_COMMIT}"
fi
else
echo "git failed"
exit 1
fi
}

# Saves the environment flags to $1
save_version_vars() {
local version_file=${1-}
[[ -n ${version_file} ]] || {
echo "!!! Internal error. No file specified in save_version_vars"
return 1
}

cat <<EOF >"${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
load_version_vars() {
local version_file=${1-}
[[ -n ${version_file} ]] || {
echo "!!! Internal error. No file specified in 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.
ldflags() {
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 '${KUBE_GO_PACKAGE}/vendor/k8s.io/client-go/pkg/version.${key}=${val}'"
"-X '${KUBE_GO_PACKAGE}/vendor/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[*]-}"
}
8 changes: 8 additions & 0 deletions tools/workspace-status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,12 @@ cat <<EOF
STABLE_IMAGE_REGISTRY ${IMAGE_REGISTRY:-gcr.io}
STABLE_IMAGE_REPO ${IMAGE_REPO:-k8s-image-staging}
STABLE_IMAGE_TAG ${IMAGE_TAG:-${KUBE_GIT_VERSION/+/_}}
gitCommit ${KUBE_GIT_COMMIT-}
gitTreeState ${KUBE_GIT_TREE_STATE-}
gitVersion ${KUBE_GIT_VERSION-}
gitMajor ${KUBE_GIT_MAJOR-}
gitMinor ${KUBE_GIT_MINOR-}
buildDate $(date \
${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} \
-u +'%Y-%m-%dT%H:%M:%SZ')
EOF

0 comments on commit 4d4bfa2

Please sign in to comment.