Skip to content

Commit 3085283

Browse files
committed
Refactor Pod API calls in tests to use singular form for consistency
1 parent 177809d commit 3085283

File tree

7 files changed

+32
-69
lines changed

7 files changed

+32
-69
lines changed

Directory.Packages.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
<PackageVersion Include="YamlDotNet" Version="16.3.0" />
3434
</ItemGroup>
3535
<ItemGroup>
36-
<PackageVersion Include="Humanizer.Core" Version="2.14.1" />
3736
<PackageVersion Include="Autofac" Version="8.2.1" />
3837
<PackageVersion Include="CaseExtensions" Version="1.1.0" />
3938
<PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.13.0" />

examples/clientset/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ private static async Task Main(string[] args)
1313
var client = new Kubernetes(config);
1414

1515
ClientSet clientSet = new ClientSet(client);
16-
var list = await clientSet.CoreV1.Pods.ListAsync("default").ConfigureAwait(false);
16+
var list = await clientSet.CoreV1.Pod.ListAsync("default").ConfigureAwait(false);
1717
foreach (var item in list)
1818
{
1919
System.Console.WriteLine(item.Metadata.Name);
2020
}
2121

22-
var pod = await clientSet.CoreV1.Pods.GetAsync("test","default").ConfigureAwait(false);
22+
var pod = await clientSet.CoreV1.Pod.GetAsync("test","default").ConfigureAwait(false);
2323
System.Console.WriteLine(pod?.Metadata?.Name);
2424
}
2525
}

src/LibKubernetesGenerator/ClientSetGenerator.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using NSwag;
44
using System.Collections.Generic;
55
using System.Linq;
6-
using Humanizer;
76

87
namespace LibKubernetesGenerator
98
{
@@ -71,7 +70,7 @@ public void Generate(OpenApiDocument swagger, IncrementalGeneratorPostInitializa
7170

7271
foreach (var item in apis.GroupBy(x => x.Kind))
7372
{
74-
var kind = item.Key.Pluralize();
73+
var kind = item.Key;
7574
apiGroups[kind] = item.Select(x => x.Api).ToArray();
7675
clients.Add(kind);
7776
}

src/LibKubernetesGenerator/GeneralNameHelper.cs

Lines changed: 15 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public void RegisterHelper(ScriptObject scriptObject)
2222
{
2323
scriptObject.Import(nameof(GetInterfaceName), new Func<JsonSchema, string>(GetInterfaceName));
2424
scriptObject.Import(nameof(GetOperationId), new Func<OpenApiOperation, string, string>(GetOperationId));
25-
scriptObject.Import(nameof(GetActionName), new Func<OpenApiOperationDescription, string, string, string>(GetActionName));
25+
scriptObject.Import(nameof(GetActionName), new Func<OpenApiOperation, string, string, string>(GetActionName));
2626
scriptObject.Import(nameof(GetDotNetName), new Func<string, string, string>(GetDotNetName));
2727
scriptObject.Import(nameof(GetDotNetNameOpenApiParameter), new Func<OpenApiParameter, string, string>(GetDotNetNameOpenApiParameter));
2828
}
@@ -164,59 +164,28 @@ public static string GetOperationId(OpenApiOperation watchOperation, string suff
164164
return methodName;
165165
}
166166

167-
public static string GetActionName(OpenApiOperationDescription apiOperation, string resource, string suffix)
167+
public static string GetActionName(OpenApiOperation apiOperation, string resource, string suffix)
168168
{
169-
var actionType = apiOperation.Operation?.ExtensionData?["x-kubernetes-action"] as string;
170-
171-
if (string.IsNullOrEmpty(actionType))
172-
{
173-
return $"{apiOperation.Method.ToPascalCase()}{suffix}";
174-
}
175-
176-
var resourceNamespace = ParsePathSegmentAfterParameter(apiOperation.Path, "namespace").ToPascalCase();
177-
var resourceName = ParsePathSegmentAfterParameter(apiOperation.Path, "name").ToPascalCase();
178-
var actionMappings = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
169+
var operationId = apiOperation.OperationId.ToPascalCase();
170+
var replacements = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
179171
{
180-
{ "get", "Get" },
181-
{ "list", "List" },
182-
{ "put", "Put" },
183-
{ "patch", "Patch" },
184-
{ "post", "Post" },
185-
{ "delete", "Delete" },
186-
{ "deletecollection", "DeleteCollection" },
187-
{ "watch", "Watch" },
188-
{ "watchlist", "WatchList" },
189-
{ "proxy", "Proxy" },
172+
{ "Replace", "Put" },
173+
{ "Read", "Get" },
174+
{ "Create", "Post" },
190175
};
191176

192-
if (actionMappings.TryGetValue(actionType, out var actionPrefix))
193-
{
194-
return Regex.Replace($"{actionPrefix}{resourceNamespace}{resourceName}{suffix}", resource, string.Empty,
195-
RegexOptions.IgnoreCase);
196-
}
197-
198-
if (string.Equals("connect", actionType, StringComparison.OrdinalIgnoreCase))
177+
foreach (var replacement in replacements)
199178
{
200-
return Regex.Replace($"Connect{apiOperation.Method}{resourceNamespace}{resourceName}{suffix}", resource,
201-
string.Empty,
202-
RegexOptions.IgnoreCase);
179+
operationId = Regex.Replace(operationId, replacement.Key, replacement.Value, RegexOptions.IgnoreCase);
203180
}
204181

205-
return $"{actionType.ToPascalCase()}{suffix}";
206-
}
207-
208-
private static string ParsePathSegmentAfterParameter(string path, string variableName = "namespace")
209-
{
210-
var pattern = $@"/\{{{variableName}\}}/([^/]+)/?";
211-
212-
var match = Regex.Match(path, pattern);
213-
214-
if (match.Success && match.Groups.Count > 1)
215-
{
216-
return match.Groups[1].Value;
217-
}
182+
var resources = new[] { resource, "ForAllNamespaces", "Namespaced" };
183+
var pattern = string.Join("|", Array.ConvertAll(resources, Regex.Escape));
184+
var actionName = pattern.Length > 0
185+
? Regex.Replace(operationId, pattern, string.Empty, RegexOptions.IgnoreCase)
186+
: operationId;
218187

219-
return string.Empty;
188+
return $"{actionName}{suffix}";
220189
}
221190
}
222191
}

