Skip to content

Commit cb06b54

Browse files
committed
Chore: Refactor
* Copy dqlite file from pod using PodExecutor and cat command * Remove dedicated function to copy file from pod
1 parent cdf2a8e commit cb06b54

File tree

2 files changed

+12
-68
lines changed

2 files changed

+12
-68
lines changed

pkg/data_collector/data_collector.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -266,68 +266,3 @@ func (c *DataCollector) AllNamespacesExist() bool {
266266

267267
return allExist
268268
}
269-
270-
// CopyFileFromPod copies a file from a pod's container to the local filesystem.
271-
func (c *DataCollector) CopyFileFromPod(namespace, pod, container, srcPath, destPath string, ctx context.Context) error {
272-
cmd := []string{"tar", "cf", "-", "-C", filepath.Dir(srcPath), filepath.Base(srcPath)}
273-
req := c.K8sCoreClientSet.CoreV1().RESTClient().Post().
274-
Namespace(namespace).
275-
Resource("pods").
276-
Name(pod).
277-
SubResource("exec").
278-
VersionedParams(&corev1.PodExecOptions{
279-
Container: container,
280-
Command: cmd,
281-
Stdin: false,
282-
Stdout: true,
283-
Stderr: true,
284-
TTY: false,
285-
}, scheme.ParameterCodec)
286-
287-
exec, err := remotecommand.NewSPDYExecutor(c.K8sRestConfig, "POST", req.URL())
288-
if err != nil {
289-
return err
290-
}
291-
292-
// Stream the data from the Pod
293-
var stdout, stderr bytes.Buffer
294-
err = exec.StreamWithContext(ctx, remotecommand.StreamOptions{
295-
Stdout: &stdout,
296-
Stderr: &stderr,
297-
})
298-
if err != nil {
299-
return err
300-
}
301-
302-
// Create a local file to save the output
303-
localFile, err := os.Create(destPath)
304-
if err != nil {
305-
// return fmt.Errorf("failed to create local file: %w", err)
306-
return err
307-
}
308-
defer localFile.Close()
309-
310-
// Untar the stream and write the content to the local file
311-
tarReader := tar.NewReader(&stdout)
312-
for {
313-
header, err := tarReader.Next()
314-
315-
if err == io.EOF {
316-
break // End of tar archive
317-
}
318-
if err != nil {
319-
return err
320-
}
321-
322-
// Ensure the tar file matches the expected file path
323-
if header.Name == filepath.Base(srcPath) {
324-
_, err = io.Copy(localFile, tarReader)
325-
if err != nil {
326-
return fmt.Errorf("failed to write to local file: %w", err)
327-
}
328-
break
329-
}
330-
}
331-
332-
return nil
333-
}

pkg/jobs/nim_job_list.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package jobs
2020

2121
import (
2222
"context"
23+
"os"
2324
"path/filepath"
2425
"strings"
2526
"time"
@@ -239,15 +240,23 @@ func NIMJobList() []Job {
239240

240241
// Copy the dumped file from the pod to the host
241242
destPathFilename := filepath.Join(dc.BaseDir, "exec", namespace, pod.Name+"__dqlite-dump-"+filepath.Base(config.outputFile))
242-
if err := dc.CopyFileFromPod(namespace, pod.Name, config.containerName, config.outputFile, destPathFilename, ctx); err != nil {
243+
copyCmd := []string{"cat", config.outputFile}
244+
fileContent, err := dc.PodExecutor(namespace, pod.Name, config.containerName, copyCmd, ctx)
245+
if err != nil {
243246
jobResult.Error = err
244247
dc.Logger.Printf("\tFailed to copy dumped file %s from pod %s in namespace %s to %s: %v\n", config.outputFile, pod.Name, namespace, destPathFilename, err)
245248
} else {
246-
dc.Logger.Printf("\tSuccessfully copied dumped file %s from pod %s in namespace %s to %s\n", config.outputFile, pod.Name, namespace, destPathFilename)
249+
err = os.WriteFile(destPathFilename, fileContent, 0644)
250+
if err != nil {
251+
jobResult.Error = err
252+
dc.Logger.Printf("\tFailed to write file to %s: %v\n", destPathFilename, err)
253+
} else {
254+
dc.Logger.Printf("\tSuccessfully copied dumped file %s from pod %s in namespace %s to %s\n", config.outputFile, pod.Name, namespace, destPathFilename)
255+
}
247256
}
248257

249258
// Remove/delete the dumped file from the pod
250-
_, err := dc.PodExecutor(namespace, pod.Name, config.containerName, []string{"rm", "-f", config.outputFile}, ctx)
259+
_, err = dc.PodExecutor(namespace, pod.Name, config.containerName, []string{"rm", "-f", config.outputFile}, ctx)
251260
if err != nil {
252261
jobResult.Error = err
253262
dc.Logger.Printf("\tFailed to remove dumped file %s from pod %s in namespace %s: %v\n", config.outputFile, pod.Name, namespace, err)

0 commit comments

Comments
 (0)