15
15
package supportbundle
16
16
17
17
import (
18
- "antrea.io/antrea/pkg/util/compress"
19
18
"bufio"
20
19
"context"
21
20
"encoding/json"
22
21
"fmt"
23
- "github.com/spf13/afero"
24
22
"io"
25
- utilerror "k8s.io/apimachinery/pkg/util/errors"
26
23
"net"
27
24
"os"
28
25
"path"
@@ -31,6 +28,11 @@ import (
31
28
"sync"
32
29
"time"
33
30
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
+
34
36
"github.com/cheggaaa/pb/v3"
35
37
"github.com/spf13/cobra"
36
38
"golang.org/x/sync/errgroup"
@@ -610,7 +612,7 @@ func controllerRemoteRunE(cmd *cobra.Command, args []string) error {
610
612
611
613
results := requestAll (agentClients , controllerClient , bar )
612
614
results = downloadAll (agentClients , controllerClient , dir , bar , results )
613
- return processResults (k8sClientset , results , dir )
615
+ return processResults (antreaClientset , k8sClientset , results , dir )
614
616
}
615
617
616
618
func genErrorMsg (resultMap map [string ]error ) string {
@@ -624,7 +626,7 @@ func genErrorMsg(resultMap map[string]error) string {
624
626
// processResults will output the failed nodes and their reasons if any. If no data was collected,
625
627
// error is returned, otherwise will return nil. For failed nodes and controller, will also trying to get logs from
626
628
// 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 {
628
630
resultStr := ""
629
631
var failedNodes []string
630
632
allFailed := true
@@ -656,7 +658,7 @@ func processResults(k8sClient kubernetes.Interface, resultMap map[string]error,
656
658
657
659
// download logs from kubernetes api
658
660
if failedNodes != nil || controllerFail {
659
- err := downloadLogsFromKubernetes ( k8sClient , failedNodes , controllerFail , dir )
661
+ err := downloadPodInfoFromKubernetes ( antreaClientset , k8sClient , failedNodes , controllerFail , dir )
660
662
if err != nil {
661
663
fmt .Println ("Failed to download logs from kubernetes api: " + err .Error ())
662
664
} else {
@@ -671,8 +673,18 @@ func processResults(k8sClient kubernetes.Interface, resultMap map[string]error,
671
673
}
672
674
}
673
675
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
+
676
688
pods , err := k8sClient .CoreV1 ().Pods ("kube-system" ).List (context .TODO (), metav1.ListOptions {
677
689
ResourceVersion : "0" ,
678
690
LabelSelector : "app=antrea" ,
@@ -689,41 +701,67 @@ func downloadLogsFromKubernetes(k8sClient kubernetes.Interface, failedNodes []st
689
701
var errors []error
690
702
691
703
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 {
694
707
errors = append (errors , err )
695
708
continue
696
709
}
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
+ }
697
730
698
731
if _ , exist := failedNodesMap [pod .Spec .NodeName ]; ! exist {
699
732
continue
700
733
}
701
734
702
735
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 )
704
747
errors = append (errors , err )
748
+
705
749
}
706
750
}
707
751
return utilerror .NewAggregate (errors )
708
752
}
709
753
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 {
711
755
var errors []error
712
756
713
- tmpDir , err := afero .TempDir (defaultFS , "" , "bundle_tmp_" )
714
- if err != nil {
715
- return err
716
- }
717
- defer defaultFS .RemoveAll (tmpDir )
718
-
719
757
for _ , containerName := range containers {
720
758
containerDirName := containerName
721
759
if strings .HasPrefix (containerName , "antrea-" ) {
722
760
containerDirName = strings .ReplaceAll (containerName , "antrea-" , "" )
723
761
}
724
762
725
763
podLogDir := filepath .Join (tmpDir , "logs" , containerDirName )
726
- err = os .MkdirAll (podLogDir , 0755 )
764
+ err : = os .MkdirAll (podLogDir , 0755 )
727
765
if err != nil {
728
766
return err
729
767
}
0 commit comments