From 38b5fbc14ceebe622b13a4fe7cca2d6ecc7dfcd3 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Thu, 23 Jul 2020 18:24:07 +0100 Subject: [PATCH] feat: add script in the repo to setup envtest --- .gitignore | 1 + scripts/setup_envtest_bins.sh | 115 +++++++++++++++++++++------------- 2 files changed, 71 insertions(+), 45 deletions(-) diff --git a/.gitignore b/.gitignore index 69415a7d940..8c3a9d0f52a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,7 @@ docs/book/book/ # skip bin bin/* +testbin/* # skip .out files (coverage tests) *.out diff --git a/scripts/setup_envtest_bins.sh b/scripts/setup_envtest_bins.sh index d5992161c85..5c436b94d24 100755 --- a/scripts/setup_envtest_bins.sh +++ b/scripts/setup_envtest_bins.sh @@ -1,9 +1,26 @@ +#!/bin/sh + +# Copyright 2020 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. + +# This file will be fetched as: curl -L https://git.io/getLatestKubebuilder | sh - +# so it should be pure bourne shell, not bash (and not reference other scripts) + set -eu -# */ -# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory. -# This script will perform this setup for linux or mac os x envs. -# */ +# To use envtest is required to have etcd, kube-apiserver and kubetcl binaries installed locally. +# This script will create the directory testbin and perform this setup for linux or mac os x envs in K8S_VER=v1.18.2 ETCD_VER=v3.4.3 @@ -12,48 +29,56 @@ ARCH=$(uname -m | sed 's/x86_64/amd64/') ETCD_EXT="tar.gz" TESTBIN_DIR=testbin -# Do nothing if the $TESTBIN_DIR directory exist already. -if [ ! -d $TESTBIN_DIR ]; then - mkdir -p $TESTBIN_DIR +function setup_testenv_bin() { + # Do nothing if the $TESTBIN_DIR directory exist already. + if [ ! -d $TESTBIN_DIR ]; then + mkdir -p $TESTBIN_DIR - # install etcd binary - # the extension for linux env is not equals for mac os x - if [ $OS == "darwin" ]; then - ETCD_EXT="zip" - fi - [[ -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 - - # install kube-apiserver and kubetcl binaries - if [ $OS == "darwin" ] - then - # kubernetes do not provide the kubernetes-server for darwin, - # In this way, to have the kube-apiserver is required to build it locally - # if the project is cloned locally already do nothing - if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then - git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + # install etcd binary + # the extension for linux env is not equals for mac os x + if [ $OS == "darwin" ]; then + ETCD_EXT="zip" fi - - # if the kube-apiserve is built already then, just copy it - if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then - DIR=$(pwd) - cd $GOPATH/src/k8s.io/kubernetes - # Build for linux first otherwise it won't work for darwin - :( - export KUBE_BUILD_PLATFORMS="linux/amd64" - make WHAT=cmd/kube-apiserver - export KUBE_BUILD_PLATFORMS="darwin/amd64" - make WHAT=cmd/kube-apiserver - cd ${DIR} + [[ -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 + + # install kube-apiserver and kubetcl binaries + if [ $OS == "darwin" ] + then + # kubernetes do not provide the kubernetes-server for darwin, + # In this way, to have the kube-apiserver is required to build it locally + # if the project is cloned locally already do nothing + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + fi + + # if the kube-apiserve is built already then, just copy it + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then + DIR=$(pwd) + cd $GOPATH/src/k8s.io/kubernetes + # Build for linux first otherwise it won't work for darwin - :( + export KUBE_BUILD_PLATFORMS="linux/amd64" + make WHAT=cmd/kube-apiserver + export KUBE_BUILD_PLATFORMS="darwin/amd64" + make WHAT=cmd/kube-apiserver + cd ${DIR} + fi + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/ + + # setup kubectl binary + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl + chmod +x kubectl + mv kubectl $TESTBIN_DIR/ + + # allow run the tests without the Mac OS Firewall popup shows for each execution + codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver + else + [[ -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 fi - cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/ - - # setup kubectl binary - curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl - chmod +x kubectl - mv kubectl $TESTBIN_DIR/ - - # allow run the tests without the Mac OS Firewall popup shows for each execution - codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver - else - [[ -x testbin/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 fi -fi + export PATH=/$TESTBIN_DIR:$PATH + export TEST_ASSET_KUBECTL=/$TESTBIN_DIR/kubectl + export TEST_ASSET_KUBE_APISERVER=/$TESTBIN_DIR/kube-apiserver + export TEST_ASSET_ETCD=/$TESTBIN_DIR/etcd +} + +setup_testenv_bin \ No newline at end of file