Skip to content

Commit

Permalink
Rewrite fetch_target_logs.sh in Golang
Browse files Browse the repository at this point in the history
Signed-off-by: peppi-lotta <[email protected]>
  • Loading branch information
peppi-lotta committed Jun 14, 2024
1 parent db065f2 commit bb6e1ab
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 59 deletions.
28 changes: 0 additions & 28 deletions scripts/fetch_target_logs.sh

This file was deleted.

143 changes: 143 additions & 0 deletions test/e2e/fetch_target_logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package e2e

import (
"bytes"
"context"
"flag"
"io"
"log"
"os"
"os/exec"
"path/filepath"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/util/homedir"
"k8s.io/kubectl/pkg/describe"
)

func FetchTargetLogs() {
dirName := "/tmp/target_cluster_logs"

var kubeconfig *string
if home := homedir.HomeDir(); home != "" {
kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
} else {
kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
}
flag.Parse()

// use the current context in kubeconfig
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
log.Printf("Error building kubeconfig: %v", err)
}

// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
log.Printf("Error creating clientset: %v", err)
}

// Print the Pods' information to file
// This does the same thing as:
// kubectl --kubeconfig="${KUBECONFIG_WORKLOAD}" get pods -A
out, err := exec.Command("kubectl", "get", "pods", "-A").Output()
if err != nil {
log.Printf("Error in getting pods: %v", err)
}
file := filepath.Join(dirName, "pods.log")
err = os.WriteFile(file, out, 0644)
if err != nil {
log.Printf("Error in writing to file: %v", err)
}

// Get all namespaces
namespaces, err := clientset.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Printf("Error in getting namespaces: %v", err)
}
for _, namespace := range namespaces.Items {
namespaceDir := filepath.Join(dirName, namespace.Name)

// Get all pods in the namespace
pods, err := clientset.CoreV1().Pods(namespace.Name).List(context.Background(), metav1.ListOptions{})
if err != nil {
log.Printf("Error listing pods in namespace %s: %v", namespace.Name, err)
}
for _, pod := range pods.Items {
// Create a directory for each pod and the path to it if
// it does not exist
podDir := filepath.Join(namespaceDir, pod.Name)
err = os.MkdirAll(podDir, 0775)
if err != nil {
log.Printf("Error in writing to file: %v", err)
}

// Get detailed information about the Pod
// This does the same thing as:
// kubectl --kubeconfig="${KUBECONFIG_WORKLOAD}" describe pods -n "${NAMESPACE}" "${POD}"
describerSettings := describe.DescriberSettings{
ShowEvents: true,
}
podDescriber := describe.PodDescriber{
Interface: clientset,
}
podDescription, err := podDescriber.Describe(namespace.Name, pod.Name, describerSettings)
if err != nil {
log.Printf("Error in describing pod %s in namespace %s: %v", pod.Name, namespace.Name, err)
}

// Print the Pod information to file
file := filepath.Join(podDir, "stdout_describe.log")
err = os.WriteFile(file, []byte(podDescription), 0644)
if err != nil {
log.Printf("Error in writing to file: %v", err)
}

// Get containers of the Pod
for _, container := range pod.Spec.Containers {
// Create a directory for each container
containerDir := filepath.Join(podDir, container.Name)
err = os.MkdirAll(containerDir, 0775)
if err != nil {
log.Printf("Error listing to containers: %v", err)
}

containerLogs := GetContainerLogs(namespace.Name, pod.Name, container.Name, clientset)
// Print the Pod information to file
file := filepath.Join(containerDir, "stdout.log")
err = os.WriteFile(file, []byte(containerLogs), 0644)
if err != nil {
log.Printf("Error in writing to file: %v", err)
}
}
}
}
}

