Skip to content

Commit f0dbebb

Browse files
Copilottg123
andcommitted
Address code review feedback: dispose GenericClient, use ternary operator, specific exception handling
Co-authored-by: tg123 <[email protected]>
1 parent 2147fb4 commit f0dbebb

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

src/KubernetesClient.Kubectl/Beta/AsyncKubectl.Get.cs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,10 @@ public async Task<T> GetAsync<T>(string name, string? @namespace = null, Cancell
1414
where T : IKubernetesObject
1515
{
1616
var metadata = typeof(T).GetKubernetesTypeMetadata();
17-
var genericClient = new GenericClient(client, metadata.Group, metadata.ApiVersion, metadata.PluralName, disposeClient: false);
17+
using var genericClient = new GenericClient(client, metadata.Group, metadata.ApiVersion, metadata.PluralName, disposeClient: false);
1818

19-
if (@namespace != null)
20-
{
21-
return await genericClient.ReadNamespacedAsync<T>(@namespace, name, cancellationToken).ConfigureAwait(false);
22-
}
23-
else
24-
{
25-
return await genericClient.ReadAsync<T>(name, cancellationToken).ConfigureAwait(false);
26-
}
19+
return @namespace != null
20+
? await genericClient.ReadNamespacedAsync<T>(@namespace, name, cancellationToken).ConfigureAwait(false)
21+
: await genericClient.ReadAsync<T>(name, cancellationToken).ConfigureAwait(false);
2722
}
2823
}

tests/Kubectl.Tests/KubectlTests.Get.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using k8s.Autorest;
12
using k8s.E2E;
23
using k8s.kubectl.beta;
34
using k8s.Models;
@@ -71,9 +72,9 @@ public void GetPod()
7172
{
7273
kubernetes.CoreV1.DeleteNamespacedPod(podName, namespaceParameter);
7374
}
74-
catch
75+
catch (HttpOperationException)
7576
{
76-
// Ignore cleanup errors
77+
// Ignore cleanup errors if pod was already deleted or doesn't exist
7778
}
7879
}
7980
}
@@ -131,9 +132,9 @@ public void GetService()
131132
{
132133
kubernetes.CoreV1.DeleteNamespacedService(serviceName, namespaceParameter);
133134
}
134-
catch
135+
catch (HttpOperationException)
135136
{
136-
// Ignore cleanup errors
137+
// Ignore cleanup errors if service was already deleted or doesn't exist
137138
}
138139
}
139140
}
@@ -207,9 +208,9 @@ public void GetDeployment()
207208
{
208209
kubernetes.AppsV1.DeleteNamespacedDeployment(deploymentName, namespaceParameter);
209210
}
210-
catch
211+
catch (HttpOperationException)
211212
{
212-
// Ignore cleanup errors
213+
// Ignore cleanup errors if deployment was already deleted or doesn't exist
213214
}
214215
}
215216
}

0 commit comments

Comments
 (0)