src/LibKubernetesGenerator/LibKubernetesGenerator.target

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,6 @@
2323
<!-- CaseExtensions No Dependency -->
2424
<PackageReference Include="CaseExtensions" GeneratePathProperty="true" PrivateAssets="all" />
2525

26-
<!-- Humanizer -->
27-
<PackageReference Include="Humanizer.Core" GeneratePathProperty="true" PrivateAssets="all"/>
28-
2926
<!-- Autofac -->
3027
<PackageReference Include="Autofac" GeneratePathProperty="true" PrivateAssets="all" />
3128
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" GeneratePathProperty="true" PrivateAssets="all" />
@@ -47,7 +44,6 @@
4744
<Target Name="GetDependencyTargetPaths">
4845
<ItemGroup>
4946
<TargetPathWithTargetPlatformMoniker Include="$(PKGCaseExtensions)\lib\netstandard2.0\CaseExtensions.dll" IncludeRuntimeDependency="false" />
50-
<TargetPathWithTargetPlatformMoniker Include="$(PKGHumanizer_Core)\lib\netstandard2.0\Humanizer.dll" IncludeRuntimeDependency="false" />
5147

5248
<TargetPathWithTargetPlatformMoniker Include="$(PKGAutofac)\lib\netstandard2.0\Autofac.dll" IncludeRuntimeDependency="false" />
5349
<TargetPathWithTargetPlatformMoniker Include="$(PKGMicrosoft_Bcl_AsyncInterfaces)\lib\netstandard2.0\Microsoft.Bcl.AsyncInterfaces.dll" IncludeRuntimeDependency="false" />

