Skip to content

Commit b0a69ea

Browse files
committed
update
Signed-off-by: Hang Yan <[email protected]>
1 parent 61038aa commit b0a69ea

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

pkg/antctl/raw/supportbundle/command.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -704,16 +704,14 @@ func processResults(ctx context.Context, antreaClientset antrea.Interface, k8sCl
704704

705705
// download logs from kubernetes api
706706
if failedNodes != nil {
707-
err = downloadFallbackAgentBundleFromKubernetes(ctx, antreaClientset, k8sClient, failedNodes, dir)
708-
if err != nil {
707+
if err = downloadFallbackAgentBundleFromKubernetes(ctx, antreaClientset, k8sClient, failedNodes, dir); err != nil {
709708
fmt.Println("Failed to download agent bundle from kubernetes api: " + err.Error())
710709
} else {
711710
allFailed = false
712711
}
713712
}
714713
if controllerFailed {
715-
err = downloadFallbackControllerBundleFromKubernetes(ctx, antreaClientset, k8sClient, dir)
716-
if err != nil {
714+
if err = downloadFallbackControllerBundleFromKubernetes(ctx, antreaClientset, k8sClient, dir); err != nil {
717715
fmt.Println("Failed to download controller bundle from kubernetes api: " + err.Error())
718716
} else {
719717
allFailed = false
@@ -762,7 +760,7 @@ func downloadFallbackControllerBundleFromKubernetes(ctx context.Context, antreaC
762760
if err := downloadPodLogs(ctx, k8sClient, pod.Namespace, pod.Name, k8s.GetPodContainerNames(pod), tmpDir); err != nil {
763761
return err
764762
}
765-
return packPodLogs(pod, dir, tmpDir)
763+
return packPodBundle(pod, dir, tmpDir)
766764
}
767765

768766
func downloadFallbackAgentBundleFromKubernetes(ctx context.Context, antreaClientset antrea.Interface, k8sClient kubernetes.Interface, failedNodes []string, dir string) error {
@@ -807,15 +805,15 @@ func downloadFallbackAgentBundleFromKubernetes(ctx context.Context, antreaClient
807805
if err != nil {
808806
return err
809807
}
810-
return packPodLogs(&pod, dir, tmpDir)
808+
return packPodBundle(&pod, dir, tmpDir)
811809
}(); err != nil {
812810
errors = append(errors, err)
813811
}
814812
}
815813
return utilerror.NewAggregate(errors)
816814
}
817815

818-
func packPodLogs(pod *corev1.Pod, dir string, logsDir string) error {
816+
func packPodBundle(pod *corev1.Pod, dir string, bundleDir string) error {
819817
prefix := "agent_"
820818
if strings.Contains(pod.Name, "controller") {
821819
prefix = "controller_"
@@ -824,17 +822,16 @@ func packPodLogs(pod *corev1.Pod, dir string, logsDir string) error {
824822
f, err := defaultFS.Create(gzFileName)
825823
if err != nil {
826824
return err
827-
} else {
828-
defer f.Close()
829-
_, err := compress.PackDir(defaultFS, logsDir, f)
830-
return err
831825
}
826+
defer f.Close()
827+
_, err = compress.PackDir(defaultFS, bundleDir, f)
828+
return err
832829
}
833830

834-
func downloadPodLogs(ctx context.Context, k8sClient kubernetes.Interface, namespace string, podName string, containers []string, tmpDir string) error {
831+
func downloadPodLogs(ctx context.Context, k8sClient kubernetes.Interface, namespace string, podName string, containers []string, dir string) error {
835832
downloadContainerLogs := func(containerName string) error {
836833
containerDirName, _ := strings.CutPrefix(containerName, "antrea-")
837-
containerLogDir := filepath.Join(tmpDir, "logs", containerDirName)
834+
containerLogDir := filepath.Join(dir, "logs", containerDirName)
838835
err := os.MkdirAll(containerLogDir, 0755)
839836
if err != nil {
840837
return err
@@ -854,8 +851,7 @@ func downloadPodLogs(ctx context.Context, k8sClient kubernetes.Interface, namesp
854851
return err
855852
}
856853

857-
_, err = io.Copy(f, logStream)
858-
if err != nil {
854+
if _, err = io.Copy(f, logStream); err != nil {
859855
return err
860856
}
861857
return logStream.Close()

pkg/antctl/raw/supportbundle/command_test.go

+5-7
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ func TestProcessResults(t *testing.T) {
410410

411411
antreaInterface := fakeclientset.NewSimpleClientset(&controllerInfo, agentInfo1, agentInfo2)
412412
k8sClient := fake.NewSimpleClientset(controllerPod, pod1, pod2)
413-
err := processResults(context.TODO(), antreaInterface, k8sClient, tt.resultMap, option.dir)
414-
require.NoError(t, err)
413+
require.NoError(t, processResults(context.TODO(), antreaInterface, k8sClient, tt.resultMap, option.dir))
415414
b, err := afero.ReadFile(defaultFS, filepath.Join(option.dir, "failed_nodes"))
416415
require.NoError(t, err)
417416
data := string(b)
@@ -421,23 +420,22 @@ func TestProcessResults(t *testing.T) {
421420
tgzFileName = "controller_node-1.tar.gz"
422421
}
423422
if err != nil {
423+
// fallback path to retrieve data from kubernetes API instead of Antrea API.
424424
ok, checkErr := afero.Exists(defaultFS, filepath.Join(option.dir, tgzFileName))
425425
require.NoError(t, checkErr)
426426
require.True(t, ok, "expected support bundle file %s not found", tgzFileName)
427427

428428
unpackError := compress.UnpackDir(defaultFS, filepath.Join(option.dir, tgzFileName), option.dir)
429429
require.NoError(t, unpackError)
430-
expectFileName := "logs/agent/antrea-agent.log"
430+
expectFileName := filepath.Join("logs", "agent", "antrea-agent.log")
431431
if node == "" {
432-
expectFileName = "logs/controller/antrea-controller.log"
432+
expectFileName = filepath.Join("logs", "controller", "antrea-controller.log")
433433
}
434434
ok, checkErr = afero.Exists(defaultFS, filepath.Join(option.dir, expectFileName))
435435
require.NoError(t, checkErr)
436436
assert.True(t, ok, "expected log file %s not found", expectFileName)
437-
deleteErr := defaultFS.Remove(filepath.Join(option.dir, expectFileName))
438-
require.NoError(t, deleteErr)
437+
defaultFS.Remove(filepath.Join(option.dir, expectFileName))
439438
}
440-
441439
if node == "" {
442440
continue
443441
}

pkg/util/compress/compress.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ import (
2929
)
3030

3131
// Sanitize archive file pathing from "G305: Zip Slip vulnerability"
32-
func sanitizeArchivePath(d, t string) (v string, err error) {
33-
v = filepath.Join(d, t)
32+
func sanitizeArchivePath(d, t string) (string, error) {
33+
v := filepath.Join(d, t)
3434
if strings.HasPrefix(v, filepath.Clean(d)) {
3535
return v, nil
3636
}
@@ -76,8 +76,7 @@ func UnpackDir(fs afero.Fs, fileName string, targetDir string) error {
7676
}
7777
for {
7878
// to resolve G110: Potential DoS vulnerability via decompression bomb
79-
_, err := io.CopyN(outFile, tarReader, 1024)
80-
if err != nil {
79+
if _, err := io.CopyN(outFile, tarReader, 1024); err != nil {
8180
if err == io.EOF {
8281
break
8382
}

0 commit comments

Comments
 (0)