Skip to content

Proposal: Improve KubernetesClient API Organization for Better Discoverability #1622

Open
@ayrloong

Description

@ayrloong

Background & Problem Statement

While working with the KubernetesClient (.NET client library), I noticed that all API methods are currently grouped under a single flat hierarchy. This design makes it challenging to discover specific methods, especially when working with different resource types across API groups/versions.

For example, in the official Go client (client-go), methods are logically organized with clear resource hierarchy:

clientset.CoreV1().Pods("default").Get(context.TODO(), "example-xxxxx", metav1.GetOptions{})

However, in the current .NET implementation, similar operations lack this intuitive grouping:

// Current structure
var pods = kubernetes.CoreV1.ListNamespacedPodWithHttpMessagesAsync(namespace);
var services = kubernetes.CoreV1.ListServiceForAllNamespacesWithHttpMessagesAsync(namespace);

This flat structure leads to:

  • Difficulty discovering related methods
  • Long method lists in IDE auto-completion
  • Inconsistent with Kubernetes API grouping conventions

Proposed Improvement

Adopt a hierarchical organization pattern similar to client-go's Clientset structure:

// Proposed structure
var pods = await kubernetes.CoreV1.Pods.ListAsync(namespace);
var deployments = await kubernetes.AppsV1.Deployments.ListAsync(namespace);

Key Benefits

  1. Improved Discoverability
    Methods are grouped by API group/version and resource types

  2. Consistency
    Aligns with Kubernetes API structure and client-go patterns

  3. Type Safety
    Stronger typing through dedicated resource classes

  4. Scalability
    Easier to maintain and extend for future API versions

Implementation Approach

  1. Hierarchical Structure:

    KubernetesClient
    ├── CoreV1
    │   ├── Pods
    │   ├── Services
    │   └── Namespaces
    ├── AppsV1
    │   ├── Deployments
    │   └── StatefulSets
    └── NetworkingV1
        ├── Ingresses
        └── NetworkPolicies
    
  2. Backward Compatibility
    Maintain existing flat methods (marked obsolete) while introducing new hierarchy

  3. Resource-Specific Classes
    Create dedicated classes for each resource type with relevant operations:

    public class PodOperations {
        Task<List<Pod>> ListAsync(string namespace);
        Task<Pod> GetAsync(string name, string namespace);
        // Other pod-specific operations
    }

Example Usage

var client = new KubernetesClient();

// List pods in default namespace
var pods = await client.CoreV1.Pods.ListAsync("default");

// Get specific deployment
var deployment = await client.AppsV1.Deployments.GetAsync("api-gateway", "production");

// Create network policy
await client.NetworkingV1.NetworkPolicies.CreateAsync(new NetworkPolicy(...), "secure-ns");

If we agree on this proposal, I would be happy to submit a PR. I'm genuinely enthusiastic about contributing to the community and would love to help implement this improvement.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions