Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 3, 2025

  • Implement ListAsync method with label/field selector support
  • Implement synchronous List method
  • Update GenericClient to pass filter parameters to underlying API
  • Add tests for List functionality
  • Move List methods into existing Get files (per review feedback)
  • Move List tests into KubectlTests.Get.cs (per review feedback)
  • Delete separate List files (per review feedback)

Summary

This PR implements List operations for the Kubectl class, extending the KubectlGet functionality (#1684) to support listing arbitrary groups of resources with filtering.

Changes

  • Kubectl.Get.cs / AsyncKubectl.Get.cs: Added List<T> and ListAsync<T> methods supporting:

    • Namespaced and cluster-scoped resource listing
    • Label selector filtering (labelSelector)
    • Field selector filtering (fieldSelector)
    • Pagination via limit and continueToken
  • GenericClient.cs: Updated ListAsync and ListNamespacedAsync to pass filter parameters through to the CustomObjects API

  • KubectlTests.Get.cs: E2E tests covering namespaces, pods, services, deployments, and selector filtering

Usage

var client = new Kubectl(kubernetes);

// List all namespaces
var namespaces = client.List<V1NamespaceList>();

// List pods in namespace with label selector
var pods = client.List<V1PodList>("default", labelSelector: "app=web,tier=frontend");

// Field selector
var pod = client.List<V1PodList>("default", fieldSelector: "metadata.name=my-pod");

// Pagination
var page1 = client.List<V1PodList>("default", limit: 100);
var page2 = client.List<V1PodList>("default", limit: 100, continueToken: page1.Metadata.ContinueProperty);
Original prompt

This section details on the original issue you should resolve

<issue_title>Implement List operations as part of KubectlGet</issue_title>
<issue_description>In #1684 we added the ability to get arbitrary resources, we should extend this capability to match the ability of kubectl to list arbitrary groups of resources. We should include all of the potential capabilities for listing like filtering on labels or other flags.

Here's the help text from kubectl to guide this development:

kubectl get --help
Display one or many resources.

 Prints a table of the most important information about the specified resources. You can filter the list using a label
selector and the --selector flag. If the desired resource type is namespaced you will only see results in the current
namespace if you don't specify any namespace.

 By specifying the output as 'template' and providing a Go template as the value of the --template flag, you can filter
the attributes of the fetched resources.

Use "kubectl api-resources" for a complete list of supported resources.

Examples:
  # List all pods in ps output format
  kubectl get pods
  
  # List all pods in ps output format with more information (such as node name)
  kubectl get pods -o wide
  
  # List a single replication controller with specified NAME in ps output format
  kubectl get replicationcontroller web
  
  # List deployments in JSON output format, in the "v1" version of the "apps" API group
  kubectl get deployments.v1.apps -o json
  
  # List a single pod in JSON output format
  kubectl get -o json pod web-pod-13je7
  
  # List a pod identified by type and name specified in "pod.yaml" in JSON output format
  kubectl get -f pod.yaml -o json
  
  # List resources from a directory with kustomization.yaml - e.g. dir/kustomization.yaml
  kubectl get -k dir/
  
  # Return only the phase value of the specified pod
  kubectl get -o template pod/web-pod-13je7 --template={{.status.phase}}
  
  # List resource information in custom columns
  kubectl get pod test-pod -o custom-columns=CONTAINER:.spec.containers[0].name,IMAGE:.spec.containers[0].image
  
  # List all replication controllers and services together in ps output format
  kubectl get rc,services
  
  # List one or more resources by their type and names
  kubectl get rc/web service/frontend pods/web-pod-13je7
  
  # List the 'status' subresource for a single pod
  kubectl get pod web-pod-13je7 --subresource status
  
  # List all deployments in namespace 'backend'
  kubectl get deployments.apps --namespace backend
  
  # List all pods existing in all namespaces
  kubectl get pods --all-namespaces

Options:
    -A, --all-namespaces=false:
        If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even
        if specified with --namespace.

    --allow-missing-template-keys=true:
        If true, ignore any errors in templates when a field or map key is missing in the template. Only applies to
        golang and jsonpath output formats.

    --chunk-size=500:
        Return large lists in chunks rather than all at once. Pass 0 to disable. This flag is beta and may change in
        the future.

    --field-selector='':
        Selector (field query) to filter on, supports '=', '==', and '!='.(e.g. --field-selector
        key1=value1,key2=value2). The server only supports a limited number of field queries per type.

    -f, --filename=[]:
        Filename, directory, or URL to files identifying the resource to get from a server.

    --ignore-not-found=false:
        If the requested object does not exist the command will return exit code 0.

    -k, --kustomize='':
        Process the kustomization directory. This flag can't be used together with -f or -R.

    -L, --label-columns=[]:
        Accepts a comma separated list of labels that are going to be presented as columns. Names are case-sensitive.
        You can also use multiple flag options like -L label1 -L label2...

    --no-headers=false:
        When using the default or custom-column output format, don't print headers (default print headers).

    -o, --output='':
        Output format. One of: (json, yaml, name, go-template, go-template-file, template, templatefile, jsonpath,
        jsonpath-as-json, jsonpath-file, custom-columns, custom-columns-file, wide). See custom columns
        [https://kubernetes.io/docs/reference/kubectl/#custom-columns], golang template
        [http://golang.org/pkg/text/template/#pkg-overview] and jsonpath template
        [https://kubernetes.io/docs/reference/kubectl/jsonpath/].

    --output-watch-events=false:
        Output watch event objects when --watch or --watch-only is used. Existing objects are output as initial ADDED
        events.

    --raw='':
        Raw URI to request from the server.  Uses the transport specified by the kubeconfig file.

    -R, --recursive=false:
        Process the director...

</details>

- Fixes kubernetes-client/csharp#1692

<!-- START COPILOT CODING AGENT TIPS -->
---

💬 We'd love your input! Share your thoughts on Copilot coding agent in our [2 minute survey](https://gh.io/copilot-coding-agent-survey).

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 3, 2025
Add List<T> and ListAsync<T> methods to Kubectl and AsyncKubectl classes
that support:
- Listing namespaced and cluster-scoped resources
- Label selector filtering (e.g., "app=test,tier=frontend")
- Field selector filtering (e.g., "metadata.name=my-pod")
- Pagination with limit and continue token

Also update GenericClient.ListAsync and ListNamespacedAsync to pass
through the filtering parameters to the underlying CustomObjects API.

Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Dec 3, 2025
Copilot AI changed the title [WIP] Implement list operations as part of KubectlGet Implement List operations for Kubectl Dec 3, 2025
Copilot AI requested a review from brendandburns December 3, 2025 02:04
Copilot finished work on behalf of brendandburns December 3, 2025 02:04
@brendandburns brendandburns marked this pull request as ready for review December 3, 2025 02:09
Copilot AI review requested due to automatic review settings December 3, 2025 02:09
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 3, 2025
@k8s-ci-robot k8s-ci-robot requested a review from tg123 December 3, 2025 02:09
Copy link
Contributor

@brendandburns brendandburns left a comment

Choose a reason for hiding this comment

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

@copilot instead of creating a different Kubectl.List class can you incorporate this into the existing Kubectl.Get class, please move the tests to the test file for KubectlGet also (and thanks for writing tests!)

Copilot finished reviewing on behalf of brendandburns December 3, 2025 02:11
Copy link
Contributor

Copilot AI left a comment

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 extends the Kubectl client to support listing arbitrary groups of Kubernetes resources with filtering capabilities, matching the functionality of kubectl get for list operations. The implementation adds List<T> and ListAsync<T> methods that support label selectors, field selectors, and pagination.

Key Changes:

  • Added List<T> and ListAsync<T> methods to Kubectl that support namespaced and cluster-scoped resource listing with label/field selector filtering and pagination
  • Updated GenericClient.cs to accept filter parameters (labelSelector, fieldSelector, limit, continueToken) and pass them to the Kubernetes CustomObjects API
  • Comprehensive E2E tests covering namespaces, pods, services, deployments, and various filtering scenarios

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/KubernetesClient.Kubectl/Beta/AsyncKubectl.List.cs Implements async List method with support for filtering and pagination parameters
src/KubernetesClient.Kubectl/Beta/Kubectl.List.cs Implements synchronous List method as a wrapper around AsyncKubectl
src/KubernetesClient/GenericClient.cs Updates ListAsync and ListNamespacedAsync to accept and pass through filter parameters
tests/Kubectl.Tests/KubectlTests.List.cs Adds comprehensive E2E tests for list functionality including label/field selectors

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

/// <summary>
/// List Kubernetes resources of a specific type.
/// </summary>
/// <typeparam name="T">The type of Kubernetes resource list to get (e.g., V1PodList).</typeparam>
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

The documentation says "The type of Kubernetes resource list to get" but should say "list" instead of "get" since this is a List operation, not a Get operation. Change to: "The type of Kubernetes resource list to list (e.g., V1PodList)."

Suggested change
/// <typeparam name="T">The type of Kubernetes resource list to get (e.g., V1PodList).</typeparam>
/// <typeparam name="T">The type of Kubernetes resource list to list (e.g., V1PodList).</typeparam>

Copilot uses AI. Check for mistakes.
/// <summary>
/// List Kubernetes resources of a specific type.
/// </summary>
/// <typeparam name="T">The type of Kubernetes resource list to get (e.g., V1PodList).</typeparam>
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

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

The documentation says "The type of Kubernetes resource list to get" but should say "list" instead of "get" since this is a List operation, not a Get operation. Change to: "The type of Kubernetes resource list to list (e.g., V1PodList)."

Suggested change
/// <typeparam name="T">The type of Kubernetes resource list to get (e.g., V1PodList).</typeparam>
/// <typeparam name="T">The type of Kubernetes resource list to list (e.g., V1PodList).</typeparam>

Copilot uses AI. Check for mistakes.
- Merged List/ListAsync methods into Kubectl.Get.cs and AsyncKubectl.Get.cs
- Moved List tests into KubectlTests.Get.cs
- Deleted separate Kubectl.List.cs, AsyncKubectl.List.cs, and KubectlTests.List.cs files

Co-authored-by: brendandburns <5751682+brendandburns@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 3, 2025

@copilot instead of creating a different Kubectl.List class can you incorporate this into the existing Kubectl.Get class, please move the tests to the test file for KubectlGet also (and thanks for wri...

Done in commit 78e5790. I've merged the List/ListAsync methods into the existing Kubectl.Get.cs and AsyncKubectl.Get.cs files, moved the tests into KubectlTests.Get.cs, and deleted the separate List files.

Copilot finished work on behalf of brendandburns December 3, 2025 02:19
Copilot AI requested a review from brendandburns December 3, 2025 02:19
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: brendandburns, Copilot

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 3, 2025
@brendandburns
Copy link
Contributor

@tg123 this is LGTM to me, feel free to merge if/when you think it's ready

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

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants