From 544a700062c12b81e0d7e0c944faa81b93685003 Mon Sep 17 00:00:00 2001 From: Ash <159829404+hedge-sparrow@users.noreply.github.com> Date: Thu, 31 Oct 2024 10:44:35 +0000 Subject: [PATCH] [sc-114813] copy HostCollector fails to copy binary files when run in cluster (#1669) * Don't convert output bytes to string This prevents binary files getting mangled when the collector ourput is being passed around between functions * Update pkg/collect/runner.go Co-authored-by: Evans Mungai * organise imports --------- Co-authored-by: Evans Mungai --- pkg/collect/runner.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/collect/runner.go b/pkg/collect/runner.go index 40b0baa7f..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" @@ -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)) + if bytes.Contains(logs, []byte("Internal Error")) { + return nil, fmt.Errorf("Fetched log contains \"Internal Error\": %q", string(logs)) } - return string(logs), nil + return logs, nil }