version.sh 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #!/usr/bin/env bash
  2. # Copyright 2014 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # -----------------------------------------------------------------------------
  16. # Version management helpers. These functions help to set, save and load the
  17. # following variables:
  18. #
  19. # KUBE_GIT_COMMIT - The git commit id corresponding to this
  20. # source code.
  21. # KUBE_GIT_TREE_STATE - "clean" indicates no changes since the git commit id
  22. # "dirty" indicates source code changes after the git commit id
  23. # "archive" indicates the tree was produced by 'git archive'
  24. # KUBE_GIT_VERSION - "vX.Y" used to indicate the last release version.
  25. # KUBE_GIT_MAJOR - The major part of the version
  26. # KUBE_GIT_MINOR - The minor component of the version
  27. # Grovels through git to set a set of env variables.
  28. #
  29. # If KUBE_GIT_VERSION_FILE, this function will load from that file instead of
  30. # querying git.
  31. kube::version::get_version_vars() {
  32. if [[ -n ${KUBE_GIT_VERSION_FILE-} ]]; then
  33. kube::version::load_version_vars "${KUBE_GIT_VERSION_FILE}"
  34. return
  35. fi
  36. # If the kubernetes source was exported through git archive, then
  37. # we likely don't have a git tree, but these magic values may be filled in.
  38. # shellcheck disable=SC2016,SC2050
  39. # Disabled as we're not expanding these at runtime, but rather expecting
  40. # that another tool may have expanded these and rewritten the source (!)
  41. if [[ '$Format:%%$' == "%" ]]; then
  42. KUBE_GIT_COMMIT='$Format:%H$'
  43. KUBE_GIT_TREE_STATE="archive"
  44. # When a 'git archive' is exported, the '$Format:%D$' below will look
  45. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  46. # can be extracted from it.
  47. if [[ '$Format:%D$' =~ tag:\ (v[^ ,]+) ]]; then
  48. KUBE_GIT_VERSION="${BASH_REMATCH[1]}"
  49. fi
  50. fi
  51. local git=(git --work-tree "${KUBE_ROOT}")
  52. if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
  53. if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then
  54. # Check if the tree is dirty. default to dirty
  55. if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  56. KUBE_GIT_TREE_STATE="clean"
  57. else
  58. KUBE_GIT_TREE_STATE="dirty"
  59. fi
  60. fi
  61. # Use git describe to find the version based on tags.
  62. if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --match='v*' --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then
  63. # This translates the "git describe" to an actual semver.org
  64. # compatible semantic version that looks something like this:
  65. # v1.1.0-alpha.0.6+84c76d1142ea4d
  66. #
  67. # TODO: We continue calling this "git version" because so many
  68. # downstream consumers are expecting it there.
  69. #
  70. # These regexes are painful enough in sed...
  71. # We don't want to do them in pure shell, so disable SC2001
  72. # shellcheck disable=SC2001
  73. DASHES_IN_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/[^-]//g")
  74. if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
  75. # shellcheck disable=SC2001
  76. # We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
  77. KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  78. elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
  79. # shellcheck disable=SC2001
  80. # We have distance to base tag (v1.1.0-1-gCommitHash)
  81. KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
  82. fi
  83. if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then
  84. # git describe --dirty only considers changes to existing files, but
  85. # that is problematic since new untracked .go files affect the build,
  86. # so use our idea of "dirty" from git status instead.
  87. KUBE_GIT_VERSION+="-dirty"
  88. fi
  89. # Try to match the "git describe" output to a regex to try to extract
  90. # the "major" and "minor" versions and whether this is the exact tagged
  91. # version or whether the tree is between two tagged versions.
  92. if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
  93. KUBE_GIT_MAJOR=${BASH_REMATCH[1]}
  94. KUBE_GIT_MINOR=${BASH_REMATCH[2]}
  95. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  96. KUBE_GIT_MINOR+="+"
  97. fi
  98. fi
  99. # If KUBE_GIT_VERSION is not a valid Semantic Version, then refuse to build.
  100. if ! [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  101. echo "KUBE_GIT_VERSION should be a valid Semantic Version. Current value: ${KUBE_GIT_VERSION}"
  102. echo "Please see more details here: https://semver.org"
  103. exit 1
  104. fi
  105. fi
  106. fi
  107. }
  108. # Saves the environment flags to $1
  109. kube::version::save_version_vars() {
  110. local version_file=${1-}
  111. [[ -n ${version_file} ]] || {
  112. echo "!!! Internal error. No file specified in kube::version::save_version_vars"
  113. return 1
  114. }
  115. cat <<EOF >"${version_file}"
  116. KUBE_GIT_COMMIT='${KUBE_GIT_COMMIT-}'
  117. KUBE_GIT_TREE_STATE='${KUBE_GIT_TREE_STATE-}'
  118. KUBE_GIT_VERSION='${KUBE_GIT_VERSION-}'
  119. KUBE_GIT_MAJOR='${KUBE_GIT_MAJOR-}'
  120. KUBE_GIT_MINOR='${KUBE_GIT_MINOR-}'
  121. EOF
  122. }
  123. # Loads up the version variables from file $1
  124. kube::version::load_version_vars() {
  125. local version_file=${1-}
  126. [[ -n ${version_file} ]] || {
  127. echo "!!! Internal error. No file specified in kube::version::load_version_vars"
  128. return 1
  129. }
  130. source "${version_file}"
  131. }
  132. # Prints the value that needs to be passed to the -ldflags parameter of go build
  133. # in order to set the Kubernetes based on the git tree status.
  134. # IMPORTANT: if you update any of these, also update the lists in
  135. # pkg/version/def.bzl and hack/print-workspace-status.sh.
  136. kube::version::ldflags() {
  137. kube::version::get_version_vars
  138. local -a ldflags
  139. function add_ldflag() {
  140. local key=${1}
  141. local val=${2}
  142. # If you update these, also update the list component-base/version/def.bzl.
  143. ldflags+=(
  144. "-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/client-go/pkg/version.${key}=${val}'"
  145. "-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/component-base/version.${key}=${val}'"
  146. )
  147. }
  148. add_ldflag "buildDate" "$(date ${SOURCE_DATE_EPOCH:+"--date=@${SOURCE_DATE_EPOCH}"} -u +'%Y-%m-%dT%H:%M:%SZ')"
  149. if [[ -n ${KUBE_GIT_COMMIT-} ]]; then
  150. add_ldflag "gitCommit" "${KUBE_GIT_COMMIT}"
  151. add_ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}"
  152. fi
  153. if [[ -n ${KUBE_GIT_VERSION-} ]]; then
  154. add_ldflag "gitVersion" "${KUBE_GIT_VERSION}"
  155. fi
  156. if [[ -n ${KUBE_GIT_MAJOR-} && -n ${KUBE_GIT_MINOR-} ]]; then
  157. add_ldflag "gitMajor" "${KUBE_GIT_MAJOR}"
  158. add_ldflag "gitMinor" "${KUBE_GIT_MINOR}"
  159. fi
  160. # The -ldflags parameter takes a single string, so join the output.
  161. echo "${ldflags[*]-}"
  162. }