-
Notifications
You must be signed in to change notification settings - Fork 303
Implementing a more modular API #1627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 4 commits
128bc43
f021a01
19cbf1d
2cf1a53
0bd3dad
7a3f951
1347826
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// See https://aka.ms/new-console-template for more information | ||
using k8s; | ||
using k8s.ClientSets; | ||
using System.Threading.Tasks; | ||
|
||
namespace clientset | ||
{ | ||
internal class Program | ||
{ | ||
private static async Task Main(string[] args) | ||
{ | ||
var config = KubernetesClientConfiguration.BuildConfigFromConfigFile(); | ||
IKubernetes client = new Kubernetes(config); | ||
|
||
ClientSet clientSet = new ClientSet(client); | ||
var list = await clientSet.CoreV1.Pod.ListNamespacedPodAsync("default").ConfigureAwait(false); | ||
foreach (var item in list) | ||
{ | ||
System.Console.WriteLine(item.Metadata.Name); | ||
} | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace k8s.ClientSets; | ||
|
||
public partial class ClientSet | ||
{ | ||
Check warning on line 4 in src/KubernetesClient/ClientSets/ClientSet.cs
|
||
|
||
} | ||
Check warning on line 6 in src/KubernetesClient/ClientSets/ClientSet.cs
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
namespace k8s.ClientSets; | ||
|
||
public abstract class ResourceClient | ||
{ | ||
protected Kubernetes Client { get; } | ||
|
||
public ResourceClient(IKubernetes kubernetes) | ||
{ | ||
Client = (Kubernetes)kubernetes; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. did not quite get here, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,7 +30,8 @@ | |
|
||
public async Task<T> CreateAsync<T>(T obj, CancellationToken cancel = default) | ||
where T : IKubernetesObject | ||
{ | ||
Check warning on line 33 in src/KubernetesClient/GenericClient.cs
|
||
|
||
var resp = await kubernetes.CustomObjects.CreateClusterCustomObjectWithHttpMessagesAsync<T>(obj, group, version, plural, cancellationToken: cancel).ConfigureAwait(false); | ||
return resp.Body; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using CaseExtensions; | ||
using Microsoft.CodeAnalysis; | ||
using NSwag; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace LibKubernetesGenerator | ||
{ | ||
internal class ClientSetGenerator | ||
{ | ||
private readonly ScriptObjectFactory _scriptObjectFactory; | ||
|
||
public ClientSetGenerator(ScriptObjectFactory scriptObjectFactory) | ||
{ | ||
_scriptObjectFactory = scriptObjectFactory; | ||
} | ||
|
||
public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context) | ||
{ | ||
var data = swagger.Operations | ||
.Where(o => o.Method != OpenApiOperationMethod.Options) | ||
.Select(o => | ||
{ | ||
var ps = o.Operation.ActualParameters.OrderBy(p => !p.IsRequired).ToArray(); | ||
|
||
o.Operation.Parameters.Clear(); | ||
|
||
var name = new HashSet<string>(); | ||
|
||
var i = 1; | ||
foreach (var p in ps) | ||
{ | ||
if (name.Contains(p.Name)) | ||
{ | ||
p.Name += i++; | ||
} | ||
|
||
o.Operation.Parameters.Add(p); | ||
name.Add(p.Name); | ||
} | ||
|
||
return o; | ||
}) | ||
.Select(o => | ||
{ | ||
o.Path = o.Path.TrimStart('/'); | ||
o.Method = char.ToUpper(o.Method[0]) + o.Method.Substring(1); | ||
return o; | ||
}) | ||
.ToArray(); | ||
|
||
var sc = _scriptObjectFactory.CreateScriptObject(); | ||
|
||
var groups = new List<string>(); | ||
var apiGroups = new Dictionary<string, OpenApiOperationDescription[]>(); | ||
|
||
foreach (var grouped in data.Where(d => HasKubernetesAction(d.Operation?.ExtensionData)) | ||
.GroupBy(d => d.Operation.Tags.First())) | ||
{ | ||
var clients = new List<string>(); | ||
var name = grouped.Key.ToPascalCase(); | ||
groups.Add(name); | ||
var apis = grouped.Select(x => | ||
{ | ||
var groupVersionKindElements = x.Operation?.ExtensionData?["x-kubernetes-group-version-kind"]; | ||
var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements; | ||
|
||
return new { Kind = groupVersionKind?["kind"], Api = x }; | ||
|
||
}); | ||
Check warning on line 70 in src/LibKubernetesGenerator/ClientSetGenerator.cs
|
||
|
||
foreach (var item in apis.GroupBy(x => x.Kind)) | ||
{ | ||
var kind = item.Key as string; | ||
apiGroups[kind] = item.Select(x => x.Api).ToArray(); | ||
clients.Add(kind); | ||
} | ||
|
||
sc.SetValue("clients", clients, true); | ||
sc.SetValue("name", name, true); | ||
context.RenderToContext("GroupClient.cs.template", sc, $"{name}GroupClient.g.cs"); | ||
} | ||
|
||
foreach (var apiGroup in apiGroups) | ||
{ | ||
var name = apiGroup.Key; | ||
var apis = apiGroup.Value.ToArray(); | ||
|
||
sc.SetValue("apis", apis, true); | ||
sc.SetValue("name", name, true); | ||
context.RenderToContext("Client.cs.template", sc, $"{name}Client.g.cs"); | ||
context.RenderToContext("ClientExtensions.cs.template", sc, $"{name}ClientExtensions.g.cs"); | ||
} | ||
|
||
sc = _scriptObjectFactory.CreateScriptObject(); | ||
sc.SetValue("groups", groups, true); | ||
|
||
context.RenderToContext("ClientSet.cs.template", sc, $"ClientSet.g.cs"); | ||
} | ||
|
||
private bool HasKubernetesAction(IDictionary<string, object> extensionData) => | ||
extensionData?.ContainsKey("x-kubernetes-action") ?? false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ListAsync?
i prefer the style in your proposal, it is simpler
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used x-kuberneter-action to define the method name. Can you help me see if there is any problem with this?https://github.com/kubernetes-client/csharp/pull/1627/files#diff-9501625d7da40bd1b40cb3b43406f26d00f5aa65c65f4877b1eaec2d40f792a0R169