Skip to content

Commit e12a6a8

Browse files
committed
fix(patch): Add kind, apiVersion, status to ignored fields and make JsonPatch preview feature
1 parent 915159f commit e12a6a8

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

src/KubeOps.Abstractions/Entities/JsonPatchExtensions.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.Versioning;
56
using System.Text;
67
using System.Text.Json.Nodes;
78

@@ -16,6 +17,9 @@ namespace KubeOps.Abstractions.Entities;
1617
/// <summary>
1718
/// Method extensions for JSON diffing between two entities (<see cref="IKubernetesObject{TMetadata}"/>).
1819
/// </summary>
20+
[RequiresPreviewFeatures("JsonPatch extensions are a preview feature and may change in the future." +
21+
"Because maybe the default filtering does not catch all volatile and non-impactful" +
22+
"properties.")]
1923
public static class JsonPatchExtensions
2024
{
2125
/// <summary>
@@ -46,6 +50,9 @@ public static class JsonPatchExtensions
4650
"/metadata/resourceVersion",
4751
"/metadata/selfLink",
4852
"/metadata/uid",
53+
"/status",
54+
"/kind",
55+
"/apiVersion",
4956
];
5057

5158
/// <summary>
@@ -64,7 +71,9 @@ public static class JsonPatchExtensions
6471
/// </list>
6572
/// </summary>
6673
public static readonly Func<IReadOnlyList<PatchOperation>, IReadOnlyList<PatchOperation>> DefaultOperationsFilter =
67-
operations => operations.Where(o => !DefaultIgnoredProperties.Contains(o.Path.ToString())).ToList();
74+
operations =>
75+
operations.Where(o => !DefaultIgnoredProperties.Any(ignored => o.Path.ToString().StartsWith(ignored)))
76+
.ToList();
6877

6978
/// <summary>
7079
/// Convert a <see cref="IKubernetesObject{TMetadata}"/> into a <see cref="JsonNode"/>.

src/KubeOps.KubernetesClient/IKubernetesClient.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Runtime.Versioning;
6+
57
using Json.Patch;
68

79
using k8s;
@@ -338,6 +340,10 @@ TEntity UpdateStatus<TEntity>(TEntity entity)
338340
/// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param>
339341
/// <returns>The patched entity.</returns>
340342
/// <exception cref="InvalidOperationException">Thrown if the entity to be patched does not exist on the API.</exception>
343+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
344+
"Return values may change (e.g. if the patch was actually applied" +
345+
"when no changes were detected. Also, the filtering may not include" +
346+
"all volatile properties yet.")]
341347
Task<TEntity> PatchAsync<TEntity>(
342348
TEntity entity,
343349
Func<IReadOnlyList<PatchOperation>, IReadOnlyList<PatchOperation>>? operationsFilter = null,
@@ -370,6 +376,10 @@ Task<TEntity> PatchAsync<TEntity>(
370376
/// <param name="operationsFilter">The filter that is applied to the <see cref="PatchOperation"/>s in the <see cref="JsonPatch"/> to determine if changes are present.</param>
371377
/// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param>
372378
/// <returns>The patched entity.</returns>
379+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
380+
"Return values may change (e.g. if the patch was actually applied" +
381+
"when no changes were detected. Also, the filtering may not include" +
382+
"all volatile properties yet.")]
373383
Task<TEntity> PatchAsync<TEntity>(
374384
TEntity from,
375385
TEntity to,
@@ -391,6 +401,10 @@ Task<TEntity> PatchAsync<TEntity>(
391401
/// <param name="patch">The <see cref="JsonPatch"/> representing the changes to apply.</param>
392402
/// <param name="cancellationToken">Cancellation token to monitor for cancellation requests.</param>
393403
/// <returns>The patched entity.</returns>
404+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
405+
"Return values may change (e.g. if the patch was actually applied" +
406+
"when no changes were detected. Also, the filtering may not include" +
407+
"all volatile properties yet.")]
394408
Task<TEntity> PatchAsync<TEntity>(TEntity entity, JsonPatch patch, CancellationToken cancellationToken = default)
395409
where TEntity : IKubernetesObject<V1ObjectMeta> =>
396410
PatchAsync(entity, patch.ToKubernetesPatch(), cancellationToken);
@@ -430,6 +444,10 @@ Task<TEntity> PatchAsync<TEntity>(
430444
/// <param name="entity">The entity containing the desired updates.</param>
431445
/// <returns>The patched entity.</returns>
432446
/// <exception cref="InvalidOperationException">Thrown if the entity to be patched does not exist on the API.</exception>
447+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
448+
"Return values may change (e.g. if the patch was actually applied" +
449+
"when no changes were detected. Also, the filtering may not include" +
450+
"all volatile properties yet.")]
433451
TEntity Patch<TEntity>(TEntity entity)
434452
where TEntity : IKubernetesObject<V1ObjectMeta>
435453
=> PatchAsync(entity).GetAwaiter().GetResult();
@@ -441,6 +459,10 @@ TEntity Patch<TEntity>(TEntity entity)
441459
/// <param name="from">The current/original entity.</param>
442460
/// <param name="to">The updated entity with desired changes.</param>
443461
/// <returns>The patched entity.</returns>
462+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
463+
"Return values may change (e.g. if the patch was actually applied" +
464+
"when no changes were detected. Also, the filtering may not include" +
465+
"all volatile properties yet.")]
444466
TEntity Patch<TEntity>(TEntity from, TEntity to)
445467
where TEntity : IKubernetesObject<V1ObjectMeta>
446468
=> PatchAsync(from, to).GetAwaiter().GetResult();
@@ -452,6 +474,10 @@ TEntity Patch<TEntity>(TEntity from, TEntity to)
452474
/// <param name="entity">The entity to patch.</param>
453475
/// <param name="patch">The <see cref="JsonPatch"/> representing the changes to apply.</param>
454476
/// <returns>The patched entity.</returns>
477+
[RequiresPreviewFeatures("This method is using the JsonPatch feature which is in preview." +
478+
"Return values may change (e.g. if the patch was actually applied" +
479+
"when no changes were detected. Also, the filtering may not include" +
480+
"all volatile properties yet.")]
455481
TEntity Patch<TEntity>(TEntity entity, JsonPatch patch)
456482
where TEntity : IKubernetesObject<V1ObjectMeta>
457483
=> PatchAsync(entity, patch).GetAwaiter().GetResult();

