Skip to content

Fix: kubernetes statistics not respecting selector#5199

Merged
shamoon merged 1 commit into
gethomepage:devfrom
xethlyx:dev
Apr 21, 2025
Merged

Fix: kubernetes statistics not respecting selector#5199
shamoon merged 1 commit into
gethomepage:devfrom
xethlyx:dev

Conversation

@xethlyx
Copy link
Copy Markdown
Contributor

@xethlyx xethlyx commented Apr 20, 2025

Proposed change

After upgrading to v1, I noticed that my Kubernetes widgets were reporting invalid statistics:

image

This clearly can't be correct, since kubectl top reports that it only uses about 1.5gb of memory:

NAME                                                     CPU(cores)   MEMORY(bytes)
...
prometheus-grafana-0                                     19m          384Mi
prometheus-kube-prometheus-operator-796b68b4c-qjswh      1m           35Mi
prometheus-kube-state-metrics-c44f8f948-r7mvd            4m           58Mi
prometheus-prometheus-kube-prometheus-prometheus-0       290m         1294Mi
prometheus-prometheus-node-exporter-bnzs7                1m           17Mi
prometheus-prometheus-node-exporter-gr6h6                1m           15Mi
prometheus-prometheus-node-exporter-wsss6                1m           27Mi
...

This issue can be reproduced with the following configuration:

- My First Group:
    - My First Service:
        href: http://localhost/
        description: Homepage is awesome
        app: homepage
        namespace: services # any namespace with multiple pods
        podSelector: app=homepage # restrict this to a single pod

Instead of showing the statistics of the pods in podSelector, homepage will show the statistics of the entire namespace multiplied by the amount of pods found in the podSelector. This is because .getPodMetrics(namespace, pod.items) is returning the metrics for the entire namespace, as it does not accept a second argument.

This pull requests fixes the issue by filtering the metrics by the pods found in the namespace. Since .getPodMetrics can only filter by namespace, I also pull the fetch outside to reduce the number of calls from the amount of matched pods to just one. I considered using a simple filter per pod for a minimal diff, but I believe using a Set is the better choice here, in case there are a lot of selected pods, to avoid a O(n^2) time complexity.

Here is the same entry, with the fixes applied:

image

Type of change

  • New service widget
  • Bug fix (non-breaking change which fixes an issue)
  • New feature or enhancement (non-breaking change which adds functionality)
  • Documentation only
  • Other (please explain)

Checklist:

  • If applicable, I have added corresponding documentation changes.
  • If applicable, I have reviewed the feature / enhancement and / or service widget guidelines.
  • I have checked that all code style checks pass using pre-commit hooks and linting checks.
  • If applicable, I have tested my code for new features & regressions on both mobile & desktop devices, using the latest version of major browsers.

@shamoon shamoon enabled auto-merge (squash) April 21, 2025 00:39
Copy link
Copy Markdown
Collaborator

@shamoon shamoon left a comment

Choose a reason for hiding this comment

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

lgtm thanks

@shamoon shamoon merged commit d99c3cb into gethomepage:dev Apr 21, 2025
2 checks passed
tombatossals pushed a commit to tombatossals/homepage that referenced this pull request Apr 21, 2025
@github-actions
Copy link
Copy Markdown
Contributor

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new discussion for related concerns. See our contributing guidelines for more details.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators May 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants