Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: kubernetes-client/csharp
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: brendandburns/csharp
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 1 contributor

Commits on Sep 26, 2020

  1. Copy the full SHA
    df102c1 View commit details
Showing with 59 additions and 1 deletion.
  1. +40 −0 src/KubernetesClient/Yaml.cs
  2. +19 −1 tests/KubernetesClient.Tests/YamlTests.cs
40 changes: 40 additions & 0 deletions src/KubernetesClient/Yaml.cs
Original file line number Diff line number Diff line change
@@ -18,6 +18,42 @@ namespace k8s
/// </summary>
public static class Yaml
{
public class ByteArrayStringYamlConverter : IYamlTypeConverter
{
public bool Accepts(Type type)
{
return type == typeof(System.Byte[]);
}

public object ReadYaml(IParser parser, Type type)
{
if (parser.Current is YamlDotNet.Core.Events.Scalar scalar)
{
try
{
if (string.IsNullOrEmpty(scalar.Value))
{
return null;
}

return Encoding.UTF8.GetBytes(scalar.Value);
}
finally
{
parser.MoveNext();
}
}

throw new InvalidOperationException(parser.Current?.ToString());
}

public void WriteYaml(IEmitter emitter, object value, Type type)
{
var obj = (System.Byte[])value;
emitter.Emit(new YamlDotNet.Core.Events.Scalar(Encoding.UTF8.GetString(obj)));
}
}

/// <summary>
/// Load a collection of objects from a stream asynchronously
/// </summary>
@@ -65,6 +101,7 @@ public static List<object> LoadAllFromString(String content, Dictionary<String,
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.IgnoreUnmatchedProperties()
.Build();
var types = new List<Type>();
@@ -81,6 +118,7 @@ public static List<object> LoadAllFromString(String content, Dictionary<String,
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.Build();
parser = new Parser(new StringReader(content));
parser.Expect<StreamStart>();
@@ -118,6 +156,7 @@ public static T LoadFromString<T>(string content)
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.Build();
var obj = deserializer.Deserialize<T>(content);
return obj;
@@ -135,6 +174,7 @@ public static string SaveToString<T>(T value)
.WithNamingConvention(new CamelCaseNamingConvention())
.WithTypeInspector(ti => new AutoRestTypeInspector(ti))
.WithTypeConverter(new IntOrStringYamlConverter())
.WithTypeConverter(new ByteArrayStringYamlConverter())
.WithEventEmitter(e => new StringQuotingEmitter(e))
.BuildValueSerializer();
emitter.Emit(new StreamStart());
20 changes: 19 additions & 1 deletion tests/KubernetesClient.Tests/YamlTests.cs
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using k8s;
using System.Text;
using k8s.Models;
using Xunit;

@@ -321,5 +321,23 @@ public void QuotedValuesShouldRemainQuotedAfterSerialization()
var objStr = Yaml.SaveToString(obj);
Assert.Equal(content, objStr);
}

[Fact]
public void LoadSecret()
{
string kManifest = @"
apiVersion: v1
kind: Secret
metadata:
name: test-secret
data:
username: bXktYXBw
password: Mzk1MjgkdmRnN0pi
";

var result = Yaml.LoadFromString<V1Secret>(kManifest);
Assert.Equal(Encoding.UTF8.GetString(result.Data["username"]), "bXktYXBw");
Assert.Equal(Encoding.UTF8.GetString(result.Data["password"]), "Mzk1MjgkdmRnN0pi");
}
}
}