k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/hack/pin-dependency.sh (about)

     1  #!/usr/bin/env bash
     2  
     3  # Copyright 2019 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  # This script switches to the preferred version for specified module.
    18  # Usage: `hack/pin-dependency.sh $MODULE $SHA-OR-TAG`.
    19  # Example: `hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7`.
    20  
    21  set -o errexit
    22  set -o nounset
    23  set -o pipefail
    24  
    25  KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    26  source "${KUBE_ROOT}/hack/lib/init.sh"
    27  
    28  # Detect problematic GOPROXY settings that prevent lookup of dependencies
    29  if [[ "${GOPROXY:-}" == "off" ]]; then
    30    kube::log::error "Cannot run with \$GOPROXY=off"
    31    exit 1
    32  fi
    33  
    34  kube::golang::setup_env
    35  kube::util::require-jq
    36  
    37  # Explicitly set GOFLAGS to ignore vendor, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
    38  export GOWORK=off
    39  export GOFLAGS=-mod=mod
    40  
    41  dep="${1:-}"
    42  sha="${2:-}"
    43  
    44  # Specifying a different repo is optional.
    45  replacement=
    46  case ${dep} in
    47      *=*)
    48          # shellcheck disable=SC2001
    49          replacement=$(echo "${dep}" | sed -e 's/.*=//')
    50          # shellcheck disable=SC2001
    51          dep=$(echo "${dep}" | sed -e 's/=.*//')
    52          ;;
    53      *)
    54          replacement="${dep}"
    55          ;;
    56  esac
    57  
    58  if [[ -z "${dep}" || -z "${replacement}" || -z "${sha}" ]]; then
    59    echo "Usage:"
    60    echo "  hack/pin-dependency.sh \$MODULE[=\$REPLACEMENT] \$SHA-OR-TAG"
    61    echo ""
    62    echo "Examples:"
    63    echo "  hack/pin-dependency.sh github.com/docker/docker 501cb131a7b7"
    64    echo "  hack/pin-dependency.sh github.com/docker/docker=github.com/johndoe/docker my-experimental-branch"
    65    echo ""
    66    echo "Replacing with a different repository is useful for testing but"
    67    echo "the result should never be merged into Kubernetes!"
    68    echo ""
    69    exit 1
    70  fi
    71  
    72  # Find the resolved version before trying to use it.
    73  echo "Running: go mod download ${replacement}@${sha}"
    74  if meta=$(go mod download -json "${replacement}@${sha}"); then
    75      rev=$(echo "${meta}" | jq -r ".Version")
    76  else
    77      error=$(echo "${meta}" | jq -r ".Error")
    78      echo "Download failed: ${error}" >&2
    79      exit 1
    80  fi
    81  echo "Resolved to ${replacement}@${rev}"
    82  
    83  # Add the require directive
    84  echo "Running: go mod edit -require ${dep}@${rev}"
    85  go mod edit -require "${dep}@${rev}"
    86  
    87  # Add the replace directive
    88  if [ "${replacement}" != "${dep}" ]; then
    89    echo "Running: go mod edit -replace ${dep}=${replacement}@${rev}"
    90    go mod edit -replace "${dep}=${replacement}@${rev}"
    91  fi
    92  
    93  # Propagate pinned version to staging repos
    94  for repo in $(kube::util::list_staging_repos); do
    95    pushd "staging/src/k8s.io/${repo}" >/dev/null 2>&1
    96      go mod edit -require "${dep}@${rev}"
    97  
    98      # When replacing with a fork, always add a replace statement in all go.mod
    99      # files (not just the root of the staging repos!) because there might be
   100      # indirect dependencies on the fork.
   101      #
   102      # This is excessive, but the resulting commit should never be merged, so it
   103      # isn't that important to get this exactly right.
   104      if [ "${replacement}" != "${dep}" ]; then
   105          find . -name go.mod -print | while read -r modfile; do
   106              (cd "$(dirname "${modfile}")" && go mod edit -replace "${dep}=${replacement}@${rev}")
   107          done
   108      fi
   109    popd >/dev/null 2>&1
   110  done
   111  
   112  echo ""
   113  echo "Run hack/update-vendor.sh to rebuild the vendor directory"