Skip to content

Commit c87bc6a

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

File tree

2 files changed

+71
-45
lines changed

2 files changed

+71
-45
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

+70-45
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
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

825
K8S_VER=v1.18.2
926
ETCD_VER=v3.4.3
@@ -12,48 +29,56 @@ ARCH=$(uname -m | sed 's/x86_64/amd64/')
1229
ETCD_EXT="tar.gz"
1330
TESTBIN_DIR=testbin
1431

15-
# Do nothing if the $TESTBIN_DIR directory exist already.
16-
if [ ! -d $TESTBIN_DIR ]; then
17-
mkdir -p $TESTBIN_DIR
32+
function setup_testenv_bin() {
33+
# Do nothing if the $TESTBIN_DIR directory exist already.
34+
if [ ! -d $TESTBIN_DIR ]; then
35+
mkdir -p $TESTBIN_DIR
1836

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
37+
# install etcd binary
38+
# the extension for linux env is not equals for mac os x
39+
if [ $OS == "darwin" ]; then
40+
ETCD_EXT="zip"
3441
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}
42+
[[ -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
43+
44+
# install kube-apiserver and kubetcl binaries
45+
if [ $OS == "darwin" ]
46+
then
47+
# kubernetes do not provide the kubernetes-server for darwin,
48+
# In this way, to have the kube-apiserver is required to build it locally
49+
# if the project is cloned locally already do nothing
50+
if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then
51+
git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b ${K8S_VER}
52+
fi
53+
54+
# if the kube-apiserve is built already then, just copy it
55+
if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then
56+
DIR=$(pwd)
57+
cd $GOPATH/src/k8s.io/kubernetes
58+
# Build for linux first otherwise it won't work for darwin - :(
59+
export KUBE_BUILD_PLATFORMS="linux/amd64"
60+
make WHAT=cmd/kube-apiserver
61+
export KUBE_BUILD_PLATFORMS="darwin/amd64"
62+
make WHAT=cmd/kube-apiserver
63+
cd ${DIR}
64+
fi
65+
cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver $TESTBIN_DIR/
66+
67+
# setup kubectl binary
68+
curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl
69+
chmod +x kubectl
70+
mv kubectl $TESTBIN_DIR/
71+
72+
# allow run the tests without the Mac OS Firewall popup shows for each execution
73+
codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver
74+
else
75+
[[ -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
4676
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
5877
fi
59-
fi
78+
export PATH=/$TESTBIN_DIR:$PATH
79+
export TEST_ASSET_KUBECTL=/$TESTBIN_DIR/kubectl
80+
export TEST_ASSET_KUBE_APISERVER=/$TESTBIN_DIR/kube-apiserver
81+
export TEST_ASSET_ETCD=/$TESTBIN_DIR/etcd
82+
}
83+
84+
setup_testenv_bin

0 commit comments

Comments
 (0)