Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Hang Yan <[email protected]>
  • Loading branch information
hangyan committed Jan 8, 2025
1 parent 61038aa commit b0a69ea
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 26 deletions.
26 changes: 11 additions & 15 deletions pkg/antctl/raw/supportbundle/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,16 +704,14 @@ func processResults(ctx context.Context, antreaClientset antrea.Interface, k8sCl

// download logs from kubernetes api
if failedNodes != nil {
err = downloadFallbackAgentBundleFromKubernetes(ctx, antreaClientset, k8sClient, failedNodes, dir)
if err != nil {
if err = downloadFallbackAgentBundleFromKubernetes(ctx, antreaClientset, k8sClient, failedNodes, dir); err != nil {
fmt.Println("Failed to download agent bundle from kubernetes api: " + err.Error())
} else {
allFailed = false
}
}
if controllerFailed {
err = downloadFallbackControllerBundleFromKubernetes(ctx, antreaClientset, k8sClient, dir)
if err != nil {
if err = downloadFallbackControllerBundleFromKubernetes(ctx, antreaClientset, k8sClient, dir); err != nil {
fmt.Println("Failed to download controller bundle from kubernetes api: " + err.Error())
} else {
allFailed = false
Expand Down Expand Up @@ -762,7 +760,7 @@ func downloadFallbackControllerBundleFromKubernetes(ctx context.Context, antreaC
if err := downloadPodLogs(ctx, k8sClient, pod.Namespace, pod.Name, k8s.GetPodContainerNames(pod), tmpDir); err != nil {
return err
}
return packPodLogs(pod, dir, tmpDir)
return packPodBundle(pod, dir, tmpDir)
}

func downloadFallbackAgentBundleFromKubernetes(ctx context.Context, antreaClientset antrea.Interface, k8sClient kubernetes.Interface, failedNodes []string, dir string) error {
Expand Down Expand Up @@ -807,15 +805,15 @@ func downloadFallbackAgentBundleFromKubernetes(ctx context.Context, antreaClient
if err != nil {
return err
}
return packPodLogs(&pod, dir, tmpDir)
return packPodBundle(&pod, dir, tmpDir)
}(); err != nil {
errors = append(errors, err)
}
}
return utilerror.NewAggregate(errors)
}

func packPodLogs(pod *corev1.Pod, dir string, logsDir string) error {
func packPodBundle(pod *corev1.Pod, dir string, bundleDir string) error {
prefix := "agent_"
if strings.Contains(pod.Name, "controller") {
prefix = "controller_"
Expand All @@ -824,17 +822,16 @@ func packPodLogs(pod *corev1.Pod, dir string, logsDir string) error {
f, err := defaultFS.Create(gzFileName)
if err != nil {
return err
} else {
defer f.Close()
_, err := compress.PackDir(defaultFS, logsDir, f)
return err
}
defer f.Close()
_, err = compress.PackDir(defaultFS, bundleDir, f)
return err
}

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

_, err = io.Copy(f, logStream)
if err != nil {
if _, err = io.Copy(f, logStream); err != nil {
return err
}
return logStream.Close()
Expand Down
12 changes: 5 additions & 7 deletions pkg/antctl/raw/supportbundle/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ func TestProcessResults(t *testing.T) {

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

unpackError := compress.UnpackDir(defaultFS, filepath.Join(option.dir, tgzFileName), option.dir)
require.NoError(t, unpackError)
expectFileName := "logs/agent/antrea-agent.log"
expectFileName := filepath.Join("logs", "agent", "antrea-agent.log")
if node == "" {
expectFileName = "logs/controller/antrea-controller.log"
expectFileName = filepath.Join("logs", "controller", "antrea-controller.log")
}
ok, checkErr = afero.Exists(defaultFS, filepath.Join(option.dir, expectFileName))
require.NoError(t, checkErr)
assert.True(t, ok, "expected log file %s not found", expectFileName)
deleteErr := defaultFS.Remove(filepath.Join(option.dir, expectFileName))
require.NoError(t, deleteErr)
defaultFS.Remove(filepath.Join(option.dir, expectFileName))
}

if node == "" {
continue
}
Expand Down
7 changes: 3 additions & 4 deletions pkg/util/compress/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import (
)

// Sanitize archive file pathing from "G305: Zip Slip vulnerability"
func sanitizeArchivePath(d, t string) (v string, err error) {
v = filepath.Join(d, t)
func sanitizeArchivePath(d, t string) (string, error) {
v := filepath.Join(d, t)
if strings.HasPrefix(v, filepath.Clean(d)) {
return v, nil
}
Expand Down Expand Up @@ -76,8 +76,7 @@ func UnpackDir(fs afero.Fs, fileName string, targetDir string) error {
}
for {
// to resolve G110: Potential DoS vulnerability via decompression bomb
_, err := io.CopyN(outFile, tarReader, 1024)
if err != nil {
if _, err := io.CopyN(outFile, tarReader, 1024); err != nil {
if err == io.EOF {
break
}
Expand Down

0 comments on commit b0a69ea

Please sign in to comment.