Skip to content

Commit

Permalink
[chore] Add e2e for receiver/kubeletstats (#26676)
Browse files Browse the repository at this point in the history
**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

Adds an E2E test for kubeletstats receiver

Updates existing e2e k8s tests to bootstrap kind cluster with valid
kubelet certificates. This will make k8s clusters used in the e2e test
more inline with the security practices used by real clusters and
improve testing with the kubeletstats receiver which connects to kubelet
server.

**Link to tracking Issue:**
[26319](#26319)

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>
  • Loading branch information
jinja2 authored Sep 14, 2023
1 parent 636cd78 commit 2d5eb37
Show file tree
Hide file tree
Showing 12 changed files with 1,563 additions and 136 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/configs/e2e-kind-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
kubeadmConfigPatches:
- |
kind: KubeletConfiguration
serverTLSBootstrap: true
9 changes: 8 additions & 1 deletion .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ jobs:
node_image: kindest/node:${{ matrix.k8s-version }}
kubectl_version: ${{ matrix.k8s-version }}
cluster_name: kind
config: ./.github/workflows/configs/e2e-kind-config.yaml
- name: Fix kubelet TLS server certificates
run: |
kubectl get csr -o=jsonpath='{range.items[?(@.spec.signerName=="kubernetes.io/kubelet-serving")]}{.metadata.name}{" "}{end}' | xargs kubectl certificate approve
- name: Download artifact
uses: actions/download-artifact@v3
with:
Expand All @@ -95,4 +99,7 @@ jobs:
run: |
cd processor/k8sattributesprocessor
go test -v --tags=e2e
- name: run receiver/kubeletstatsreceiver e2e tests
run: |
cd receiver/kubeletstatsreceiver
go test -v --tags=e2e
86 changes: 86 additions & 0 deletions receiver/kubeletstatsreceiver/e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build e2e
// +build e2e

package kubeletstatsreceiver

import (
"context"
"path/filepath"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumertest"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/receivertest"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/tools/clientcmd"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/golden"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest"
)

const testKubeConfig = "/tmp/kube-config-otelcol-e2e-testing"

func TestE2E(t *testing.T) {
var expected pmetric.Metrics
expectedFile := filepath.Join("testdata", "e2e", "expected.yaml")
expected, err := golden.ReadMetrics(expectedFile)
require.NoError(t, err)
kubeConfig, err := clientcmd.BuildConfigFromFlags("", testKubeConfig)
require.NoError(t, err)
dynamicClient, err := dynamic.NewForConfig(kubeConfig)
require.NoError(t, err)

testID := uuid.NewString()[:8]
collectorObjs := k8stest.CreateCollectorObjects(t, dynamicClient, testID)

defer func() {
for _, obj := range append(collectorObjs) {
require.NoErrorf(t, k8stest.DeleteObject(dynamicClient, obj), "failed to delete object %s", obj.GetName())
}
}()

metricsConsumer := new(consumertest.MetricsSink)
wantEntries := 10 // Minimal number of metrics to wait for.
waitForData(t, wantEntries, metricsConsumer)

require.NoError(t, pmetrictest.CompareMetrics(expected, metricsConsumer.AllMetrics()[len(metricsConsumer.AllMetrics())-1],
pmetrictest.IgnoreTimestamp(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreScopeVersion(),
pmetrictest.IgnoreResourceMetricsOrder(),
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreScopeMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreMetricValues(),
),
)
}

func waitForData(t *testing.T, entriesNum int, mc *consumertest.MetricsSink) {
f := otlpreceiver.NewFactory()
cfg := f.CreateDefaultConfig().(*otlpreceiver.Config)

rcvr, err := f.CreateMetricsReceiver(context.Background(), receivertest.NewNopCreateSettings(), cfg, mc)
require.NoError(t, rcvr.Start(context.Background(), componenttest.NewNopHost()))
require.NoError(t, err, "failed creating metrics receiver")
defer func() {
assert.NoError(t, rcvr.Shutdown(context.Background()))
}()

timeoutMinutes := 3
require.Eventuallyf(t, func() bool {
return len(mc.AllMetrics()) > entriesNum
}, time.Duration(timeoutMinutes)*time.Minute, 1*time.Second,
"failed to receive %d entries, received %d metrics in %d minutes", entriesNum,
len(mc.AllMetrics()), timeoutMinutes)
}
42 changes: 41 additions & 1 deletion receiver/kubeletstatsreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ go 1.20

require (
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.1
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.85.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8sconfig v0.85.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest v0.85.0
github.com/open-telemetry/opentelemetry-collector-contrib/internal/kubelet v0.85.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.85.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.85.0
go.opentelemetry.io/collector/config/confignet v0.85.0
Expand All @@ -14,6 +18,7 @@ require (
go.opentelemetry.io/collector/consumer v0.85.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014
go.opentelemetry.io/collector/receiver v0.85.0
go.opentelemetry.io/collector/receiver/otlpreceiver v0.85.0
go.opentelemetry.io/collector/semconv v0.85.0
go.uber.org/zap v1.25.0
k8s.io/api v0.28.1
Expand All @@ -23,9 +28,16 @@ require (
)

require (
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.6+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
Expand All @@ -34,12 +46,13 @@ require (
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/imdario/mergo v0.3.11 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/knadh/koanf v1.5.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand All @@ -48,32 +61,48 @@ require (
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.2.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/internal/common v0.85.0 // indirect
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.85.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc4 // indirect
github.com/openshift/api v3.9.0+incompatible // indirect
github.com/openshift/client-go v0.0.0-20210521082421-73d9475a9142 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v1.10.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector v0.85.0 // indirect
go.opentelemetry.io/collector/config/configauth v0.85.0 // indirect
go.opentelemetry.io/collector/config/configcompression v0.85.0 // indirect
go.opentelemetry.io/collector/config/configgrpc v0.85.0 // indirect
go.opentelemetry.io/collector/config/confighttp v0.85.0 // indirect
go.opentelemetry.io/collector/config/configopaque v0.85.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.85.0 // indirect
go.opentelemetry.io/collector/config/internal v0.85.0 // indirect
go.opentelemetry.io/collector/exporter v0.85.0 // indirect
go.opentelemetry.io/collector/extension v0.85.0 // indirect
go.opentelemetry.io/collector/extension/auth v0.85.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014 // indirect
go.opentelemetry.io/collector/processor v0.85.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.43.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.43.0 // indirect
go.opentelemetry.io/otel v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.17.0 // indirect
go.opentelemetry.io/otel/sdk v1.17.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.40.0 // indirect
go.opentelemetry.io/otel/trace v1.17.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/oauth2 v0.12.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/term v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.0 // indirect
Expand Down Expand Up @@ -103,3 +132,14 @@ retract (
v0.76.1
v0.65.0
)

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal => ../../internal/coreinternal

replace github.com/open-telemetry/opentelemetry-collector-contrib/internal/k8stest => ../../internal/k8stest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest => ../../pkg/pdatatest

replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../pkg/pdatautil

// ambiguous import: found package cloud.google.com/go/compute/metadata in multiple modules
replace cloud.google.com/go => cloud.google.com/go v0.110.7
Loading

0 comments on commit 2d5eb37

Please sign in to comment.