func GetContainerLogs(namespace string, podName string, containerName string, clientset *kubernetes.Clientset) string {
// Get logs of a container
// Does the same thing as:
// kubectl --kubeconfig="${KUBECONFIG_WORKLOAD}" logs -n "${NAMESPACE}" "${POD}" "${CONTAINER}"
podLogOptions := corev1.PodLogOptions{
Container: containerName,
}
podLogs, err := clientset.CoreV1().Pods(namespace).GetLogs(podName, &podLogOptions).Stream(context.Background())
if err != nil {
log.Printf("Error getting container logs: %v", err)
}
defer podLogs.Close()

// Read the logs into a string
buf := new(bytes.Buffer)
_, err = io.Copy(buf, podLogs)
if err != nil {
log.Printf("Error in buffering container logs: %v", err)
}
podStr := buf.String()

return podStr
}
15 changes: 3 additions & 12 deletions test/e2e/pivoting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package e2e
import (
"context"
"fmt"
"io"
"log"
"os"
"os/exec"
Expand Down Expand Up @@ -424,19 +423,11 @@ func rePivoting(ctx context.Context, inputGetter func() RePivotingInput) {
numberOfAllBmh := numberOfWorkers + numberOfControlplane

By("Fetch logs from target cluster")
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
cmd := exec.Command("./fetch_target_logs.sh") // #nosec G204:gosec
cmd.Dir = path
errorPipe, _ := cmd.StderrPipe()
_ = cmd.Start()
errorData, _ := io.ReadAll(errorPipe)
if len(errorData) > 0 {
Logf("Error of the shell: %v\n", string(errorData))
}
FetchTargetLogs()

By("Fetch manifest for workload cluster after pivot")
path = filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
cmd = exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
cmd := exec.Command("./fetch_manifests.sh") // #nosec G204:gosec
cmd.Dir = path
_ = cmd.Run()
os.Unsetenv("KUBECONFIG_WORKLOAD")
Expand Down
21 changes: 2 additions & 19 deletions test/e2e/upgrade_clusterctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package e2e

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -398,27 +397,11 @@ func preCleanupManagementCluster(clusterProxy framework.ClusterProxy) {
if CurrentSpecReport().Failed() {
// Fetch logs in case of failure in management cluster
By("Fetch logs from management cluster")
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
cmd := exec.Command("./fetch_target_logs.sh") // #nosec G204:gosec
cmd.Dir = path
errorPipe, _ := cmd.StderrPipe()
_ = cmd.Start()
errorData, _ := io.ReadAll(errorPipe)
if len(errorData) > 0 {
Logf("Error of the shell: %v\n", string(errorData))
}
FetchTargetLogs()
}
// Fetch logs from management cluster
By("Fetch logs from management cluster")
path := filepath.Join(os.Getenv("CAPM3PATH"), "scripts")
cmd := exec.Command("./fetch_target_logs.sh") //#nosec G204:gosec
cmd.Dir = path
errorPipe, _ := cmd.StderrPipe()
_ = cmd.Start()
errorData, _ := io.ReadAll(errorPipe)
if len(errorData) > 0 {
Logf("Error of the shell: %v\n", string(errorData))
}
FetchTargetLogs()
os.Unsetenv("KUBECONFIG_WORKLOAD")
os.Unsetenv("KUBECONFIG_BOOTSTRAP")

Expand Down
16 changes: 16 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
k8s.io/apimachinery v0.29.5
k8s.io/client-go v0.29.5
k8s.io/klog/v2 v2.110.1
k8s.io/kubectl v0.29.3
k8s.io/utils v0.0.0-20231127182322-b307cd553661
sigs.k8s.io/cluster-api v1.7.2
sigs.k8s.io/cluster-api/test v1.7.2
Expand All @@ -27,6 +28,7 @@ require (
replace github.com/metal3-io/cluster-api-provider-metal3/api => ./../api

require (
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/BurntSushi/toml v1.0.0 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
Expand All @@ -51,8 +53,10 @@ require (
github.com/emicklei/go-restful/v3 v3.12.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-errors/errors v1.4.2 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
Expand All @@ -63,6 +67,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/cel-go v0.17.7 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
Expand All @@ -71,13 +76,16 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/pprof v0.0.0-20240424215950-a892ee059fd6 // indirect
github.com/google/safetext v0.0.0-20220905092116-b49f7bc46da2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/xstrings v1.4.0 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand All @@ -87,13 +95,16 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/docker-image-spec v1.3.1 // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
Expand All @@ -110,10 +121,12 @@ require (
github.com/stoewer/go-strcase v1.2.0 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
github.com/valyala/fastjson v1.6.4 // indirect
github.com/xlab/treeprint v1.2.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.0 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/mod v0.17.0 // indirect
Expand All @@ -135,11 +148,14 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
gotest.tools/v3 v3.5.0 // indirect
k8s.io/apiserver v0.29.5 // indirect
k8s.io/cli-runtime v0.29.3 // indirect
k8s.io/cluster-bootstrap v0.29.3 // indirect
k8s.io/component-base v0.29.5 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kind v0.22.0 // indirect
sigs.k8s.io/kustomize/api v0.13.5-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/kustomize/kyaml v0.14.3-0.20230601165947-6ce0bf390ce3 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading

0 comments on commit bb6e1ab

Please sign in to comment.