Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize GetCurrentPodCount() by listing pods only once #36

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
15 changes: 10 additions & 5 deletions ocp-metadata/ocp-metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ func (meta *Metadata) GetCurrentPodCount() (int, error) {
if err != nil {
return podCount, err
}
for _, node := range nodeList.Items {
podList, err := meta.clientSet.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase=Running,spec.nodeName=" + node.Name})
if err != nil {
return podCount, err
podList, err := meta.clientSet.CoreV1().Pods(metav1.NamespaceAll).List(context.TODO(), metav1.ListOptions{FieldSelector: "status.phase=Running"})
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure on how this logic is any different from previous code. Its seems like previously code is also collecting podsCount per each node. Are we seeing any duplicate counts in the previous logic?

Copy link
Member

Choose a reason for hiding this comment

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

From what I can see, with the previous code if you have 100 nodes, this will create 100 API calls... Where the code change that @rsevilla87 has will be a single API call no matter the node count then using the pod description he filters per-node. Effectively doing the same thing, but with less API calls.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes perfect. Make sense to me now.

if err != nil {
return podCount, err
}
for _, pod := range podList.Items {
for _, node := range nodeList.Items {
if pod.Spec.NodeName == node.Name {
podCount++
break
}
}
podCount += len(podList.Items)
}
return podCount, nil
}
Expand Down
Loading