Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support step to install kind/kubectl and configure kind #1530

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions resources/scripts/install/kind-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash
set -exuo pipefail

kind create cluster --image "kindest/node:${K8S_VERSION}" --config - <<EOF
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
kubeadmConfigPatches:
- |
kind: ClusterConfiguration
scheduler:
extraArgs:
bind-address: "0.0.0.0"
port: "10251"
secure-port: "10259"
controllerManager:
extraArgs:
bind-address: "0.0.0.0"
port: "10252"
secure-port: "10257"
EOF
kubectl cluster-info
43 changes: 43 additions & 0 deletions resources/scripts/install/kind.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -exuo pipefail

MSG="environment variable missing."
DEFAULT_HOME="/usr/local"
KIND_VERSION=${KIND_VERSION:?$MSG}
HOME=${HOME:?$DEFAULT_HOME}
KIND_CMD="${HOME}/bin/kind"

if command -v kind
then
set +e
echo "Found Kind. Checking version.."
FOUND_KIND_VERSION=$(kind --version 2>&1 >/dev/null | awk '{print $3}')
if [ "$FOUND_KIND_VERSION" == "$KIND_VERSION" ]
then
echo "Versions match. No need to install Kind. Exiting."
exit 0
fi
set -e
fi

echo "UNMET DEP: Installing Kind"

OS=$(uname -s| tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m| tr '[:upper:]' '[:lower:]')
if [ "${ARCH}" == "aarch64" ] ; then
ARCH_SUFFIX=arm64
else
ARCH_SUFFIX=amd64
fi

mkdir -p "${HOME}/bin"

if curl -sSLo "${KIND_CMD}" "https://github.com/kubernetes-sigs/kind/releases/download/${KIND_VERSION}/kind-${OS}-${ARCH_SUFFIX}" ; then
chmod +x "${KIND_CMD}"
else
echo "Something bad with the download, let's delete the corrupted binary"
if [ -e "${KIND_CMD}" ] ; then
rm "${KIND_CMD}"
fi
exit 1
fi
43 changes: 43 additions & 0 deletions resources/scripts/install/kubectl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -exuo pipefail

MSG="parameter missing."
DEFAULT_HOME="/usr/local"
K8S_VERSION=${K8S_VERSION:?$MSG}
HOME=${HOME:?$DEFAULT_HOME}
KUBECTL_CMD="${HOME}/bin/kubectl"

if command -v kubectl
then
set +e
echo "Found kubectl. Checking version.."
FOUND_KUBECTL_VERSION=$(kubectl version --client --short 2>&1 >/dev/null | awk '{print $3}')
if [ "${FOUND_KUBECTL_VERSION}" == "${K8S_VERSION}" ]
then
echo "Versions match. No need to install kubectl. Exiting."
exit 0
fi
set -e
fi

echo "UNMET DEP: Installing kubectl"

mkdir -p "${HOME}/bin"

OS=$(uname -s| tr '[:upper:]' '[:lower:]')
ARCH=$(uname -m| tr '[:upper:]' '[:lower:]')
if [ "${ARCH}" == "aarch64" ] ; then
ARCH_SUFFIX=arm64
else
ARCH_SUFFIX=amd64
fi

if curl -sSLo "${KUBECTL_CMD}" "https://storage.googleapis.com/kubernetes-release/release/${K8S_VERSION}/bin/${OS}/${ARCH_SUFFIX}/kubectl" ; then
chmod +x "${KUBECTL_CMD}"
else
echo "Something bad with the download, let's delete the corrupted binary"
if [ -e "${KUBECTL_CMD}" ] ; then
rm "${KUBECTL_CMD}"
fi
exit 1
fi
79 changes: 79 additions & 0 deletions src/test/groovy/WithKindEnvStepTests.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.

import org.junit.Before
import org.junit.Test
import static org.junit.Assert.assertFalse
import static org.junit.Assert.assertTrue

class WithKindEnvStepTests extends ApmBasePipelineTest {

@Override
@Before
void setUp() throws Exception {
super.setUp()
script = loadScript('vars/withKindEnv.groovy')
}

@Test
void test_default() throws Exception {
def isOK = false
script.call {
isOK = true
}
printCallStack()
assertTrue(isOK)
assertTrue(assertMethodCallContainsPattern('withEnv', 'K8S_VERSION=v1.23.0, KIND_VERSION=v0.11.1'))
assertJobStatusSuccess()
}

@Test
void test_with_versions() throws Exception {
def isOK = false
script.call(k8sVersion: 'v1.0.0', kindVersion: 'v2.0.0') {
isOK = true
}
printCallStack()
assertTrue(isOK)
assertTrue(assertMethodCallContainsPattern('withEnv', 'K8S_VERSION=v1.0.0, KIND_VERSION=v2.0.0'))
assertTrue(assertMethodCallContainsPattern('sh', 'delete cluster'))
assertJobStatusSuccess()
}

@Test
void test_with_body_error() throws Exception {
def ret = false
try {
script.call(retries: 3) {
throw new Exception('Force failure')
ret = true
}
} catch(e) {
//NOOP
}
printCallStack()
assertFalse(ret)
assertTrue(assertMethodCallContainsPattern('sh', 'delete cluster'))
}

@Test
void test_windows() throws Exception {
testWindows() {
script.call() { }
}
}
}
72 changes: 72 additions & 0 deletions vars/withKindEnv.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you 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.

/**
Install Kind, Kubectl and configure Kind to run some command within the kind/kubectl context

withKindEnv(k8sVersion: 'v1.23.0', kindVersion: 'v0.11.1'){
..
}
**/

def call(Map args = [:], Closure body) {
if(!isUnix()){
error('withKindEnv: windows is not supported yet.')
}
def k8sVersion = args.get('k8sVersion', 'v1.23.0')
def kindVersion = args.get('kindVersion', 'v0.11.1')

withEnv(["K8S_VERSION=${k8sVersion}", "KIND_VERSION=${kindVersion}", "KUBECONFIG=${env.WORKSPACE}/kubecfg"]){
installKind()
installKubectl()
try {
setupKind()
body()
} finally {
sh(label: 'Delete cluster', script: 'kind delete cluster')
}
}
}

def setupKind(Map args = [:]) {
def i = 0
// See https://github.com/elastic/beats/pull/21857
// Add some environmental resilience when setup does not work the very first time.
retryWithSleep(retries: 3, seconds: 5, backoff: true){
try {
sh(label: "Setup kind", script: libraryResource('scripts/install/kind-setup.sh'))
} catch(err) {
i++
sh(label: 'Delete cluster', script: 'kind delete cluster')
if (i > 2) {
error("Setup kind failed with error '${err.toString()}'")
}
}
}
}

def installKind(Map args = [:]) {
retryWithSleep(retries: 3, seconds: 5, backoff: true){
sh(label: "Install kind", script: libraryResource('scripts/install/kind.sh'))
}
}

def installKubectl(Map args = [:]) {
retryWithSleep(retries: 3, seconds: 5, backoff: true){
sh(label: "Install kubectl", script: libraryResource('scripts/install/kubectl.sh'))
}
}
10 changes: 10 additions & 0 deletions vars/withKindEnv.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Install Kind, Kubectl and configure Kind to run some command within the kind/kubectl context

```
withKindEnv(k8sVersion: 'v0.11.1', kindVersion: 'v1.23.0'){
..
}
```

* k8sVersion: K8s version to install. Optional
* kindVersion: Kind version to install. Optional