Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions internal/collectors/kubernetes/argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand Down Expand Up @@ -41,16 +40,12 @@ func (c *Collector) argoCD(ctx context.Context) map[workloadKey]agentproto.GitOp
}
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
list, err := c.dyn.Resource(argoApplications).Namespace(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil { // no Argo CD here, no permission, or a passing failure: the workloads still count
return nil
}
windows := c.argoSyncWindows(ctx)
windows := c.argoSyncWindows(ctx) // first, so each application is read once and not kept
now := c.clock()
out := map[workloadKey]agentproto.GitOps{}
for _, app := range list.Items {
g := argoGitOps(app)
blockedBy(&g, app, windows, now)
err := c.eachResource(ctx, argoApplications, func(app *unstructured.Unstructured) {
g := argoGitOps(*app)
blockedBy(&g, *app, windows, now)
resources, _, _ := unstructured.NestedSlice(app.Object, "status", "resources")
for _, r := range resources {
res, ok := r.(map[string]any)
Expand All @@ -63,6 +58,9 @@ func (c *Collector) argoCD(ctx context.Context) map[workloadKey]agentproto.GitOp
}
out[workloadKey{kind, str(res["namespace"]), str(res["name"])}] = g
}
})
if err != nil { // no Argo CD here, no permission, or a passing failure: the workloads still count
return nil
}
return out
}
Expand Down
9 changes: 3 additions & 6 deletions internal/collectors/kubernetes/argocd_syncwindow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
Expand All @@ -34,12 +33,8 @@ type syncWindow struct {

// argoSyncWindows reads every AppProject's sync windows, by project name.
func (c *Collector) argoSyncWindows(ctx context.Context) map[string][]syncWindow {
list, err := c.dyn.Resource(argoProjects).Namespace(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil
}
out := map[string][]syncWindow{}
for _, p := range list.Items {
if err := c.eachResource(ctx, argoProjects, func(p *unstructured.Unstructured) {
ws, _, _ := unstructured.NestedSlice(p.Object, "spec", "syncWindows")
for _, w := range ws {
m, ok := w.(map[string]any)
Expand All @@ -50,6 +45,8 @@ func (c *Collector) argoSyncWindows(ctx context.Context) map[string][]syncWindow
out[p.GetName()] = append(out[p.GetName()], sw)
}
}
}); err != nil {
return nil
}
return out
}
Expand Down
59 changes: 34 additions & 25 deletions internal/collectors/kubernetes/exposure.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
)

