Skip to content

Commit 28e92a5

Browse files
feat: add script in the repo to setup envtest
1 parent b047665 commit 28e92a5

File tree

2 files changed

+75
-47
lines changed

2 files changed

+75
-47
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ docs/book/book/
1313

1414
# skip bin
1515
bin/*
16+
testbin/*
1617

1718
# skip .out files (coverage tests)
1819
*.out

scripts/setup_envtest_bins.sh

+74-47
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,86 @@
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+
120
set -eu
221

3-
# */
4-
# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory.
5-
# This script will perform this setup for linux or mac os x envs.
6-
# */
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
724

8-
K8S_VER=v1.18.2
9-
ETCD_VER=v3.4.3
25+
# Kubernetes version e.g v1.18.2
26+
K8S_VER=$1
27+
# ETCD version e.g v3.4.3
28+
ETCD_VER=$2
1029
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
1130
ARCH=$(uname -m | sed 's/x86_64/amd64/')
1231
ETCD_EXT="tar.gz"
1332
TESTBIN_DIR=testbin
1433

15-
# Do nothing if the $TESTBIN_DIR directory exist already.
16-
if [ ! -d $TESTBIN_DIR ]; then
17-
mkdir -p $TESTBIN_DIR
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
1838

19-
# install etcd binary
20-
# the extension for linux env is not equals for mac os x
21-
if [ $OS == "darwin" ]; then
22-
ETCD_EXT="zip"
23-
fi
24-
[[ -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
25-
26-
# install kube-apiserver and kubetcl binaries
27-
if [ $OS == "darwin" ]
28-
then
29-
# kubernetes do not provide the kubernetes-server for darwin,
30-
# In this way, to have the kube-apiserver is required to build it locally
31-
# if the project is cloned locally already do nothing
32-
if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then
33-
git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2
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"
3443
fi
35-
36-
# if the kube-apiserve is built already then, just copy it
37-
if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then
38-
DIR=$(pwd)
39-
cd $GOPATH/src/k8s.io/kubernetes
40-
# Build for linux first otherwise it won't work for darwin - :(
41-
export KUBE_BUILD_PLATFORMS="linux/amd64"
42-
make WHAT=cmd/kube-apiserver
43-
export KUBE_BUILD_PLATFORMS="darwin/amd64"
44-
make WHAT=cmd/kube-apiserver
45-
cd ${DIR}
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
4678
fi
47-
cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/
48-
49-
# setup kubectl binary
50-
curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl
51-
chmod +x kubectl
52-
mv kubectl $TESTBIN_DIR/
53-
54-
# allow run the tests without the Mac OS Firewall popup shows for each execution
55-
codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver
56-
else
57-
[[ -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
5879
fi
59-
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

Comments
 (0)