-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ad2f5ec
Upgrade to KubeClient v3 and log failed Kubernetes API requests
tintoy fd98410
Don't attempt to deserialise response payload from Kubernetes API if …
tintoy 4b5a6ef
Simplify logging implementation by moving it out of EndPointClientV1…
tintoy 9a8bf9c
Add support for returning error responses from the fake Kubernetes AP…
tintoy 8472f4a
Revert "Add support for returning error responses from the fake Kuber…
tintoy 8a4faa3
Upgrade KubeClient to v3.0.1
tintoy 6c1ac8a
Use consistent serialisation settings for fake Kubernetes API responses
tintoy 6a6c374
Add unit tests for handling of error responses from Kubernetes API
tintoy c853a6f
code review by @raman-m
raman-m 9b4e5cd
Change EndPointClientV1.GetAsync into a non-async wrapper method, sin…
tintoy f083fda
Merge branch 'feature/kubeclient-v3' of https://github.com/tintoy/Oce…
tintoy 71c36e4
Update docs with user's instructions
raman-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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,44 @@ 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 string Message(string details) | ||
| => $"Failed to retrieve {EndPointsKubeKind.ResourceApiVersion}/{EndPointsKubeKind.ResourceKind} '{_configuration.KeyOfServiceInK8s}' in namespace '{_configuration.KubeNamespace}': {details}"; | ||
|
|
||
| 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). |
||
| { | ||
| try | ||
| { | ||
| return await _kubeApi | ||
| .ResourceClient<IEndPointClient>(client => new EndPointClientV1(client)) | ||
| .GetAsync(_configuration.KeyOfServiceInK8s, _configuration.KubeNamespace); | ||
| } | ||
| catch (KubeApiException ex) | ||
| { | ||
| string Msg() | ||
| { | ||
| StatusV1 status = ex.Status; | ||
| string httpStatusCode = "-"; // Unknown | ||
| if (ex.InnerException is HttpRequestException e) | ||
| { | ||
| httpStatusCode = e.StatusCode.ToString(); | ||
| } | ||
|
|
||
| return Message($"(HTTP.{httpStatusCode}/{status.Status}/{status.Reason}): {status.Message}"); | ||
| } | ||
|
|
||
| _logger.LogError(Msg, ex); | ||
| } | ||
| catch (HttpRequestException ex) | ||
| { | ||
| _logger.LogError(() => Message($"({ex.HttpRequestError}/HTTP.{ex.StatusCode})."), ex); | ||
| } | ||
| catch (Exception unexpected) | ||
| { | ||
| _logger.LogError(() => Message($"(an unexpected ex occurred)."), unexpected); | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| private bool CheckErroneousState(EndpointsV1 endpoint) | ||
| => (endpoint?.Subsets?.Count ?? 0) == 0; // null or count is zero | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.