Collect Hubble traffic continuously; graph from informer caches
The ring buffer on each agent holds 4095 flows, ~10 s at the observed ~380 flows/s, so the 5/15/60 min windows were really showing the last few seconds. A background follow stream now fills per-minute buckets for the last hour, and counts are distinct connections (5-tuples) instead of Hubble events, with opened (SYN) and dropped broken out. /api/flows reports coveredSec so the UI can say how much of the window the collector has seen since start. /api/graph reads informer caches instead of listing every kind per request (~0.9 s before). Pod-to-workload resolution for flows uses the same cache. YAML view drops managedFields. Frontend: traffic refreshes in place every 30 s without relayout; dash animation rationed (12 fps, only highlighted edges in dense views, paused after a minute idle) since each step redraws the whole element layer, ~15 ms on a 200-element view. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cf84e9976f
commit
6ead0a0d6d
@@ -0,0 +1,87 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/meta"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/client-go/informers"
|
||||
appslisters "k8s.io/client-go/listers/apps/v1"
|
||||
batchlisters "k8s.io/client-go/listers/batch/v1"
|
||||
corelisters "k8s.io/client-go/listers/core/v1"
|
||||
netlisters "k8s.io/client-go/listers/networking/v1"
|
||||
)
|
||||
|
||||
var (
|
||||
nsLister corelisters.NamespaceLister
|
||||
podLister corelisters.PodLister
|
||||
svcLister corelisters.ServiceLister
|
||||
nodeLister corelisters.NodeLister
|
||||
deployLister appslisters.DeploymentLister
|
||||
stsLister appslisters.StatefulSetLister
|
||||
dsLister appslisters.DaemonSetLister
|
||||
cronLister batchlisters.CronJobLister
|
||||
ingLister netlisters.IngressLister
|
||||
)
|
||||
|
||||
func startInformers() error {
|
||||
f := informers.NewSharedInformerFactoryWithOptions(clientset, 0,
|
||||
// managedFields is most of a cached object's size and nothing here reads it
|
||||
informers.WithTransform(func(obj any) (any, error) {
|
||||
if a, err := meta.Accessor(obj); err == nil {
|
||||
a.SetManagedFields(nil)
|
||||
}
|
||||
return obj, nil
|
||||
}))
|
||||
nsLister = f.Core().V1().Namespaces().Lister()
|
||||
podLister = f.Core().V1().Pods().Lister()
|
||||
svcLister = f.Core().V1().Services().Lister()
|
||||
nodeLister = f.Core().V1().Nodes().Lister()
|
||||
deployLister = f.Apps().V1().Deployments().Lister()
|
||||
stsLister = f.Apps().V1().StatefulSets().Lister()
|
||||
dsLister = f.Apps().V1().DaemonSets().Lister()
|
||||
cronLister = f.Batch().V1().CronJobs().Lister()
|
||||
ingLister = f.Networking().V1().Ingresses().Lister()
|
||||
|
||||
stop := make(chan struct{})
|
||||
f.Start(stop)
|
||||
done := make(chan map[string]bool, 1)
|
||||
go func() {
|
||||
synced := map[string]bool{}
|
||||
for t, ok := range f.WaitForCacheSync(stop) {
|
||||
synced[fmt.Sprint(t)] = ok
|
||||
}
|
||||
done <- synced
|
||||
}()
|
||||
select {
|
||||
case synced := <-done:
|
||||
for t, ok := range synced {
|
||||
if !ok {
|
||||
return fmt.Errorf("cache for %s did not sync", t)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
case <-time.After(90 * time.Second):
|
||||
return fmt.Errorf("caches did not sync in 90s")
|
||||
}
|
||||
}
|
||||
|
||||
type listOf[T any] struct{ Items []T }
|
||||
|
||||
// listAll reads a lister into the same shape a typed List() call returns,
|
||||
// so graph code iterates .Items unchanged. Items are copies: callers may not
|
||||
// mutate cached objects, and copying the top-level struct is cheap.
|
||||
func listAll[T any](l interface {
|
||||
List(labels.Selector) ([]*T, error)
|
||||
}) (*listOf[T], error) {
|
||||
ptrs, err := l.List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := &listOf[T]{Items: make([]T, len(ptrs))}
|
||||
for i, p := range ptrs {
|
||||
out.Items[i] = *p
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
Reference in New Issue
Block a user