Repository navigation
Description
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
-
Improved Discoverability
Methods are grouped by API group/version and resource types -
Consistency
Aligns with Kubernetes API structure and client-go patterns -
Type Safety
Stronger typing through dedicated resource classes -
Scalability
Easier to maintain and extend for future API versions
Implementation Approach
-
Hierarchical Structure:
KubernetesClient ├── CoreV1 │ ├── Pods │ ├── Services │ └── Namespaces ├── AppsV1 │ ├── Deployments │ └── StatefulSets └── NetworkingV1 ├── Ingresses └── NetworkPolicies -
Backward Compatibility
Maintain existing flat methods (marked obsolete) while introducing new hierarchy -
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.
Activity
tg123 commentedon Apr 17, 2025
It is a long story.
The project originated with
autorestcode where all APIs consolidated intoIKubernetesthe .XXX was introduced in #850, to maintain backward compatibility,
IKubernetesimplements everything, minimizing breaking changes for users who do not rely on the interface.I hope the SDK bridge the gap with
client-goand incorporate-friendly features. Perhaps akubectl-like API could be a good starting point, especially if backward compatibility is essential.The SDK no longer depends on
autorestand now includes a built-in code generator,[LibubernetesGenerator](https://github.com/kubernetes-client/csharp/tree/master/src/LibKubernetesGenerator). This provides the capability to transition to a more refined API structure.lqlive commentedon Apr 17, 2025
Thank you for your reply, I have seen LibubernetesGenerator and now all APIs are generated by it, which means we only need to change it. But if we are sure to change it, we can only do it in a major version.
tg123 commentedon Apr 17, 2025
lets wait for more input from @brendandburns
lqlive commentedon Apr 18, 2025
Thank you so much
brendandburns commentedon Apr 18, 2025
Is there any way that we could do both for a few releases?
fwiw, we have a more modular API structure in the Javascript client and we had a user complain that it was too complicated and they wanted a flat API:
kubernetes-client/javascript#2339
So whatever we do here, I'm not sure we're going to make everyone happy :)
lqlive commentedon Apr 19, 2025
Thank you for your reply. If we implement both methods at the same time, we may need to redefine the implementation of the IKubernetes interface. We provide both methods for users to choose from, or we can keep the previous method on the Kubernetes Object and re-add the method.
brendandburns commentedon Apr 19, 2025
I think that if we do this, it would make sense to make the new modular methods live in their own namespaces (e.g.
using k8s.CoreV1) vs layering them on top of theIKubernetesobject. We can then implement theIKubernetesmethods in terms of the methods in that packagelqlive commentedon Apr 19, 2025
Is this what you mean?
brendandburns commentedon Apr 22, 2025
Yeah, something like that looks good to me.
lqlive commentedon May 22, 2025
Thank you very much for your help in completing this proposal.
kimpenhaus commentedon May 23, 2025
@ayrloong in the implementation I saw that it is using
Kubernetesinstead ofIKubernetes(as shown here) - is there any particular reason for that?lqlive commentedon May 23, 2025
There is a discussion about this in the PR. Since all methods are now called on the
#1627 (comment)
Kubernetesclass, I will also convertIKubernetestoKubernetesin the initial implementation.kimpenhaus commentedon May 23, 2025
ah - oversaw that, thanks for pointing out.
one more question if you don't mind: how is it meant to be used? should one register specific clients (eg podclient) in the DI?
lqlive commentedon May 23, 2025
No need. Here is an example https://github.com/kubernetes-client/csharp/blob/master/examples/clientset/Program.cs
kimpenhaus commentedon May 23, 2025
got it - thanks mate!