github.com/wmuizelaar/kpt@v0.0.0-20221018115725-bd564717b2ed/scripts/lib/version.sh (about)

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