Skip to content

Agent Mesh

controlplane, storage: the metrics snapshot counts enrollment requests instead of listing them

Antonio Ojeaddad98d9cea7
diff --git a/internal/controlplane/metrics.go b/internal/controlplane/metrics.go
index 22a8483b..47d382dd 100644
--- a/internal/controlplane/metrics.go
+++ b/internal/controlplane/metrics.go
@@ -323,12 +323,14 @@ func readMeshSnapshot(ctx context.Context, store storage.Store, now time.Time) (
}
snap.users = len(users)
- reqs, err := store.ListEnrollmentRequests(ctx)
+ // A count, not a list: the table holds one row per enrollment within the
+ // retention, tens of thousands on a busy mesh, and this runs every 30s.
+ requests, err := store.CountEnrollmentRequests(ctx)
if err != nil {
return snap, err
}
- for _, r := range reqs {
- snap.enrollmentRequests[enrollmentStatusLabel(r.Status)]++
+ for status, n := range requests {
+ snap.enrollmentRequests[enrollmentStatusLabel(status)] += int(n)
}
tokens, err := store.ListBootstrapTokens(ctx)
diff --git a/internal/storage/round_trip_test.go b/internal/storage/round_trip_test.go
index 721fd46f..7a0512e9 100644
--- a/internal/storage/round_trip_test.go
+++ b/internal/storage/round_trip_test.go
@@ -17,6 +17,7 @@ package storage
import (
"context"
"errors"
+ "fmt"
"reflect"
"slices"
"strconv"
@@ -388,6 +389,52 @@ func TestDeleteStaleEnrollmentRecords(t *testing.T) {
}
}
+func TestCountEnrollmentRequestsByStatus(t *testing.T) {
+ store := newTestStore(t)
+ ctx := context.Background()
+ now := time.Now()
+ if err := store.SaveBootstrapToken(ctx, &BootstrapToken{ID: "tok", TokenHash: "hash", Role: api.RoleNode, MaxUsages: 10, CreatedAt: now, ExpiresAt: now.Add(time.Hour)}); err != nil {
+ t.Fatal(err)
+ }
+ for i, status := range []api.EnrollmentStatus{
+ api.EnrollmentStatus_ENROLLMENT_STATUS_PENDING,
+ api.EnrollmentStatus_ENROLLMENT_STATUS_APPROVED,
+ api.EnrollmentStatus_ENROLLMENT_STATUS_APPROVED,
+ api.EnrollmentStatus_ENROLLMENT_STATUS_REJECTED,
+ } {
+ if err := store.CreateEnrollmentRequest(ctx, &EnrollmentRequest{
+ ID: fmt.Sprintf("req-%d", i), PeerID: fmt.Sprintf("peer-%d", i), PublicKey: []byte("pub"), TokenID: "tok", Status: status, CreatedAt: now,
+ }); err != nil {
+ t.Fatal(err)
+ }
+ }
+ counts, err := store.CountEnrollmentRequests(ctx)
+ if err != nil {
+ t.Fatalf("CountEnrollmentRequests: %v", err)
+ }
+ want := map[api.EnrollmentStatus]int64{
+ api.EnrollmentStatus_ENROLLMENT_STATUS_PENDING: 1,
+ api.EnrollmentStatus_ENROLLMENT_STATUS_APPROVED: 2,
+ api.EnrollmentStatus_ENROLLMENT_STATUS_REJECTED: 1,
+ }
+ if len(counts) != len(want) {
+ t.Fatalf("counts = %v, want %v", counts, want)
+ }
+ for status, n := range want {
+ if counts[status] != n {
+ t.Errorf("%s = %d, want %d", status, counts[status], n)
+ }
+ }
+ // The list and the count agree, so the admin view and the gauge do.
+ list, err := store.ListEnrollmentRequests(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if len(list) != 4 {
+ t.Errorf("ListEnrollmentRequests returned %d, want 4", len(list))
+ }
+}
+
func TestDeleteExpiredRouterLeases(t *testing.T) {
store := newTestStore(t)
ctx := context.Background()
diff --git a/internal/storage/sql_store.go b/internal/storage/sql_store.go
index f7e75157..6a266743 100644
--- a/internal/storage/sql_store.go
+++ b/internal/storage/sql_store.go
@@ -1965,6 +1965,26 @@ func (s *SQLStore) CountNodes(ctx context.Context, now time.Time) ([]NodeCount,
return counts, rows.Err()
}
+// CountEnrollmentRequests implements Store.
+func (s *SQLStore) CountEnrollmentRequests(ctx context.Context) (map[api.EnrollmentStatus]int64, error) {
+ rows, err := s.db.QueryContext(ctx, `SELECT status, COUNT(*) FROM enrollment_requests GROUP BY status`)
+ if err != nil {
+ return nil, fmt.Errorf("failed to count enrollment requests: %w", err)
+ }
+ defer func() { _ = rows.Close() }()
+
+ counts := make(map[api.EnrollmentStatus]int64)
+ for rows.Next() {
+ var status int32
+ var n int64
+ if err := rows.Scan(&status, &n); err != nil {
+ return nil, fmt.Errorf("failed to scan enrollment request count: %w", err)
+ }
+ counts[api.EnrollmentStatus(status)] = n
+ }
+ return counts, rows.Err()
+}
+
// ListBootstrapTokens retrieves all bootstrap tokens.
func (s *SQLStore) ListBootstrapTokens(ctx context.Context) ([]BootstrapToken, error) {
query := s.rebind(`SELECT id, token_hash, role, owner_id, max_usages, usages_count, description, created_at, expires_at, revoked_at, autonomous_recovery, ephemeral FROM bootstrap_tokens ORDER BY created_at DESC`)
diff --git a/internal/storage/storage.go b/internal/storage/storage.go
index ca9095a1..b6724ced 100644
--- a/internal/storage/storage.go
+++ b/internal/storage/storage.go
@@ -377,6 +377,9 @@ type Store interface {
// CountNodes counts every enrolled node by role and by its state at now.
CountNodes(ctx context.Context, now time.Time) ([]NodeCount, error)
+ // CountEnrollmentRequests counts the enrollment requests by status.
+ CountEnrollmentRequests(ctx context.Context) (map[api.EnrollmentStatus]int64, error)
+
// DeleteExpiredNodes removes enrolled nodes whose session expired before
// the given time and that are not banned, returning how many went. A
// lapsed session already refuses every refresh, so the row only records