Skip to content

Commit 9e4e9d4

Browse files
committed
Add operator logs
Signed-off-by: Israel Blancas <[email protected]>
1 parent b8d8ba0 commit 9e4e9d4

File tree

4 files changed

+73
-8
lines changed

4 files changed

+73
-8
lines changed

cmd/gather/cluster/cluster.go

+25
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,31 @@ func (c *Cluster) getOperatorDeployment() (appsv1.Deployment, error) {
7070

7171
}
7272

73+
func (c *Cluster) GetOperatorLogs() error {
74+
deployment, err := c.getOperatorDeployment()
75+
if err != nil {
76+
return err
77+
}
78+
79+
labelSelector := labels.Set(deployment.Spec.Selector.MatchLabels).AsSelectorPreValidated()
80+
operatorPods := corev1.PodList{}
81+
err = c.config.KubernetesClient.List(context.TODO(), &operatorPods, &client.ListOptions{
82+
LabelSelector: labelSelector,
83+
})
84+
if err != nil {
85+
return err
86+
}
87+
88+
pod := operatorPods.Items[0]
89+
c.getPodLogs(pod.Name, pod.Namespace, "manager")
90+
return nil
91+
}
92+
93+
func (c *Cluster) getPodLogs(podName, namespace, container string) {
94+
pods := c.config.KubernetesClientSet.CoreV1().Pods(namespace)
95+
writeLogToFile(c.config.CollectionDir, podName, container, pods)
96+
}
97+
7398
func (c *Cluster) GetOperatorDeploymentInfo() error {
7499
err := os.MkdirAll(c.config.CollectionDir, os.ModePerm)
75100
if err != nil {

cmd/gather/cluster/write.go

+31
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
package cluster
22

33
import (
4+
"context"
45
"fmt"
6+
"io"
57
"log"
68
"os"
79
"path/filepath"
810
"reflect"
911
"strings"
1012

1113
"github.com/open-telemetry/opentelemetry-operator/apis/v1beta1"
14+
corev1 "k8s.io/api/core/v1"
1215
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1316
"k8s.io/apimachinery/pkg/runtime"
1417
"k8s.io/apimachinery/pkg/runtime/serializer/json"
18+
cgocorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
1519
"sigs.k8s.io/controller-runtime/pkg/client"
1620
)
1721

@@ -44,6 +48,33 @@ func createFile(outputDir string, obj client.Object) (*os.File, error) {
4448
return os.Create(path)
4549
}
4650

51+
func writeLogToFile(outputDir, podName, container string, p cgocorev1.PodInterface) {
52+
req := p.GetLogs(podName, &corev1.PodLogOptions{Container: container})
53+
podLogs, err := req.Stream(context.Background())
54+
if err != nil {
55+
log.Fatalf("Error getting pod logs: %v\n", err)
56+
return
57+
}
58+
defer podLogs.Close()
59+
60+
err = os.MkdirAll(outputDir, os.ModePerm)
61+
if err != nil {
62+
log.Fatalln(err)
63+
return
64+
}
65+
66+
outputFile, err := os.Create(filepath.Join(outputDir, podName))
67+
if err != nil {
68+
log.Fatalf("Error getting pod logs: %v\n", err)
69+
return
70+
}
71+
72+
_, err = io.Copy(outputFile, podLogs)
73+
if err != nil {
74+
log.Fatalf("Error copying logs to file: %v\n", err)
75+
}
76+
}
77+
4778
func writeToFile(outputDir string, o client.Object) {
4879
// Open or create the file for writing
4980
outputFile, err := createFile(outputDir, o)

cmd/gather/config/config.go

+16-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ import (
66

77
"github.com/spf13/pflag"
88
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/client-go/kubernetes"
910
"k8s.io/client-go/tools/clientcmd"
1011
"k8s.io/client-go/util/homedir"
1112
"sigs.k8s.io/controller-runtime/pkg/client"
1213
)
1314

1415
type Config struct {
15-
CollectionDir string
16-
OperatorName string
17-
OperatorNamespace string
18-
KubernetesClient client.Client
16+
CollectionDir string
17+
OperatorName string
18+
OperatorNamespace string
19+
KubernetesClient client.Client
20+
KubernetesClientSet *kubernetes.Clientset
1921
}
2022

2123
func NewConfig(scheme *runtime.Scheme) (Config, error) {
@@ -37,10 +39,16 @@ func NewConfig(scheme *runtime.Scheme) (Config, error) {
3739
return Config{}, fmt.Errorf("Creating the Kubernetes client: %s\n", err)
3840
}
3941

42+
clientset, err := kubernetes.NewForConfig(config)
43+
if err != nil {
44+
return Config{}, fmt.Errorf("Creating the Kubernetes clienset: %s\n", err)
45+
}
46+
4047
return Config{
41-
CollectionDir: collectionDir,
42-
KubernetesClient: clusterClient,
43-
OperatorName: operatorName,
44-
OperatorNamespace: operatorNamespace,
48+
CollectionDir: collectionDir,
49+
KubernetesClient: clusterClient,
50+
KubernetesClientSet: clientset,
51+
OperatorName: operatorName,
52+
OperatorNamespace: operatorNamespace,
4553
}, nil
4654
}

cmd/gather/main.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func main() {
4848
}
4949

5050
cluster := cluster.NewCluster(&config)
51+
cluster.GetOperatorLogs()
5152
cluster.GetOperatorDeploymentInfo()
5253
cluster.GetOLMInfo()
5354
cluster.GetOpenTelemetryCollectors()

0 commit comments

Comments
 (0)