Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(pbs): stop re-fetching the node name every poll cycle

rcourtmane597325a4acb
diff --git a/pkg/pbs/client.go b/pkg/pbs/client.go
index 24b417f754..1cf3a03dc4 100644
--- a/pkg/pbs/client.go
+++ b/pkg/pbs/client.go
@@ -37,8 +37,20 @@ type Client struct {
httpClient *http.Client
auth auth
config ClientConfig
+
+ // GetNodeName caching. The node hostname is stable for the lifetime of a
+ // connection, and polling /nodes every cycle makes PBS log a 403 every few
+ // seconds when the token lacks permission (refs #1691).
+ nodeNameMu sync.Mutex
+ cachedNodeName string
+ nodeNameRetryAfter time.Time
}
+// nodeNamePermissionRetryInterval throttles /nodes retries after a permission
+// denial. Long enough to stop per-cycle 403 spam in the PBS syslog, short
+// enough to self-heal once an operator widens the token's ACL.
+const nodeNamePermissionRetryInterval = 30 * time.Minute
+
func (c *Client) apiBaseURL() (*url.URL, error) {
if c.baseAPIURL != nil {
return c.baseAPIURL, nil
@@ -606,12 +618,34 @@ func (c *Client) GetVersion(ctx context.Context) (*Version, error) {
return &result.Data, nil
}
-// GetNodeName returns the PBS node's hostname
+// GetNodeName returns the PBS node's hostname. The result is cached for the
+// lifetime of the client: the hostname cannot change underneath an open
+// connection, and re-fetching /nodes on every poll cycle floods the PBS syslog
+// with 403s when the token cannot read it (refs #1691). Permission denials
+// defer the next attempt by nodeNamePermissionRetryInterval; transient
+// failures retry on the next call.
func (c *Client) GetNodeName(ctx context.Context) (string, error) {
+ c.nodeNameMu.Lock()
+ if c.cachedNodeName != "" {
+ name := c.cachedNodeName
+ c.nodeNameMu.Unlock()
+ return name, nil
+ }
+ if !c.nodeNameRetryAfter.IsZero() && time.Now().Before(c.nodeNameRetryAfter) {
+ c.nodeNameMu.Unlock()
+ return "", fmt.Errorf("node name unavailable: /nodes permission denied, retry deferred")
+ }
+ c.nodeNameMu.Unlock()
+
log.Debug().Msg("PBS GetNodeName: fetching node name")
resp, err := c.get(ctx, "/nodes")
if err != nil {
+ if strings.Contains(err.Error(), "403") || strings.Contains(err.Error(), "permission") {
+ c.nodeNameMu.Lock()
+ c.nodeNameRetryAfter = time.Now().Add(nodeNamePermissionRetryInterval)
+ c.nodeNameMu.Unlock()
+ }
return "", fmt.Errorf("failed to get nodes: %w", err)
}
defer resp.Body.Close()
@@ -632,6 +666,10 @@ func (c *Client) GetNodeName(ctx context.Context) (string, error) {
// Return the first (usually only) node name
nodeName := result.Data[0].Node
+ c.nodeNameMu.Lock()
+ c.cachedNodeName = nodeName
+ c.nodeNameRetryAfter = time.Time{}
+ c.nodeNameMu.Unlock()
log.Debug().Str("nodeName", nodeName).Msg("PBS GetNodeName: found node name")
return nodeName, nil
}
diff --git a/pkg/pbs/client_http_test.go b/pkg/pbs/client_http_test.go
index ce0b462937..41e7e7279f 100644
--- a/pkg/pbs/client_http_test.go
+++ b/pkg/pbs/client_http_test.go
@@ -426,3 +426,84 @@ func TestClient_GetDatastores_HTMLResponseOnHTTP(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
}
+
+func TestClient_GetNodeName_CachesResult(t *testing.T) {
+ var nodesCalls atomic.Int64
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/api2/json/nodes" {
+ t.Fatalf("unexpected path: %s", r.URL.Path)
+ }
+ nodesCalls.Add(1)
+ _ = json.NewEncoder(w).Encode(map[string]any{
+ "data": []map[string]any{{"node": "pbs-main"}},
+ })
+ }))
+ defer server.Close()
+
+ client, err := NewClient(ClientConfig{
+ Host: server.URL,
+ TokenName: "root@pam!pulse-token",
+ TokenValue: "secret",
+ Timeout: 2 * time.Second,
+ })
+ if err != nil {
+ t.Fatalf("NewClient: %v", err)
+ }
+
+ for i := 0; i < 3; i++ {
+ name, err := client.GetNodeName(context.Background())
+ if err != nil {
+ t.Fatalf("GetNodeName call %d: %v", i, err)
+ }
+ if name != "pbs-main" {
+ t.Fatalf("GetNodeName call %d = %q, want pbs-main", i, name)
+ }
+ }
+ if got := nodesCalls.Load(); got != 1 {
+ t.Fatalf("/nodes hit %d times, want 1 (cached after first success)", got)
+ }
+}
+
+func TestClient_GetNodeName_PermissionDenialDefersRetry(t *testing.T) {
+ var nodesCalls atomic.Int64
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.URL.Path != "/api2/json/nodes" {
+ t.Fatalf("unexpected path: %s", r.URL.Path)
+ }
+ nodesCalls.Add(1)
+ w.WriteHeader(http.StatusForbidden)
+ _, _ = w.Write([]byte(`{"message":"permission check failed"}`))
+ }))
+ defer server.Close()
+
+ client, err := NewClient(ClientConfig{
+ Host: server.URL,
+ TokenName: "root@pam!pulse-token",
+ TokenValue: "secret",
+ Timeout: 2 * time.Second,
+ })
+ if err != nil {
+ t.Fatalf("NewClient: %v", err)
+ }
+
+ for i := 0; i < 3; i++ {
+ if _, err := client.GetNodeName(context.Background()); err == nil {
+ t.Fatalf("GetNodeName call %d: expected error", i)
+ }
+ }
+ if got := nodesCalls.Load(); got != 1 {
+ t.Fatalf("/nodes hit %d times, want 1 (403 defers retries)", got)
+ }
+
+ // A transient (non-permission) failure must not defer: reset the deferral
+ // and confirm the next call goes back to the server.
+ client.nodeNameMu.Lock()
+ client.nodeNameRetryAfter = time.Time{}
+ client.nodeNameMu.Unlock()
+ if _, err := client.GetNodeName(context.Background()); err == nil {
+ t.Fatal("expected error after deferral reset")
+ }
+ if got := nodesCalls.Load(); got != 2 {
+ t.Fatalf("/nodes hit %d times after deferral reset, want 2", got)
+ }
+}