Expand All @@ -37,15 +38,22 @@ var internalLB = map[string][]string{
func (c *Collector) exposure(ctx context.Context, pods []*corev1.Pod, owner owners) map[string][]string {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
svcs, err := c.cs.CoreV1().Services(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil {
return nil
// Each service's ways in: its own type, and the routes that send to it. Only the selector is kept of a
// service, as a large cluster has many.
type service struct {
namespace, name string
selector map[string]string
}
// Each service's ways in: its own type, and the routes that send to it.
var svcs []service
ways := map[string][]string{} // namespace/name -> ways
for _, s := range svcs.Items {
err := eachItem(ctx, func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return c.cs.CoreV1().Services(metav1.NamespaceAll).List(ctx, opts)
}, func(s *corev1.Service) {
if len(s.Spec.Selector) > 0 {
svcs = append(svcs, service{s.Namespace, s.Name, s.Spec.Selector})
}
if s.Annotations["goliash.exposed"] == "internal" || s.Labels["goliash.exposed"] == "internal" {
continue
return
}
key := s.Namespace + "/" + s.Name
switch s.Spec.Type {
Expand All @@ -56,35 +64,36 @@ func (c *Collector) exposure(ctx context.Context, pods []*corev1.Pod, owner owne
case corev1.ServiceTypeNodePort:
ways[key] = append(ways[key], "NodePort service "+key)
}
})
if err != nil {
return nil
}
if ings, err := c.cs.NetworkingV1().Ingresses(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}); err == nil {
for _, ing := range ings.Items {
for svc, host := range ingressBackends(ing) {
key := ing.Namespace + "/" + svc
ways[key] = append(ways[key], "ingress "+host)
}
_ = eachItem(ctx, func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return c.cs.NetworkingV1().Ingresses(metav1.NamespaceAll).List(ctx, opts)
}, func(ing *networkingv1.Ingress) {
for svc, host := range ingressBackends(*ing) {
key := ing.Namespace + "/" + svc
ways[key] = append(ways[key], "ingress "+host)
}
}
})
if c.dyn != nil {
if routes, err := c.dyn.Resource(httpRoutes).Namespace(metav1.NamespaceAll).List(ctx, metav1.ListOptions{}); err == nil {
for _, r := range routes.Items {
for svc, host := range routeBackends(r) {
key := r.GetNamespace() + "/" + svc
ways[key] = append(ways[key], "HTTPRoute "+host)
}
_ = c.eachResource(ctx, httpRoutes, func(r *unstructured.Unstructured) {
for svc, host := range routeBackends(*r) {
key := r.GetNamespace() + "/" + svc
ways[key] = append(ways[key], "HTTPRoute "+host)
}
}
})
}

out := map[string][]string{}
for _, s := range svcs.Items {
w := ways[s.Namespace+"/"+s.Name]
if len(w) == 0 || len(s.Spec.Selector) == 0 {
for _, s := range svcs {
w := ways[s.namespace+"/"+s.name]
if len(w) == 0 {
continue
}
sel := labels.SelectorFromSet(s.Spec.Selector)
sel := labels.SelectorFromSet(s.selector)
for _, p := range pods {
if p.Namespace != s.Namespace || !sel.Matches(labels.Set(p.Labels)) {
if p.Namespace != s.namespace || !sel.Matches(labels.Set(p.Labels)) {
continue
}
if uid, ok := owner.workload(p); ok {
Expand Down
9 changes: 5 additions & 4 deletions internal/collectors/kubernetes/flux.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"

Expand Down Expand Up @@ -48,11 +47,13 @@ func (c *Collector) flux(ctx context.Context) fluxObjects {
ctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
list := func(gvr schema.GroupVersionResource) []unstructured.Unstructured {
l, err := c.dyn.Resource(gvr).Namespace(metav1.NamespaceAll).List(ctx, metav1.ListOptions{})
if err != nil { // no Flux, no permission or a passing failure: the workloads still count
var items []unstructured.Unstructured
if err := c.eachResource(ctx, gvr, func(u *unstructured.Unstructured) {
items = append(items, *u)
}); err != nil { // no Flux, no permission or a passing failure: the workloads still count
return nil
}
return l.Items
return items
}
ks, hrs := list(fluxKustomizations), list(fluxHelmReleases)
if len(ks) == 0 && len(hrs) == 0 {
Expand Down
33 changes: 33 additions & 0 deletions internal/collectors/kubernetes/paging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2026 The Goliash Authors
// SPDX-License-Identifier: Apache-2.0

package kubernetes

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/tools/pager"
)

// eachItem lists in pages of pager.PageSize, so a large cluster is not read in one response, and calls fn
// for every item of type T. It returns the list error; fn sees no items then, or only part of them.
func eachItem[T any](ctx context.Context, list func(context.Context, metav1.ListOptions) (runtime.Object, error), fn func(T)) error {
p := pager.New(pager.ListPageFunc(list))
return p.EachListItem(ctx, metav1.ListOptions{}, func(obj runtime.Object) error {
if item, ok := obj.(T); ok {
fn(item)
}
return nil
})
}

// eachResource pages through a custom resource in every namespace.
func (c *Collector) eachResource(ctx context.Context, gvr schema.GroupVersionResource, fn func(*unstructured.Unstructured)) error {
return eachItem(ctx, func(ctx context.Context, opts metav1.ListOptions) (runtime.Object, error) {
return c.dyn.Resource(gvr).Namespace(metav1.NamespaceAll).List(ctx, opts)
}, fn)
}
39 changes: 39 additions & 0 deletions internal/collectors/kubernetes/paging_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2026 The Goliash Authors
// SPDX-License-Identifier: Apache-2.0

package kubernetes

import (
"context"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
)

// A large list comes in pages: every page is read, each asked for with the previous one's continue token.
func TestEachItemReadsEveryPage(t *testing.T) {
pages := map[string]*corev1.ServiceList{
"": {ListMeta: metav1.ListMeta{Continue: "p2"}, Items: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "a"}}}},
"p2": {ListMeta: metav1.ListMeta{Continue: "p3"}, Items: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "b"}}}},
"p3": {Items: []corev1.Service{{ObjectMeta: metav1.ObjectMeta{Name: "c"}}}},
}
var limits []int64
var names []string
err := eachItem(context.Background(), func(_ context.Context, opts metav1.ListOptions) (runtime.Object, error) {
limits = append(limits, opts.Limit)
return pages[opts.Continue], nil
}, func(s *corev1.Service) { names = append(names, s.Name) })
if err != nil {
t.Fatal(err)
}
if len(names) != 3 || names[0] != "a" || names[2] != "c" {
t.Errorf("items = %v, want a b c", names)
}
for _, l := range limits {
if l <= 0 {
t.Errorf("a page was asked for without a limit: %v", limits)
}
}
}
Loading