@@ -2,13 +2,18 @@ package data_collector
2
2
3
3
import (
4
4
"archive/tar"
5
+ "bytes"
5
6
"compress/gzip"
6
7
"fmt"
7
8
helmClient "github.com/mittwald/go-helm-client"
8
9
"io"
10
+ corev1 "k8s.io/api/core/v1"
9
11
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
10
12
"k8s.io/client-go/kubernetes"
13
+ "k8s.io/client-go/kubernetes/scheme"
14
+ "k8s.io/client-go/rest"
11
15
"k8s.io/client-go/tools/clientcmd"
16
+ "k8s.io/client-go/tools/remotecommand"
12
17
"k8s.io/client-go/util/homedir"
13
18
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned"
14
19
"os"
@@ -20,6 +25,7 @@ import (
20
25
type DataCollector struct {
21
26
BaseDir string
22
27
Namespaces []string
28
+ K8sRestConfig * rest.Config
23
29
K8sCoreClientSet * kubernetes.Clientset
24
30
K8sCrdClientSet * crdClient.Clientset
25
31
K8sMetricsClientSet * metricsClient.Clientset
@@ -52,6 +58,7 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
52
58
}
53
59
54
60
//Initialize clients
61
+ dc .K8sRestConfig = config
55
62
dc .K8sCoreClientSet , _ = kubernetes .NewForConfig (config )
56
63
dc .K8sCrdClientSet , _ = crdClient .NewForConfig (config )
57
64
dc .K8sMetricsClientSet , _ = metricsClient .NewForConfig (config )
@@ -124,3 +131,27 @@ func (c *DataCollector) WrapUp() (string, error) {
124
131
_ = os .RemoveAll (c .BaseDir )
125
132
return tarballName , nil
126
133
}
134
+
135
+ func (c * DataCollector ) PodExecutor (namespace string , pod string , command []string ) ([]byte ,error ) {
136
+ req := c .K8sCoreClientSet .CoreV1 ().RESTClient ().Post ().
137
+ Namespace (namespace ).
138
+ Resource ("pods" ).
139
+ Name (pod ).
140
+ SubResource ("exec" ).
141
+ VersionedParams (& corev1.PodExecOptions {
142
+ Command : command ,
143
+ Stdin : false ,
144
+ Stdout : true ,
145
+ Stderr : true ,
146
+ TTY : true ,
147
+ }, scheme .ParameterCodec )
148
+
149
+ exec , _ := remotecommand .NewSPDYExecutor (c .K8sRestConfig , "POST" , req .URL ())
150
+ var stdout , stderr bytes.Buffer
151
+ err := exec .Stream (remotecommand.StreamOptions {
152
+ Stdin : nil ,
153
+ Stdout : & stdout ,
154
+ Stderr : & stderr ,
155
+ })
156
+ return stdout .Bytes (), err
157
+ }
0 commit comments