diff --git a/src/KubeOps.KubernetesClient/KubernetesClient.cs b/src/KubeOps.KubernetesClient/KubernetesClient.cs index 39944e60..cbdc442e 100644 --- a/src/KubeOps.KubernetesClient/KubernetesClient.cs +++ b/src/KubeOps.KubernetesClient/KubernetesClient.cs @@ -104,26 +104,26 @@ public string GetCurrentNamespace(string downwardApiEnvName = "POD_NAMESPACE") where TEntity : IKubernetesObject { var metadata = GetMetadata(); - var list = @namespace switch - { - null => await _client.CustomObjects.ListClusterCustomObjectAsync>( - metadata.Group ?? string.Empty, - metadata.Version, - metadata.PluralName, - fieldSelector: $"metadata.name={name}"), - _ => await _client.CustomObjects.ListNamespacedCustomObjectAsync>( - metadata.Group ?? string.Empty, - metadata.Version, - @namespace, - metadata.PluralName, - fieldSelector: $"metadata.name={name}"), - }; - return list switch + try { - { Items: [var existing] } => existing, - _ => default, - }; + return await (string.IsNullOrWhiteSpace(@namespace) + ? _client.CustomObjects.GetClusterCustomObjectAsync( + metadata.Group ?? string.Empty, + metadata.Version, + metadata.PluralName, + name) + : _client.CustomObjects.GetNamespacedCustomObjectAsync( + metadata.Group ?? string.Empty, + metadata.Version, + @namespace, + metadata.PluralName, + name)); + } + catch (HttpOperationException e) when (e.Response.StatusCode == HttpStatusCode.NotFound) + { + return default; + } } /// @@ -131,26 +131,26 @@ public string GetCurrentNamespace(string downwardApiEnvName = "POD_NAMESPACE") where TEntity : IKubernetesObject { var metadata = GetMetadata(); - var list = @namespace switch - { - null => _client.CustomObjects.ListClusterCustomObject>( - metadata.Group ?? string.Empty, - metadata.Version, - metadata.PluralName, - fieldSelector: $"metadata.name={name}"), - _ => _client.CustomObjects.ListNamespacedCustomObject>( - metadata.Group ?? string.Empty, - metadata.Version, - @namespace, - metadata.PluralName, - fieldSelector: $"metadata.name={name}"), - }; - return list switch + try { - { Items: [var existing] } => existing, - _ => default, - }; + return string.IsNullOrWhiteSpace(@namespace) + ? _client.CustomObjects.GetClusterCustomObject( + metadata.Group ?? string.Empty, + metadata.Version, + metadata.PluralName, + name) + : _client.CustomObjects.GetNamespacedCustomObject( + metadata.Group ?? string.Empty, + metadata.Version, + @namespace, + metadata.PluralName, + name); + } + catch (HttpOperationException e) when (e.Response.StatusCode == HttpStatusCode.NotFound) + { + return default; + } } ///