...

Text file src/k8s.io/kubernetes/hack/pin-dependency.sh

Documentation: k8s.io/kubernetes/hack

     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
    21set -o errexit
    22set -o nounset
    23set -o pipefail
    24
    25KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
    26source "${KUBE_ROOT}/hack/lib/init.sh"
    27
    28# Detect problematic GOPROXY settings that prevent lookup of dependencies
    29if [[ "${GOPROXY:-}" == "off" ]]; then
    30  kube::log::error "Cannot run with \$GOPROXY=off"
    31  exit 1
    32fi
    33
    34kube::golang::setup_env
    35kube::util::require-jq
    36
    37# Explicitly set GOFLAGS to ignore vendor, since GOFLAGS=-mod=vendor breaks dependency resolution while rebuilding vendor
    38export GOWORK=off
    39export GOFLAGS=-mod=mod
    40
    41dep="${1:-}"
    42sha="${2:-}"
    43
    44# Specifying a different repo is optional.
    45replacement=
    46case ${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        ;;
    56esac
    57
    58if [[ -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
    70fi
    71
    72# Find the resolved version before trying to use it.
    73echo "Running: go mod download ${replacement}@${sha}"
    74if meta=$(go mod download -json "${replacement}@${sha}"); then
    75    rev=$(echo "${meta}" | jq -r ".Version")
    76else
    77    error=$(echo "${meta}" | jq -r ".Error")
    78    echo "Download failed: ${error}" >&2
    79    exit 1
    80fi
    81echo "Resolved to ${replacement}@${rev}"
    82
    83# Add the require directive
    84echo "Running: go mod edit -require ${dep}@${rev}"
    85go mod edit -require "${dep}@${rev}"
    86
    87# Add the replace directive
    88if [ "${replacement}" != "${dep}" ]; then
    89  echo "Running: go mod edit -replace ${dep}=${replacement}@${rev}"
    90  go mod edit -replace "${dep}=${replacement}@${rev}"
    91fi
    92
    93# Propagate pinned version to staging repos
    94for 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
   110done
   111
   112echo ""
   113echo "Run hack/update-vendor.sh to rebuild the vendor directory"

View as plain text