src/KubeOps.Operator.Web/Webhooks/Admission/Mutation/MutationResult.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public async Task ExecuteResultAsync(ActionContext context)
6868
return;
6969
}
7070

71+
#pragma warning disable CA2252 // TODO: as soon as patch is fully stable, remove this.
7172
await response.WriteAsJsonAsync(
7273
new AdmissionResponse
7374
{
@@ -83,5 +84,6 @@ await response.WriteAsJsonAsync(
8384
: OriginalObject!.CreatePatch(ModifiedObject.ToNode()).ToBase64String(),
8485
},
8586
});
87+
#pragma warning restore CA2252
8688
}
8789
}

src/KubeOps.Operator.Web/Webhooks/Admission/Mutation/MutationWebhook{TEntity}.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,13 @@ public async Task<IActionResult> Mutate(
110110
[FromBody] AdmissionRequest<TEntity> request,
111111
CancellationToken cancellationToken)
112112
{
113+
#pragma warning disable CA2252 // TODO: remove this once the patch is stable.
113114
var original = request.Request.Operation switch
114115
{
115116
CreateOperation or UpdateOperation => request.Request.Object!.ToNode(),
116117
_ => request.Request.OldObject!.ToNode(),
117118
};
119+
#pragma warning restore CA2252
118120

119121
var result = request.Request.Operation switch
120122
{

test/KubeOps.Abstractions.Test/Entities/JsonPatchExtensions.Test.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using FluentAssertions;
66

7+
using k8s;
78
using k8s.Models;
89

910
using KubeOps.Abstractions.Entities;
@@ -192,7 +193,8 @@ public void GetJsonDiff_Removes_Object_From_Containers_List()
192193
[Fact]
193194
public void GetJsonDiff_Filters_Metadata_Fields()
194195
{
195-
var from = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = "test", ResourceVersion = "1" } };
196+
var from = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = "test", ResourceVersion = "1" } }
197+
.Initialize();
196198
var to = new V1ConfigMap { Metadata = new V1ObjectMeta { Name = "test", ResourceVersion = "2" } };
197199
var diff = from.CreateJsonPatch(to);
198200
diff.Operations.Should().HaveCount(0);

0 commit comments

Comments
 (0)