mirror of
https://github.com/cloudflare/cloudflared.git
synced 2026-06-23 04:10:20 +00:00
7585e38948
Check / check (1.22.x, macos-latest) (push) Has been cancelled
Check / check (1.22.x, ubuntu-latest) (push) Has been cancelled
Check / check (1.22.x, windows-latest) (push) Has been cancelled
Semgrep config / semgrep/ci (push) Has been cancelled
Fixing warnings in cloudflared log collector. This attempts to fix errors like the ones shown below: ``` diagnostic/diagnostic.go:132:23: Error return value of `logHandle.Close` is not checked (errcheck) defer logHandle.Close() diagnostic/diagnostic.go:134:26: G303: File creation in shared tmp directory without using ioutil.Tempfile (gosec) outputLogHandle, err := os.Create(filepath.Join(os.TempDir(), logFilename)) ```
66 lines
1.3 KiB
Go
66 lines
1.3 KiB
Go
package diagnostic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
type KubernetesLogCollector struct {
|
|
containerID string // This member identifies the container by identifier or name
|
|
pod string // This member identifies the pod where the container is deployed
|
|
}
|
|
|
|
func NewKubernetesLogCollector(containerID, pod string) *KubernetesLogCollector {
|
|
return &KubernetesLogCollector{
|
|
containerID,
|
|
pod,
|
|
}
|
|
}
|
|
|
|
func (collector *KubernetesLogCollector) Collect(ctx context.Context) (*LogInformation, error) {
|
|
// nolint: gosec
|
|
outputHandle, err := os.Create(filepath.Join(os.TempDir(), logFilename))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error opening output file: %w", err)
|
|
}
|
|
|
|
defer func() { _ = outputHandle.Close() }()
|
|
|
|
var command *exec.Cmd
|
|
// Calculate 2 weeks ago
|
|
since := time.Now().Add(twoWeeksOffset).Format(time.RFC3339)
|
|
if collector.containerID != "" {
|
|
// nolint: gosec
|
|
command = exec.CommandContext(
|
|
ctx,
|
|
"kubectl",
|
|
"logs",
|
|
collector.pod,
|
|
"--since-time",
|
|
since,
|
|
"--tail",
|
|
tailMaxNumberOfLines,
|
|
"-c",
|
|
collector.containerID,
|
|
)
|
|
} else {
|
|
// nolint: gosec
|
|
command = exec.CommandContext(
|
|
ctx,
|
|
"kubectl",
|
|
"logs",
|
|
collector.pod,
|
|
"--since-time",
|
|
since,
|
|
"--tail",
|
|
tailMaxNumberOfLines,
|
|
)
|
|
}
|
|
|
|
return PipeCommandOutputToFile(command, outputHandle)
|
|
}
|