-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Upgrade to KubeClient v3 and log failed Kubernetes API requests #2266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ad2f5ec
fd98410
4b5a6ef
9a8bf9c
8472f4a
8a4faa3
6c1ac8a
6a6c374
c853a6f
9b4e5cd
f083fda
71c36e4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,6 +15,8 @@ namespace Ocelot.Provider.Kubernetes; | |
| /// </remarks> | ||
| public class Kube : IServiceDiscoveryProvider | ||
| { | ||
| private static readonly (string ResourceKind, string ResourceApiVersion) EndPointsKubeKind = KubeObjectV1.GetKubeKind<EndpointsV1>(); | ||
|
|
||
| private readonly KubeRegistryConfiguration _configuration; | ||
| private readonly IOcelotLogger _logger; | ||
| private readonly IKubeApiClient _kubeApi; | ||
|
|
@@ -46,9 +48,42 @@ public virtual async Task<List<Service>> GetAsync() | |
| .ToList(); | ||
| } | ||
|
|
||
| private Task<EndpointsV1> GetEndpoint() => _kubeApi | ||
| .ResourceClient<IEndPointClient>(client => new EndPointClientV1(client)) | ||
| .GetAsync(_configuration.KeyOfServiceInK8s, _configuration.KubeNamespace); | ||
| private async Task<EndpointsV1> GetEndpoint() | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've moved this logic here because it keeps the resource-client implementation cleaner (since it wouldn't otherwise be clear to someone who's not seen this code before what the lifetime of the resource client is, vs the lifetime of the containing type or its logger). |
||
| { | ||
| string serviceName = _configuration.KeyOfServiceInK8s; | ||
| string kubeNamespace = _configuration.KubeNamespace; | ||
|
|
||
| try | ||
| { | ||
| return await _kubeApi | ||
| .ResourceClient<IEndPointClient>(client => new EndPointClientV1(client)) | ||
| .GetAsync(serviceName, kubeNamespace); | ||
| } | ||
| catch (KubeApiException kubeApiError) | ||
| { | ||
| _logger.LogError(() => | ||
| { | ||
| StatusV1 errorResponse = kubeApiError.Status; | ||
| string httpStatusCode = "Unknown"; | ||
raman-m marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (kubeApiError.InnerException is HttpRequestException httpRequestError) | ||
| { | ||
| httpStatusCode = httpRequestError.StatusCode.ToString(); | ||
| } | ||
|
|
||
| return $"Failed to retrieve {EndPointsKubeKind.ResourceApiVersion}/{EndPointsKubeKind.ResourceKind} {serviceName} in namespace {kubeNamespace} ({httpStatusCode}/{errorResponse.Status}/{errorResponse.Reason}): {errorResponse.Message}"; | ||
| }, kubeApiError); | ||
| } | ||
| catch (HttpRequestException unexpectedRequestError) | ||
| { | ||
| _logger.LogError(() => $"Failed to retrieve {EndPointsKubeKind.ResourceApiVersion}/{EndPointsKubeKind.ResourceKind} {serviceName} in namespace {kubeNamespace} ({unexpectedRequestError.HttpRequestError}/{unexpectedRequestError.StatusCode}).", unexpectedRequestError); | ||
| } | ||
| catch (Exception unexpectedError) | ||
| { | ||
| _logger.LogError(() => $"Failed to retrieve {EndPointsKubeKind.ResourceApiVersion}/{EndPointsKubeKind.ResourceKind} {serviceName} in namespace {kubeNamespace} (an unexpected error occurred).", unexpectedError); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private bool CheckErroneousState(EndpointsV1 endpoint) | ||
| => (endpoint?.Subsets?.Count ?? 0) == 0; // null or count is zero | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,8 +25,8 @@ | |
| <NoWarn>1591</NoWarn> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <PackageReference Include="KubeClient" Version="2.5.12" /> | ||
| <PackageReference Include="KubeClient.Extensions.DependencyInjection" Version="2.5.12" /> | ||
| <PackageReference Include="KubeClient" Version="3.0.0" /> | ||
|
||
| <PackageReference Include="KubeClient.Extensions.DependencyInjection" Version="3.0.0" /> | ||
| <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| </PackageReference> | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.