-
Notifications
You must be signed in to change notification settings - Fork 307
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
Changes from 6 commits
128bc43
f021a01
19cbf1d
2cf1a53
0bd3dad
7a3f951
1347826
47b8330
05da1c4
56f63e5
5c3f9a4
177809d
3085283
218bb31
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,28 @@ | ||
// 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.Pods.ListAsync("default").ConfigureAwait(false); | ||
foreach (var item in list) | ||
{ | ||
System.Console.WriteLine(item.Metadata.Name); | ||
} | ||
|
||
var pod = await clientSet.CoreV1.Pods.GetAsync("test","default").ConfigureAwait(false); | ||
System.Console.WriteLine(pod?.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; | ||
|
||
lqlive marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
public abstract class ResourceClient | ||
{ | ||
protected Kubernetes Client { get; } | ||
|
||
public ResourceClient(IKubernetes kubernetes) | ||
{ | ||
Client = (Kubernetes)kubernetes; | ||
tg123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
} |
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; | ||
using Humanizer; | ||
|
||
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"] as string, Api = x }; | ||
}); | ||
|
||
foreach (var item in apis.GroupBy(x => x.Kind)) | ||
{ | ||
var kind = item.Key.Pluralize(); | ||
tg123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
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(); | ||
var group = apis.Select(x => x.Operation.Tags[0]).First(); | ||
sc.SetValue("apis", apis, true); | ||
sc.SetValue("name", name, true); | ||
sc.SetValue("group", group.ToPascalCase(), true); | ||
context.RenderToContext("Client.cs.template", sc, $"{name}Client.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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,8 +22,11 @@ | |
{ | ||
scriptObject.Import(nameof(GetInterfaceName), new Func<JsonSchema, string>(GetInterfaceName)); | ||
scriptObject.Import(nameof(GetMethodName), new Func<OpenApiOperation, string, string>(GetMethodName)); | ||
scriptObject.Import(nameof(GetActionName), | ||
Check warning on line 25 in src/LibKubernetesGenerator/GeneralNameHelper.cs
|
||
new Func<OpenApiOperationDescription, string, string, string>(GetActionName)); | ||
scriptObject.Import(nameof(GetDotNetName), new Func<string, string, string>(GetDotNetName)); | ||
scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), new Func<OpenApiParameter, string, string>(GetDotNetNameOpenApiParameter)); | ||
scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), | ||
Check warning on line 28 in src/LibKubernetesGenerator/GeneralNameHelper.cs
|
||
new Func<OpenApiParameter, string, string>(GetDotNetNameOpenApiParameter)); | ||
} | ||
|
||
private string GetInterfaceName(JsonSchema definition) | ||
|
@@ -162,5 +165,60 @@ | |
|
||
return methodName; | ||
} | ||
|
||
public static string GetActionName(OpenApiOperationDescription apiOperation, string resource, string suffix) | ||
{ | ||
tg123 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
var actionType = apiOperation.Operation?.ExtensionData?["x-kubernetes-action"] as string; | ||
|
||
if (string.IsNullOrEmpty(actionType)) | ||
{ | ||
return $"{apiOperation.Method.ToPascalCase()}{suffix}"; | ||
} | ||
|
||
var resourceNamespace = ParsePathSegmentAfterParameter(apiOperation.Path, "namespace").ToPascalCase(); | ||
var resourceName = ParsePathSegmentAfterParameter(apiOperation.Path, "name").ToPascalCase(); | ||
var actionMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
{ "get", "Get" }, | ||
{ "list", "List" }, | ||
{ "put", "Put" }, | ||
{ "patch", "Patch" }, | ||
{ "post", "Post" }, | ||
{ "delete", "Delete" }, | ||
{ "deletecollection", "DeleteCollection" }, | ||
{ "watch", "Watch" }, | ||
{ "watchlist", "WatchList" }, | ||
{ "proxy", "Proxy" }, | ||
}; | ||
|
||
if (actionMappings.TryGetValue(actionType, out var actionPrefix)) | ||
{ | ||
return Regex.Replace($"{actionPrefix}{resourceNamespace}{resourceName}{suffix}", resource, string.Empty, | ||
RegexOptions.IgnoreCase); | ||
} | ||
|
||
if (string.Equals("connect", actionType, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return Regex.Replace($"Connect{apiOperation.Method}{resourceNamespace}{resourceName}{suffix}", resource, | ||
string.Empty, | ||
RegexOptions.IgnoreCase); | ||
} | ||
|
||
return $"{actionType.ToPascalCase()}{suffix}"; | ||
} | ||
|
||
private static string ParsePathSegmentAfterParameter(string path, string variableName = "namespace") | ||
{ | ||
var pattern = $@"/\{{{variableName}\}}/([^/]+)/?"; | ||
|
||
var match = Regex.Match(path, pattern); | ||
|
||
if (match.Success && match.Groups.Count > 1) | ||
{ | ||
return match.Groups[1].Value; | ||
} | ||
|
||
return string.Empty; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.