Skip to content

Commit

Permalink
Fix getPodsPerNode error (#48)
Browse files Browse the repository at this point in the history
`spec.nodeName` is not a valid field selector for node objects.
Use `Get` with node name instead of `List` to get the node object.
  • Loading branch information
nsavoire authored Nov 15, 2024
1 parent f246a06 commit d08fe64
Showing 1 changed file with 4 additions and 12 deletions.
16 changes: 4 additions & 12 deletions containermetadata/containermetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,24 +238,16 @@ func NewContainerMetadataProvider(ctx context.Context, nodeName string, monitorI
// for the allocatable information of the nodes.
func getPodsPerNode(ctx context.Context, h *containerMetadataProvider) (int, error) {
h.kubernetesClientQueryCount.Add(1)
nodeList, err := h.kubeClientSet.CoreV1().Nodes().List(ctx, v1.ListOptions{
FieldSelector: "spec.nodeName=" + h.nodeName,
})
node, err := h.kubeClientSet.CoreV1().Nodes().Get(ctx, h.nodeName, v1.GetOptions{})
if err != nil {
return 0, fmt.Errorf("failed to get kubernetes nodes for '%s': %w",
return 0, fmt.Errorf("failed to get kubernetes node '%s': %w",
h.nodeName, err)
}

if len(nodeList.Items) == 0 {
return 0, errors.New("empty node list")
}

// With the ListOptions filter in place, there should be only one node listed in the
// return we get from the API.
quantity, ok := nodeList.Items[0].Status.Allocatable[corev1.ResourcePods]
quantity, ok := node.Status.Allocatable[corev1.ResourcePods]
if !ok {
return 0, fmt.Errorf("failed to get allocatable information from %s",
nodeList.Items[0].Name)
node.Name)
}

return int(quantity.Value()), nil
Expand Down

0 comments on commit d08fe64

Please sign in to comment.