|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +# Copyright 2020 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 file will be fetched as: curl -L https://git.io/getLatestKubebuilder | sh - |
| 18 | +# so it should be pure bourne shell, not bash (and not reference other scripts) |
| 19 | + |
| 20 | +set -eu |
| 21 | + |
| 22 | +# To use envtest is required to have etcd, kube-apiserver and kubetcl binaries installed locally. |
| 23 | +# This script will create the directory testbin and perform this setup for linux or mac os x envs in |
| 24 | + |
| 25 | +# Kubernetes version e.g v1.18.2 |
| 26 | +K8S_VER=$1 |
| 27 | +# ETCD version e.g v3.4.3 |
| 28 | +ETCD_VER=$2 |
| 29 | +OS=$(uname -s | tr '[:upper:]' '[:lower:]') |
| 30 | +ARCH=$(uname -m | sed 's/x86_64/amd64/') |
| 31 | +ETCD_EXT="tar.gz" |
| 32 | +TESTBIN_DIR=testbin |
| 33 | + |
| 34 | +function setup_testenv_bin() { |
| 35 | + # Do nothing if the $TESTBIN_DIR directory exist already. |
| 36 | + if [ ! -d $TESTBIN_DIR ]; then |
| 37 | + mkdir -p $TESTBIN_DIR |
| 38 | + |
| 39 | + # install etcd binary |
| 40 | + # the extension for linux env is not equals for mac os x |
| 41 | + if [ $OS == "darwin" ]; then |
| 42 | + ETCD_EXT="zip" |
| 43 | + fi |
| 44 | + [[ -x ${TESTBIN_DIR}/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C ${TESTBIN_DIR} --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd |
| 45 | + |
| 46 | + # install kube-apiserver and kubetcl binaries |
| 47 | + if [ $OS == "darwin" ] |
| 48 | + then |
| 49 | + # kubernetes do not provide the kubernetes-server for darwin, |
| 50 | + # In this way, to have the kube-apiserver is required to build it locally |
| 51 | + # if the project is cloned locally already do nothing |
| 52 | + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then |
| 53 | + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b ${K8S_VER} |
| 54 | + fi |
| 55 | + |
| 56 | + # if the kube-apiserve is built already then, just copy it |
| 57 | + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then |
| 58 | + DIR=$(pwd) |
| 59 | + cd $GOPATH/src/k8s.io/kubernetes |
| 60 | + # Build for linux first otherwise it won't work for darwin - :( |
| 61 | + export KUBE_BUILD_PLATFORMS="linux/amd64" |
| 62 | + make WHAT=cmd/kube-apiserver |
| 63 | + export KUBE_BUILD_PLATFORMS="darwin/amd64" |
| 64 | + make WHAT=cmd/kube-apiserver |
| 65 | + cd ${DIR} |
| 66 | + fi |
| 67 | + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/ |
| 68 | + |
| 69 | + # setup kubectl binary |
| 70 | + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl |
| 71 | + chmod +x kubectl |
| 72 | + mv kubectl $TESTBIN_DIR/ |
| 73 | + |
| 74 | + # allow run the tests without the Mac OS Firewall popup shows for each execution |
| 75 | + codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver |
| 76 | + else |
| 77 | + [[ -x $TESTBIN_DIR/kube-apiserver && -x ${TESTBIN_DIR}/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C ${TESTBIN_DIR} --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl |
| 78 | + fi |
| 79 | + fi |
| 80 | + export PATH=/$TESTBIN_DIR:$PATH |
| 81 | + export TEST_ASSET_KUBECTL=/$TESTBIN_DIR/kubectl |
| 82 | + export TEST_ASSET_KUBE_APISERVER=/$TESTBIN_DIR/kube-apiserver |
| 83 | + export TEST_ASSET_ETCD=/$TESTBIN_DIR/etcd |
| 84 | +} |
| 85 | + |
| 86 | +setup_testenv_bin |
0 commit comments