From 2ea7f4d60b77ceb6d18809235feb440929ef2a80 Mon Sep 17 00:00:00 2001 From: Tam Mach Date: Fri, 24 Dec 2021 18:25:35 +1100 Subject: [PATCH] Compress cilium-bugtool tar file before copy to local system 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 https://github.com/cilium/cilium-cli/issues/616#issuecomment-992250594 Signed-off-by: Tam Mach --- sysdump/constants.go | 2 +- sysdump/sysdump.go | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/sysdump/constants.go b/sysdump/constants.go index e5377892f1..3ba3125381 100644 --- a/sysdump/constants.go +++ b/sysdump/constants.go @@ -32,7 +32,7 @@ const ( const ( awsNodeDaemonSetFileName = "aws-node-daemonset-.yaml" - ciliumBugtoolFileName = "cilium-bugtool-%s-.tar" + ciliumBugtoolFileName = "cilium-bugtool-%s-.tar.gz" ciliumClusterWideNetworkPoliciesFileName = "ciliumclusterwidenetworkpolicies-.yaml" ciliumConfigMapFileName = "cilium-configmap-.yaml" ciliumDaemonSetFileName = "cilium-daemonset-.yaml" diff --git a/sysdump/sysdump.go b/sysdump/sysdump.go index 5959c35f30..ef630cdae4 100644 --- a/sysdump/sysdump.go +++ b/sysdump/sysdump.go @@ -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" @@ -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 } @@ -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 }