Skip to content

Commit a8e559a

Browse files
SilvengaChristoph Bühler
authored andcommitted
feat: Migrate to System.Text.Json (#436)
BREAKING CHANGE: Existing projects must migrate from using `JsonPropertyAttributes` to `JsonPropertyNameAttributes`. The YAML serializer is also shared with `kubernetes-client` and is no longer overridable via injection. `Microsoft.Rest.HttpOperationException` no longer exists, any place this is used must move to `k8s.Autorest.HttpOperationException`.
1 parent aee0d09 commit a8e559a

20 files changed

+111
-149
lines changed

src/KubeOps/KubeOps.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<Import Project="..\..\config\Common.targets" />
44
<Import Project="..\..\config\CodeAnalysis.targets" />
@@ -25,7 +25,7 @@
2525

2626
<ItemGroup>
2727
<PackageReference Include="CompareNETObjects" Version="4.77.0" />
28-
<PackageReference Include="DotnetKubernetesClient" Version="2.1.11" />
28+
<PackageReference Include="DotnetKubernetesClient" Version="2.1.12" />
2929
<PackageReference Include="JsonDiffPatch" Version="2.0.55" />
3030
<PackageReference Include="Localtunnel" Version="1.0.4" />
3131
<PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="4.0.1" />

src/KubeOps/Operator/Builder/OperatorBuilder.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using DotnetKubernetesClient;
44
using k8s;
55
using k8s.Models;
6-
using KellermanSoftware.CompareNetObjects;
76
using KubeOps.Operator.Caching;
87
using KubeOps.Operator.Controller;
98
using KubeOps.Operator.DevOps;
@@ -20,7 +19,6 @@
2019
using Microsoft.Extensions.Diagnostics.HealthChecks;
2120
using Microsoft.Extensions.Logging;
2221
using Prometheus;
23-
using YamlDotNet.Serialization;
2422

2523
namespace KubeOps.Operator.Builder;
2624

@@ -189,14 +187,6 @@ internal IOperatorBuilder AddOperatorBase(OperatorSettings settings)
189187
Services.AddTransient<ValidatingWebhookConfigurationBuilder>();
190188
Services.AddTransient<IWebhookMetadataBuilder, WebhookMetadataBuilder>();
191189

192-
Services.AddTransient(
193-
_ => new SerializerBuilder()
194-
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitNull)
195-
.WithNamingConvention(new NamingConvention())
196-
.WithTypeConverter(new Yaml.ByteArrayStringYamlConverter())
197-
.WithTypeConverter(new IntOrStringYamlConverter())
198-
.Build());
199-
200190
Services.AddTransient<EntitySerializer>();
201191

202192
Services.AddSingleton<IKubernetesClient, KubernetesClient>();

src/KubeOps/Operator/Commands/Management/Install.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44
using DotnetKubernetesClient;
5+
using k8s.Autorest;
56
using KubeOps.Operator.Entities;
67
using McMaster.Extensions.CommandLineUtils;
78
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Rest;
99

1010
namespace KubeOps.Operator.Commands.Management;
1111

src/KubeOps/Operator/Commands/Management/Uninstall.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
using System.Linq;
33
using System.Threading.Tasks;
44
using DotnetKubernetesClient;
5+
using k8s.Autorest;
56
using KubeOps.Operator.Entities;
67
using McMaster.Extensions.CommandLineUtils;
78
using Microsoft.Extensions.DependencyInjection;
8-
using Microsoft.Rest;
99

1010
namespace KubeOps.Operator.Commands.Management;
1111

src/KubeOps/Operator/Entities/Extensions/EntityToCrdExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
66
using System.Reflection;
7+
using System.Text.Json.Serialization;
78
using DotnetKubernetesClient.Entities;
89
using k8s;
910
using k8s.Models;
1011
using KubeOps.Operator.Entities.Annotations;
1112
using KubeOps.Operator.Errors;
1213
using KubeOps.Operator.Util;
1314
using Namotion.Reflection;
14-
using Newtonsoft.Json;
1515

1616
namespace KubeOps.Operator.Entities.Extensions;
1717

