version.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. if [[ '%' == "%" ]]; then
  39. KUBE_GIT_COMMIT='67d2fcf276fcd9cf743ad4be9a9ef5828adc082f'
  40. KUBE_GIT_TREE_STATE="archive"
  41. # When a 'git archive' is exported, the 'tag: v1.15.4' below will look
  42. # something like 'HEAD -> release-1.8, tag: v1.8.3' where then 'tag: '
  43. # can be extracted from it.
  44. if [[ 'tag: v1.15.4' =~ tag:\ (v[^ ,]+) ]]; then
  45. KUBE_GIT_VERSION="${BASH_REMATCH[1]}"
  46. fi
  47. fi
  48. local git=(git --work-tree "${KUBE_ROOT}")
  49. if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
  50. if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then
  51. # Check if the tree is dirty. default to dirty
  52. if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  53. KUBE_GIT_TREE_STATE="clean"
  54. else
  55. KUBE_GIT_TREE_STATE="dirty"
  56. fi
  57. fi
  58. # Use git describe to find the version based on tags.
  59. if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then
  60. # This translates the "git describe" to an actual semver.org
  61. # compatible semantic version that looks something like this:
  62. # v1.1.0-alpha.0.6+84c76d1142ea4d
  63. #
  64. # TODO: We continue calling this "git version" because so many
  65. # downstream consumers are expecting it there.
  66. DASHES_IN_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/[^-]//g")
  67. if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then
  68. # We have distance to subversion (v1.1.0-subversion-1-gCommitHash)
  69. KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  70. elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then
  71. # We have distance to base tag (v1.1.0-1-gCommitHash)
  72. KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/+\1/")
  73. fi
  74. if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then
  75. # git describe --dirty only considers changes to existing files, but
  76. # that is problematic since new untracked .go files affect the build,
  77. # so use our idea of "dirty" from git status instead.
  78. KUBE_GIT_VERSION+="-dirty"
  79. fi
  80. # Try to match the "git describe" output to a regex to try to extract
  81. # the "major" and "minor" versions and whether this is the exact tagged
  82. # version or whether the tree is between two tagged versions.
  83. if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then
  84. KUBE_GIT_MAJOR=${BASH_REMATCH[1]}
  85. KUBE_GIT_MINOR=${BASH_REMATCH[2]}
  86. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  87. KUBE_GIT_MINOR+="+"
  88. fi
  89. fi
  90. # If KUBE_GIT_VERSION is not a valid Semantic Version, then refuse to build.
  91. if ! [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then
  92. echo "KUBE_GIT_VERSION should be a valid Semantic Version. Current value: ${KUBE_GIT_VERSION}"
  93. echo "Please see more details here: https://semver.org"
  94. exit 1
  95. fi
  96. fi
  97. fi
  98. }
  99. # Saves the environment flags to $1
  100. kube::version::save_version_vars() {
  101. local version_file=${1-}
  102. [[ -n ${version_file} ]] || {
  103. echo "!!! Internal error. No file specified in kube::version::save_version_vars"
  104. return 1
  105. }
  106. cat <<EOF >"${version_file}"
  107. KUBE_GIT_COMMIT='${KUBE_GIT_COMMIT-}'
  108. KUBE_GIT_TREE_STATE='${KUBE_GIT_TREE_STATE-}'
  109. KUBE_GIT_VERSION='${KUBE_GIT_VERSION-}'
  110. KUBE_GIT_MAJOR='${KUBE_GIT_MAJOR-}'
  111. KUBE_GIT_MINOR='${KUBE_GIT_MINOR-}'
  112. EOF
  113. }
  114. # Loads up the version variables from file $1
  115. kube::version::load_version_vars() {
  116. local version_file=${1-}
  117. [[ -n ${version_file} ]] || {
  118. echo "!!! Internal error. No file specified in kube::version::load_version_vars"
  119. return 1
  120. }
  121. source "${version_file}"
  122. }
  123. kube::version::ldflag() {
  124. local key=${1}
  125. local val=${2}
  126. # If you update these, also update the list pkg/version/def.bzl.
  127. echo "-X '${KUBE_GO_PACKAGE}/pkg/version.${key}=${val}'"
  128. echo "-X '${KUBE_GO_PACKAGE}/vendor/k8s.io/client-go/pkg/version.${key}=${val}'"
  129. echo "-X '${KUBE_GO_PACKAGE}/pkg/kubectl/version.${key}=${val}'"
  130. }
  131. # Prints the value that needs to be passed to the -ldflags parameter of go build
  132. # in order to set the Kubernetes based on the git tree status.
  133. # IMPORTANT: if you update any of these, also update the lists in
  134. # pkg/version/def.bzl and hack/print-workspace-status.sh.
  135. kube::version::ldflags() {
  136. kube::version::get_version_vars
  137. local buildDate=
  138. [[ -z ${SOURCE_DATE_EPOCH-} ]] || buildDate="--date=@${SOURCE_DATE_EPOCH}"
  139. local -a ldflags=($(kube::version::ldflag "buildDate" "$(date ${buildDate} -u +'%Y-%m-%dT%H:%M:%SZ')"))
  140. if [[ -n ${KUBE_GIT_COMMIT-} ]]; then
  141. ldflags+=($(kube::version::ldflag "gitCommit" "${KUBE_GIT_COMMIT}"))
  142. ldflags+=($(kube::version::ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}"))
  143. fi
  144. if [[ -n ${KUBE_GIT_VERSION-} ]]; then
  145. ldflags+=($(kube::version::ldflag "gitVersion" "${KUBE_GIT_VERSION}"))
  146. fi
  147. if [[ -n ${KUBE_GIT_MAJOR-} && -n ${KUBE_GIT_MINOR-} ]]; then
  148. ldflags+=(
  149. $(kube::version::ldflag "gitMajor" "${KUBE_GIT_MAJOR}")
  150. $(kube::version::ldflag "gitMinor" "${KUBE_GIT_MINOR}")
  151. )
  152. fi
  153. # The -ldflags parameter takes a single string, so join the output.
  154. echo "${ldflags[*]-}"
  155. }