move kubectl config and yaml related to model module (#806)
* move yaml and config to models module * better naming * address comments
This commit is contained in:
@@ -15,7 +15,7 @@ namespace yaml
|
||||
typeMap.Add("v1/Service", typeof(V1Service));
|
||||
typeMap.Add("apps/v1/Deployment", typeof(V1Deployment));
|
||||
|
||||
var objects = await Yaml.LoadAllFromFileAsync(args[0], typeMap);
|
||||
var objects = await KubernetesYaml.LoadAllFromFileAsync(args[0], typeMap);
|
||||
|
||||
foreach (var obj in objects) {
|
||||
Console.WriteLine(obj);
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
global using System;
|
||||
global using System.Collections.Generic;
|
||||
global using System.Linq;
|
||||
global using System.Text.Json;
|
||||
global using System.Text.Json.Serialization;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace k8s.Models
|
||||
{
|
||||
internal sealed class IntOrStringConverter : JsonConverter<IntstrIntOrString>
|
||||
internal sealed class IntOrStringJsonConverter : JsonConverter<IntstrIntOrString>
|
||||
{
|
||||
public override IntstrIntOrString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace k8s.Models
|
||||
{
|
||||
[JsonConverter(typeof(IntOrStringConverter))]
|
||||
[JsonConverter(typeof(IntOrStringJsonConverter))]
|
||||
public partial class IntstrIntOrString
|
||||
{
|
||||
public static implicit operator IntstrIntOrString(int v)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
<PropertyGroup>
|
||||
<TargetFrameworks>netstandard2.0</TargetFrameworks>
|
||||
<RootNamespace>k8s.Models</RootNamespace>
|
||||
@@ -11,8 +11,9 @@
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Json" Version="6.0.0" />
|
||||
<PackageReference Include="System.Text.Json" Version="6.0.2" />
|
||||
<PackageReference Include="AutoMapper" Version="10.1.1" />
|
||||
<PackageReference Include="Fractions" Version="7.0.0" />
|
||||
<PackageReference Include="YamlDotNet" Version="11.2.1" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -5,7 +5,7 @@ using System.Xml;
|
||||
|
||||
namespace k8s
|
||||
{
|
||||
internal static class KubernetesJson
|
||||
public static class KubernetesJson
|
||||
{
|
||||
private static readonly JsonSerializerOptions JsonSerializerOptions = new JsonSerializerOptions();
|
||||
|
||||
@@ -13,7 +13,7 @@ namespace k8s
|
||||
/// <summary>
|
||||
/// This is a utility class that helps you load objects from YAML files.
|
||||
/// </summary>
|
||||
public static class Yaml
|
||||
public static class KubernetesYaml
|
||||
{
|
||||
private static readonly IDeserializer Deserializer =
|
||||
new DeserializerBuilder()
|
||||
@@ -50,7 +50,7 @@ namespace k8s
|
||||
},
|
||||
t => t);
|
||||
|
||||
public class ByteArrayStringYamlConverter : IYamlTypeConverter
|
||||
private class ByteArrayStringYamlConverter : IYamlTypeConverter
|
||||
{
|
||||
public bool Accepts(Type type)
|
||||
{
|
||||
@@ -179,21 +179,42 @@ namespace k8s
|
||||
}
|
||||
}
|
||||
|
||||
[Obsolete("use Deserialize")]
|
||||
public static T LoadFromString<T>(string content)
|
||||
{
|
||||
var obj = Deserializer.Deserialize<T>(content);
|
||||
return obj;
|
||||
return Deserialize<T>(content);
|
||||
}
|
||||
|
||||
[Obsolete("use Serialize")]
|
||||
public static string SaveToString<T>(T value)
|
||||
{
|
||||
return Serialize(value);
|
||||
}
|
||||
|
||||
public static TValue Deserialize<TValue>(string yaml)
|
||||
{
|
||||
return Deserializer.Deserialize<TValue>(yaml);
|
||||
}
|
||||
|
||||
public static TValue Deserialize<TValue>(Stream yaml)
|
||||
{
|
||||
return Deserializer.Deserialize<TValue>(new StreamReader(yaml));
|
||||
}
|
||||
|
||||
public static string Serialize(object value)
|
||||
{
|
||||
if (value == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
var stringBuilder = new StringBuilder();
|
||||
var writer = new StringWriter(stringBuilder);
|
||||
var emitter = new Emitter(writer);
|
||||
|
||||
emitter.Emit(new StreamStart());
|
||||
emitter.Emit(new DocumentStart());
|
||||
Serializer.SerializeValue(emitter, value, typeof(T));
|
||||
Serializer.SerializeValue(emitter, value, value.GetType());
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
@@ -53,7 +53,7 @@ namespace k8s.Models
|
||||
/// writing some sort of special handling code in the hopes that that will
|
||||
/// cause implementors to also use a fixed point implementation.
|
||||
/// </summary>
|
||||
[JsonConverter(typeof(QuantityConverter))]
|
||||
[JsonConverter(typeof(ResourceQuantityJsonConverter))]
|
||||
public partial class ResourceQuantity
|
||||
{
|
||||
public enum SuffixFormat
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace k8s.Models
|
||||
{
|
||||
internal sealed class QuantityConverter : JsonConverter<ResourceQuantity>
|
||||
internal sealed class ResourceQuantityJsonConverter : JsonConverter<ResourceQuantity>
|
||||
{
|
||||
public override ResourceQuantity Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||
{
|
||||
@@ -9,7 +9,6 @@
|
||||
<PackageReference Include="prometheus-net" Version="5.0.1" />
|
||||
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.13.1" />
|
||||
<PackageReference Include="System.IO.Abstractions" Version="13.2.47" />
|
||||
<PackageReference Include="YamlDotNet" Version="11.2.1" />
|
||||
<PackageReference Include="IdentityModel.OidcClient" Version="4.0.0" />
|
||||
|
||||
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" Condition="'$(TargetFramework)' == 'netstandard2.1'" />
|
||||
|
||||
@@ -161,7 +161,7 @@ namespace k8s
|
||||
|
||||
kubeconfig.Position = 0;
|
||||
|
||||
var k8SConfig = await Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfig).ConfigureAwait(false);
|
||||
var k8SConfig = await KubernetesYaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfig).ConfigureAwait(false);
|
||||
var k8SConfiguration = GetKubernetesClientConfiguration(currentContext, masterUrl, k8SConfig);
|
||||
|
||||
return k8SConfiguration;
|
||||
@@ -639,7 +639,7 @@ namespace k8s
|
||||
|
||||
using (var stream = kubeconfig.OpenRead())
|
||||
{
|
||||
var config = await Yaml.LoadFromStreamAsync<K8SConfiguration>(stream).ConfigureAwait(false);
|
||||
var config = await KubernetesYaml.LoadFromStreamAsync<K8SConfiguration>(stream).ConfigureAwait(false);
|
||||
|
||||
if (useRelativePaths)
|
||||
{
|
||||
@@ -669,7 +669,7 @@ namespace k8s
|
||||
/// <returns>Instance of the <see cref="K8SConfiguration"/> class</returns>
|
||||
public static async Task<K8SConfiguration> LoadKubeConfigAsync(Stream kubeconfigStream)
|
||||
{
|
||||
return await Yaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfigStream).ConfigureAwait(false);
|
||||
return await KubernetesYaml.LoadFromStreamAsync<K8SConfiguration>(kubeconfigStream).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -445,7 +445,7 @@ namespace k8s.Tests
|
||||
public void LoadKubeConfigExplicitFilePath()
|
||||
{
|
||||
var txt = File.ReadAllText("assets/kubeconfig.yml");
|
||||
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
||||
var expectedCfg = KubernetesYaml.LoadFromString<K8SConfiguration>(txt);
|
||||
|
||||
var cfg = KubernetesClientConfiguration.LoadKubeConfig("assets/kubeconfig.yml");
|
||||
|
||||
@@ -458,7 +458,7 @@ namespace k8s.Tests
|
||||
{
|
||||
var filePath = "assets/kubeconfig.yml";
|
||||
var txt = File.ReadAllText(filePath);
|
||||
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
||||
var expectedCfg = KubernetesYaml.LoadFromString<K8SConfiguration>(txt);
|
||||
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
var cfg = KubernetesClientConfiguration.LoadKubeConfig(fileInfo);
|
||||
@@ -472,7 +472,7 @@ namespace k8s.Tests
|
||||
{
|
||||
var filePath = "assets/kubeconfig.yml";
|
||||
var txt = File.ReadAllText(filePath);
|
||||
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
||||
var expectedCfg = KubernetesYaml.LoadFromString<K8SConfiguration>(txt);
|
||||
|
||||
var fileInfo = new FileInfo(filePath);
|
||||
K8SConfiguration cfg;
|
||||
@@ -524,7 +524,7 @@ namespace k8s.Tests
|
||||
public void LoadSameKubeConfigFromEnvironmentVariableUnmodified()
|
||||
{
|
||||
var txt = File.ReadAllText("assets/kubeconfig.yml");
|
||||
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
||||
var expectedCfg = KubernetesYaml.LoadFromString<K8SConfiguration>(txt);
|
||||
|
||||
var fileInfo = new FileInfo(Path.GetFullPath("assets/kubeconfig.yml"));
|
||||
|
||||
@@ -537,7 +537,7 @@ namespace k8s.Tests
|
||||
public void LoadKubeConfigWithAdditionalProperties()
|
||||
{
|
||||
var txt = File.ReadAllText("assets/kubeconfig.additional-properties.yml");
|
||||
var expectedCfg = Yaml.LoadFromString<K8SConfiguration>(txt);
|
||||
var expectedCfg = KubernetesYaml.LoadFromString<K8SConfiguration>(txt);
|
||||
|
||||
var fileInfo = new FileInfo(Path.GetFullPath("assets/kubeconfig.additional-properties.yml"));
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ using Xunit;
|
||||
|
||||
namespace k8s.Tests
|
||||
{
|
||||
public class YamlTests
|
||||
public class KubernetesYamlTests
|
||||
{
|
||||
[Fact]
|
||||
public void LoadAllFromString()
|
||||
@@ -23,7 +23,7 @@ kind: Namespace
|
||||
metadata:
|
||||
name: ns";
|
||||
|
||||
var objs = Yaml.LoadAllFromString(content);
|
||||
var objs = KubernetesYaml.LoadAllFromString(content);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<V1Pod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -53,7 +53,7 @@ kind: Namespace
|
||||
metadata:
|
||||
name: ns";
|
||||
|
||||
var objs = Yaml.LoadAllFromString(content, types);
|
||||
var objs = KubernetesYaml.LoadAllFromString(content, types);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<MyPod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -77,7 +77,7 @@ metadata:
|
||||
name: ns
|
||||
youDontKnow: Me";
|
||||
|
||||
var objs = Yaml.LoadAllFromString(content);
|
||||
var objs = KubernetesYaml.LoadAllFromString(content);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<V1Pod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -104,7 +104,7 @@ metadata:
|
||||
name: ns
|
||||
youDontKnow: Me";
|
||||
|
||||
var objs = Yaml.LoadAllFromString(content, types);
|
||||
var objs = KubernetesYaml.LoadAllFromString(content, types);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<MyPod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -131,7 +131,7 @@ metadata:
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
|
||||
|
||||
var objs = await Yaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(false);
|
||||
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName).ConfigureAwait(false);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<V1Pod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -168,7 +168,7 @@ metadata:
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
|
||||
|
||||
var objs = await Yaml.LoadAllFromFileAsync(tempFileName, types).ConfigureAwait(false);
|
||||
var objs = await KubernetesYaml.LoadAllFromFileAsync(tempFileName, types).ConfigureAwait(false);
|
||||
Assert.Equal(2, objs.Count);
|
||||
Assert.IsType<MyPod>(objs[0]);
|
||||
Assert.IsType<V1Namespace>(objs[1]);
|
||||
@@ -193,7 +193,7 @@ metadata:
|
||||
name: foo
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
@@ -208,7 +208,7 @@ metadata:
|
||||
youDontKnow: Me
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
@@ -223,7 +223,7 @@ metadata:
|
||||
youDontKnow: Me
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
@@ -238,7 +238,7 @@ metadata:
|
||||
name: foo
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
Assert.Equal("bar", obj.Metadata.NamespaceProperty);
|
||||
@@ -264,7 +264,7 @@ spec:
|
||||
readOnly: false
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.True(obj.Spec.Containers[0].VolumeMounts[0].ReadOnlyProperty);
|
||||
Assert.False(obj.Spec.Containers[0].VolumeMounts[1].ReadOnlyProperty);
|
||||
@@ -281,7 +281,7 @@ metadata:
|
||||
|
||||
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
|
||||
{
|
||||
var obj = Yaml.LoadFromStreamAsync<V1Pod>(stream).Result;
|
||||
var obj = KubernetesYaml.LoadFromStreamAsync<V1Pod>(stream).Result;
|
||||
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
@@ -301,7 +301,7 @@ metadata:
|
||||
{
|
||||
await File.WriteAllTextAsync(tempFileName, content).ConfigureAwait(false);
|
||||
|
||||
var obj = await Yaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(false);
|
||||
var obj = await KubernetesYaml.LoadFromFileAsync<V1Pod>(tempFileName).ConfigureAwait(false);
|
||||
Assert.Equal("foo", obj.Metadata.Name);
|
||||
}
|
||||
finally
|
||||
@@ -318,10 +318,10 @@ metadata:
|
||||
{
|
||||
var content = @"namespace: foo";
|
||||
|
||||
var deserialized = Yaml.LoadFromString<V1ObjectMeta>(content);
|
||||
var deserialized = KubernetesYaml.LoadFromString<V1ObjectMeta>(content);
|
||||
Assert.Equal("foo", deserialized.NamespaceProperty);
|
||||
|
||||
var serialized = Yaml.SaveToString(deserialized);
|
||||
var serialized = KubernetesYaml.SaveToString(deserialized);
|
||||
Assert.Equal(content, serialized);
|
||||
}
|
||||
|
||||
@@ -330,7 +330,7 @@ metadata:
|
||||
{
|
||||
var pod = new V1Pod() { ApiVersion = "v1", Kind = "Pod", Metadata = new V1ObjectMeta() { Name = "foo" } };
|
||||
|
||||
var yaml = Yaml.SaveToString(pod);
|
||||
var yaml = KubernetesYaml.SaveToString(pod);
|
||||
Assert.Equal(
|
||||
ToLines(@"apiVersion: v1
|
||||
kind: Pod
|
||||
@@ -348,7 +348,7 @@ metadata:
|
||||
Metadata = new V1ObjectMeta() { Name = "foo", NamespaceProperty = "bar" },
|
||||
};
|
||||
|
||||
var yaml = Yaml.SaveToString(pod);
|
||||
var yaml = KubernetesYaml.SaveToString(pod);
|
||||
Assert.Equal(
|
||||
ToLines(@"apiVersion: v1
|
||||
kind: Pod
|
||||
@@ -388,7 +388,7 @@ metadata:
|
||||
},
|
||||
};
|
||||
|
||||
var yaml = Yaml.SaveToString(pod);
|
||||
var yaml = KubernetesYaml.SaveToString(pod);
|
||||
Assert.Equal(
|
||||
ToLines(@"apiVersion: v1
|
||||
kind: Pod
|
||||
@@ -446,7 +446,7 @@ spec:
|
||||
- -cpus
|
||||
- ""2""";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
|
||||
Assert.NotNull(obj?.Spec?.Containers);
|
||||
var container = Assert.Single(obj.Spec.Containers);
|
||||
@@ -476,7 +476,7 @@ spec:
|
||||
targetPort: 3000
|
||||
";
|
||||
|
||||
var obj = Yaml.LoadFromString<V1Service>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Service>(content);
|
||||
|
||||
Assert.Equal(3000, obj.Spec.Ports[0].Port);
|
||||
Assert.Equal(3000, int.Parse(obj.Spec.Ports[0].TargetPort));
|
||||
@@ -508,7 +508,7 @@ spec:
|
||||
},
|
||||
};
|
||||
|
||||
var output = Yaml.SaveToString(obj);
|
||||
var output = KubernetesYaml.SaveToString(obj);
|
||||
Assert.Equal(ToLines(output), ToLines(content));
|
||||
}
|
||||
|
||||
@@ -534,11 +534,11 @@ spec:
|
||||
value: ""false""
|
||||
image: vish/stress
|
||||
name: cpu-demo-ctr";
|
||||
var obj = Yaml.LoadFromString<V1Pod>(content);
|
||||
var obj = KubernetesYaml.LoadFromString<V1Pod>(content);
|
||||
Assert.NotNull(obj?.Spec?.Containers);
|
||||
var container = Assert.Single(obj.Spec.Containers);
|
||||
Assert.NotNull(container.Env);
|
||||
var objStr = Yaml.SaveToString(obj);
|
||||
var objStr = KubernetesYaml.SaveToString(obj);
|
||||
Assert.Equal(content.Replace("\r\n", "\n"), objStr.Replace("\r\n", "\n"));
|
||||
}
|
||||
|
||||
@@ -555,7 +555,7 @@ data:
|
||||
password: Mzk1MjgkdmRnN0pi
|
||||
";
|
||||
|
||||
var result = Yaml.LoadFromString<V1Secret>(kManifest);
|
||||
var result = KubernetesYaml.LoadFromString<V1Secret>(kManifest);
|
||||
Assert.Equal("bXktYXBw", Encoding.UTF8.GetString(result.Data["username"]));
|
||||
Assert.Equal("Mzk1MjgkdmRnN0pi", Encoding.UTF8.GetString(result.Data["password"]));
|
||||
}
|
||||
@@ -589,7 +589,7 @@ spec:
|
||||
served: true
|
||||
storage: true
|
||||
";
|
||||
var result = Yaml.LoadFromString<V1CustomResourceDefinition>(kManifest);
|
||||
var result = KubernetesYaml.LoadFromString<V1CustomResourceDefinition>(kManifest);
|
||||
Assert.Single(result?.Spec?.Versions);
|
||||
var ver = result.Spec.Versions[0];
|
||||
Assert.Equal(true, ver?.Schema?.OpenAPIV3Schema?.XKubernetesIntOrString);
|
||||
@@ -211,14 +211,14 @@ namespace k8s.Tests
|
||||
[Fact]
|
||||
public void DeserializeYaml()
|
||||
{
|
||||
var value = Yaml.LoadFromString<ResourceQuantity>("\"1\"");
|
||||
var value = KubernetesYaml.LoadFromString<ResourceQuantity>("\"1\"");
|
||||
Assert.Equal(new ResourceQuantity(1, 0, DecimalSI), value);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void SerializeYaml()
|
||||
{
|
||||
var value = Yaml.SaveToString(new ResourceQuantity(1, -1, DecimalSI));
|
||||
var value = KubernetesYaml.SaveToString(new ResourceQuantity(1, -1, DecimalSI));
|
||||
Assert.Equal("100m", value);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user