Skip to content

Commit

Permalink
Compress cilium-bugtool tar file before copy to local system
Browse files Browse the repository at this point in the history
This commit is to add gzip compression for cilium-bugtool to reduce size
of transferred file. In local cluster, the size is reduced significantly
(e.g. from 21 MB to ~3MB). Once copied to local system, this file is
extracted again so that there is no change in existing behavior.

```
root@kind-control-plane:/tmp# ls -lrth cilium-bugtool-20211224-072*
-rw-r--r-- 1 root root  21M Dec 24 07:20 cilium-bugtool-20211224-072047.092+0000-UTC-1266452003.tar
-rw-r--r-- 1 root root 3.1M Dec 24 07:21 cilium-bugtool-20211224-072122.815+0000-UTC-2647778632.tar.gz
```

Relates to #616 (comment)

Signed-off-by: Tam Mach <[email protected]>
  • Loading branch information
sayboras committed Dec 24, 2021
1 parent 1db91a3 commit 2ea7f4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion sysdump/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const (

const (
awsNodeDaemonSetFileName = "aws-node-daemonset-<ts>.yaml"
ciliumBugtoolFileName = "cilium-bugtool-%s-<ts>.tar"
ciliumBugtoolFileName = "cilium-bugtool-%s-<ts>.tar.gz"
ciliumClusterWideNetworkPoliciesFileName = "ciliumclusterwidenetworkpolicies-<ts>.yaml"
ciliumConfigMapFileName = "cilium-configmap-<ts>.yaml"
ciliumDaemonSetFileName = "cilium-daemonset-<ts>.yaml"
Expand Down
24 changes: 17 additions & 7 deletions sysdump/sysdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/cilium/cilium-cli/internal/utils"

"github.com/cilium/workerpool"
archiver "github.com/mholt/archiver/v3"
"github.com/mholt/archiver/v3"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -899,21 +899,31 @@ func (c *Collector) submitBugtoolTasks(ctx context.Context, pods []*corev1.Pod,
if len(m) != 2 || len(m[1]) == 0 {
return fmt.Errorf("failed to collect 'cilium-bugtool' output for %q in namespace %q: output doesn't contain archive name: %s", p.Name, p.Namespace, outString)
}

// Gzip bugtool tar file to reduce the size of transferred file
o, e, err = c.Client.ExecInPodWithStderr(ctx, p.Namespace, p.Name, containerName, []string{"gzip", m[1]})
if err != nil {
return fmt.Errorf("failed to compress 'cilium-bugtool' output for %q in namespace %q: %w: %s", p.Name, p.Namespace, err, e.String())
}
tarGzFile := m[1] + ".gz"

// Grab the resulting archive's contents from the pod.
b, err := c.Client.ExecInPod(ctx, p.Namespace, p.Name, containerName, []string{"cat", m[1]})
b, err := c.Client.ExecInPod(ctx, p.Namespace, p.Name, containerName, []string{"cat", tarGzFile})
if err != nil {
return fmt.Errorf("failed to collect 'cilium-bugtool' output for %q: %w", p.Name, err)
}
// Dump the resulting file's contents to the temporary directory.

f := c.AbsoluteTempPath(fmt.Sprintf(ciliumBugtoolFileName, p.Name))
if err := writeBytes(f, b.Bytes()); err != nil {
return fmt.Errorf("failed to collect 'cilium-bugtool' output for %q: %w", p.Name, err)
}
// Untar the resulting file.
t := archiver.Tar{
StripComponents: 1,
t := archiver.TarGz{
Tar: &archiver.Tar{
StripComponents: 1,
},
}
if err := t.Unarchive(f, strings.Replace(f, ".tar", "", -1)); err != nil {
if err := t.Unarchive(f, strings.Replace(f, ".tar.gz", "", -1)); err != nil {
c.logWarn("Failed to unarchive 'cilium-bugtool' output for %q: %v", p.Name, err)
return nil
}
Expand All @@ -923,7 +933,7 @@ func (c *Collector) submitBugtoolTasks(ctx context.Context, pods []*corev1.Pod,
return nil
}
// Remove the file from the pod.
if _, err = c.Client.ExecInPod(ctx, p.Namespace, p.Name, containerName, []string{rmCommand, m[1]}); err != nil {
if _, err = c.Client.ExecInPod(ctx, p.Namespace, p.Name, containerName, []string{rmCommand, tarGzFile}); err != nil {
c.logWarn("Failed to delete 'cilium-bugtool' output from pod %q in namespace %q: %w", p.Name, p.Namespace, err)
return nil
}
Expand Down

0 comments on commit 2ea7f4d

Please sign in to comment.