@@ -362,7 +362,7 @@ private static void ProcessType(
362362
MapProperty(prop, additionalColumns, $"{jsonPath}.{GetPropertyName(prop)}"))));
363363
props.Required = type.GetProperties()
364364
.Where(prop => prop.GetCustomAttribute<RequiredAttribute>() != null)
365-
.Select(prop => GetPropertyName(prop))
365+
.Select(GetPropertyName)
366366
.ToList();
367367
if (props.Required.Count == 0)
368368
{
@@ -389,8 +389,8 @@ private static bool IsSimpleType(Type type) =>
389389

390390
private static string GetPropertyName(PropertyInfo property)
391391
{
392-
var attribute = property.GetCustomAttribute<JsonPropertyAttribute>();
393-
var propertyName = attribute?.PropertyName ?? property.Name;
392+
var attribute = property.GetCustomAttribute<JsonPropertyNameAttribute>();
393+
var propertyName = attribute?.Name ?? property.Name;
394394
return propertyName.ToCamelCase();
395395
}
396396

src/KubeOps/Operator/Leadership/LeaderElector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
using System.Threading.Tasks;
77
using DotnetKubernetesClient;
88
using DotnetKubernetesClient.LabelSelectors;
9+
using k8s.Autorest;
910
using k8s.Models;
1011
using KubeOps.Operator.Entities.Extensions;
1112
using Microsoft.Extensions.Hosting;
1213
using Microsoft.Extensions.Logging;
13-
using Microsoft.Rest;
1414
using Timer = System.Timers.Timer;
1515

1616
namespace KubeOps.Operator.Leadership;

src/KubeOps/Operator/OperatorSettings.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
using DotnetKubernetesClient;
55
using KellermanSoftware.CompareNetObjects;
66
using KubeOps.Operator.Errors;
7-
using KubeOps.Operator.Serialization;
8-
using Microsoft.Rest.Serialization;
9-
using Newtonsoft.Json;
10-
using Newtonsoft.Json.Converters;
117

128
namespace KubeOps.Operator;
139

@@ -185,15 +181,4 @@ public sealed class OperatorSettings
185181
AutoClearCache = false,
186182
MembersToIgnore = new List<string> { "ResourceVersion", "ManagedFields" },
187183
};
188-
189-
internal JsonSerializerSettings SerializerSettings { get; } = new()
190-
{
191-
DateFormatHandling = DateFormatHandling.IsoDateFormat,
192-
DateTimeZoneHandling = DateTimeZoneHandling.Utc,
193-
NullValueHandling = NullValueHandling.Ignore,
194-
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
195-
ContractResolver = new NamingConvention(),
196-
Converters = new List<JsonConverter> { new StringEnumConverter(), new Iso8601TimeSpanConverter(), },
197-
DateFormatString = "yyyy'-'MM'-'dd'T'HH':'mm':'ss.ffffffK",
198-
};
199184
}
Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,15 @@
11
using System;
2-
using Newtonsoft.Json;
3-
using YamlDotNet.Serialization;
2+
using k8s;
43

54
namespace KubeOps.Operator.Serialization;
65

76
internal class EntitySerializer
87
{
9-
private readonly ISerializer _yaml;
10-
private readonly JsonSerializerSettings _jsonSettings;
11-
12-
public EntitySerializer(ISerializer yaml, OperatorSettings operatorSettings)
13-
{
14-
_yaml = yaml;
15-
_jsonSettings = operatorSettings.SerializerSettings;
16-
_jsonSettings.Formatting = Formatting.Indented;
17-
_jsonSettings.NullValueHandling = NullValueHandling.Ignore;
18-
}
19-
208
public string Serialize(object @object, SerializerOutputFormat format = default)
219
=> format switch
2210
{
23-
SerializerOutputFormat.Yaml => _yaml.Serialize(@object),
24-
SerializerOutputFormat.Json => JsonConvert.SerializeObject(@object, _jsonSettings),
11+
SerializerOutputFormat.Yaml => KubernetesYaml.Serialize(@object),
12+
SerializerOutputFormat.Json => KubernetesJson.Serialize(@object),
2513
_ => throw new ArgumentOutOfRangeException(),
2614
};
2715
}

src/KubeOps/Operator/Serialization/NamingConvention.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
1-
namespace KubeOps.Operator.Webhooks;
1+
using System.Text.Json.Serialization;
2+
3+
namespace KubeOps.Operator.Webhooks;
24

35
internal sealed class AdmissionRequest<TEntity>
46
{
7+
[JsonPropertyName("uid")]
58
public string Uid { get; init; } = string.Empty;
69

10+
[JsonPropertyName("operation")]
711
public string Operation { get; init; } = string.Empty;
812

13+
[JsonPropertyName("object")]
914
public TEntity? Object { get; set; }
1015

16+
[JsonPropertyName("oldObject")]
1117
public TEntity? OldObject { get; set; }
1218

19+
[JsonPropertyName("dryRun")]
1320
public bool DryRun { get; set; }
1421
}

0 commit comments

Comments
 (0)