src/LibKubernetesGenerator/templates/Client.cs.template

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public partial class {{name}}Client : ResourceClient
2828
/// <param name="cancellationToken">
2929
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
3030
/// </param>
31-
public async Task{{GetReturnType api.operation "<>"}} {{GetActionName api name "Async"}}(
31+
public async Task{{GetReturnType api.operation "<>"}} {{GetActionName api.operation name "Async"}}(
3232
{{ for parameter in api.operation.parameters}}
3333
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "true"}},
3434
{{ end }}
@@ -79,7 +79,7 @@ public partial class {{name}}Client : ResourceClient
7979
/// <param name="cancellationToken">
8080
/// A <see cref="CancellationToken"/> which can be used to cancel the asynchronous operation.
8181
/// </param>
82-
public async Task<T> {{GetActionName api name "Async"}}<T>(
82+
public async Task<T> {{GetActionName api.operation name "Async"}}<T>(
8383
{{ for parameter in api.operation.parameters}}
8484
{{GetDotNetTypeOpenApiParameter parameter}} {{GetDotNetNameOpenApiParameter parameter "false"}},
8585
{{ end }}

tests/E2E.Tests/MinikubeTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,12 @@ public async Task ClientSetTest()
596596
var clientSet = new ClientSet((Kubernetes)kubernetes);
597597
async Task Cleanup()
598598
{
599-
var pods = await clientSet.CoreV1.Pods.ListAsync(namespaceParameter).ConfigureAwait(false);
599+
var pods = await clientSet.CoreV1.Pod.ListAsync(namespaceParameter).ConfigureAwait(false);
600600
while (pods.Items.Any(p => p.Metadata.Name == podName))
601601
{
602602
try
603603
{
604-
await clientSet.CoreV1.Pods.DeleteAsync(podName, namespaceParameter).ConfigureAwait(false);
604+
await clientSet.CoreV1.Pod.DeleteAsync(podName, namespaceParameter).ConfigureAwait(false);
605605
}
606606
catch (HttpOperationException e)
607607
{
@@ -619,7 +619,7 @@ async Task Cleanup()
619619

620620
// create + list
621621
{
622-
await clientSet.CoreV1.Pods.PostAsync(
622+
await clientSet.CoreV1.Pod.PostAsync(
623623
new V1Pod()
624624
{
625625
Metadata = new V1ObjectMeta { Name = podName, Labels = new Dictionary<string, string> { { "place", "holder" }, }, },
@@ -630,13 +630,13 @@ await clientSet.CoreV1.Pods.PostAsync(
630630
},
631631
namespaceParameter).ConfigureAwait(false);
632632

633-
var pods = await clientSet.CoreV1.Pods.ListAsync(namespaceParameter).ConfigureAwait(false);
633+
var pods = await clientSet.CoreV1.Pod.ListAsync(namespaceParameter).ConfigureAwait(false);
634634
Assert.Contains(pods.Items, p => p.Metadata.Name == podName);
635635
}
636636

637637
// replace + get
638638
{
639-
var pod = await clientSet.CoreV1.Pods.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
639+
var pod = await clientSet.CoreV1.Pod.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
640640
var old = JsonSerializer.SerializeToDocument(pod);
641641

642642
var newLabels = new Dictionary<string, string>(pod.Metadata.Labels) { ["test"] = "clientset-test-jsonpatch" };
@@ -645,20 +645,20 @@ await clientSet.CoreV1.Pods.PostAsync(
645645
var expected = JsonSerializer.SerializeToDocument(pod);
646646
var patch = old.CreatePatch(expected);
647647

648-
await clientSet.CoreV1.Pods
648+
await clientSet.CoreV1.Pod
649649
.PatchAsync(new V1Patch(patch, V1Patch.PatchType.JsonPatch), podName, namespaceParameter)
650650
.ConfigureAwait(false);
651-
var pods = await clientSet.CoreV1.Pods.ListAsync(namespaceParameter).ConfigureAwait(false);
651+
var pods = await clientSet.CoreV1.Pod.ListAsync(namespaceParameter).ConfigureAwait(false);
652652
Assert.Contains(pods.Items, p => p.Labels().Contains(new KeyValuePair<string, string>("test", "clientset-test-jsonpatch")));
653653
}
654654

655655
// replace + get
656656
{
657-
var pod = await clientSet.CoreV1.Pods.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
657+
var pod = await clientSet.CoreV1.Pod.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
658658
pod.Spec.Containers[0].Image = "httpd";
659-
await clientSet.CoreV1.Pods.PutAsync(pod, podName, namespaceParameter).ConfigureAwait(false);
659+
await clientSet.CoreV1.Pod.PutAsync(pod, podName, namespaceParameter).ConfigureAwait(false);
660660

661-
pod = await clientSet.CoreV1.Pods.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
661+
pod = await clientSet.CoreV1.Pod.GetAsync(podName, namespaceParameter).ConfigureAwait(false);
662662
Assert.Equal("httpd", pod.Spec.Containers[0].Image);
663663
}
664664

@@ -670,7 +670,7 @@ await clientSet.CoreV1.Pods
670670
{
671671
try
672672
{
673-
await clientSet.CoreV1.Pods.DeleteAsync(podName, namespaceParameter).ConfigureAwait(false);
673+
await clientSet.CoreV1.Pod.DeleteAsync(podName, namespaceParameter).ConfigureAwait(false);
674674
}
675675
catch (HttpOperationException e)
676676
{
@@ -680,7 +680,7 @@ await clientSet.CoreV1.Pods
680680
}
681681
}
682682

683-
pods = await clientSet.CoreV1.Pods.ListAsync(namespaceParameter).ConfigureAwait(false);
683+
pods = await clientSet.CoreV1.Pod.ListAsync(namespaceParameter).ConfigureAwait(false);
684684
if (pods.Items.All(p => p.Metadata.Name != podName))
685685
{
686686
break;

0 commit comments

Comments
 (0)