Skip to content

Commit 04b620d

Browse files
Add crust-gather collection for e2e run
Signed-off-by: Danil Grigorev <[email protected]>
1 parent ae4cdc7 commit 04b620d

File tree

7 files changed

+164
-0
lines changed

7 files changed

+164
-0
lines changed

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ E2E_DATA_DIR ?= $(ROOT_DIR)/test/e2e/data
4848
E2E_CONF_FILE ?= $(ROOT_DIR)/test/e2e/config/e2e_conf.yaml
4949

5050
export PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
51+
export KREW_ROOT := $(abspath $(TOOLS_BIN_DIR))
52+
export PATH := $(KREW_ROOT)/bin:$(PATH)
5153

5254
# Set --output-base for conversion-gen if we are not within GOPATH
5355
ifneq ($(abspath $(ROOT_DIR)),$(shell go env GOPATH)/src/github.com/rancher-sandbox/cluster-api-provider-rke2)
@@ -363,6 +365,12 @@ kind-cluster: ## Create a new kind cluster designed for development with Tilt
363365
tilt-up: kind-cluster ## Start tilt and build kind cluster if needed.
364366
tilt up
365367

368+
.PHONY: kubectl
369+
kubectl: # Download kubectl cli into tools bin folder
370+
hack/ensure-kubectl.sh \
371+
-b $(TOOLS_BIN_DIR) \
372+
$(KUBECTL_VERSION)
373+
366374
## --------------------------------------
367375
## E2E
368376
## --------------------------------------

hack/ensure-kubectl.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright © 2023 - 2024 SUSE LLC
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+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
if [[ "${TRACE-0}" == "1" ]]; then
22+
set -o xtrace
23+
fi
24+
25+
# shellcheck source=./hack/utils.sh
26+
source "$(dirname "${BASH_SOURCE[0]}")/utils.sh"
27+
28+
GOPATH_BIN="$(go env GOPATH)/bin/"
29+
MINIMUM_KUBECTL_VERSION=v1.27.0
30+
goarch="$(go env GOARCH)"
31+
goos="$(go env GOOS)"
32+
33+
# Ensure the kubectl tool exists and is a viable version, or installs it
34+
verify_kubectl_version() {
35+
36+
# If kubectl is not available on the path, get it
37+
if ! [ -x "$(command -v kubectl)" ]; then
38+
if [ "$goos" == "linux" ] || [ "$goos" == "darwin" ]; then
39+
if ! [ -d "${GOPATH_BIN}" ]; then
40+
mkdir -p "${GOPATH_BIN}"
41+
fi
42+
echo 'kubectl not found, installing'
43+
curl -sLo "${GOPATH_BIN}/kubectl" "https://dl.k8s.io/release/${MINIMUM_KUBECTL_VERSION}/bin/${goos}/${goarch}/kubectl"
44+
chmod +x "${GOPATH_BIN}/kubectl"
45+
verify_gopath_bin
46+
else
47+
echo "Missing required binary in path: kubectl"
48+
return 2
49+
fi
50+
fi
51+
52+
local kubectl_version
53+
IFS=" " read -ra kubectl_version <<< "$(kubectl version --client)"
54+
if [[ "${MINIMUM_KUBECTL_VERSION}" != $(echo -e "${MINIMUM_KUBECTL_VERSION}\n${kubectl_version[2]}" | sort -s -t. -k 1,1 -k 2,2n -k 3,3n | head -n1) ]]; then
55+
cat <<EOF
56+
Detected kubectl version: ${kubectl_version[2]}.
57+
Requires ${MINIMUM_KUBECTL_VERSION} or greater.
58+
Please install ${MINIMUM_KUBECTL_VERSION} or later.
59+
EOF
60+
return 2
61+
fi
62+
}
63+
64+
install_plugins() {
65+
(
66+
set -x; cd "$(mktemp -d)" &&
67+
OS="$(uname | tr '[:upper:]' '[:lower:]')" &&
68+
ARCH="$(uname -m | sed -e 's/x86_64/amd64/' -e 's/\(arm\)\(64\)\?.*/\1\2/' -e 's/aarch64$/arm64/')" &&
69+
KREW="krew-${OS}_${ARCH}" &&
70+
curl -fsSLO "https://github.com/kubernetes-sigs/krew/releases/latest/download/${KREW}.tar.gz" &&
71+
tar zxvf "${KREW}.tar.gz" &&
72+
./"${KREW}" install krew
73+
)
74+
kubectl krew index add crust-gather https://github.com/crust-gather/crust-gather.git || true
75+
kubectl krew install crust-gather/crust-gather
76+
}
77+
78+
verify_kubectl_version
79+
install_plugins

