-
Notifications
You must be signed in to change notification settings - Fork 305
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
Merged
+488
−34
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
128bc43
fix typo
ayrloong f021a01
Merge branch 'kubernetes-client:master' into master
ayrloong 19cbf1d
Merge branch 'kubernetes-client:master' into master
ayrloong 2cf1a53
Add modular API
ayrloong 0bd3dad
Use x-kubernetes-action instead of operationId to generate method names
ayrloong 7a3f951
fix
ayrloong 1347826
Clean code style warnings
ayrloong 47b8330
Refactor client constructors to use Kubernetes type instead of IKuber…
ayrloong 05da1c4
Add ClientSet tests for Kubernetes pod operations
ayrloong 56f63e5
Fix order of parameters in Pod API calls for consistency
ayrloong 5c3f9a4
Enhance documentation for ClientSet and ResourceClient classes
ayrloong 177809d
Refactor ClientSet and GroupClient for Kubernetes usage
ayrloong 3085283
Refactor Pod API calls in tests to use singular form for consistency
ayrloong 218bb31
Refactor Pod API calls to use 'Create' and 'Update' methods for consi…
ayrloong File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// 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(); | ||
var client = new Kubernetes(config); | ||
|
||
ClientSet clientSet = new ClientSet(client); | ||
var list = await clientSet.CoreV1.Pod.ListAsync("default").ConfigureAwait(false); | ||
foreach (var item in list) | ||
{ | ||
System.Console.WriteLine(item.Metadata.Name); | ||
} | ||
|
||
var pod = await clientSet.CoreV1.Pod.GetAsync("test","default").ConfigureAwait(false); | ||
Check warning on line 22 in examples/clientset/Program.cs
|
||
System.Console.WriteLine(pod?.Metadata?.Name); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace k8s.ClientSets | ||
{ | ||
/// <summary> | ||
/// Represents a base class for clients that interact with Kubernetes resources. | ||
/// Provides shared functionality for derived resource-specific clients. | ||
/// </summary> | ||
public partial class ClientSet | ||
{ | ||
private readonly Kubernetes _kubernetes; | ||
|
||
public ClientSet(Kubernetes kubernetes) | ||
{ | ||
_kubernetes = kubernetes; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace k8s.ClientSets | ||
{ | ||
/// <summary> | ||
/// Represents a set of Kubernetes clients for interacting with the Kubernetes API. | ||
/// This class provides access to various client implementations for managing Kubernetes resources. | ||
/// </summary> | ||
public abstract class ResourceClient | ||
{ | ||
protected Kubernetes Client { get; } | ||
|
||
public ResourceClient(Kubernetes kubernetes) | ||
{ | ||
Client = kubernetes; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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"] as string, Api = x }; | ||
}); | ||
|
||
foreach (var item in apis.GroupBy(x => x.Kind)) | ||
{ | ||
var kind = item.Key; | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.