Skip to content

Commit 3702fd6

Browse files
authored
Standardization of using order and object initialization (#1028)
* Code cleanup KubernetesClient * KubernetesClient.Basic code cleanup * KubernetesClient.Models cleanup * LibKubernetesGenerator code cleanup * Improved readability of object initialization * FIx namespace order * Fixed some compilation warning
1 parent bbd3b6c commit 3702fd6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+200
-164
lines changed

examples/namespace/NamespaceExample.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ private static async Task DeleteAsync(IKubernetes client, string name, int delay
3535
{
3636
foreach (var innerEx in ex.InnerExceptions)
3737
{
38-
if (innerEx is k8s.Autorest.HttpOperationException)
38+
if (innerEx is k8s.Autorest.HttpOperationException exception)
3939
{
40-
var code = ((k8s.Autorest.HttpOperationException)innerEx).Response.StatusCode;
40+
var code = exception.Response.StatusCode;
4141
if (code == HttpStatusCode.NotFound)
4242
{
4343
return;

examples/yaml/Program.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ internal class Program
1010
{
1111
private static async Task Main(string[] args)
1212
{
13-
var typeMap = new Dictionary<String, Type>();
14-
typeMap.Add("v1/Pod", typeof(V1Pod));
15-
typeMap.Add("v1/Service", typeof(V1Service));
16-
typeMap.Add("apps/v1/Deployment", typeof(V1Deployment));
13+
var typeMap = new Dictionary<String, Type>
14+
{
15+
{ "v1/Pod", typeof(V1Pod) },
16+
{ "v1/Service", typeof(V1Service) },
17+
{ "apps/v1/Deployment", typeof(V1Deployment) }
18+
};
1719

1820
var objects = await KubernetesYaml.LoadAllFromFileAsync(args[0], typeMap);
1921

src/KubernetesClient.Basic/AbstractKubernetes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ private static class HttpMethods
1919

2020
private sealed class QueryBuilder
2121
{
22-
private List<string> parameters = new List<string>();
22+
private readonly List<string> parameters = new List<string>();
2323

2424
public void Append(string key, params object[] values)
2525
{

src/KubernetesClient.Basic/Autorest/HttpRequestMessageWrapper.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ public HttpRequestMessageWrapper(HttpRequestMessage httpRequest, string content)
3030
this.Content = content;
3131
this.Method = httpRequest.Method;
3232
this.RequestUri = httpRequest.RequestUri;
33-
#pragma warning disable CS0618 // Type or member is obsolete
3433
if (httpRequest.Properties != null)
3534
{
3635
Properties = new Dictionary<string, object>();
3736
foreach (KeyValuePair<string, object> pair in httpRequest.Properties)
38-
#pragma warning restore CS0618 // Type or member is obsolete
3937
{
4038
this.Properties[pair.Key] = pair.Value;
4139
}

src/KubernetesClient.Basic/Autorest/TokenCredentials.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public TokenCredentials(ITokenProvider tokenProvider, string tenantId, string ca
9898
/// <returns>
9999
/// Task that will complete when processing has completed.
100100
/// </returns>
101-
public async override Task ProcessHttpRequestAsync(
101+
public override async Task ProcessHttpRequestAsync(
102102
HttpRequestMessage request,
103103
CancellationToken cancellationToken)
104104
{

src/KubernetesClient.Basic/Global.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
global using System;
2-
global using System.Collections.Generic;
3-
global using System.Linq;
41
global using k8s.Autorest;
52
global using k8s.Models;
3+
global using System;
4+
global using System.Collections.Generic;
65
global using System.IO;
6+
global using System.Linq;
77
global using System.Threading;
88
global using System.Threading.Tasks;

src/KubernetesClient.Models/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
using k8s.Models;
12
using System.Reflection;
23
using System.Text.RegularExpressions;
3-
using k8s.Models;
44

55
namespace k8s
66
{

src/KubernetesClient.Models/IItems.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public static class ItemsExt
1717
{
1818
public static IEnumerator<T> GetEnumerator<T>(this IItems<T> items)
1919
{
20+
if (items is null)
21+
{
22+
throw new ArgumentNullException(nameof(items));
23+
}
24+
2025
return items.Items.GetEnumerator();
2126
}
2227
}

src/KubernetesClient.Models/KubeConfigModels/ExecCredentialResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class ExecStatus
1313

1414
public bool IsValid()
1515
{
16-
return (!string.IsNullOrEmpty(Token) ||
17-
(!string.IsNullOrEmpty(ClientCertificateData) && !string.IsNullOrEmpty(ClientKeyData)));
16+
return !string.IsNullOrEmpty(Token) ||
17+
(!string.IsNullOrEmpty(ClientCertificateData) && !string.IsNullOrEmpty(ClientKeyData));
1818
}
1919
}
2020

src/KubernetesClient.Models/KubernetesYaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using k8s.Models;
12
using System.IO;
23
using System.Reflection;
34
using System.Text;
@@ -6,7 +7,6 @@
67
using YamlDotNet.Core.Events;
78
using YamlDotNet.Serialization;
89
using YamlDotNet.Serialization.NamingConventions;
9-
using k8s.Models;
1010

1111
namespace k8s
1212
{

src/KubernetesClient.Models/ResourceQuantity.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
using Fractions;
12
using System.Globalization;
23
using System.Numerics;
3-
using Fractions;
44

55
namespace k8s.Models
66
{

src/KubernetesClient.Models/Versioning/VersionConverter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// WARNING: DO NOT LEAVE COMMENTED CODE IN THIS FILE. IT GETS SCANNED BY GEN PROJECT SO IT CAN EXCLUDE ANY MANUALLY DEFINED MAPS
22

3-
using System.Reflection;
43
using AutoMapper;
54
using k8s.Models;
5+
using System.Reflection;
66

77
namespace k8s.Versioning
88
{

src/KubernetesClient/Authentication/ExecTokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
using k8s.Autorest;
2+
using k8s.KubeConfigModels;
13
using System.Net.Http.Headers;
24
using System.Threading;
35
using System.Threading.Tasks;
4-
using k8s.KubeConfigModels;
5-
using k8s.Autorest;
66

77
namespace k8s.Authentication
88
{

src/KubernetesClient/Authentication/GcpTokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
using k8s.Autorest;
2+
using k8s.Exceptions;
13
using System.Diagnostics;
24
using System.Net.Http.Headers;
35
using System.Threading;
46
using System.Threading.Tasks;
5-
using k8s.Exceptions;
6-
using k8s.Autorest;
77

88
namespace k8s.Authentication
99
{

src/KubernetesClient/Authentication/OidcTokenProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using IdentityModel.OidcClient;
2-
using k8s.Exceptions;
32
using k8s.Autorest;
3+
using k8s.Exceptions;
44
using System.IdentityModel.Tokens.Jwt;
55
using System.Net.Http.Headers;
66
using System.Threading;
@@ -10,7 +10,7 @@ namespace k8s.Authentication
1010
{
1111
public class OidcTokenProvider : ITokenProvider
1212
{
13-
private OidcClient _oidcClient;
13+
private readonly OidcClient _oidcClient;
1414
private string _idToken;
1515
private string _refreshToken;
1616
private DateTimeOffset _expiry;

src/KubernetesClient/Authentication/TokenFileAuth.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
using System.Net.Http.Headers;
1+
using k8s.Autorest;
22
using System.IO;
3+
using System.Net.Http.Headers;
34
using System.Threading;
45
using System.Threading.Tasks;
5-
using k8s.Autorest;
66

77
namespace k8s.Authentication
88
{

src/KubernetesClient/CertUtils.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,7 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
156156
using (var reader = new StreamReader(new MemoryStream(keyData)))
157157
{
158158
obj = new PemReader(reader).ReadObject();
159-
var key = obj as AsymmetricCipherKeyPair;
160-
if (key != null)
159+
if (obj is AsymmetricCipherKeyPair key)
161160
{
162161
var cipherKey = key;
163162
obj = cipherKey.Private;
@@ -169,18 +168,17 @@ public static X509Certificate2 GeneratePfx(KubernetesClientConfiguration config)
169168
var store = new Pkcs12StoreBuilder().Build();
170169
store.SetKeyEntry("K8SKEY", new AsymmetricKeyEntry(keyParams), new[] { new X509CertificateEntry(cert) });
171170

172-
using (var pkcs = new MemoryStream())
173-
{
174-
store.Save(pkcs, new char[0], new SecureRandom());
171+
using var pkcs = new MemoryStream();
175172

176-
if (config.ClientCertificateKeyStoreFlags.HasValue)
177-
{
178-
return new X509Certificate2(pkcs.ToArray(), "", config.ClientCertificateKeyStoreFlags.Value);
179-
}
180-
else
181-
{
182-
return new X509Certificate2(pkcs.ToArray());
183-
}
173+
store.Save(pkcs, new char[0], new SecureRandom());
174+
175+
if (config.ClientCertificateKeyStoreFlags.HasValue)
176+
{
177+
return new X509Certificate2(pkcs.ToArray(), "", config.ClientCertificateKeyStoreFlags.Value);
178+
}
179+
else
180+
{
181+
return new X509Certificate2(pkcs.ToArray());
184182
}
185183
#endif
186184
}

src/KubernetesClient/Kubernetes.ConfigInit.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
using k8s.Autorest;
2+
using k8s.Exceptions;
13
using System.Net.Http;
24
using System.Net.Security;
35
using System.Security.Cryptography.X509Certificates;
46
using System.Threading;
5-
using k8s.Exceptions;
6-
using k8s.Autorest;
77

88
namespace k8s
99
{

src/KubernetesClient/Kubernetes.Exec.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
using k8s.Models;
21
using k8s.Autorest;
2+
using k8s.Models;
33
using System.IO;
44
using System.Threading;
55
using System.Threading.Tasks;
@@ -19,31 +19,31 @@ public async Task<int> NamespacedPodExecAsync(string name, string @namespace, st
1919

2020
try
2121
{
22-
using (var muxedStream = await MuxedStreamNamespacedPodExecAsync(
22+
using var muxedStream = await MuxedStreamNamespacedPodExecAsync(
2323
name,
2424
@namespace, command, container, tty: tty,
25-
cancellationToken: cancellationToken).ConfigureAwait(false))
26-
using (var stdIn = muxedStream.GetStream(null, ChannelIndex.StdIn))
27-
using (var stdOut = muxedStream.GetStream(ChannelIndex.StdOut, null))
28-
using (var stdErr = muxedStream.GetStream(ChannelIndex.StdErr, null))
29-
using (var error = muxedStream.GetStream(ChannelIndex.Error, null))
30-
using (var errorReader = new StreamReader(error))
31-
{
32-
muxedStream.Start();
25+
cancellationToken: cancellationToken).ConfigureAwait(false);
3326

34-
await action(stdIn, stdOut, stdErr).ConfigureAwait(false);
27+
using var stdIn = muxedStream.GetStream(null, ChannelIndex.StdIn);
28+
using var stdOut = muxedStream.GetStream(ChannelIndex.StdOut, null);
29+
using var stdErr = muxedStream.GetStream(ChannelIndex.StdErr, null);
30+
using var error = muxedStream.GetStream(ChannelIndex.Error, null);
31+
using var errorReader = new StreamReader(error);
3532

36-
var errors = await errorReader.ReadToEndAsync().ConfigureAwait(false);
33+
muxedStream.Start();
3734

38-
// StatusError is defined here:
39-
// https://github.com/kubernetes/kubernetes/blob/068e1642f63a1a8c48c16c18510e8854a4f4e7c5/staging/src/k8s.io/apimachinery/pkg/api/errors/errors.go#L37
40-
var returnMessage = KubernetesJson.Deserialize<V1Status>(errors);
41-
return GetExitCodeOrThrow(returnMessage);
42-
}
35+
await action(stdIn, stdOut, stdErr).ConfigureAwait(false);
36+
37+
var errors = await errorReader.ReadToEndAsync().ConfigureAwait(false);
38+
39+
// StatusError is defined here:
40+
// https://github.com/kubernetes/kubernetes/blob/068e1642f63a1a8c48c16c18510e8854a4f4e7c5/staging/src/k8s.io/apimachinery/pkg/api/errors/errors.go#L37
41+
var returnMessage = KubernetesJson.Deserialize<V1Status>(errors);
42+
return GetExitCodeOrThrow(returnMessage);
4343
}
44-
catch (HttpOperationException httpEx) when (httpEx.Body is V1Status)
44+
catch (HttpOperationException httpEx) when (httpEx.Body is V1Status status)
4545
{
46-
throw new KubernetesException((V1Status)httpEx.Body, httpEx);
46+
throw new KubernetesException(status, httpEx);
4747
}
4848
}
4949

src/KubernetesClient/Kubernetes.WebSocket.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using k8s.Models;
21
using k8s.Autorest;
2+
using k8s.Models;
3+
using System.Globalization;
34
using System.Net;
45
using System.Net.Http;
56
using System.Net.WebSockets;
67
using System.Security.Cryptography.X509Certificates;
8+
using System.Text;
79
using System.Threading;
810
using System.Threading.Tasks;
9-
using System.Text;
10-
using System.Globalization;
1111

1212
namespace k8s
1313
{
@@ -84,8 +84,10 @@ public virtual Task<WebSocket> WebSocketNamespacedPodExecAsync(string name, stri
8484
}
8585

8686
// Construct URL
87-
var uriBuilder = new UriBuilder(BaseUri);
88-
uriBuilder.Scheme = BaseUri.Scheme == "https" ? "wss" : "ws";
87+
var uriBuilder = new UriBuilder(BaseUri)
88+
{
89+
Scheme = BaseUri.Scheme == "https" ? "wss" : "ws",
90+
};
8991

9092
if (!uriBuilder.Path.EndsWith("/", StringComparison.InvariantCulture))
9193
{
@@ -143,8 +145,10 @@ public Task<WebSocket> WebSocketNamespacedPodPortForwardAsync(string name, strin
143145

144146

145147
// Construct URL
146-
var uriBuilder = new UriBuilder(BaseUri);
147-
uriBuilder.Scheme = BaseUri.Scheme == "https" ? "wss" : "ws";
148+
var uriBuilder = new UriBuilder(BaseUri)
149+
{
150+
Scheme = BaseUri.Scheme == "https" ? "wss" : "ws",
151+
};
148152

149153
if (!uriBuilder.Path.EndsWith("/", StringComparison.InvariantCulture))
150154
{
@@ -187,8 +191,10 @@ public Task<WebSocket> WebSocketNamespacedPodAttachAsync(string name, string @na
187191
}
188192

189193
// Construct URL
190-
var uriBuilder = new UriBuilder(BaseUri);
191-
uriBuilder.Scheme = BaseUri.Scheme == "https" ? "wss" : "ws";
194+
var uriBuilder = new UriBuilder(BaseUri)
195+
{
196+
Scheme = BaseUri.Scheme == "https" ? "wss" : "ws",
197+
};
192198

193199
if (!uriBuilder.Path.EndsWith("/", StringComparison.InvariantCulture))
194200
{
@@ -294,8 +300,10 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
294300
{
295301
// This usually indicates the server sent an error message, like 400 Bad Request. Unfortunately, the WebSocket client
296302
// class doesn't give us a lot of information about what went wrong. So, retry the connection.
297-
var uriBuilder = new UriBuilder(uri);
298-
uriBuilder.Scheme = uri.Scheme == "wss" ? "https" : "http";
303+
var uriBuilder = new UriBuilder(uri)
304+
{
305+
Scheme = uri.Scheme == "wss" ? "https" : "http",
306+
};
299307

300308
var response = await HttpClient.GetAsync(uriBuilder.Uri, cancellationToken).ConfigureAwait(false);
301309

@@ -327,7 +335,7 @@ protected async Task<WebSocket> StreamConnectAsync(Uri uri, string webSocketSubP
327335
$"The operation returned an invalid status code: {response.StatusCode}", wse)
328336
{
329337
Response = new HttpResponseMessageWrapper(response, content),
330-
Body = status != null ? (object)status : content,
338+
Body = status != null ? status : content,
331339
};
332340

333341
response.Dispose();

src/KubernetesClient/Kubernetes.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
using k8s.Autorest;
12
using System.IO;
23
using System.Net;
34
using System.Net.Http;
45
using System.Threading;
56
using System.Threading.Tasks;
6-
using k8s.Autorest;
77

88
namespace k8s
99
{
@@ -101,9 +101,12 @@ protected override async Task<HttpOperationResponse<T>> CreateResultAsync<T>(Htt
101101

102102
protected override HttpRequestMessage CreateRequest(string relativeUri, HttpMethod method, IReadOnlyDictionary<string, IReadOnlyList<string>> customHeaders)
103103
{
104-
var httpRequest = new HttpRequestMessage();
105-
httpRequest.Method = method;
106-
httpRequest.RequestUri = new Uri(BaseUri, relativeUri);
104+
var httpRequest = new HttpRequestMessage
105+
{
106+
Method = method,
107+
RequestUri = new Uri(BaseUri, relativeUri),
108+
};
109+
107110
#if NETSTANDARD2_1_OR_GREATER || NET5_0_OR_GREATER
108111
httpRequest.Version = HttpVersion.Version20;
109112
#endif

0 commit comments

Comments
 (0)