Skip to content

Conversation

pdbrito
Copy link

@pdbrito pdbrito commented Aug 25, 2025

Currently if you go to a pod and hit o to go to its parent node, the pod count is always 0.

Tracked the cause to Node.Get where PodCount wasn't being set.

Defaulted podCount to -1 so it appears as "n/a" instead of 0.

@uozalp
Copy link
Contributor

uozalp commented Aug 26, 2025

I can’t reproduce this. For me, it shows nodes(all)[1]

@pdbrito
Copy link
Author

pdbrito commented Aug 26, 2025

Heya @uozalp ! I added a couple screenshots to make it clearer. Please see the value in the Pods column

Currently:
image
After this pr:
image

@uozalp
Copy link
Contributor

uozalp commented Aug 27, 2025

Thanks for posting the screenshot, I see now what you meant.
I tested your PR and it fixes the issue.

Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pdbrito Good catch!

@@ -152,7 +152,21 @@ func (n *Node) Get(ctx context.Context, path string) (runtime.Object, error) {
nmx, _ = client.DialMetrics(n.Client()).FetchNodeMetrics(ctx, path)
}

return &render.NodeWithMetrics{Raw: raw, MX: nmx}, nil
podCount := -1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should DRY this up and convert this into a helper so that we can reuse for both list/get aka something like getPodCount

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, will refactor it this weekend.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got a little carried away refactoring, need a couple more days to tidy things up!

@derailed derailed added the needs-tlc Pr needs additional updates label Sep 1, 2025
@@ -132,96 +132,118 @@ func (n *Node) Drain(path string, opts DrainOptions, w io.Writer) error {

// Get returns a node resource.
func (n *Node) Get(ctx context.Context, path string) (runtime.Object, error) {
oo, err := n.Resource.List(ctx, "")
o, err := n.Resource.Get(ctx, client.FQN(client.ClusterScope, path))
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to Get here instead of List, means we don't have to filter below.

}

var nmx client.NodesMetricsMap
if withMx, ok := ctx.Value(internal.KeyWithMetrics).(bool); withMx || !ok {
if withMx, ok := ctx.Value(internal.KeyWithMetrics).(bool); ok && withMx {
Copy link
Author

@pdbrito pdbrito Sep 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched this to match how checks are being made currently elsewhere.

res := make([]runtime.Object, 0, len(oo))
for _, o := range oo {
u, ok := o.(*unstructured.Unstructured)
if !ok {
return res, fmt.Errorf("expecting *unstructured.Unstructured but got `%T", o)
return nil, fmt.Errorf("expecting *unstructured.Unstructured but got `%T", o)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning nil on err instead of res.

}

// List returns a collection of node resources.
func (n *Node) List(ctx context.Context, ns string) ([]runtime.Object, error) {
oo, err := n.Resource.List(ctx, ns)
if err != nil {
return oo, err
return nil, err
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning nil on err instead of oo.


var podCounts map[string]int
if shouldCountPods {
podCounts, err = n.CountPodsByNode(pods)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved pod counting outside the loop and made CountPodsByNode return a map[nodeName]podCount. Only requires looping through the pods once. This is more performant that calling CountPods for each node.

return r, nil
}

pods, err := n.getFactory().List(client.PodGVR, client.BlankNamespace, false, labels.Everything())
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be nice to filter the pods we're interested in here using a field selector on spec.nodeName but it doens't look like it's currently possible?

@pdbrito
Copy link
Author

pdbrito commented Sep 8, 2025

Hey @derailed, please let me know if you'd like any changes here.

My local tests against a kind cluster have gone well, will run it against some bigger aws clusters next week too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-tlc Pr needs additional updates
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants