-
Notifications
You must be signed in to change notification settings - Fork 305
Add a fluent Kubernetes API #406
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
Closed
Closed
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f3dfef6
Add useful model extensions
admilazz aa07378
Rename a function
admilazz 6602a4c
Add more model extensions; add unit tests
admilazz 58e135d
Add a couple more extensions requested in PR
admilazz b200147
Add a fluent Kubernetes API
admilazz 861c341
Fix and add unit tests
admilazz 15a0884
Actually add the tests...
admilazz 575eddf
Merge from modelExtensions
admilazz e220d97
Address some PR comments
admilazz 05d4034
Reformat
admilazz fa8aaa8
Move to Kubernetes.Fluent namespace
admilazz 6e265bc
Git add the test file too
admilazz cf00214
Simplify ReplaceAsync
admilazz 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Net.Http; | ||
using k8s.Models; | ||
|
||
namespace k8s.Fluent | ||
{ | ||
public static class KubernetesFluent | ||
{ | ||
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/> and | ||
/// <see cref="IKubernetesObject.Kind"/>. | ||
/// </summary> | ||
public static T New<T>(this Kubernetes client) where T : IKubernetesObject, new() => client.Scheme.New<T>(); | ||
|
||
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>, | ||
/// <see cref="IKubernetesObject.Kind"/>, and <see cref="V1ObjectMeta.Name"/>. | ||
/// </summary> | ||
public static T New<T>(this Kubernetes client, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => client.Scheme.New<T>(name); | ||
|
||
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>, | ||
/// <see cref="IKubernetesObject.Kind"/>, <see cref="V1ObjectMeta.Namespace"/>, and <see cref="V1ObjectMeta.Name"/>. | ||
/// </summary> | ||
public static T New<T>(this Kubernetes client, string ns, string name) where T : IKubernetesObject<V1ObjectMeta>, new() => client.Scheme.New<T>(ns, name); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/> | ||
/// (<see cref="HttpMethod.Get"/> by default). | ||
/// </summary> | ||
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method = null) => new KubernetesRequest(client).Method(method); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/> | ||
/// and resource URI components. | ||
/// </summary> | ||
public static KubernetesRequest Request(this Kubernetes client, | ||
HttpMethod method, string type = null, string ns = null, string name = null, string group = null, string version = null) => | ||
new KubernetesRequest(client).Method(method).Group(group).Version(version).Type(type).Namespace(ns).Name(name); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object.</summary> | ||
public static KubernetesRequest Request(this Kubernetes client, Type type) => new KubernetesRequest(client).GVK(type); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary> | ||
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method, Type type, string ns = null, string name = null) => | ||
Request(client, method).GVK(type).Namespace(ns).Name(name); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary> | ||
public static KubernetesRequest Request<T>(this Kubernetes client, string ns = null, string name = null) => Request(client, null, typeof(T), ns, name); | ||
|
||
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given object.</summary> | ||
public static KubernetesRequest Request(this Kubernetes client, IKubernetesObject obj, bool setBody = true) => new KubernetesRequest(client).Set(obj, setBody); | ||
} | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using k8s.Models; | ||
using Newtonsoft.Json; | ||
|
||
namespace k8s.Fluent | ||
{ | ||
/// <summary>Represents a response to a <see cref="KubernetesRequest"/>.</summary> | ||
public sealed class KubernetesResponse : IDisposable | ||
{ | ||
/// <summary>Initializes a new <see cref="KubernetesResponse"/> from an <see cref="HttpResponseMessage"/>.</summary> | ||
public KubernetesResponse(HttpResponseMessage message) => Message = message ?? throw new ArgumentNullException(nameof(message)); | ||
|
||
/// <summary>Indicates whether the server returned an error response.</summary> | ||
public bool IsError => (int)StatusCode >= 400; | ||
|
||
/// <summary>Indicates whether the server returned a 404 Not Found response.</summary> | ||
public bool IsNotFound => StatusCode == HttpStatusCode.NotFound; | ||
|
||
/// <summary>Gets the underlying <see cref="HttpResponseMessage"/>.</summary> | ||
public HttpResponseMessage Message { get; } | ||
|
||
/// <summary>Gets the <see cref="HttpStatusCode"/> of the response.</summary> | ||
public HttpStatusCode StatusCode => Message.StatusCode; | ||
|
||
/// <inheritdoc/> | ||
public void Dispose() => Message.Dispose(); | ||
|
||
/// <summary>Returns the response body as a string.</summary> | ||
public async Task<string> GetBodyAsync() | ||
{ | ||
if (body == null) | ||
{ | ||
body = Message.Content != null ? await Message.Content.ReadAsStringAsync().ConfigureAwait(false) : string.Empty; | ||
} | ||
return body; | ||
} | ||
|
||
/// <summary>Deserializes the response body from JSON as a value of the given type, or null if the response body is empty.</summary> | ||
/// <param name="type">The type of object to return</param> | ||
/// <param name="failIfEmpty">If false, an empty response body will be returned as null. If true, an exception will be thrown if | ||
/// the body is empty. The default is false. | ||
/// </param> | ||
public async Task<object> GetBodyAsync(Type type, bool failIfEmpty = false) | ||
{ | ||
string body = await GetBodyAsync().ConfigureAwait(false); | ||
if (string.IsNullOrWhiteSpace(body)) | ||
{ | ||
if (!failIfEmpty) throw new InvalidOperationException("The response body was empty."); | ||
return null; | ||
} | ||
return JsonConvert.DeserializeObject(body, type, Kubernetes.DefaultJsonSettings); | ||
} | ||
|
||
/// <summary>Deserializes the response body from JSON as a value of type <typeparamref name="T"/>, or the default value of | ||
/// type <typeparamref name="T"/> if the response body is empty. | ||
/// </summary> | ||
/// <param name="failIfEmpty">If false, an empty response body will be returned as the default value of type | ||
/// <typeparamref name="T"/>. If true, an exception will be thrown if the body is empty. The default is false. | ||
/// </param> | ||
public async Task<T> GetBodyAsync<T>(bool failIfEmpty = false) | ||
{ | ||
string body = await GetBodyAsync().ConfigureAwait(false); | ||
if (string.IsNullOrWhiteSpace(body)) | ||
{ | ||
if (failIfEmpty) throw new InvalidOperationException("The response body was empty."); | ||
return default(T); | ||
} | ||
return JsonConvert.DeserializeObject<T>(body, Kubernetes.DefaultJsonSettings); | ||
} | ||
|
||
/// <summary>Deserializes the response body as a <see cref="V1Status"/> object, or creates one from the status code if the | ||
/// response body is not a JSON object. | ||
/// </summary> | ||
public async Task<V1Status> GetStatusAsync() | ||
{ | ||
try | ||
{ | ||
var status = await GetBodyAsync<V1Status>().ConfigureAwait(false); | ||
if (status != null && (status.Status == "Success" || status.Status == "Failure")) return status; | ||
} | ||
catch (JsonException) { } | ||
return new V1Status() | ||
{ | ||
Status = IsError ? "Failure" : "Success", Code = (int)StatusCode, Reason = StatusCode.ToString(), Message = body | ||
}; | ||
} | ||
|
||
string body; | ||
} | ||
} |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this is getting dropped?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The config object is saved below in
this.config = config
, and they are accessible from there. (I'm not 100% sure what you mean by "getting dropped".)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any detail reason for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed it to save the entire config object rather than just two fields out of it, because it was useful to have it when instantiating a request - in particular to construct the credentials. Now that I use the ServiceCredentials object from the Kubernetes client, this can probably be reverted. But it's not like any information is being lost here. :-)