A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/kubernetes-client/csharp/commit/f1125e9435ca26b8fcff206657be2f9cba90c9e5 below:

Implementing a more modular API (#1627) · kubernetes-client/csharp@f1125e9 · GitHub

File tree Expand file treeCollapse file tree 20 files changed

+488

-34

lines changed

Filter options

Expand file treeCollapse file tree 20 files changed

+488

-34

lines changed Original file line number Diff line number Diff line change

@@ -50,4 +50,4 @@

50 50

<GlobalPackageReference Include="Nerdbank.GitVersioning" Version="3.7.112" />

51 51

<GlobalPackageReference Include="StyleCop.Analyzers" Version="1.1.118" />

52 52

</ItemGroup>

53 -

</Project>

53 +

</Project>

Original file line number Diff line number Diff line change

@@ -0,0 +1,26 @@

1 +

// See https://aka.ms/new-console-template for more information

2 +

using k8s;

3 +

using k8s.ClientSets;

4 +

using System.Threading.Tasks;

5 + 6 +

namespace clientset

7 +

{

8 +

internal class Program

9 +

{

10 +

private static async Task Main(string[] args)

11 +

{

12 +

var config = KubernetesClientConfiguration.BuildConfigFromConfigFile();

13 +

var client = new Kubernetes(config);

14 + 15 +

ClientSet clientSet = new ClientSet(client);

16 +

var list = await clientSet.CoreV1.Pod.ListAsync("default").ConfigureAwait(false);

17 +

foreach (var item in list)

18 +

{

19 +

System.Console.WriteLine(item.Metadata.Name);

20 +

}

21 + 22 +

var pod = await clientSet.CoreV1.Pod.GetAsync("test","default").ConfigureAwait(false);

23 +

System.Console.WriteLine(pod?.Metadata?.Name);

24 +

}

25 +

}

26 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,6 @@

1 +

<Project Sdk="Microsoft.NET.Sdk">

2 +

<PropertyGroup>

3 +

<OutputType>Exe</OutputType>

4 +

</PropertyGroup>

5 + 6 +

</Project>

Original file line number Diff line number Diff line change

@@ -46,6 +46,10 @@

46 46

<Compile Include="..\KubernetesClient\Models\V1Status.cs" />

47 47 48 48

</ItemGroup>

49 +

<ItemGroup>

50 +

<Compile Include="..\KubernetesClient\ClientSets\ClientSet.cs" />

51 +

<Compile Include="..\KubernetesClient\ClientSets\ResourceClient.cs"/>

52 +

</ItemGroup>

49 53

<ItemGroup>

50 54

<Compile Include="..\KubernetesClient\AbstractKubernetes.cs" />

51 55

<Compile Include="..\KubernetesClient\GeneratedApiVersion.cs" />

@@ -107,4 +111,4 @@

107 111

<ItemGroup>

108 112

<ProjectReference Include="..\LibKubernetesGenerator\generators\LibKubernetesGenerator\LibKubernetesGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

109 113

</ItemGroup>

110 -

</Project>

114 +

</Project>

Original file line number Diff line number Diff line change

@@ -75,7 +75,10 @@

75 75

<Compile Include="..\KubernetesClient\Autorest\HttpRequestMessageWrapper.cs" />

76 76

<Compile Include="..\KubernetesClient\Autorest\HttpResponseMessageWrapper.cs" />

77 77

</ItemGroup>

78 - 78 +

<ItemGroup>

79 +

<Compile Include="..\KubernetesClient\ClientSets\ClientSet.cs" />

80 +

<Compile Include="..\KubernetesClient\ClientSets\ResourceClient.cs"/>

81 +

</ItemGroup>

79 82

<ItemGroup>

80 83

<Compile Include="..\KubernetesClient\FileSystem.cs" />

81 84

<Compile Include="..\KubernetesClient\IKubernetes.cs" />

Original file line number Diff line number Diff line change

@@ -0,0 +1,16 @@

1 +

namespace k8s.ClientSets

2 +

{

3 +

/// <summary>

4 +

/// Represents a base class for clients that interact with Kubernetes resources.

5 +

/// Provides shared functionality for derived resource-specific clients.

6 +

/// </summary>

7 +

public partial class ClientSet

8 +

{

9 +

private readonly Kubernetes _kubernetes;

10 + 11 +

public ClientSet(Kubernetes kubernetes)

12 +

{

13 +

_kubernetes = kubernetes;

14 +

}

15 +

}

16 +

}

Original file line number Diff line number Diff line change

@@ -0,0 +1,16 @@

1 +

namespace k8s.ClientSets

2 +

{

3 +

/// <summary>

4 +

/// Represents a set of Kubernetes clients for interacting with the Kubernetes API.

5 +

/// This class provides access to various client implementations for managing Kubernetes resources.

6 +

/// </summary>

7 +

public abstract class ResourceClient

8 +

{

9 +

protected Kubernetes Client { get; }

10 + 11 +

public ResourceClient(Kubernetes kubernetes)

12 +

{

13 +

Client = kubernetes;

14 +

}

15 +

}

16 +

}

Original file line number Diff line number Diff line change

@@ -18,19 +18,19 @@ public partial class Kubernetes

18 18

/// <inheritdoc/>

19 19

public Task<WebSocket> WebSocketNamespacedPodExecAsync(string name, string @namespace = "default",

20 20

string command = null, string container = null, bool stderr = true, bool stdin = true, bool stdout = true,

21 -

bool tty = true, string webSocketSubProtol = null, Dictionary<string, List<string>> customHeaders = null,

21 +

bool tty = true, string webSocketSubProtocol = null, Dictionary<string, List<string>> customHeaders = null,

22 22

CancellationToken cancellationToken = default)

23 23

{

24 24

return WebSocketNamespacedPodExecAsync(name, @namespace, new string[] { command }, container, stderr, stdin,

25 -

stdout, tty, webSocketSubProtol, customHeaders, cancellationToken);

25 +

stdout, tty, webSocketSubProtocol, customHeaders, cancellationToken);

26 26

}

27 27 28 28

/// <inheritdoc/>

29 29

public virtual async Task<IStreamDemuxer> MuxedStreamNamespacedPodExecAsync(

30 30

string name,

31 31

string @namespace = "default", IEnumerable<string> command = null, string container = null,

32 32

bool stderr = true, bool stdin = true, bool stdout = true, bool tty = true,

33 -

string webSocketSubProtol = WebSocketProtocol.V4BinaryWebsocketProtocol,

33 +

string webSocketSubProtocol = WebSocketProtocol.V4BinaryWebsocketProtocol,

34 34

Dictionary<string, List<string>> customHeaders = null,

35 35

CancellationToken cancellationToken = default)

36 36

{

@@ -45,7 +45,7 @@ public virtual async Task<IStreamDemuxer> MuxedStreamNamespacedPodExecAsync(

45 45

public virtual Task<WebSocket> WebSocketNamespacedPodExecAsync(string name, string @namespace = "default",

46 46

IEnumerable<string> command = null, string container = null, bool stderr = true, bool stdin = true,

47 47

bool stdout = true, bool tty = true,

48 -

string webSocketSubProtol = WebSocketProtocol.V4BinaryWebsocketProtocol,

48 +

string webSocketSubProtocol = WebSocketProtocol.V4BinaryWebsocketProtocol,

49 49

Dictionary<string, List<string>> customHeaders = null,

50 50

CancellationToken cancellationToken = default)

51 51

{

@@ -114,7 +114,7 @@ public virtual Task<WebSocket> WebSocketNamespacedPodExecAsync(string name, stri

114 114

uriBuilder.Query =

115 115

query.ToString(1, query.Length - 1); // UriBuilder.Query doesn't like leading '?' chars, so trim it

116 116 117 -

return StreamConnectAsync(uriBuilder.Uri, webSocketSubProtol, customHeaders,

117 +

return StreamConnectAsync(uriBuilder.Uri, webSocketSubProtocol, customHeaders,

118 118

cancellationToken);

119 119

}

120 120

@@ -173,7 +173,7 @@ public Task<WebSocket> WebSocketNamespacedPodPortForwardAsync(string name, strin

173 173

/// <inheritdoc/>

174 174

public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @namespace,

175 175

string container = default, bool stderr = true, bool stdin = false, bool stdout = true,

176 -

bool tty = false, string webSocketSubProtol = null, Dictionary<string, List<string>> customHeaders = null,

176 +

bool tty = false, string webSocketSubProtocol = null, Dictionary<string, List<string>> customHeaders = null,

177 177

CancellationToken cancellationToken = default)

178 178

{

179 179

if (name == null)

@@ -208,7 +208,7 @@ public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @na

208 208

uriBuilder.Query =

209 209

query.ToString(1, query.Length - 1); // UriBuilder.Query doesn't like leading '?' chars, so trim it

210 210 211 -

return StreamConnectAsync(uriBuilder.Uri, webSocketSubProtol, customHeaders,

211 +

return StreamConnectAsync(uriBuilder.Uri, webSocketSubProtocol, customHeaders,

212 212

cancellationToken);

213 213

}

214 214 Original file line number Diff line number Diff line change

@@ -0,0 +1,103 @@

1 +

using CaseExtensions;

2 +

using Microsoft.CodeAnalysis;

3 +

using NSwag;

4 +

using System.Collections.Generic;

5 +

using System.Linq;

6 + 7 +

namespace LibKubernetesGenerator

8 +

{

9 +

internal class ClientSetGenerator

10 +

{

11 +

private readonly ScriptObjectFactory _scriptObjectFactory;

12 + 13 +

public ClientSetGenerator(ScriptObjectFactory scriptObjectFactory)

14 +

{

15 +

_scriptObjectFactory = scriptObjectFactory;

16 +

}

17 + 18 +

public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializationContext context)

19 +

{

20 +

var data = swagger.Operations

21 +

.Where(o => o.Method != OpenApiOperationMethod.Options)

22 +

.Select(o =>

23 +

{

24 +

var ps = o.Operation.ActualParameters.OrderBy(p => !p.IsRequired).ToArray();

25 + 26 +

o.Operation.Parameters.Clear();

27 + 28 +

var name = new HashSet<string>();

29 + 30 +

var i = 1;

31 +

foreach (var p in ps)

32 +

{

33 +

if (name.Contains(p.Name))

34 +

{

35 +

p.Name += i++;

36 +

}

37 + 38 +

o.Operation.Parameters.Add(p);

39 +

name.Add(p.Name);

40 +

}

41 + 42 +

return o;

43 +

})

44 +

.Select(o =>

45 +

{

46 +

o.Path = o.Path.TrimStart('/');

47 +

o.Method = char.ToUpper(o.Method[0]) + o.Method.Substring(1);

48 +

return o;

49 +

})

50 +

.ToArray();

51 + 52 +

var sc = _scriptObjectFactory.CreateScriptObject();

53 + 54 +

var groups = new List<string>();

55 +

var apiGroups = new Dictionary<string, OpenApiOperationDescription[]>();

56 + 57 +

foreach (var grouped in data.Where(d => HasKubernetesAction(d.Operation?.ExtensionData))

58 +

.GroupBy(d => d.Operation.Tags.First()))

59 +

{

60 +

var clients = new List<string>();

61 +

var name = grouped.Key.ToPascalCase();

62 +

groups.Add(name);

63 +

var apis = grouped.Select(x =>

64 +

{

65 +

var groupVersionKindElements = x.Operation?.ExtensionData?["x-kubernetes-group-version-kind"];

66 +

var groupVersionKind = (Dictionary<string, object>)groupVersionKindElements;

67 + 68 +

return new { Kind = groupVersionKind?["kind"] as string, Api = x };

69 +

});

70 + 71 +

foreach (var item in apis.GroupBy(x => x.Kind))

72 +

{

73 +

var kind = item.Key;

74 +

apiGroups[kind] = item.Select(x => x.Api).ToArray();

75 +

clients.Add(kind);

76 +

}

77 + 78 +

sc.SetValue("clients", clients, true);

79 +

sc.SetValue("name", name, true);

80 +

context.RenderToContext("GroupClient.cs.template", sc, $"{name}GroupClient.g.cs");

81 +

}

82 + 83 +

foreach (var apiGroup in apiGroups)

84 +

{

85 +

var name = apiGroup.Key;

86 +

var apis = apiGroup.Value.ToArray();

87 +

var group = apis.Select(x => x.Operation.Tags[0]).First();

88 +

sc.SetValue("apis", apis, true);

89 +

sc.SetValue("name", name, true);

90 +

sc.SetValue("group", group.ToPascalCase(), true);

91 +

context.RenderToContext("Client.cs.template", sc, $"{name}Client.g.cs");

92 +

}

93 + 94 +

sc = _scriptObjectFactory.CreateScriptObject();

95 +

sc.SetValue("groups", groups, true);

96 + 97 +

context.RenderToContext("ClientSet.cs.template", sc, $"ClientSet.g.cs");

98 +

}

99 + 100 +

private bool HasKubernetesAction(IDictionary<string, object> extensionData) =>

101 +

extensionData?.ContainsKey("x-kubernetes-action") ?? false;

102 +

}

103 +

}

Original file line number Diff line number Diff line change

@@ -21,7 +21,8 @@ public GeneralNameHelper(ClassNameHelper classNameHelper)

21 21

public void RegisterHelper(ScriptObject scriptObject)

22 22

{

23 23

scriptObject.Import(nameof(GetInterfaceName), new Func<JsonSchema, string>(GetInterfaceName));

24 -

scriptObject.Import(nameof(GetMethodName), new Func<OpenApiOperation, string, string>(GetMethodName));

24 +

scriptObject.Import(nameof(GetOperationId), new Func<OpenApiOperation, string, string>(GetOperationId));

25 +

scriptObject.Import(nameof(GetActionName), new Func<OpenApiOperation, string, string, string>(GetActionName));

25 26

scriptObject.Import(nameof(GetDotNetName), new Func<string, string, string>(GetDotNetName));

26 27

scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), new Func<OpenApiParameter, string, string>(GetDotNetNameOpenApiParameter));

27 28

}

@@ -138,7 +139,7 @@ public string GetDotNetName(string jsonName, string style = "parameter")

138 139

return jsonName.ToCamelCase();

139 140

}

140 141 141 -

public static string GetMethodName(OpenApiOperation watchOperation, string suffix)

142 +

public static string GetOperationId(OpenApiOperation watchOperation, string suffix)

142 143

{

143 144

var tag = watchOperation.Tags[0];

144 145

tag = tag.Replace("_", string.Empty);

@@ -162,5 +163,28 @@ public static string GetMethodName(OpenApiOperation watchOperation, string suffi

162 163 163 164

return methodName;

164 165

}

166 + 167 +

public static string GetActionName(OpenApiOperation apiOperation, string resource, string suffix)

168 +

{

169 +

var operationId = apiOperation.OperationId.ToPascalCase();

170 +

var replacements = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)

171 +

{

172 +

{ "Replace", "Update" },

173 +

{ "Read", "Get" },

174 +

};

175 + 176 +

foreach (var replacement in replacements)

177 +

{

178 +

operationId = Regex.Replace(operationId, replacement.Key, replacement.Value, RegexOptions.IgnoreCase);

179 +

}

180 + 181 +

var resources = new[] { resource, "ForAllNamespaces", "Namespaced" };

182 +

var pattern = string.Join("|", Array.ConvertAll(resources, Regex.Escape));

183 +

var actionName = pattern.Length > 0

184 +

? Regex.Replace(operationId, pattern, string.Empty, RegexOptions.IgnoreCase)

185 +

: operationId;

186 + 187 +

return $"{actionName}{suffix}";

188 +

}

165 189

}

166 190

}

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4