Skip to content

Commit a550b11

Browse files
committed
Support dump controller/agent info when fail
Signed-off-by: Hang Yan <[email protected]>
1 parent 89be42c commit a550b11

File tree

1 file changed

+57
-19
lines changed

1 file changed

+57
-19
lines changed

pkg/antctl/raw/supportbundle/command.go

+57-19
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,11 @@
1515
package supportbundle
1616

1717
import (
18-
"antrea.io/antrea/pkg/util/compress"
1918
"bufio"
2019
"context"
2120
"encoding/json"
2221
"fmt"
23-
"github.com/spf13/afero"
2422
"io"
25-
utilerror "k8s.io/apimachinery/pkg/util/errors"
2623
"net"
2724
"os"
2825
"path"
@@ -31,6 +28,11 @@ import (
3128
"sync"
3229
"time"
3330

31+
"antrea.io/antrea/pkg/apis/crd/v1beta1"
32+
"antrea.io/antrea/pkg/util/compress"
33+
"github.com/spf13/afero"
34+
utilerror "k8s.io/apimachinery/pkg/util/errors"
35+
3436
"github.com/cheggaaa/pb/v3"
3537
"github.com/spf13/cobra"
3638
"golang.org/x/sync/errgroup"
@@ -610,7 +612,7 @@ func controllerRemoteRunE(cmd *cobra.Command, args []string) error {
610612

611613
results := requestAll(agentClients, controllerClient, bar)
612614
results = downloadAll(agentClients, controllerClient, dir, bar, results)
613-
return processResults(k8sClientset, results, dir)
615+
return processResults(antreaClientset, k8sClientset, results, dir)
614616
}
615617

616618
func genErrorMsg(resultMap map[string]error) string {
@@ -624,7 +626,7 @@ func genErrorMsg(resultMap map[string]error) string {
624626
// processResults will output the failed nodes and their reasons if any. If no data was collected,
625627
// error is returned, otherwise will return nil. For failed nodes and controller, will also trying to get logs from
626628
// kubernetes api.
627-
func processResults(k8sClient kubernetes.Interface, resultMap map[string]error, dir string) error {
629+
func processResults(antreaClientset antrea.Interface, k8sClient kubernetes.Interface, resultMap map[string]error, dir string) error {
628630
resultStr := ""
629631
var failedNodes []string
630632
allFailed := true
@@ -656,7 +658,7 @@ func processResults(k8sClient kubernetes.Interface, resultMap map[string]error,
656658

657659
// download logs from kubernetes api
658660
if failedNodes != nil || controllerFail {
659-
err := downloadLogsFromKubernetes(k8sClient, failedNodes, controllerFail, dir)
661+
err := downloadPodInfoFromKubernetes(antreaClientset, k8sClient, failedNodes, controllerFail, dir)
660662
if err != nil {
661663
fmt.Println("Failed to download logs from kubernetes api: " + err.Error())
662664
} else {
@@ -671,8 +673,18 @@ func processResults(k8sClient kubernetes.Interface, resultMap map[string]error,
671673
}
672674
}
673675

674-
// downloadLogsFromKubernetes will try to download logs from kubernetes api for failed nodes and controller.
675-
func downloadLogsFromKubernetes(k8sClient kubernetes.Interface, failedNodes []string, isControllerFail bool, dir string) error {
676+
// downloadLogsFromKubernetes will try to download pod logs from kubernetes api for failed nodes and controller, as well as controllerinfo/agentinfo...
677+
func downloadPodInfoFromKubernetes(antreaClientset antrea.Interface, k8sClient kubernetes.Interface, failedNodes []string, isControllerFail bool, dir string) error {
678+
agentInfoList, err := antreaClientset.CrdV1beta1().AntreaAgentInfos().List(context.TODO(), metav1.ListOptions{ResourceVersion: "0"})
679+
if err != nil {
680+
return err
681+
}
682+
683+
agentInfoMap := map[string]v1beta1.AntreaAgentInfo{}
684+
for _, agentInfo := range agentInfoList.Items {
685+
agentInfoMap[agentInfo.Name] = agentInfo
686+
}
687+
676688
pods, err := k8sClient.CoreV1().Pods("kube-system").List(context.TODO(), metav1.ListOptions{
677689
ResourceVersion: "0",
678690
LabelSelector: "app=antrea",
@@ -689,41 +701,67 @@ func downloadLogsFromKubernetes(k8sClient kubernetes.Interface, failedNodes []st
689701
var errors []error
690702

691703
for _, pod := range pods.Items {
692-
if pod.Labels["component"] == "antrea-controller" && isControllerFail {
693-
err := downloadPodLogs(k8sClient, "controller", pod.Namespace, pod.Name, []string{"antrea-controller"}, dir)
704+
705+
tmpDir, err := afero.TempDir(defaultFS, "", "bundle_tmp_")
706+
if err != nil {
694707
errors = append(errors, err)
695708
continue
696709
}
710+
defer defaultFS.RemoveAll(tmpDir)
711+
712+
if pod.Labels["component"] == "antrea-controller" && isControllerFail {
713+
controllerInfo, err := antreaClientset.CrdV1beta1().AntreaControllerInfos().Get(context.TODO(), "antrea-controller", metav1.GetOptions{})
714+
if err != nil {
715+
errors = append(errors, err)
716+
continue
717+
}
718+
data, err := yaml.Marshal(controllerInfo)
719+
if err != nil {
720+
errors = append(errors, err)
721+
continue
722+
}
723+
err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "controllerinfo"), data, 0644)
724+
errors = append(errors, err)
725+
726+
err = downloadPodLogs(k8sClient, "controller", pod.Namespace, pod.Name, []string{"antrea-controller"}, dir, tmpDir)
727+
errors = append(errors, err)
728+
729+
}
697730

698731
if _, exist := failedNodesMap[pod.Spec.NodeName]; !exist {
699732
continue
700733
}
701734

702735
if pod.Labels["component"] == "antrea-agent" {
703-
err := downloadPodLogs(k8sClient, "agent_"+pod.Spec.NodeName, pod.Namespace, pod.Name, []string{"antrea-agent", "antrea-ovs", "install-cni"}, dir)
736+
if agentInfo, ok := agentInfoMap[pod.Spec.NodeName]; ok {
737+
data, err := yaml.Marshal(agentInfo)
738+
if err != nil {
739+
errors = append(errors, err)
740+
continue
741+
}
742+
err = afero.WriteFile(defaultFS, filepath.Join(tmpDir, "agentinfo"), data, 0644)
743+
errors = append(errors, err)
744+
}
745+
746+
err = downloadPodLogs(k8sClient, "agent_"+pod.Spec.NodeName, pod.Namespace, pod.Name, []string{"antrea-agent", "antrea-ovs", "install-cni"}, dir, tmpDir)
704747
errors = append(errors, err)
748+
705749
}
706750
}
707751
return utilerror.NewAggregate(errors)
708752
}
709753

710-
func downloadPodLogs(k8sClient kubernetes.Interface, comp string, namespace string, podName string, containers []string, dir string) error {
754+
func downloadPodLogs(k8sClient kubernetes.Interface, comp string, namespace string, podName string, containers []string, dir string, tmpDir string) error {
711755
var errors []error
712756

713-
tmpDir, err := afero.TempDir(defaultFS, "", "bundle_tmp_")
714-
if err != nil {
715-
return err
716-
}
717-
defer defaultFS.RemoveAll(tmpDir)
718-
719757
for _, containerName := range containers {
720758
containerDirName := containerName
721759
if strings.HasPrefix(containerName, "antrea-") {
722760
containerDirName = strings.ReplaceAll(containerName, "antrea-", "")
723761
}
724762

725763
podLogDir := filepath.Join(tmpDir, "logs", containerDirName)
726-
err = os.MkdirAll(podLogDir, 0755)
764+
err := os.MkdirAll(podLogDir, 0755)
727765
if err != nil {
728766
return err
729767
}

0 commit comments

Comments
 (0)