Skip to content

Commit 77c80d7

Browse files
committed
Replace context.Background()
1 parent 2c44c93 commit 77c80d7

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed

pkg/kubelet/metrics/collectors/cri_metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ func (c *criMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
7070
// Collect implements the metrics.CollectWithStability interface.
7171
// TODO(haircommander): would it be better if these were processed async?
7272
func (c *criMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
73+
// Use context.TODO() because we currently do not have a proper context to pass in.
74+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
7375
ctx := context.TODO()
7476
logger := klog.FromContext(ctx)
7577
podMetrics, err := c.listPodSandboxMetricsFn(ctx)

pkg/kubelet/metrics/collectors/log_metrics.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ func (c *logMetricsCollector) DescribeWithStability(ch chan<- *metrics.Desc) {
6363

6464
// CollectWithStability implements the metrics.StableCollector interface.
6565
func (c *logMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
66+
// Use context.TODO() because we currently do not have a proper context to pass in.
67+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
6668
ctx := context.TODO()
6769
logger := klog.FromContext(ctx)
6870
podStats, err := c.podStats(ctx)

pkg/kubelet/metrics/collectors/resource_metrics.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,9 @@ func (rc *resourceMetricsCollector) DescribeWithStability(ch chan<- *metrics.Des
149149
// leak metric collectors for containers or pods that no longer exist. Instead, implement
150150
// custom collector in a way that only collects metrics for active containers.
151151
func (rc *resourceMetricsCollector) CollectWithStability(ch chan<- metrics.Metric) {
152-
ctx := context.Background()
152+
// Use context.TODO() because we currently do not have a proper context to pass in.
153+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
154+
ctx := context.TODO()
153155
var errorCount float64
154156
defer func() {
155157
ch <- metrics.NewLazyConstMetric(resourceScrapeResultDesc, metrics.GaugeValue, errorCount)

pkg/kubelet/metrics/collectors/resource_metrics_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package collectors
1818

1919
import (
20-
"context"
2120
"fmt"
2221
"strings"
2322
"testing"
@@ -27,6 +26,7 @@ import (
2726
"k8s.io/component-base/metrics/testutil"
2827
statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
2928
summaryprovidertest "k8s.io/kubernetes/pkg/kubelet/server/stats/testing"
29+
"k8s.io/kubernetes/test/utils/ktesting"
3030
)
3131

3232
func TestCollectResourceMetrics(t *testing.T) {
@@ -404,7 +404,7 @@ func TestCollectResourceMetrics(t *testing.T) {
404404
for _, test := range tests {
405405
tc := test
406406
t.Run(tc.name, func(t *testing.T) {
407-
ctx := context.Background()
407+
ctx := ktesting.Init(t)
408408
provider := summaryprovidertest.NewMockSummaryProvider(t)
409409
provider.EXPECT().GetCPUAndMemoryStats(ctx).Return(tc.summary, tc.summaryErr).Maybe()
410410
collector := NewResourceMetricsCollector(provider)

pkg/kubelet/metrics/collectors/volume_stats.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ func (collector *volumeStatsCollector) DescribeWithStability(ch chan<- *metrics.
9898

9999
// CollectWithStability implements the metrics.StableCollector interface.
100100
func (collector *volumeStatsCollector) CollectWithStability(ch chan<- metrics.Metric) {
101-
ctx := context.Background()
101+
// Use context.TODO() because we currently do not have a proper context to pass in.
102+
// Replace this with an appropriate context when refactoring this function to accept a context parameter.
103+
ctx := context.TODO()
102104
podStats, err := collector.statsProvider.ListPodStats(ctx)
103105
if err != nil {
104106
return

pkg/kubelet/metrics/collectors/volume_stats_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ limitations under the License.
1717
package collectors
1818

1919
import (
20-
"context"
2120
"strings"
2221
"testing"
2322

2423
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2524
"k8s.io/component-base/metrics/testutil"
2625
statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1"
2726
statstest "k8s.io/kubernetes/pkg/kubelet/server/stats/testing"
27+
"k8s.io/kubernetes/test/utils/ktesting"
2828
)
2929

3030
func newUint64Pointer(i uint64) *uint64 {
3131
return &i
3232
}
3333

3434
func TestVolumeStatsCollector(t *testing.T) {
35-
ctx := context.Background()
35+
ctx := ktesting.Init(t)
3636
// Fixed metadata on type and help text. We prepend this to every expected
3737
// output so we only have to modify a single place when doing adjustments.
3838
const metadata = `
@@ -151,7 +151,7 @@ func TestVolumeStatsCollector(t *testing.T) {
151151
}
152152

153153
func TestVolumeStatsCollectorWithNullVolumeStatus(t *testing.T) {
154-
ctx := context.Background()
154+
ctx := ktesting.Init(t)
155155
// Fixed metadata on type and help text. We prepend this to every expected
156156
// output so we only have to modify a single place when doing adjustments.
157157
const metadata = `

0 commit comments

Comments
 (0)