hack/utils.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright © 2023 - 2024 SUSE LLC
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+
# get_root_path returns the root path of the project source tree
18+
get_root_path() {
19+
git rev-parse --show-toplevel
20+
}
21+
22+
# cd_root_path cds to the root path of the project source tree
23+
cd_root_path() {
24+
cd "$(get_root_path)" || exit
25+
}
26+
27+
# ensure GOPATH/bin is in PATH as we may install binaries to that directory in
28+
# other ensure-* scripts, and expect them to be found in PATH later on
29+
verify_gopath_bin() {
30+
local gopath_bin
31+
32+
gopath_bin="$(go env GOPATH)/bin"
33+
if ! printenv PATH | grep -q "${gopath_bin}"; then
34+
cat <<EOF
35+
error: \$GOPATH/bin=${gopath_bin} is not in your PATH.
36+
See https://go.dev/doc/gopath_code for more instructions.
37+
EOF
38+
return 2
39+
fi
40+
}
41+

test/e2e/e2e_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ var _ = Describe("Workload cluster creation", func() {
7272
})
7373

7474
AfterEach(func() {
75+
err := CollectArtifacts(ctx, bootstrapClusterProxy.GetKubeconfigPath(), filepath.Join(artifactFolder, bootstrapClusterProxy.GetName(), clusterName+specName))
76+
Expect(err).ToNot(HaveOccurred())
77+
7578
cleanInput := cleanupInput{
7679
SpecName: specName,
7780
Cluster: result.Cluster,

test/e2e/e2e_upgrade_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ var _ = Describe("Workload cluster creation", func() {
6969
})
7070

7171
AfterEach(func() {
72+
err := CollectArtifacts(ctx, bootstrapClusterProxy.GetKubeconfigPath(), filepath.Join(artifactFolder, bootstrapClusterProxy.GetName(), clusterName+specName))
73+
Expect(err).ToNot(HaveOccurred())
74+
7275
cleanInput := cleanupInput{
7376
SpecName: specName,
7477
Cluster: result.Cluster,

test/e2e/helpers.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
package e2e
2020

2121
import (
22+
"bytes"
2223
"context"
2324
"encoding/json"
2425
"fmt"
@@ -433,6 +434,32 @@ func setDefaults(input *ApplyClusterTemplateAndWaitInput) {
433434
}
434435
}
435436

437+
var secrets = []string{}
438+
439+
func CollectArtifacts(ctx context.Context, kubeconfigPath, name string, args ...string) error {
440+
if kubeconfigPath == "" {
441+
return fmt.Errorf("Unable to collect artifacts: kubeconfig path is empty")
442+
}
443+
444+
aargs := append([]string{"crust-gather", "collect", "--kubeconfig", kubeconfigPath, "-v", "ERROR", "-f", name}, args...)
445+
for _, secret := range secrets {
446+
aargs = append(aargs, "-s", secret)
447+
}
448+
449+
cmd := exec.Command("kubectl", aargs...)
450+
451+
var stdout, stderr bytes.Buffer
452+
cmd.Stdout = &stdout
453+
cmd.Stderr = &stderr
454+
cmd.WaitDelay = time.Minute
455+
456+
fmt.Printf("Running kubectl %s\n", strings.Join(aargs, " "))
457+
err := cmd.Run()
458+
fmt.Printf("stderr:\n%s\n", string(stderr.Bytes()))
459+
fmt.Printf("stdout:\n%s\n", string(stdout.Bytes()))
460+
return err
461+
}
462+
436463
type networks []network
437464

438465
type network struct {

test/e2e/metadata_e2e_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ var _ = Describe("Workload cluster creation", func() {
7676
})
7777

7878
AfterEach(func() {
79+
err := CollectArtifacts(ctx, bootstrapClusterProxy.GetKubeconfigPath(), filepath.Join(artifactFolder, bootstrapClusterProxy.GetName(), clusterName+specName))
80+
Expect(err).ToNot(HaveOccurred())
81+
7982
cleanInput := cleanupInput{
8083
SpecName: specName,
8184
Cluster: result.Cluster,

0 commit comments

Comments
 (0)