Skip to content

Commit

Permalink
[receiver/k8scluster] fix lint error for k8sclusterreceiver (open-tel…
Browse files Browse the repository at this point in the history
…emetry#12214)

Signed-off-by: Ziqi Zhao <[email protected]>
  • Loading branch information
fatsheep9146 committed Jul 11, 2022
1 parent 2452d43 commit 2b1cf4c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:errcheck
package collection

import (
Expand Down Expand Up @@ -50,11 +49,11 @@ func TestMetricsStoreOperations(t *testing.T) {

// Update metric store with metrics
for _, u := range updates {
ms.update(&corev1.Pod{
require.NoError(t, ms.update(&corev1.Pod{
ObjectMeta: v1.ObjectMeta{
UID: u.id,
},
}, u.rm)
}, u.rm))
}

// Asset values form updates
Expand All @@ -67,19 +66,19 @@ func TestMetricsStoreOperations(t *testing.T) {
require.Equal(t, expectedMetricData, ms.getMetricData(time.Now()).ResourceMetrics().Len())

// Remove non existent item
ms.remove(&corev1.Pod{
require.NoError(t, ms.remove(&corev1.Pod{
ObjectMeta: v1.ObjectMeta{
UID: "1",
},
})
}))
require.Equal(t, len(updates), len(ms.metricsCache))

// Remove valid item from cache
ms.remove(&corev1.Pod{
require.NoError(t, ms.remove(&corev1.Pod{
ObjectMeta: v1.ObjectMeta{
UID: updates[1].id,
},
})
}))
expectedMetricData -= len(updates[1].rm)
require.Equal(t, len(updates)-1, len(ms.metricsCache))
require.Equal(t, expectedMetricData, ms.getMetricData(time.Now()).ResourceMetrics().Len())
Expand Down
5 changes: 2 additions & 3 deletions receiver/k8sclusterreceiver/internal/collection/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:gocritic
package collection // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver/internal/collection"

import (
Expand Down Expand Up @@ -189,10 +188,10 @@ func getMetadataForPod(pod *corev1.Pod, mc *metadataStore, logger *zap.Logger) m

// collectPodJobProperties checks if pod owner of type Job is cached. Check owners reference
// on Job to see if it was created by a CronJob. Sync metadata accordingly.
func collectPodJobProperties(pod *corev1.Pod, JobStore cache.Store, logger *zap.Logger) map[string]string {
func collectPodJobProperties(pod *corev1.Pod, jobStore cache.Store, logger *zap.Logger) map[string]string {
jobRef := utils.FindOwnerWithKind(pod.OwnerReferences, k8sKindJob)
if jobRef != nil {
job, exists, err := JobStore.GetByKey(utils.GetIDForCache(pod.Namespace, jobRef.Name))
job, exists, err := jobStore.GetByKey(utils.GetIDForCache(pod.Namespace, jobRef.Name))
if err != nil {
logError(err, jobRef, pod.UID, logger)
return nil
Expand Down
23 changes: 15 additions & 8 deletions receiver/k8sclusterreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:errcheck
package k8sclusterreceiver

import (
Expand Down Expand Up @@ -42,7 +41,9 @@ import (
func TestReceiver(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()

client := newFakeClientWithAllResources()
osQuotaClient := fakeQuota.NewSimpleClientset()
Expand Down Expand Up @@ -85,13 +86,15 @@ func TestReceiver(t *testing.T) {
}, 10*time.Second, 100*time.Millisecond,
"updated metrics not collected")

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

func TestReceiverTimesOutAfterStartup(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()
client := newFakeClientWithAllResources()

// Mock initial cache sync timing out, using a small timeout.
Expand All @@ -111,7 +114,9 @@ func TestReceiverTimesOutAfterStartup(t *testing.T) {
func TestReceiverWithManyResources(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()

client := newFakeClientWithAllResources()
osQuotaClient := fakeQuota.NewSimpleClientset()
Expand All @@ -135,7 +140,7 @@ func TestReceiverWithManyResources(t *testing.T) {
}, 10*time.Second, 100*time.Millisecond,
"metrics not collected")

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

var numCalls *atomic.Int32
Expand All @@ -148,7 +153,9 @@ var consumeMetadataInvocation = func() {
func TestReceiverWithMetadata(t *testing.T) {
tt, err := obsreporttest.SetupTelemetry()
require.NoError(t, err)
defer tt.Shutdown(context.Background())
defer func() {
require.NoError(t, tt.Shutdown(context.Background()))
}()

client := newFakeClientWithAllResources()
next := &mockExporterWithK8sMetadata{MetricsSink: new(consumertest.MetricsSink)}
Expand Down Expand Up @@ -182,7 +189,7 @@ func TestReceiverWithMetadata(t *testing.T) {
}, 10*time.Second, 100*time.Millisecond,
"metadata not collected")

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

func getUpdatedPod(pod *corev1.Pod) interface{} {
Expand Down
3 changes: 1 addition & 2 deletions receiver/k8sclusterreceiver/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:errcheck
package k8sclusterreceiver // import "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/k8sclusterreceiver"

import (
Expand Down Expand Up @@ -320,6 +319,6 @@ func (rw *resourceWatcher) syncMetadataUpdate(oldMetadata,
}

for _, consume := range rw.metadataConsumers {
consume(metadataUpdate)
_ = consume(metadataUpdate)
}
}
3 changes: 1 addition & 2 deletions receiver/k8sclusterreceiver/watcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// nolint:errcheck
package k8sclusterreceiver

import (
Expand Down Expand Up @@ -204,7 +203,7 @@ func TestPrepareSharedInformerFactory(t *testing.T) {
dataCollector: collection.NewDataCollector(zap.NewNop(), []string{}, []string{}),
}

rw.prepareSharedInformerFactory()
assert.NoError(t, rw.prepareSharedInformerFactory())

// Make sure no warning or error logs are raised
assert.Equal(t, 0, logs.Len())
Expand Down

0 comments on commit 2b1cf4c

Please sign in to comment.