2016-08-06 05:56:30 +00:00
#!/bin/bash
# Copyright 2016 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -o errexit
set -o nounset
set -o pipefail
2017-02-23 17:13:15 +00:00
KUBE_ROOT = $( dirname " ${ BASH_SOURCE } " ) /..
source " ${ KUBE_ROOT } /hack/lib/init.sh "
2017-01-31 08:47:01 +00:00
FAIL_ON_CHANGES = false
DRY_RUN = false
2017-02-23 17:13:15 +00:00
RUN_FROM_UPDATE_SCRIPT = false
while getopts ":fdu" opt; do
2016-10-11 23:18:50 +00:00
case $opt in
2017-01-31 08:47:01 +00:00
f)
FAIL_ON_CHANGES = true
; ;
d)
DRY_RUN = true
2016-10-11 23:18:50 +00:00
; ;
2017-02-23 17:13:15 +00:00
u)
RUN_FROM_UPDATE_SCRIPT = true
; ;
2016-10-11 23:18:50 +00:00
\? )
echo " Invalid option: - $OPTARG " >& 2
2017-02-23 17:13:15 +00:00
exit 1
2016-10-11 23:18:50 +00:00
; ;
esac
done
2017-01-31 08:47:01 +00:00
readonly FAIL_ON_CHANGES DRY_RUN
2016-10-11 23:18:50 +00:00
2017-02-23 17:13:15 +00:00
if [ " ${ RUN_FROM_UPDATE_SCRIPT } " != true ] ; then
echo "Do not run this script directly, but via hack/update-staging-client-go.sh."
exit 1
fi
2016-08-06 05:56:30 +00:00
# PREREQUISITES: run `godep restore` in the main repo before calling this script.
2017-02-28 07:37:28 +00:00
kube::util::ensure-temp-dir
TMP_GOPATH = " ${ KUBE_TEMP } /go "
mkdir -p " ${ TMP_GOPATH } /src/k8s.io "
2016-12-05 17:15:59 +00:00
CLIENTSET = "clientset"
2016-10-11 23:18:50 +00:00
MAIN_REPO_FROM_SRC = "k8s.io/kubernetes"
2017-02-23 17:13:15 +00:00
MAIN_REPO = " $( cd " ${ KUBE_ROOT } " ; pwd ) " # absolute path
2016-10-11 23:18:50 +00:00
CLIENT_REPO_FROM_SRC = "k8s.io/client-go"
2016-08-06 05:56:30 +00:00
CLIENT_REPO = " ${ MAIN_REPO } /staging/src/ ${ CLIENT_REPO_FROM_SRC } "
2017-02-28 07:37:28 +00:00
CLIENT_REPO_TEMP = " ${ TMP_GOPATH } /src/k8s.io/construct-client-go "
2016-08-06 05:56:30 +00:00
2017-01-30 10:59:29 +00:00
if LANG = C sed --help 2>& 1 | grep -q GNU; then
SED = "sed"
elif which gsed & >/dev/null; then
SED = "gsed"
else
echo "Failed to find GNU sed as sed or gsed. If you are on Mac: brew install gnu-sed." >& 2
exit 1
fi
2016-08-29 21:10:06 +00:00
2016-08-06 05:56:30 +00:00
# working in the ${CLIENT_REPO_TEMP} so 'godep save' won't complain about dirty working tree.
2016-10-11 23:18:50 +00:00
echo "creating the tmp directory"
2016-08-06 05:56:30 +00:00
mkdir -p " ${ CLIENT_REPO_TEMP } "
cd " ${ CLIENT_REPO } "
2017-01-16 21:20:38 +00:00
# there are two classes of package in staging/client-go, those which are authoritative (client-go has the only copy)
# and those which are copied and rewritten (client-go is not authoritative).
# we first copy out the authoritative packages to the temp location, then copy non-authoritative packages
# then save over the original
# save copies code from client-go into the temp folder to make sure we don't lose it by accident
# TODO this is temporary until everything in certain directories is authoritative
function save( ) {
2017-02-24 15:03:08 +00:00
mkdir -p " $( dirname " ${ CLIENT_REPO_TEMP } / $1 " ) "
cp -r " ${ CLIENT_REPO } / $1 " * " ${ CLIENT_REPO_TEMP } / "
2017-01-16 21:20:38 +00:00
}
# save everything for which the staging directory is the source of truth
2017-01-25 19:00:30 +00:00
save "discovery"
save "dynamic"
2017-01-19 15:07:51 +00:00
save "rest"
2017-01-25 20:07:10 +00:00
save "testing"
2017-01-30 18:39:54 +00:00
save "tools"
2017-01-20 17:54:43 +00:00
save "transport"
2017-01-27 15:20:40 +00:00
save "third_party"
2017-01-19 21:35:04 +00:00
save "plugin"
2017-01-23 18:37:22 +00:00
save "util"
2017-02-13 13:00:18 +00:00
save "examples"
save "OWNERS"
2017-01-16 21:20:38 +00:00
2016-08-06 05:56:30 +00:00
# mkcp copies file from the main repo to the client repo, it creates the directory if it doesn't exist in the client repo.
function mkcp( ) {
2017-02-24 15:03:08 +00:00
mkdir -p " ${ CLIENT_REPO_TEMP } / $2 " && cp -r " ${ MAIN_REPO } / $1 " " ${ CLIENT_REPO_TEMP } / $2 "
2016-08-06 05:56:30 +00:00
}
2017-01-16 21:20:38 +00:00
# assemble all the other parts of the staging directory
2016-08-06 05:56:30 +00:00
echo "copying client packages"
2017-01-25 20:07:10 +00:00
# need to copy version. We aren't authoritative here
# version has subdirs which we don't need. Only copy the files we want
mkdir -p " ${ CLIENT_REPO_TEMP } /pkg/version "
find " ${ MAIN_REPO } /pkg/version " -maxdepth 1 -type f | xargs -I{ } cp { } " ${ CLIENT_REPO_TEMP } /pkg/version "
# need to copy clientsets, though later we should copy APIs and later generate clientsets
2016-09-16 20:20:03 +00:00
mkcp " pkg/client/clientset_generated/ ${ CLIENTSET } " "pkg/client/clientset_generated"
2017-02-13 16:50:12 +00:00
mkcp "pkg/client/informers/informers_generated/externalversions" "pkg/client/informers/informers_generated"
2017-04-13 20:19:08 +00:00
mkcp "pkg/api/helper" "pkg/api"
2017-04-17 17:56:40 +00:00
mkcp "pkg/api/v1/resource" "pkg/api/v1"
mkcp "pkg/api/v1/node" "pkg/api/v1"
2016-08-06 05:56:30 +00:00
2016-08-29 21:10:06 +00:00
pushd " ${ CLIENT_REPO_TEMP } " > /dev/null
2017-02-28 07:37:28 +00:00
echo "generating vendor/"
# make snapshots for repos in staging/"
for repo in $( ls ${ KUBE_ROOT } /staging/src/k8s.io) ; do
cp -a " ${ KUBE_ROOT } /staging/src/k8s.io/ ${ repo } " " ${ TMP_GOPATH } /src/k8s.io/ "
pushd " ${ TMP_GOPATH } /src/k8s.io/ ${ repo } " >/dev/null
git init >/dev/null
git config --local user.email "nobody@k8s.io"
git config --local user.name " $0 "
git add . >/dev/null
git commit -q -m "Snapshot" >/dev/null
popd >/dev/null
done
# client-go depends on some apimachinery packages. Adding ${TMP_GOPATH} to the
# GOPATH so that if client-go has new dependencies on apimachinery, `godep save`
# can find the dependent packages from ${TMP_GOPATH}, instead of failing. Note
# that in Godeps.json, the "Rev"s of the entries for k8s.io/apimachinery will be
# invalid, they will be updated later by the publish robot to point to the real
# k8s.io/apimachinery commit.
GOPATH = " ${ TMP_GOPATH } : ${ GOPATH } " godep save ./...
2016-08-29 21:10:06 +00:00
popd > /dev/null
2016-08-06 05:56:30 +00:00
2016-11-22 22:18:50 +00:00
echo "moving vendor/k8s.io/kubernetes"
2017-01-15 21:13:24 +00:00
cp -r " ${ CLIENT_REPO_TEMP } " /vendor/k8s.io/kubernetes/* " ${ CLIENT_REPO_TEMP } " /
2017-02-28 07:37:28 +00:00
# the publish robot will refill the vendor/
rm -rf " ${ CLIENT_REPO_TEMP } " /vendor
2016-08-06 05:56:30 +00:00
2016-08-29 21:10:06 +00:00
echo "rewriting Godeps.json"
2017-02-28 07:37:28 +00:00
# The entries for k8s.io/apimahcinery are not removed from Godeps.json, though
# they contain the invalid commit revision. The publish robot will set the
# correct commit revision.
2017-04-06 07:21:27 +00:00
go run " ${ KUBE_ROOT } /staging/godeps-json-updater.go " --godeps-file= " ${ CLIENT_REPO_TEMP } /Godeps/Godeps.json " --override-import-path= " ${ CLIENT_REPO_FROM_SRC } " --ignored-prefixes= "k8s.io/client-go,k8s.io/kubernetes" --rewritten-prefixes= "k8s.io/apimachinery"
2016-08-29 21:10:06 +00:00
2016-08-06 05:56:30 +00:00
echo "rewriting imports"
2016-10-11 23:18:50 +00:00
grep -Rl " \" ${ MAIN_REPO_FROM_SRC } " " ${ CLIENT_REPO_TEMP } " | \
2017-02-24 15:03:08 +00:00
grep "\.go" | \
grep -v "vendor/" | \
xargs ${ SED } -i " s|\" ${ MAIN_REPO_FROM_SRC } |\" ${ CLIENT_REPO_FROM_SRC } |g "
2016-08-06 05:56:30 +00:00
echo "rewrite proto names in proto.RegisterType"
2017-01-30 10:59:29 +00:00
find " ${ CLIENT_REPO_TEMP } " -type f -name "generated.pb.go" -print0 | xargs -0 ${ SED } -i "s/k8s\.io\.kubernetes/k8s.io.client-go/g"
2016-08-06 05:56:30 +00:00
2017-01-15 21:13:24 +00:00
# strip all generator tags from client-go
2017-01-30 10:59:29 +00:00
find " ${ CLIENT_REPO_TEMP } " -type f -name "*.go" -print0 | xargs -0 ${ SED } -i '/^\/\/ +k8s:openapi-gen=true/d'
find " ${ CLIENT_REPO_TEMP } " -type f -name "*.go" -print0 | xargs -0 ${ SED } -i '/^\/\/ +k8s:defaulter-gen=/d'
find " ${ CLIENT_REPO_TEMP } " -type f -name "*.go" -print0 | xargs -0 ${ SED } -i '/^\/\/ +k8s:deepcopy-gen=/d'
find " ${ CLIENT_REPO_TEMP } " -type f -name "*.go" -print0 | xargs -0 ${ SED } -i '/^\/\/ +k8s:conversion-gen=/d'
2017-01-15 21:13:24 +00:00
2016-08-06 05:56:30 +00:00
echo "rearranging directory layout"
2016-10-11 23:18:50 +00:00
# $1 and $2 are relative to ${CLIENT_REPO_TEMP}
2016-08-06 05:56:30 +00:00
function mvfolder {
2017-02-24 15:03:08 +00:00
local src = ${ 1 %/#/ }
local dst = ${ 2 %/#/ }
mkdir -p " ${ CLIENT_REPO_TEMP } / ${ dst } "
# move
mv " ${ CLIENT_REPO_TEMP } / ${ src } " /* " ${ CLIENT_REPO_TEMP } / ${ dst } "
# rewrite package
local src_package = " ${ src ##*/ } "
local dst_package = " ${ dst ##*/ } "
find " ${ CLIENT_REPO_TEMP } / ${ dst } " -type f -name "*.go" -print0 | xargs -0 ${ SED } -i " s,package ${ src_package } ,package ${ dst_package } ,g "
{ grep -Rl " \" ${ CLIENT_REPO_FROM_SRC } / ${ src } " " ${ CLIENT_REPO_TEMP } " || true ; } | while read -r target ; do
# rewrite imports
# the first rule is to convert import lines like `restclient "k8s.io/client-go/pkg/client/restclient"`,
# where a package alias is the same the package name.
${ SED } -i " s,\< ${ src_package } \" ${ CLIENT_REPO_FROM_SRC } / ${ src } , ${ dst_package } \" ${ CLIENT_REPO_FROM_SRC } / ${ dst } ,g " " ${ target } "
${ SED } -i " s,\" ${ CLIENT_REPO_FROM_SRC } / ${ src } ,\" ${ CLIENT_REPO_FROM_SRC } / ${ dst } ,g " " ${ target } "
# rewrite import invocation
if [ " ${ src_package } " != " ${ dst_package } " ] ; then
${ SED } -i " s,\< ${ src_package } \.\([a-zA-Z]\), ${ dst_package } \.\1,g " " ${ target } "
fi
done
2016-08-06 05:56:30 +00:00
}
2016-09-16 20:20:03 +00:00
mvfolder " pkg/client/clientset_generated/ ${ CLIENTSET } " kubernetes
2017-04-14 21:45:03 +00:00
rm -f " ${ CLIENT_REPO_TEMP } /kubernetes/import_known_versions.go "
2017-02-13 16:50:12 +00:00
mvfolder "pkg/client/informers/informers_generated/externalversions" informers
mvfolder "pkg/client/listers" listers
2016-10-11 23:18:50 +00:00
if [ " $( find " ${ CLIENT_REPO_TEMP } " /pkg/client -type f -name "*.go" ) " ] ; then
2017-02-24 15:03:08 +00:00
echo " ${ CLIENT_REPO_TEMP } /pkg/client is expected to be empty "
exit 1
2016-11-15 02:46:06 +00:00
else
2017-02-24 15:03:08 +00:00
rm -r " ${ CLIENT_REPO_TEMP } " /pkg/client
2016-08-06 05:56:30 +00:00
fi
echo "running gofmt"
2016-10-11 23:18:50 +00:00
find " ${ CLIENT_REPO_TEMP } " -type f -name "*.go" -print0 | xargs -0 gofmt -w
echo "remove black listed files"
find " ${ CLIENT_REPO_TEMP } " -type f \( \
2017-02-24 15:03:08 +00:00
-name "*.json" -not -name "Godeps.json" -o \
-name "*.yaml" -o \
-name "*.yml" -o \
-name "*.sh" \
\) -delete
2016-10-11 23:18:50 +00:00
2017-01-31 08:47:01 +00:00
if [ " ${ FAIL_ON_CHANGES } " = true ] ; then
2017-02-23 17:13:15 +00:00
echo "running FAIL_ON_CHANGES"
2017-03-01 01:13:53 +00:00
# ignore base.go in diff
cp " ${ CLIENT_REPO } /pkg/version/base.go " " ${ CLIENT_REPO_TEMP } /pkg/version/ "
2017-04-12 20:30:47 +00:00
# TODO(mikedanese): figure out what to do with BUILD files here
if diff -NauprB -x '*BUILD' -I '^\s*\"Comment\"' -I "GoVersion.*\|GodepVersion.*" " ${ CLIENT_REPO } " " ${ CLIENT_REPO_TEMP } " ; then
2017-02-23 17:13:15 +00:00
echo " ${ CLIENT_REPO } up to date. "
exit 0
else
echo " ${ CLIENT_REPO } is out of date. Please run hack/update-staging-client-go.sh "
exit 1
fi
2016-10-11 23:18:50 +00:00
fi
# clean the ${CLIENT_REPO}
2017-01-31 08:47:01 +00:00
echo "move to the client repo"
if [ " ${ DRY_RUN } " = false ] ; then
2017-02-24 15:03:08 +00:00
ls " ${ CLIENT_REPO } " | { grep -v '_tmp' || true; } | xargs rm -rf
mv " ${ CLIENT_REPO_TEMP } " /* " ${ CLIENT_REPO } "
2017-04-17 17:43:53 +00:00
git checkout HEAD -- $( find " ${ CLIENT_REPO } " -name BUILD)
2017-01-31 08:47:01 +00:00
fi