Skip to content

Commit

Permalink
do not try to observe cluster resource quotas when namespace limit ha…
Browse files Browse the repository at this point in the history
…s been specified

Signed-off-by: Florian Bacher <[email protected]>
  • Loading branch information
bacherfl committed Oct 17, 2024
1 parent c8b1f29 commit f94b075
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
51 changes: 51 additions & 0 deletions receiver/k8sclusterreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,57 @@ func TestReceiver(t *testing.T) {
require.NoError(t, r.Shutdown(ctx))
}

func TestNamespacedReceiver(t *testing.T) {
tt, err := componenttest.SetupTelemetry(component.NewID(metadata.Type))
require.NoError(t, err)
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()

client := newFakeClientWithAllResources()
osQuotaClient := fakeQuota.NewSimpleClientset()
sink := new(consumertest.MetricsSink)

r := setupReceiver(client, osQuotaClient, sink, nil, 10*time.Second, tt, "test")

// Setup k8s resources.
numPods := 2
numNodes := 1
numQuotas := 2

createPods(t, client, numPods)
createNodes(t, client, numNodes)
createClusterQuota(t, osQuotaClient, numQuotas)

ctx := context.Background()
require.NoError(t, r.Start(ctx, newNopHost()))

// Expects metric data from pods only, where each metric data
// struct corresponds to one resource.
// Nodes and ClusterResourceQuotas should not be observed as these are non-namespaced resources
expectedNumMetrics := numPods
var initialDataPointCount int
require.Eventually(t, func() bool {
initialDataPointCount = sink.DataPointCount()
return initialDataPointCount == expectedNumMetrics
}, 10*time.Second, 100*time.Millisecond,
"metrics not collected")

numPodsToDelete := 1
deletePods(t, client, numPodsToDelete)

// Expects metric data from a node, since other resources were deleted.
expectedNumMetrics = numPods - numPodsToDelete
var metricsCountDelta int
require.Eventually(t, func() bool {
metricsCountDelta = sink.DataPointCount() - initialDataPointCount
return metricsCountDelta == expectedNumMetrics
}, 10*time.Second, 100*time.Millisecond,
"updated metrics not collected")

require.NoError(t, r.Shutdown(ctx))
}

func TestReceiverTimesOutAfterStartup(t *testing.T) {
tt, err := componenttest.SetupTelemetry(component.NewID(metadata.Type))
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ rules:
- pods/status
- replicationcontrollers
- replicationcontrollers/status
- resourcequotas
- services
verbs:
- get
Expand Down
2 changes: 1 addition & 1 deletion receiver/k8sclusterreceiver/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (rw *resourceWatcher) initialize() error {
}
rw.client = client

if rw.config.Distribution == distributionOpenShift {
if rw.config.Distribution == distributionOpenShift && rw.config.Namespace == "" {
rw.osQuotaClient, err = rw.makeOpenShiftQuotaClient(rw.config.APIConfig)
if err != nil {
return fmt.Errorf("Failed to create OpenShift quota API client: %w", err)
Expand Down

0 comments on commit f94b075

Please sign in to comment.