|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Copyright 2017 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 | +# updates the godeps.json file in the staging folders to allow clean vendoring |
| 18 | +# based on kubernetes levels. |
| 19 | + |
| 20 | +set -o errexit |
| 21 | +set -o nounset |
| 22 | +set -o pipefail |
| 23 | + |
| 24 | +KUBE_ROOT=$(dirname "${BASH_SOURCE}")/.. |
| 25 | +source "${KUBE_ROOT}/hack/lib/init.sh" |
| 26 | + |
| 27 | +# Sets default values |
| 28 | +DRY_RUN=false |
| 29 | +FAIL_ON_DIFF=false |
| 30 | +GODEP_OPTS="" |
| 31 | + |
| 32 | +# Controls verbosity of the script output and logging. |
| 33 | +KUBE_VERBOSE="${KUBE_VERBOSE:-5}" |
| 34 | +if (( ${KUBE_VERBOSE} >= 6 )); then |
| 35 | + GODEP_OPTS+=("-v") |
| 36 | +fi |
| 37 | + |
| 38 | +while getopts ":df" opt; do |
| 39 | + case $opt in |
| 40 | + d) # do not godep-restore into a temporary directory, but use the existing GOPATH |
| 41 | + DRY_RUN=true |
| 42 | + ;; |
| 43 | + f) # fail if something in the Godeps.json files changed |
| 44 | + FAIL_ON_DIFF=true |
| 45 | + ;; |
| 46 | + \?) |
| 47 | + echo "Invalid option: -$OPTARG" >&2 |
| 48 | + exit 1 |
| 49 | + ;; |
| 50 | + esac |
| 51 | +done |
| 52 | + |
| 53 | +kube::util::ensure_dockerized |
| 54 | +kube::golang::setup_env |
| 55 | +kube::util::ensure_single_dir_gopath |
| 56 | +kube::util::ensure_no_staging_repos_in_gopath |
| 57 | +kube::util::ensure_godep_version v79 |
| 58 | + |
| 59 | +kube::log::status "Checking whether godeps are restored" |
| 60 | +if ! kube::util::godep_restored 2>&1 | sed 's/^/ /'; then |
| 61 | + ${KUBE_ROOT}/hack/godep-restore.sh |
| 62 | +fi |
| 63 | + |
| 64 | +kube::util::ensure-temp-dir |
| 65 | +TMP_GOPATH="${KUBE_TEMP}/go" |
| 66 | + |
| 67 | +function updateGodepManifest() { |
| 68 | + pushd "${TMP_GOPATH}/src/k8s.io/${repo}" >/dev/null |
| 69 | + kube::log::status "Updating godeps for k8s.io/${repo}" |
| 70 | + rm -rf Godeps # remove the current Godeps.json so we always rebuild it |
| 71 | + GOPATH="${TMP_GOPATH}:${GOPATH}:${GOPATH}/src/k8s.io/kubernetes/staging" godep save ${GODEP_OPTS} ./... 2>&1 | sed 's/^/ /' |
| 72 | + |
| 73 | + # Rewriting Godeps.json to remove commits that don't really exist because we haven't pushed the prereqs yet |
| 74 | + go run "${KUBE_ROOT}/staging/godeps-json-updater.go" --godeps-file="${TMP_GOPATH}/src/k8s.io/${repo}/Godeps/Godeps.json" --override-import-path="k8s.io/${repo}" |
| 75 | + |
| 76 | + # commit so that following repos do not see this repo as dirty |
| 77 | + git add vendor >/dev/null |
| 78 | + git commit -a -m "Updated Godeps.json" >/dev/null |
| 79 | + popd >/dev/null |
| 80 | +} |
| 81 | + |
| 82 | +function diffGodepManifest() { |
| 83 | + local ret=0 |
| 84 | + diff --ignore-matching-lines='^\s*\"GoVersion\":' --ignore-matching-line='^\s*\"GodepVersion\":' --ignore-matching-lines='^\s*\"Comment\"' -u "${KUBE_ROOT}/staging/src/k8s.io/${repo}/Godeps/Godeps.json" "${TMP_GOPATH}/src/k8s.io/${repo}/Godeps/Godeps.json" || ret=$? |
| 85 | + if [[ "${ret}" != "0" && "${FAIL_ON_DIFF}" == true ]]; then |
| 86 | + exit ${ret} |
| 87 | + fi |
| 88 | +} |
| 89 | + |
| 90 | +# move into staging and save the dependencies for everything in order |
| 91 | +mkdir -p "${TMP_GOPATH}/src/k8s.io" |
| 92 | +for repo in $(ls ${KUBE_ROOT}/staging/src/k8s.io); do |
| 93 | + cp -a "${KUBE_ROOT}/staging/src/k8s.io/${repo}" "${TMP_GOPATH}/src/k8s.io/" |
| 94 | + |
| 95 | + pushd "${TMP_GOPATH}/src/k8s.io/${repo}" >/dev/null |
| 96 | + git init >/dev/null |
| 97 | + git config --local user.email "nobody@k8s.io" |
| 98 | + git config --local user.name "$0" |
| 99 | + git add . >/dev/null |
| 100 | + git commit -q -m "Snapshot" >/dev/null |
| 101 | + popd >/dev/null |
| 102 | + |
| 103 | + updateGodepManifest |
| 104 | + diffGodepManifest |
| 105 | + |
| 106 | + if [ "${DRY_RUN}" != true ]; then |
| 107 | + cp "${TMP_GOPATH}/src/k8s.io/${repo}/Godeps/Godeps.json" "${KUBE_ROOT}/staging/src/k8s.io/${repo}/Godeps/Godeps.json" |
| 108 | + # Assume Godeps.json is not updated, as the working tree needs to be clean |
| 109 | + # Without this, the script would pause after each staging repo to prompt the |
| 110 | + # user to commit all changes (difficult inside a container). It's safe to |
| 111 | + # ignore this file, as we know it's going to be changing, and we don't copy |
| 112 | + # the git tree back out from the container. |
| 113 | + git update-index --assume-unchanged "${KUBE_ROOT}/staging/src/k8s.io/${repo}/Godeps/Godeps.json" |
| 114 | + fi |
| 115 | +done |
0 commit comments