Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to get logs of telemetrygen #33538

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/k8stest/k8s_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package k8stest // import "github.com/open-telemetry/opentelemetry-collector-con
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"testing"
Expand All @@ -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())))
Expand Down
49 changes: 48 additions & 1 deletion processor/k8sattributesprocessor/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
package k8sattributesprocessor

import (
"bytes"
"context"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Loading