|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# Copyright 2018 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 | +set -e |
| 18 | +set -x |
| 19 | + |
| 20 | +# Make sure, we run in the root of the repo and |
| 21 | +# therefore run the tests on all packages |
| 22 | +base_dir="$( cd "$(dirname "$0")/" && pwd )" |
| 23 | +cd "$base_dir" || { |
| 24 | + echo "Cannot cd to '$base_dir'. Aborting." >&2 |
| 25 | + exit 1 |
| 26 | +} |
| 27 | + |
| 28 | +k8s_version=1.9 |
| 29 | +goarch=amd64 |
| 30 | +goos="unknown" |
| 31 | + |
| 32 | +if [[ "$OSTYPE" == "linux-gnu" ]]; then |
| 33 | + goos="linux" |
| 34 | +elif [[ "$OSTYPE" == "darwin"* ]]; then |
| 35 | + goos="darwin" |
| 36 | +fi |
| 37 | + |
| 38 | +if [[ "$goos" == "unknown" ]]; then |
| 39 | + echo "OS '$OSTYPE' not supported. Aborting." >&2 |
| 40 | + exit 1 |
| 41 | +fi |
| 42 | + |
| 43 | +rc=0 |
| 44 | +tmp_root=/tmp |
| 45 | + |
| 46 | +kb_root_dir=$tmp_root/kubebuilder |
| 47 | +kb_vendor_dir=$tmp_root/vendor |
| 48 | + |
| 49 | +function prepare_staging_dir { |
| 50 | + rm -rf $kb_root_dir |
| 51 | +} |
| 52 | + |
| 53 | +# fetch k8s API gen tools and make it available under kb_root_dir/bin. |
| 54 | +function fetch_tools { |
| 55 | + kb_tools_archive_name=kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz |
| 56 | + kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name" |
| 57 | + |
| 58 | + kb_tools_archive_path=$tmp_root/$kb_tools_archive_name |
| 59 | + if [ ! -f $kb_tools_archive_path ]; then |
| 60 | + curl -sL ${kb_tools_download_url} -o $kb_tools_archive_path |
| 61 | + fi |
| 62 | + tar -zxf $kb_tools_archive_path -C $tmp_root/ |
| 63 | +} |
| 64 | + |
| 65 | +function build_kb { |
| 66 | + go build -o $tmp_root/kubebuilder/bin/kubebuilder ./cmd/kubebuilder |
| 67 | + go build -o $tmp_root/kubebuilder/bin/kubebuilder-gen ./cmd/kubebuilder-gen |
| 68 | +} |
| 69 | + |
| 70 | +function prepare_vendor_deps { |
| 71 | + # TODO(droot): clean up this function |
| 72 | + rm -rf $kb_vendor_dir && rm -f $tmp_root/Gopkg.toml && rm -f $tmp_root/Gopkg.lock |
| 73 | + mkdir -p $kb_vendor_dir/github.com/kubernetes-sigs/kubebuilder/pkg/ || echo "" |
| 74 | + cp -r pkg/* $kb_vendor_dir/github.com/kubernetes-sigs/kubebuilder/pkg/ |
| 75 | + cp LICENSE $kb_vendor_dir/github.com/kubernetes-sigs/kubebuilder/LICENSE |
| 76 | + cp Gopkg.toml Gopkg.lock $tmp_root/ |
| 77 | + cp -a vendor/* $kb_vendor_dir/ |
| 78 | + cd $tmp_root |
| 79 | + sed -i "s/KUBEBUILDER_VERSION/"${VERSION-master}"/" Gopkg.toml |
| 80 | + tar -czf $kb_root_dir/bin/vendor.tar.gz vendor/ Gopkg.lock Gopkg.toml |
| 81 | +} |
| 82 | + |
| 83 | +function prepare_testdir_under_gopath { |
| 84 | + kb_test_dir=$GOPATH/src/github.com/kubernetes-sigs/kubebuilder-test |
| 85 | + rm -rf $kb_test_dir && mkdir -p $kb_test_dir && cd $kb_test_dir |
| 86 | +} |
| 87 | + |
| 88 | +function generate_crd_resources { |
| 89 | +# Setup env vars |
| 90 | + export PATH=/tmp/kubebuilder/bin/:$PATH |
| 91 | + export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl |
| 92 | + export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver |
| 93 | + export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd |
| 94 | + |
| 95 | + # Run the commands |
| 96 | + kubebuilder init repo --domain sample.kubernetes.io |
| 97 | + kubebuilder create resource --group insect --version v1beta1 --kind Bee |
| 98 | + kubebuilder create resource --group insect --version v1beta1 --kind Wasp |
| 99 | +} |
| 100 | + |
| 101 | +function test_generated_controller { |
| 102 | + # Verify the controller-manager builds and the tests pass |
| 103 | + go build ./cmd/... |
| 104 | + go build ./pkg/... |
| 105 | + go test ./cmd/... |
| 106 | + go test ./pkg/... |
| 107 | +} |
| 108 | + |
| 109 | +prepare_staging_dir |
| 110 | +fetch_tools |
| 111 | +build_kb |
| 112 | +prepare_vendor_deps |
| 113 | +prepare_testdir_under_gopath |
| 114 | +generate_crd_resources |
| 115 | +test_generated_controller |
| 116 | + |
| 117 | +exit $rc |
0 commit comments