From 933a339a4d85ca461e82edf1ba03aa1ee19c59aa Mon Sep 17 00:00:00 2001 From: Hang Yan Date: Fri, 15 Nov 2024 13:00:08 +0800 Subject: [PATCH] update Signed-off-by: Hang Yan --- pkg/antctl/raw/supportbundle/command.go | 31 +++++++++---------------- pkg/util/k8s/pod.go | 12 ++++++++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/pkg/antctl/raw/supportbundle/command.go b/pkg/antctl/raw/supportbundle/command.go index bb7667f8412..a6499d57afb 100644 --- a/pkg/antctl/raw/supportbundle/command.go +++ b/pkg/antctl/raw/supportbundle/command.go @@ -55,6 +55,7 @@ import ( systemclientset "antrea.io/antrea/pkg/client/clientset/versioned/typed/system/v1beta1" "antrea.io/antrea/pkg/util/compress" + "antrea.io/antrea/pkg/util/k8s" ) const ( @@ -747,9 +748,7 @@ func downloadPodInfoFromKubernetes(antreaClientset antrea.Interface, k8sClient k } var errors []error - for _, pod := range pods.Items { - tmpDir, err := afero.TempDir(defaultFS, "", "bundle_tmp_") if err != nil { errors = append(errors, err) @@ -759,21 +758,16 @@ func downloadPodInfoFromKubernetes(antreaClientset antrea.Interface, k8sClient k if pod.Labels["component"] == "antrea-controller" && isControllerFail { controllerInfo, err := antreaClientset.CrdV1beta1().AntreaControllerInfos().Get(context.TODO(), "antrea-controller", metav1.GetOptions{}) - if err != nil { - errors = append(errors, err) - continue - } - data, err := yaml.Marshal(controllerInfo) - if err != nil { - errors = append(errors, err) - continue + if err == nil { + data, err := yaml.Marshal(controllerInfo) + if err == nil { + err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "controllerinfo"), data, 0644) + errors = append(errors, err) + } } - err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "controllerinfo"), data, 0644) errors = append(errors, err) - err = downloadPodLogs(k8sClient, "controller", pod.Namespace, pod.Name, []string{"antrea-controller"}, dir, tmpDir) errors = append(errors, err) - } if _, exist := failedNodesMap[pod.Spec.NodeName]; !exist { @@ -783,17 +777,14 @@ func downloadPodInfoFromKubernetes(antreaClientset antrea.Interface, k8sClient k if pod.Labels["component"] == "antrea-agent" { if agentInfo, ok := agentInfoMap[pod.Spec.NodeName]; ok { data, err := yaml.Marshal(agentInfo) - if err != nil { + errors = append(errors, err) + if err == nil { + err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "agentinfo"), data, 0644) errors = append(errors, err) - continue } - err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "agentinfo"), data, 0644) - errors = append(errors, err) } - - err = downloadPodLogs(k8sClient, "agent_"+pod.Spec.NodeName, pod.Namespace, pod.Name, []string{"antrea-agent", "antrea-ovs", "install-cni"}, dir, tmpDir) + err = downloadPodLogs(k8sClient, "agent_"+pod.Spec.NodeName, pod.Namespace, pod.Name, k8s.GetPodContainerNames(&pod), dir, tmpDir) errors = append(errors, err) - } } return utilerror.NewAggregate(errors) diff --git a/pkg/util/k8s/pod.go b/pkg/util/k8s/pod.go index f14c2a73a56..c19d84f5ffc 100644 --- a/pkg/util/k8s/pod.go +++ b/pkg/util/k8s/pod.go @@ -20,3 +20,15 @@ import v1 "k8s.io/api/core/v1" func IsPodTerminated(pod *v1.Pod) bool { return pod.Status.Phase == v1.PodFailed || pod.Status.Phase == v1.PodSucceeded } + +// GetPodContainersNames return all the container names in a pod, including init container. +func GetPodContainerNames(pod *v1.Pod) []string { + var names []string + for _, c := range pod.Spec.InitContainers { + names = append(names, c.Name) + } + for _, c := range pod.Spec.Containers { + names = append(names, c.Name) + } + return names +}