From 87596db23f237080855c19bfcf566b67d68e688a Mon Sep 17 00:00:00 2001 From: hedge-sparrow Date: Wed, 30 Oct 2024 16:16:09 +0000 Subject: [PATCH 1/3] Don't convert output bytes to string This prevents binary files getting mangled when the collector ourput is being passed around between functions --- pkg/collect/runner.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pkg/collect/runner.go b/pkg/collect/runner.go index 40b0baa7f..e26dd583b 100644 --- a/pkg/collect/runner.go +++ b/pkg/collect/runner.go @@ -57,7 +57,7 @@ func (r *podRunner) run(ctx context.Context, collector *troubleshootv1beta2.Host } results <- map[string][]byte{ - nodeName: []byte(logs), + nodeName: logs, } return nil @@ -314,16 +314,16 @@ func WaitForPodCompleted(ctx context.Context, client kubernetes.Interface, names }) } -func GetContainerLogs(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, waitForComplete bool, interval time.Duration) (string, error) { +func GetContainerLogs(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, waitForComplete bool, interval time.Duration) ([]byte, error) { if waitForComplete { if err := WaitForPodCompleted(ctx, client, namespace, podName, interval); err != nil { - return "", err + return nil, err } } return getContainerLogsInternal(ctx, client, namespace, podName, containerName, false) } -func getContainerLogsInternal(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, previous bool) (string, error) { +func getContainerLogsInternal(ctx context.Context, client kubernetes.Interface, namespace string, podName string, containerName string, previous bool) ([]byte, error) { var logs []byte var err error @@ -349,11 +349,11 @@ func getContainerLogsInternal(ctx context.Context, client kubernetes.Interface, err = retry.OnError(retry.DefaultBackoff, retryableFn, logsFn) if err != nil { - return "", err + return nil, err } if strings.Contains(string(logs), "Internal Error") { - return "", fmt.Errorf("Fetched log contains \"Internal Error\": %q", string(logs)) + return nil, fmt.Errorf("Fetched log contains \"Internal Error\": %q", string(logs)) } - return string(logs), nil + return logs, nil } From b4ea4a5a36b0a68e070b34f54a454b082977987f Mon Sep 17 00:00:00 2001 From: Ash <159829404+hedge-sparrow@users.noreply.github.com> Date: Wed, 30 Oct 2024 18:42:43 +0000 Subject: [PATCH 2/3] Update pkg/collect/runner.go Co-authored-by: Evans Mungai --- pkg/collect/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/collect/runner.go b/pkg/collect/runner.go index e26dd583b..abde4c62a 100644 --- a/pkg/collect/runner.go +++ b/pkg/collect/runner.go @@ -351,7 +351,7 @@ func getContainerLogsInternal(ctx context.Context, client kubernetes.Interface, if err != nil { return nil, err } - if strings.Contains(string(logs), "Internal Error") { + if bytes.Contains(logs, []byte("Internal Error")) { return nil, fmt.Errorf("Fetched log contains \"Internal Error\": %q", string(logs)) } From e93864840771c42e1d47af4bbaf674c1189a3448 Mon Sep 17 00:00:00 2001 From: hedge-sparrow Date: Wed, 30 Oct 2024 20:30:29 +0000 Subject: [PATCH 3/3] organise imports --- pkg/collect/runner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/collect/runner.go b/pkg/collect/runner.go index abde4c62a..c5ac7ae0d 100644 --- a/pkg/collect/runner.go +++ b/pkg/collect/runner.go @@ -1,11 +1,11 @@ package collect import ( + "bytes" "context" "encoding/json" "fmt" "strconv" - "strings" "time" "github.com/pkg/errors"