Skip to content

Commit

Permalink
fix: use get instead of list in kubernetes client.
Browse files Browse the repository at this point in the history
this fixes #647
  • Loading branch information
buehler committed Nov 23, 2023
1 parent 8ae11b6 commit bb84f0b
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/KubeOps.KubernetesClient/KubernetesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,53 +104,53 @@ public string GetCurrentNamespace(string downwardApiEnvName = "POD_NAMESPACE")
where TEntity : IKubernetesObject<V1ObjectMeta>
{
var metadata = GetMetadata<TEntity>();
var list = @namespace switch
{
null => await _client.CustomObjects.ListClusterCustomObjectAsync<EntityList<TEntity>>(
metadata.Group ?? string.Empty,
metadata.Version,
metadata.PluralName,
fieldSelector: $"metadata.name={name}"),
_ => await _client.CustomObjects.ListNamespacedCustomObjectAsync<EntityList<TEntity>>(
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<TEntity>(
metadata.Group ?? string.Empty,
metadata.Version,
metadata.PluralName,
name)
: _client.CustomObjects.GetNamespacedCustomObjectAsync<TEntity>(
metadata.Group ?? string.Empty,
metadata.Version,
@namespace,
metadata.PluralName,
name));
}
catch (HttpOperationException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
{
return default;
}
}

/// <inheritdoc />
public TEntity? Get<TEntity>(string name, string? @namespace = null)
where TEntity : IKubernetesObject<V1ObjectMeta>
{
var metadata = GetMetadata<TEntity>();
var list = @namespace switch
{
null => _client.CustomObjects.ListClusterCustomObject<EntityList<TEntity>>(
metadata.Group ?? string.Empty,
metadata.Version,
metadata.PluralName,
fieldSelector: $"metadata.name={name}"),
_ => _client.CustomObjects.ListNamespacedCustomObject<EntityList<TEntity>>(
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<TEntity>(
metadata.Group ?? string.Empty,
metadata.Version,
metadata.PluralName,
name)
: _client.CustomObjects.GetNamespacedCustomObject<TEntity>(
metadata.Group ?? string.Empty,
metadata.Version,
@namespace,
metadata.PluralName,
name);
}
catch (HttpOperationException e) when (e.Response.StatusCode == HttpStatusCode.NotFound)
{
return default;
}
}

/// <inheritdoc />
Expand Down

0 comments on commit bb84f0b

Please sign in to comment.