mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-12-03 10:33:40 +01:00
Every list-shaped resources request deep-cloned the whole registry: HandleListResources via ListForPresentation, the storage summary and incidents handlers and the k8s namespaces handler via List/ListByType, and the stats handler once more. With the frontend polling three pages plus summaries every few seconds, the same unchanged world was cloned dozens of times between snapshot updates. Cache the raw and presentation lists on the existing per-generation registry cache entry (same invalidation: entries rebuild when the seed lastUpdate moves) and hand requests a flat top-level copy instead. Every decorator in the request pipeline was audited to write only top-level fields on request-owned elements: action availability, discovery targets and readiness, metrics targets, canonical metadata refresh, and contract types all assign freshly built values. The one nested writer — the PMG list prune, which cleared relay domains through the shared pointer — now clones the PMG struct before clearing, with a regression test pinning both the clone-on-write and the cache's immunity to request decoration. Read-only consumers (stats aggregation, storage filtering, namespace counting) use the shared list with no copy at all. Single-resource lookups (presentationResourceByReference and its callers) still deep-clone per lookup; they are cold paths and stay as-is. Contract-Neutral: per-generation shared resource lists: response bytes pinned by contract tests, no payload delta
150 lines
3.7 KiB
Go
150 lines
3.7 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"sort"
|
|
"strings"
|
|
|
|
unified "github.com/rcourtman/pulse-go-rewrite/internal/unifiedresources"
|
|
)
|
|
|
|
type k8sNamespaceCounts struct {
|
|
Total int `json:"total"`
|
|
Online int `json:"online"`
|
|
Warning int `json:"warning"`
|
|
Offline int `json:"offline"`
|
|
Unknown int `json:"unknown"`
|
|
}
|
|
|
|
type k8sNamespaceSummary struct {
|
|
Namespace string `json:"namespace"`
|
|
Pods k8sNamespaceCounts `json:"pods"`
|
|
Deployments k8sNamespaceCounts `json:"deployments"`
|
|
}
|
|
|
|
type k8sNamespacesResponse struct {
|
|
Cluster string `json:"cluster"`
|
|
Data []k8sNamespaceSummary `json:"data"`
|
|
}
|
|
|
|
func emptyK8sNamespacesResponse() k8sNamespacesResponse {
|
|
return k8sNamespacesResponse{}.normalizeCollections()
|
|
}
|
|
|
|
func (r k8sNamespacesResponse) normalizeCollections() k8sNamespacesResponse {
|
|
if r.Data == nil {
|
|
r.Data = []k8sNamespaceSummary{}
|
|
}
|
|
return r
|
|
}
|
|
|
|
// HandleK8sNamespaces handles GET /api/resources/k8s/namespaces?cluster=<clusterName>
|
|
// and returns namespace-level counts for Pods and Deployments.
|
|
func (h *ResourceHandlers) HandleK8sNamespaces(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
cluster := strings.TrimSpace(r.URL.Query().Get("cluster"))
|
|
if cluster == "" {
|
|
http.Error(w, "cluster query param required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
orgID := GetOrgID(r.Context())
|
|
resources, _, err := h.sharedRawResources(orgID)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
normalize := func(value string) string { return strings.ToLower(strings.TrimSpace(value)) }
|
|
wantCluster := normalize(cluster)
|
|
|
|
type bucket struct {
|
|
pods k8sNamespaceCounts
|
|
deployments k8sNamespaceCounts
|
|
}
|
|
byNamespace := make(map[string]*bucket)
|
|
|
|
addCount := func(counts *k8sNamespaceCounts, status unified.ResourceStatus) {
|
|
counts.Total++
|
|
switch status {
|
|
case unified.StatusOnline:
|
|
counts.Online++
|
|
case unified.StatusWarning:
|
|
counts.Warning++
|
|
case unified.StatusOffline:
|
|
counts.Offline++
|
|
default:
|
|
counts.Unknown++
|
|
}
|
|
}
|
|
|
|
ensureBucket := func(namespace string) *bucket {
|
|
b := byNamespace[namespace]
|
|
if b == nil {
|
|
b = &bucket{}
|
|
byNamespace[namespace] = b
|
|
}
|
|
return b
|
|
}
|
|
|
|
ingest := func(resource unified.Resource, isDeployment bool) {
|
|
if resource.Kubernetes == nil {
|
|
return
|
|
}
|
|
if normalize(resource.Kubernetes.ClusterName) != wantCluster {
|
|
return
|
|
}
|
|
ns := strings.TrimSpace(resource.Kubernetes.Namespace)
|
|
if ns == "" {
|
|
ns = "default"
|
|
}
|
|
b := ensureBucket(ns)
|
|
if isDeployment {
|
|
addCount(&b.deployments, resource.Status)
|
|
} else {
|
|
addCount(&b.pods, resource.Status)
|
|
}
|
|
}
|
|
|
|
// Read-only aggregation over the shared per-generation list; ingest only
|
|
// counts, so no copies are needed.
|
|
for i := range resources {
|
|
switch unified.CanonicalResourceType(resources[i].Type) {
|
|
case unified.ResourceTypePod:
|
|
ingest(resources[i], false)
|
|
case unified.ResourceTypeK8sDeployment:
|
|
ingest(resources[i], true)
|
|
}
|
|
}
|
|
|
|
namespaces := make([]string, 0, len(byNamespace))
|
|
for ns := range byNamespace {
|
|
namespaces = append(namespaces, ns)
|
|
}
|
|
sort.Slice(namespaces, func(i, j int) bool { return namespaces[i] < namespaces[j] })
|
|
|
|
out := make([]k8sNamespaceSummary, 0, len(namespaces))
|
|
for _, ns := range namespaces {
|
|
b := byNamespace[ns]
|
|
if b == nil {
|
|
continue
|
|
}
|
|
out = append(out, k8sNamespaceSummary{
|
|
Namespace: ns,
|
|
Pods: b.pods,
|
|
Deployments: b.deployments,
|
|
})
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
response := emptyK8sNamespacesResponse()
|
|
response.Cluster = cluster
|
|
response.Data = out
|
|
json.NewEncoder(w).Encode(response.normalizeCollections())
|
|
}
|