k8s_telemetrygen.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package k8stest // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest"
  4. import (
  5. "bytes"
  6. "context"
  7. "os"
  8. "path/filepath"
  9. "testing"
  10. "text/template"
  11. "time"
  12. "github.com/stretchr/testify/require"
  13. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  14. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  15. "k8s.io/apimachinery/pkg/runtime/schema"
  16. "k8s.io/client-go/dynamic"
  17. )
  18. type TelemetrygenObjInfo struct {
  19. Namespace string
  20. PodLabelSelectors map[string]any
  21. DataType string
  22. Workload string
  23. }
  24. func CreateTelemetryGenObjects(t *testing.T, client *dynamic.DynamicClient, testID string) ([]*unstructured.Unstructured, []*TelemetrygenObjInfo) {
  25. telemetrygenObjInfos := make([]*TelemetrygenObjInfo, 0)
  26. manifestsDir := filepath.Join(".", "testdata", "e2e", "telemetrygen")
  27. manifestFiles, err := os.ReadDir(manifestsDir)
  28. require.NoErrorf(t, err, "failed to read telemetrygen manifests directory %s", manifestsDir)
  29. createdObjs := make([]*unstructured.Unstructured, 0, len(manifestFiles))
  30. for _, manifestFile := range manifestFiles {
  31. tmpl := template.Must(template.New(manifestFile.Name()).ParseFiles(filepath.Join(manifestsDir, manifestFile.Name())))
  32. for _, dataType := range []string{"metrics", "logs", "traces"} {
  33. manifest := &bytes.Buffer{}
  34. require.NoError(t, tmpl.Execute(manifest, map[string]string{
  35. "Name": "telemetrygen-" + testID,
  36. "DataType": dataType,
  37. "OTLPEndpoint": "otelcol-" + testID + ":4317",
  38. }))
  39. obj, err := CreateObject(client, manifest.Bytes())
  40. require.NoErrorf(t, err, "failed to create telemetrygen object from manifest %s", manifestFile.Name())
  41. selector := obj.Object["spec"].(map[string]any)["selector"]
  42. telemetrygenObjInfos = append(telemetrygenObjInfos, &TelemetrygenObjInfo{
  43. Namespace: "default",
  44. PodLabelSelectors: selector.(map[string]any)["matchLabels"].(map[string]any),
  45. DataType: dataType,
  46. Workload: obj.GetKind(),
  47. })
  48. createdObjs = append(createdObjs, obj)
  49. }
  50. }
  51. return createdObjs, telemetrygenObjInfos
  52. }
  53. func WaitForTelemetryGenToStart(t *testing.T, client *dynamic.DynamicClient, podNamespace string, podLabels map[string]any, workload, dataType string) {
  54. podGVR := schema.GroupVersionResource{Version: "v1", Resource: "pods"}
  55. listOptions := metav1.ListOptions{LabelSelector: SelectorFromMap(podLabels).String()}
  56. podTimeoutMinutes := 3
  57. var podPhase string
  58. require.Eventually(t, func() bool {
  59. list, err := client.Resource(podGVR).Namespace(podNamespace).List(context.Background(), listOptions)
  60. require.NoError(t, err, "failed to list collector pods")
  61. if len(list.Items) == 0 {
  62. return false
  63. }
  64. podPhase = list.Items[0].Object["status"].(map[string]any)["phase"].(string)
  65. return podPhase == "Running"
  66. }, time.Duration(podTimeoutMinutes)*time.Minute, 50*time.Millisecond,
  67. "telemetrygen pod of Workload [%s] in datatype [%s] haven't started within %d minutes, latest pod phase is %s", workload, dataType, podTimeoutMinutes, podPhase)
  68. }