4
4
"archive/tar"
5
5
"bytes"
6
6
"compress/gzip"
7
+ "context"
7
8
"fmt"
8
9
helmClient "github.com/mittwald/go-helm-client"
9
10
"io"
@@ -16,6 +17,7 @@ import (
16
17
"k8s.io/client-go/tools/remotecommand"
17
18
"k8s.io/client-go/util/homedir"
18
19
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned"
20
+ "log"
19
21
"os"
20
22
"path/filepath"
21
23
"strconv"
@@ -25,6 +27,8 @@ import (
25
27
type DataCollector struct {
26
28
BaseDir string
27
29
Namespaces []string
30
+ Logger * log.Logger
31
+ LogFile * os.File
28
32
K8sRestConfig * rest.Config
29
33
K8sCoreClientSet * kubernetes.Clientset
30
34
K8sCrdClientSet * crdClient.Clientset
@@ -35,11 +39,15 @@ type DataCollector struct {
35
39
func NewDataCollector (namespaces ... string ) (* DataCollector , error ) {
36
40
37
41
tmpDir , err := os .MkdirTemp ("" , "kic-diag" )
38
-
39
42
if err != nil {
40
43
return nil , fmt .Errorf ("unable to create temp directory: %s" , err )
41
44
}
42
45
46
+ logFile , err := os .OpenFile (filepath .Join (tmpDir , "kic-supportpkg.log" ), os .O_APPEND | os .O_CREATE | os .O_WRONLY , 0644 )
47
+ if err != nil {
48
+ return nil , fmt .Errorf ("unable to create log file: %s" , err )
49
+ }
50
+
43
51
// Find config
44
52
kubeConfig := os .Getenv ("KUBECONFIG" )
45
53
if kubeConfig == "" {
@@ -54,6 +62,8 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
54
62
dc := DataCollector {
55
63
BaseDir : tmpDir ,
56
64
Namespaces : namespaces ,
65
+ LogFile : logFile ,
66
+ Logger : log .New (logFile , "" , log .LstdFlags | log .LUTC | log .Lmicroseconds | log .Lshortfile ),
57
67
K8sHelmClientSet : make (map [string ]helmClient.Client ),
58
68
}
59
69
@@ -79,6 +89,8 @@ func (c *DataCollector) WrapUp() (string, error) {
79
89
tarballName := fmt .Sprintf ("kic-supportpkg-%s.tar.gz" , unixTimeString )
80
90
tarballRootDirName := fmt .Sprintf ("kic-supportpkg-%s" , unixTimeString )
81
91
92
+ c .LogFile .Close ()
93
+
82
94
file , err := os .Create (tarballName )
83
95
if err != nil {
84
96
return "" , err
@@ -132,7 +144,7 @@ func (c *DataCollector) WrapUp() (string, error) {
132
144
return tarballName , nil
133
145
}
134
146
135
- func (c * DataCollector ) PodExecutor (namespace string , pod string , command []string ) ([]byte ,error ) {
147
+ func (c * DataCollector ) PodExecutor (namespace string , pod string , command []string , ctx context. Context ) ([]byte , error ) {
136
148
req := c .K8sCoreClientSet .CoreV1 ().RESTClient ().Post ().
137
149
Namespace (namespace ).
138
150
Resource ("pods" ).
@@ -148,10 +160,10 @@ func (c *DataCollector) PodExecutor(namespace string, pod string, command []stri
148
160
149
161
exec , _ := remotecommand .NewSPDYExecutor (c .K8sRestConfig , "POST" , req .URL ())
150
162
var stdout , stderr bytes.Buffer
151
- err := exec .Stream ( remotecommand.StreamOptions {
163
+ err := exec .StreamWithContext ( ctx , remotecommand.StreamOptions {
152
164
Stdin : nil ,
153
165
Stdout : & stdout ,
154
166
Stderr : & stderr ,
155
167
})
156
168
return stdout .Bytes (), err
157
- }
169
+ }
0 commit comments