Skip to content

Commit

Permalink
[sc-114813] copy HostCollector fails to copy binary files when run in…
Browse files Browse the repository at this point in the history
… 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 <[email protected]>

* organise imports

---------

Co-authored-by: Evans Mungai <[email protected]>
  • Loading branch information
hedge-sparrow and banjoh authored Oct 31, 2024
1 parent 059b5d1 commit 544a700
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions pkg/collect/runner.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package collect

import (
"bytes"
"context"
"encoding/json"
"fmt"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
}

0 comments on commit 544a700

Please sign in to comment.