Skip to content

Commit 0f7a383

Browse files
immanuwellweyfonk
andauthored
fix: collect dump metrics services across namespaces (#5188)
* fix: collect dump metrics services across namespaces Signed-off-by: immanuwell <pchpr.00@list.ru> * Fix expectation on metrics file name Dumping metrics is now done over multiple namespaces, meaning that target files names now include the namespace. --------- Signed-off-by: immanuwell <pchpr.00@list.ru> Co-authored-by: Corentin Néau <tan.neau@suse.com>
1 parent 49aeb01 commit 0f7a383

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

e2e/single-cluster/cli_dump_test.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ var _ = Describe("Fleet dump", Label("sharding"), func() {
8787
continue
8888
}
8989

90-
Expect(fileName).To(HaveLen(2))
90+
Expect(fileName).To(HaveLen(3), "metrics file name should have format metrics_<namespace>_<servicename>")
9191
Expect(content).ToNot(BeEmpty())
9292

9393
// Run a few basic checks on expected strings, checking full contents would be cumbersome
@@ -97,11 +97,12 @@ var _ = Describe("Fleet dump", Label("sharding"), func() {
9797
Expect(c).To(ContainSubstring("controller_runtime_reconcile_total"))
9898

9999
exampleMonitoredRsc := "bundle"
100-
if strings.Contains(fileName[1], "gitjob") {
100+
if strings.Contains(fileName[2], "gitjob") {
101101
exampleMonitoredRsc = "gitrepo"
102-
} else if !strings.Contains(fileName[1], "shard") {
102+
} else if !strings.Contains(fileName[2], "shard") {
103103
// Check for fleet_*_desired_ready metrics on non-sharded services
104-
Expect(c).To(ContainSubstring(fmt.Sprintf("fleet_%s_desired_ready", exampleMonitoredRsc)))
104+
expectedStr := fmt.Sprintf("fleet_%s_desired_ready", exampleMonitoredRsc)
105+
Expect(c).To(ContainSubstring(expectedStr), fmt.Sprintf("expected file %q to contain %q", header.Name, expectedStr))
105106
}
106107

107108
Expect(c).To(ContainSubstring(fmt.Sprintf(`workqueue_work_duration_seconds_bucket{controller="%s",name="%s",`, exampleMonitoredRsc, exampleMonitoredRsc)))
@@ -110,14 +111,14 @@ var _ = Describe("Fleet dump", Label("sharding"), func() {
110111
}
111112

112113
Expect(foundFiles).To(HaveLen(8))
113-
Expect(foundFiles).To(ContainElement("metrics_monitoring-gitjob"))
114-
Expect(foundFiles).To(ContainElement("metrics_monitoring-gitjob-shard-shard0"))
115-
Expect(foundFiles).To(ContainElement("metrics_monitoring-gitjob-shard-shard1"))
116-
Expect(foundFiles).To(ContainElement("metrics_monitoring-gitjob-shard-shard2"))
117-
Expect(foundFiles).To(ContainElement("metrics_monitoring-fleet-controller"))
118-
Expect(foundFiles).To(ContainElement("metrics_monitoring-fleet-controller-shard-shard0"))
119-
Expect(foundFiles).To(ContainElement("metrics_monitoring-fleet-controller-shard-shard1"))
120-
Expect(foundFiles).To(ContainElement("metrics_monitoring-fleet-controller-shard-shard2"))
114+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-gitjob"))
115+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-gitjob-shard-shard0"))
116+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-gitjob-shard-shard1"))
117+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-gitjob-shard-shard2"))
118+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-fleet-controller"))
119+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-fleet-controller-shard-shard0"))
120+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-fleet-controller-shard-shard1"))
121+
Expect(foundFiles).To(ContainElement("metrics_cattle-fleet-system_monitoring-fleet-controller-shard-shard2"))
121122
})
122123
})
123124

internal/cmd/cli/dump/dump.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,12 +483,10 @@ func addEventsToArchive(
483483
}
484484

485485
func addMetricsToArchive(ctx context.Context, c client.Client, logger logr.Logger, cfg *rest.Config, w *tar.Writer, opt Options) error {
486-
ns := config.DefaultNamespace // XXX: support installation in non-default namespace, and check for services across all namespaces, by label?
487-
488486
var monitoringSvcs []corev1.Service
489487
var svcs corev1.ServiceList
490488
for {
491-
opts := []client.ListOption{client.InNamespace(ns), client.Limit(opt.FetchLimit), client.Continue(svcs.Continue)}
489+
opts := []client.ListOption{client.Limit(opt.FetchLimit), client.Continue(svcs.Continue)}
492490

493491
if err := c.List(ctx, &svcs, opts...); err != nil {
494492
return fmt.Errorf("failed to list services for extracting metrics: %w", err)
@@ -508,7 +506,7 @@ func addMetricsToArchive(ctx context.Context, c client.Client, logger logr.Logge
508506
}
509507

510508
if len(monitoringSvcs) == 0 {
511-
logger.Info("No monitoring services found; Fleet has probably been installed with metrics disabled.", "namespace", ns)
509+
logger.Info("No monitoring services found; Fleet has probably been installed with metrics disabled.")
512510

513511
return nil
514512
}
@@ -553,7 +551,8 @@ func addMetricsToArchive(ctx context.Context, c client.Client, logger logr.Logge
553551

554552
logger.Info("Extracted metrics", "service", svc.Name)
555553

556-
if err := addFileToArchive(body, "metrics_"+svc.Name, w); err != nil {
554+
fileName := fmt.Sprintf("metrics_%s_%s", svc.Namespace, svc.Name)
555+
if err := addFileToArchive(body, fileName, w); err != nil {
557556
return fmt.Errorf("failed to write metrics to archive from service %s: %w", svc.Name, err)
558557
}
559558
}

internal/cmd/cli/dump/dump_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ func Test_addMetrics(t *testing.T) {
388388
},
389389
expErrStr: "service cattle-fleet-system/monitoring-prefixed does not have any exposed ports",
390390
},
391+
{
392+
name: "monitoring service in non-default namespace is still discovered",
393+
svcs: []corev1.Service{
394+
{
395+
ObjectMeta: metav1.ObjectMeta{
396+
Name: "monitoring-prefixed",
397+
Namespace: "custom-fleet-system",
398+
},
399+
},
400+
},
401+
expErrStr: "service custom-fleet-system/monitoring-prefixed does not have any exposed ports",
402+
},
391403
{
392404
name: "monitoring service with exposed ports but no labels",
393405
svcs: []corev1.Service{
@@ -501,7 +513,6 @@ func Test_addMetrics(t *testing.T) {
501513
mockClient.EXPECT().List(
502514
ctx,
503515
gomock.AssignableToTypeOf(&corev1.ServiceList{}),
504-
client.InNamespace("cattle-fleet-system"),
505516
gomock.Any(), // client.Limit(...)
506517
gomock.Any(), // client.Continue(...)
507518
).

0 commit comments

Comments
 (0)