From e634447b55ec4bed615372004a0a0d73018c4bb8 Mon Sep 17 00:00:00 2001 From: ChrsMark Date: Thu, 13 Jun 2024 12:09:22 +0300 Subject: [PATCH 1/2] try to get logs of telemetrygen Signed-off-by: ChrsMark --- processor/k8sattributesprocessor/e2e_test.go | 49 +++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/processor/k8sattributesprocessor/e2e_test.go b/processor/k8sattributesprocessor/e2e_test.go index 942e9c98f94e..7e547d12dfa1 100644 --- a/processor/k8sattributesprocessor/e2e_test.go +++ b/processor/k8sattributesprocessor/e2e_test.go @@ -6,8 +6,10 @@ package k8sattributesprocessor import ( + "bytes" "context" "fmt" + "io" "os" "path/filepath" "regexp" @@ -26,6 +28,10 @@ import ( "go.uber.org/multierr" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" ) const ( @@ -422,13 +428,18 @@ func TestE2E_ClusterRBAC(t *testing.T) { // Test with `filter::namespace` set and only role binding to collector's SA. We can't get node and namespace labels/annotations. func TestE2E_NamespacedRBAC(t *testing.T) { - t.Skip("skipping flaky test, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/33520") + //t.Skip("skipping flaky test, see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/33520") testDir := filepath.Join("testdata", "e2e", "namespacedrbac") k8sClient, err := k8stest.NewK8sClient(testKubeConfig) require.NoError(t, err) + config, err := clientcmd.BuildConfigFromFlags("", testKubeConfig) + if err != nil { + } + k8sclient, err := kubernetes.NewForConfig(config) + nsFile := filepath.Join(testDir, "namespace.yaml") buf, err := os.ReadFile(nsFile) require.NoErrorf(t, err, "failed to read namespace object file %s", nsFile) @@ -465,6 +476,9 @@ func TestE2E_NamespacedRBAC(t *testing.T) { } wantEntries := 20 // Minimal number of metrics/traces/logs to wait for. + time.Sleep(10 * time.Second) + getPodsLogs(k8sclient) + waitForData(t, wantEntries, metricsConsumer, tracesConsumer, logsConsumer) tcs := []struct { @@ -849,3 +863,36 @@ func waitForData(t *testing.T, entriesNum int, mc *consumertest.MetricsSink, tc "failed to receive %d entries, received %d metrics, %d traces, %d logs in %d minutes", entriesNum, len(mc.AllMetrics()), len(tc.AllTraces()), len(lc.AllLogs()), timeoutMinutes) } + +func getPodsLogs(k8sclient kubernetes.Interface) { + getPodLogs(k8sclient, "component=telemetrygen") + getPodLogs(k8sclient, "app.kubernetes.io/name=opentelemetry-collector") +} + +func getPodLogs(k8sclient kubernetes.Interface, l string) { + ns := "e2ek8sattribute-namespacedrbac" + podLogOpts := corev1.PodLogOptions{} + listOptions := metav1.ListOptions{ + LabelSelector: l, + } + pods, _ := k8sclient.CoreV1().Pods("").List(context.TODO(), listOptions) + + for _, p := range pods.Items { + req := k8sclient.CoreV1().Pods(ns).GetLogs(p.Name, &podLogOpts) + podLogs, err := req.Stream(context.TODO()) + if err != nil { + fmt.Println("error in opening stream") + } + defer podLogs.Close() + + buf := new(bytes.Buffer) + _, err = io.Copy(buf, podLogs) + if err != nil { + fmt.Println("error in copy information from podLogs to buf") + } + str := buf.String() + + fmt.Println("Printing Logs of Pod: ", p.Name) + fmt.Println(str) + } +} From 1f9ef5a4e4f85403c7048d4a93ed853a37d0273e Mon Sep 17 00:00:00 2001 From: ChrsMark Date: Thu, 13 Jun 2024 13:54:30 +0300 Subject: [PATCH 2/2] print host endpoint Signed-off-by: ChrsMark --- internal/k8stest/k8s_collector.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/k8stest/k8s_collector.go b/internal/k8stest/k8s_collector.go index d607fd194e1a..e6f288ab926c 100644 --- a/internal/k8stest/k8s_collector.go +++ b/internal/k8stest/k8s_collector.go @@ -6,6 +6,7 @@ package k8stest // import "github.com/open-telemetry/opentelemetry-collector-con import ( "bytes" "context" + "fmt" "os" "path/filepath" "testing" @@ -29,6 +30,7 @@ func CreateCollectorObjects(t *testing.T, client *K8sClient, testID string, mani host := HostEndpoint(t) var podNamespace string var podLabels map[string]any + fmt.Println("Using HostEndpoint: ", host) createdObjs := make([]*unstructured.Unstructured, 0, len(manifestFiles)) for _, manifestFile := range manifestFiles { tmpl := template.Must(template.New(manifestFile.Name()).ParseFiles(filepath.Join(manifestsDir, manifestFile.Name())))