version.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/bin/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. # KUBE_GIT_VERSION - "vX.Y" used to indicate the last release version.
  24. # KUBE_GIT_MAJOR - The major part of the version
  25. # KUBE_GIT_MINOR - The minor component of the version
  26. # Grovels through git to set a set of env variables.
  27. #
  28. # If KUBE_GIT_VERSION_FILE, this function will load from that file instead of
  29. # querying git.
  30. kube::version::get_version_vars() {
  31. if [[ -n ${KUBE_GIT_VERSION_FILE-} ]]; then
  32. kube::version::load_version_vars "${KUBE_GIT_VERSION_FILE}"
  33. return
  34. fi
  35. local git=(git --work-tree "${KUBE_ROOT}")
  36. if [[ -n ${KUBE_GIT_COMMIT-} ]] || KUBE_GIT_COMMIT=$("${git[@]}" rev-parse "HEAD^{commit}" 2>/dev/null); then
  37. if [[ -z ${KUBE_GIT_TREE_STATE-} ]]; then
  38. # Check if the tree is dirty. default to dirty
  39. if git_status=$("${git[@]}" status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then
  40. KUBE_GIT_TREE_STATE="clean"
  41. else
  42. KUBE_GIT_TREE_STATE="dirty"
  43. fi
  44. fi
  45. # Use git describe to find the version based on annotated tags.
  46. if [[ -n ${KUBE_GIT_VERSION-} ]] || KUBE_GIT_VERSION=$("${git[@]}" describe --tags --abbrev=14 "${KUBE_GIT_COMMIT}^{commit}" 2>/dev/null); then
  47. # This translates the "git describe" to an actual semver.org
  48. # compatible semantic version that looks something like this:
  49. # v1.1.0-alpha.0.6+84c76d1142ea4d
  50. #
  51. # TODO: We continue calling this "git version" because so many
  52. # downstream consumers are expecting it there.
  53. KUBE_GIT_VERSION=$(echo "${KUBE_GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\+\2/")
  54. if [[ "${KUBE_GIT_TREE_STATE}" == "dirty" ]]; then
  55. # git describe --dirty only considers changes to existing files, but
  56. # that is problematic since new untracked .go files affect the build,
  57. # so use our idea of "dirty" from git status instead.
  58. KUBE_GIT_VERSION+="-dirty"
  59. fi
  60. # Try to match the "git describe" output to a regex to try to extract
  61. # the "major" and "minor" versions and whether this is the exact tagged
  62. # version or whether the tree is between two tagged versions.
  63. if [[ "${KUBE_GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?$ ]]; then
  64. KUBE_GIT_MAJOR=${BASH_REMATCH[1]}
  65. KUBE_GIT_MINOR=${BASH_REMATCH[2]}
  66. if [[ -n "${BASH_REMATCH[4]}" ]]; then
  67. KUBE_GIT_MINOR+="+"
  68. fi
  69. fi
  70. fi
  71. fi
  72. }
  73. # Saves the environment flags to $1
  74. kube::version::save_version_vars() {
  75. local version_file=${1-}
  76. [[ -n ${version_file} ]] || {
  77. echo "!!! Internal error. No file specified in kube::version::save_version_vars"
  78. return 1
  79. }
  80. cat <<EOF >"${version_file}"
  81. KUBE_GIT_COMMIT='${KUBE_GIT_COMMIT-}'
  82. KUBE_GIT_TREE_STATE='${KUBE_GIT_TREE_STATE-}'
  83. KUBE_GIT_VERSION='${KUBE_GIT_VERSION-}'
  84. KUBE_GIT_MAJOR='${KUBE_GIT_MAJOR-}'
  85. KUBE_GIT_MINOR='${KUBE_GIT_MINOR-}'
  86. EOF
  87. }
  88. # Loads up the version variables from file $1
  89. kube::version::load_version_vars() {
  90. local version_file=${1-}
  91. [[ -n ${version_file} ]] || {
  92. echo "!!! Internal error. No file specified in kube::version::load_version_vars"
  93. return 1
  94. }
  95. source "${version_file}"
  96. }
  97. kube::version::ldflag() {
  98. local key=${1}
  99. local val=${2}
  100. echo "-X ${KUBE_GO_PACKAGE}/pkg/version.${key}=${val}"
  101. }
  102. # Prints the value that needs to be passed to the -ldflags parameter of go build
  103. # in order to set the Kubernetes based on the git tree status.
  104. kube::version::ldflags() {
  105. kube::version::get_version_vars
  106. local -a ldflags=($(kube::version::ldflag "buildDate" "$(date -u +'%Y-%m-%dT%H:%M:%SZ')"))
  107. if [[ -n ${KUBE_GIT_COMMIT-} ]]; then
  108. ldflags+=($(kube::version::ldflag "gitCommit" "${KUBE_GIT_COMMIT}"))
  109. ldflags+=($(kube::version::ldflag "gitTreeState" "${KUBE_GIT_TREE_STATE}"))
  110. fi
  111. if [[ -n ${KUBE_GIT_VERSION-} ]]; then
  112. ldflags+=($(kube::version::ldflag "gitVersion" "${KUBE_GIT_VERSION}"))
  113. fi
  114. if [[ -n ${KUBE_GIT_MAJOR-} && -n ${KUBE_GIT_MINOR-} ]]; then
  115. ldflags+=(
  116. $(kube::version::ldflag "gitMajor" "${KUBE_GIT_MAJOR}")
  117. $(kube::version::ldflag "gitMinor" "${KUBE_GIT_MINOR}")
  118. )
  119. fi
  120. # The -ldflags parameter takes a single string, so join the output.
  121. echo "${ldflags[*]-}"
  122. }