Skip to content

Commit 657c69c

Browse files
committed
feat: new job to collect nginx -T output
1 parent 02e8f31 commit 657c69c

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

pkg/data_collector/data_collector.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ package data_collector
22

33
import (
44
"archive/tar"
5+
"bytes"
56
"compress/gzip"
67
"fmt"
78
helmClient "github.com/mittwald/go-helm-client"
89
"io"
10+
corev1 "k8s.io/api/core/v1"
911
crdClient "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
1012
"k8s.io/client-go/kubernetes"
13+
"k8s.io/client-go/kubernetes/scheme"
14+
"k8s.io/client-go/rest"
1115
"k8s.io/client-go/tools/clientcmd"
16+
"k8s.io/client-go/tools/remotecommand"
1217
"k8s.io/client-go/util/homedir"
1318
metricsClient "k8s.io/metrics/pkg/client/clientset/versioned"
1419
"os"
@@ -20,6 +25,7 @@ import (
2025
type DataCollector struct {
2126
BaseDir string
2227
Namespaces []string
28+
K8sRestConfig *rest.Config
2329
K8sCoreClientSet *kubernetes.Clientset
2430
K8sCrdClientSet *crdClient.Clientset
2531
K8sMetricsClientSet *metricsClient.Clientset
@@ -52,6 +58,7 @@ func NewDataCollector(namespaces ...string) (*DataCollector, error) {
5258
}
5359

5460
//Initialize clients
61+
dc.K8sRestConfig = config
5562
dc.K8sCoreClientSet, _ = kubernetes.NewForConfig(config)
5663
dc.K8sCrdClientSet, _ = crdClient.NewForConfig(config)
5764
dc.K8sMetricsClientSet, _ = metricsClient.NewForConfig(config)
@@ -124,3 +131,27 @@ func (c *DataCollector) WrapUp() (string, error) {
124131
_ = os.RemoveAll(c.BaseDir)
125132
return tarballName, nil
126133
}
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+
}

pkg/jobs/job_list.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"io"
1010
corev1 "k8s.io/api/core/v1"
1111
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12-
"log"
1312
"path"
13+
"strings"
1414
"time"
1515
)
1616

@@ -43,10 +43,7 @@ func JobList() []Job {
4343
for _, container := range pod.Spec.Containers {
4444
logFileName := path.Join(dc.BaseDir, namespace, "logs", fmt.Sprintf("%s__%s.txt", pod.Name, container.Name))
4545
bufferedLogs := dc.K8sCoreClientSet.CoreV1().Pods(namespace).GetLogs(pod.Name, &corev1.PodLogOptions{Container: container.Name})
46-
podLogs, err := bufferedLogs.Stream(context.TODO())
47-
if err != nil {
48-
log.Fatal("error in opening stream")
49-
}
46+
podLogs, _ := bufferedLogs.Stream(context.TODO())
5047
buf := new(bytes.Buffer)
5148
_, _ = io.Copy(buf, podLogs)
5249
podLogs.Close()
@@ -268,6 +265,28 @@ func JobList() []Job {
268265
ch <- jobResult
269266
},
270267
},
268+
{
269+
Name: "exec-nginx-t",
270+
Global: true,
271+
Timeout: time.Second * 10,
272+
Execute: func(dc *data_collector.DataCollector, ctx context.Context, ch chan JobResult) {
273+
jobResult := JobResult{Files: make(map[string][]byte), Error: nil}
274+
command := []string{"/bin/sh", "-c", "nginx -T"}
275+
for _, namespace := range dc.Namespaces {
276+
pods, _ := dc.K8sCoreClientSet.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
277+
for _, pod := range pods.Items {
278+
if strings.Contains(pod.Name, "ingress") {
279+
res, err := dc.PodExecutor(namespace, pod.Name, command)
280+
if err != nil {
281+
jobResult.Error = err
282+
}
283+
jobResult.Files[path.Join(dc.BaseDir, namespace, pod.Name+"-nginx-t.txt")] = res
284+
}
285+
}
286+
}
287+
ch <- jobResult
288+
},
289+
},
271290
}
272291
return jobList
273292
}

0 commit comments

Comments
 (0)