Skip to content

fix: limit pod queries using label selectors and dynamically resolve IG daemonset namespaces - #95

Open
Utkarshpandey0001 wants to merge 3 commits into
inspektor-gadget:mainfrom
Utkarshpandey0001:fix-unfiltered-pods
Open

fix: limit pod queries using label selectors and dynamically resolve IG daemonset namespaces#95
Utkarshpandey0001 wants to merge 3 commits into
inspektor-gadget:mainfrom
Utkarshpandey0001:fix-unfiltered-pods

Conversation

@Utkarshpandey0001

Copy link
Copy Markdown
Contributor

fixes #88

This PR heavily optimizes how the plugin queries Kubernetes for Inspektor Gadget pods, directly fixing issue #88. Previously, the plugin called K8s.ResourceClasses.Pod.useList() without any filters across multiple components (including the globally injected resource details view). In large clusters, querying the entire cluster's pod list on every single resource page navigation caused severe network latency, excessive memory overhead, and unneeded load on the kube-apiserver.

To solve this, all instances of Pod.useList() have been updated to include a labelSelector filter targeting the k8s-app=gadget label (utilizing existing IG_CONTAINER_KEY and IG_CONTAINER_VALUE constants).

Additionally, this PR removes the hardcoded [headlamp-plugin/src/common/GenericGadgetRenderer/index.tsx:53:2-110:3) namespace assumption when establishing port-forward connections. The plugin now dynamically reads the namespace directly from the retrieved pod metadata (pod.jsonData.metadata.namespace), ensuring robust compatibility with custom namespace deployments of Inspektor Gadget.

How to use

  1. Run Headlamp with the modified plugin linked/installed against an active cluster.
  2. Ensure you have Inspektor Gadget running in the cluster (in the gadget namespace or a custom one).
  3. Navigate to a cluster resource view (e.g., a Deployment or Node details page).
  4. Open the browser's Developer Tools Network tab and verify that the GET /api/v1/pods request now correctly scopes the query using the ?labelSelector=k8s-app%3Dgadget parameter, returning a significantly smaller JSON payload.
  5. Verify that port-forwarding metrics and gadgets establish a connection and load correctly in the resource details section.

Testing done

Executed local testing of the Headlamp UI to ensure the websocket port-forward connections start correctly under the new dynamic namespace logic. Also ran the automated code quality suite:

# Formatted the codebase
npm run format

# Verified TypeScript typings and linting
npm run tsc && npm run lint

# Built the final distributions
npm run build

@Utkarshpandey0001

Copy link
Copy Markdown
Contributor Author

Hi @illume, i have raised pr for this issue. Could you pls review it once and let me know your suggestion. Thanks!

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR optimizes Kubernetes Pod listing across the Headlamp Inspektor Gadget plugin by scoping pod queries to Inspektor Gadget pods via label selectors, and it removes hardcoded assumptions about the Inspektor Gadget namespace when establishing port-forward connections by deriving the namespace from the selected pod.

Changes:

  • Scope K8s.ResourceClasses.Pod.useList() calls using a labelSelector for k8s-app=gadget to avoid expensive cluster-wide pod listings.
  • Dynamically build port-forward URLs using the IG pod’s metadata.namespace instead of assuming the gadget namespace.
  • Minor formatting/import ordering cleanup in a few utility/context files.

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/index.tsx Filters details-view pod list with labelSelector to reduce cluster-wide load.
src/gadgets/list.tsx Filters gadget list pod query with labelSelector.
src/gadgets/gadgetGrid.tsx Filters pod queries used by gadget grid/embed/run panels.
src/gadgets/gadgetDetails.tsx Filters pod query and forwards selected pod namespace into renderer.
src/gadgets/backgroundgadgets.tsx Filters pod query for background-running gadgets view.
src/common/NodeSelection/index.tsx Filters pod query used for node/pod selection UI.
src/common/gadgetbackgroundinstanceform.tsx Filters pod query used during background instance creation.
src/gadgets/resourcegadgets.tsx Filters pod query and uses pod-derived namespace for port-forward URL.
src/gadgets/conn.tsx Uses pod-derived namespace for port-forward URL in shared connection hook.
src/common/GenericGadgetRenderer/index.tsx Uses selected pod namespace (fallbacking to gadget) for port-forward URL.
src/gadgets/utility.tsx Formatting-only changes in helper utilities.
src/common/GadgetContext/index.tsx Import ordering adjustment (no functional change).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/index.tsx
@Utkarshpandey0001

Copy link
Copy Markdown
Contributor Author

@ashu8912 Hi , i have applies filter in k8s.Resourceclasses.pod.useList() also as suggested by copilot also resolved conflicts. Could you pls look into it once more. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: Unfiltered cluster-wide Pod list fetch on every resource details page causes performance degradation

2 participants