Skip to content

Commit f077b55

Browse files
committed
Initial commit
0 parents  commit f077b55

File tree

10 files changed

+2615
-0
lines changed

10 files changed

+2615
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/AdamMil.snk
2+
/bin
3+
/obj

FluentExtensions.cs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using System;
2+
using System.Net.Http;
3+
using k8s.Models;
4+
using Newtonsoft.Json;
5+
6+
namespace k8s.Fluent
7+
{
8+
/// <summary>Provides extension methods that implement a fluent interface for a <see cref="Kubernetes"/> client.</summary>
9+
public static class FluentExtensions
10+
{
11+
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/> and
12+
/// <see cref="IKubernetesObject.Kind"/>.
13+
/// </summary>
14+
/// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks>
15+
public static T New<T>(this IKubernetes client) where T : IKubernetesObject, new() => KubernetesScheme.Default.New<T>();
16+
17+
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>,
18+
/// <see cref="IKubernetesObject.Kind"/>, and <see cref="V1ObjectMeta.Name"/>.
19+
/// </summary>
20+
/// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks>
21+
public static T New<T>(this IKubernetes client, string name) where T : IKubernetesObject<V1ObjectMeta>, new() =>
22+
KubernetesScheme.Default.New<T>(name);
23+
24+
/// <summary>Creates a new Kubernetes object of the given type and sets its <see cref="IKubernetesObject.ApiVersion"/>,
25+
/// <see cref="IKubernetesObject.Kind"/>, <see cref="V1ObjectMeta.NamespaceProperty"/>, and <see cref="V1ObjectMeta.Name"/>.
26+
/// </summary>
27+
/// <remarks>This method uses the <see cref="KubernetesScheme.Default"/> <see cref="KubernetesScheme"/>.</remarks>
28+
public static T New<T>(this IKubernetes client, string ns, string name) where T : IKubernetesObject<V1ObjectMeta>, new() =>
29+
KubernetesScheme.Default.New<T>(ns, name);
30+
31+
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/>
32+
/// (<see cref="HttpMethod.Get"/> by default).
33+
/// </summary>
34+
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method = null) =>
35+
new KubernetesRequest(client).Method(method);
36+
37+
/// <summary>Creates a new <see cref="KubernetesRequest"/> using the given <see cref="HttpMethod"/>
38+
/// and resource URI components.
39+
/// </summary>
40+
public static KubernetesRequest Request(this Kubernetes client,
41+
HttpMethod method, string type = null, string ns = null, string name = null, string group = null, string version = null) =>
42+
new KubernetesRequest(client).Method(method).Group(group).Version(version).Type(type).Namespace(ns).Name(name);
43+
44+
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object.</summary>
45+
public static KubernetesRequest Request(this Kubernetes client, Type type) => new KubernetesRequest(client).GVK(type);
46+
47+
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary>
48+
public static KubernetesRequest Request(this Kubernetes client, HttpMethod method, Type type, string ns = null, string name = null) =>
49+
Request(client, method).GVK(type).Namespace(ns).Name(name);
50+
51+
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given type of object with an optional name and namespace.</summary>
52+
public static KubernetesRequest Request<T>(this Kubernetes client, string ns = null, string name = null) =>
53+
Request(client, null, typeof(T), ns, name);
54+
55+
/// <summary>Creates a new <see cref="KubernetesRequest"/> to access the given object.</summary>
56+
public static KubernetesRequest Request(this Kubernetes client, IKubernetesObject obj, bool setBody = true) =>
57+
new KubernetesRequest(client).Set(obj, setBody);
58+
59+
/// <summary>Serializes an object using the <see cref="DefaultSerializer"/>.</summary>
60+
internal static object Deserialize(string json, Type type)
61+
{
62+
using(var reader = new JsonTextReader(new System.IO.StringReader(json))) return DefaultSerializer.Deserialize(reader, type);
63+
}
64+
65+
/// <summary>Serializes an object using the <see cref="DefaultSerializer"/>.</summary>
66+
internal static string Serialize(object value)
67+
{
68+
var sw = new System.IO.StringWriter(new System.Text.StringBuilder(256), System.Globalization.CultureInfo.InvariantCulture);
69+
using(var writer = new JsonTextWriter(sw)) // do it the same way JsonConvert.SerializeObject does
70+
{
71+
writer.Formatting = DefaultSerializer.Formatting;
72+
DefaultSerializer.Serialize(writer, value, null);
73+
}
74+
return sw.ToString();
75+
}
76+
77+
/// <summary>Gets the <see cref="JsonSerializerSettings"/> used to serialize and deserialize Kubernetes objects.</summary>
78+
internal static readonly JsonSerializer DefaultSerializer = JsonSerializer.Create(CreateSerializerSettings());
79+
80+
/// <summary>Creates the JSON serializer settings used for serializing request bodies and deserializing responses.</summary>
81+
static JsonSerializerSettings CreateSerializerSettings()
82+
{
83+
var settings = new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore };
84+
settings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
85+
return settings;
86+
}
87+
}
88+
}

KubernetesClient.Fluent.csproj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
4+
<AssemblyOriginatorKeyFile>AdamMil.snk</AssemblyOriginatorKeyFile>
5+
<Authors>Adam Milazzo (www.adammil.net)</Authors>
6+
<Copyright>2020 Adam Milazzo</Copyright>
7+
<Description>This package implements a flexible and fluent Kubernetes client.</Description>
8+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
9+
<IncludeSymbols>true</IncludeSymbols>
10+
<LangVersion>7.3</LangVersion>
11+
<PackageLicenseExpression>LGPL-2.1-or-later OR Apache-2.0</PackageLicenseExpression>
12+
<PackageProjectUrl>https://github.com/AdamMil/KubernetesClient.Fluent</PackageProjectUrl>
13+
<PackageTags>kubernetes;client;fluent</PackageTags>
14+
<PublishRepositoryUrl>true</PublishRepositoryUrl>
15+
<RepositoryUrl>https://github.com/AdamMil/KubernetesClient.Fluent.git</RepositoryUrl>
16+
<RepositoryType>git</RepositoryType>
17+
<RootNamespace>k8s.Fluent</RootNamespace>
18+
<SignAssembly>true</SignAssembly>
19+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
20+
<TargetFrameworks>netstandard2.0;netstandard2.1;net452;netcoreapp2.1</TargetFrameworks>
21+
<TargetFrameworks Condition="'$(OS)' != 'Windows_NT'">netstandard2.0;netstandard2.1;netcoreapp2.1</TargetFrameworks>
22+
<AssemblyVersion>1.0.0.0</AssemblyVersion>
23+
<FileVersion>1.0.0.0</FileVersion>
24+
<Version>1.0.0</Version>
25+
</PropertyGroup>
26+
<ItemGroup>
27+
<PackageReference Include="KubernetesClient" Version="2.0.6" />
28+
<PackageReference Include="SPDY.net" Version="1.0.1" />
29+
</ItemGroup>
30+
</Project>

0 commit comments

Comments
 (0)