Skip to content

Commit 744eb77

Browse files
author
valtaroth
committed
Added ability to reflect whitelisted labels
1 parent 8985492 commit 744eb77

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

src/ES.Kubernetes.Reflector/Mirroring/Core/Annotations.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace ES.Kubernetes.Reflector.Mirroring.Core;
1+
namespace ES.Kubernetes.Reflector.Mirroring.Core;
22

33
public static class Annotations
44
{
@@ -10,6 +10,8 @@ public static class Reflection
1010
public static string AllowedNamespaces => $"{Prefix}/reflection-allowed-namespaces";
1111
public static string AutoEnabled => $"{Prefix}/reflection-auto-enabled";
1212
public static string AutoNamespaces => $"{Prefix}/reflection-auto-namespaces";
13+
public static string Labels => $"{Prefix}/reflection-labels";
14+
public static string LabelsIncluded => $"{Prefix}/reflection-labels-included";
1315
public static string Reflects => $"{Prefix}/reflects";
1416

1517

src/ES.Kubernetes.Reflector/Mirroring/Core/MirroringProperties.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Diagnostics.CodeAnalysis;
1+
using System.Diagnostics.CodeAnalysis;
22
using ES.FX.KubernetesClient.Models;
33

44
namespace ES.Kubernetes.Reflector.Mirroring.Core;
@@ -9,6 +9,8 @@ public class MirroringProperties
99
public string AllowedNamespaces { get; set; } = string.Empty;
1010
public bool AutoEnabled { get; set; }
1111
public string AutoNamespaces { get; set; } = string.Empty;
12+
public bool Labels { get; set; }
13+
public string LabelsIncluded { get; set; } = string.Empty;
1214
public NamespacedName? Reflects { get; set; }
1315

1416
public string ResourceVersion { get; set; } = string.Empty;

src/ES.Kubernetes.Reflector/Mirroring/Core/MirroringPropertiesExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ public static MirroringProperties GetMirroringProperties(this V1ObjectMeta metad
3232
? autoNamespaces ?? string.Empty
3333
: string.Empty,
3434

35+
Labels = metadata
36+
.TryGetAnnotationValue(Annotations.Reflection.Labels, out bool labels) && labels,
37+
38+
LabelsIncluded = metadata
39+
.TryGetAnnotationValue(Annotations.Reflection.LabelsIncluded, out string? labelsIncluded)
40+
? labelsIncluded ?? string.Empty
41+
: string.Empty,
42+
3543
Reflects = metadata
3644
.TryGetAnnotationValue(Annotations.Reflection.Reflects, out string? metaReflects)
3745
? NamespacedName.TryParse(metaReflects, out var id) ? id : null
@@ -63,6 +71,8 @@ public static bool CanBeAutoReflectedToNamespace(this MirroringProperties proper
6371
properties.CanBeReflectedToNamespace(ns) && properties.AutoEnabled &&
6472
PatternListMatch(properties.AutoNamespaces, ns);
6573

74+
public static bool CanLabelBeReflected(this MirroringProperties properties, string label) =>
75+
properties.Labels && PatternListMatch(properties.LabelsIncluded, label);
6676

6777
private static bool PatternListMatch(string patternList, string value)
6878
{

src/ES.Kubernetes.Reflector/Mirroring/Core/ResourceMirror.cs

+25
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ private async Task ResourceReflect(NamespacedName sourceNsName, NamespacedName r
459459
source = sourceObj;
460460
}
461461

462+
var sourceProperties = source.GetMirroringProperties();
462463

463464
var patchAnnotations = new Dictionary<string, string>
464465
{
@@ -487,6 +488,17 @@ private async Task ResourceReflect(NamespacedName sourceNsName, NamespacedName r
487488
newResourceAnnotations[Annotations.Reflection.MetaReflectedVersion] = source.Metadata.ResourceVersion;
488489
newResourceAnnotations[Annotations.Reflection.MetaReflectedAt] = DateTimeOffset.UtcNow.ToString("O");
489490

491+
if (sourceProperties.Labels)
492+
{
493+
newResource.Metadata.Labels ??= new Dictionary<string, string>();
494+
var newResourceLabels = newResource.Metadata.Labels;
495+
foreach (var label in source.Metadata.Labels)
496+
{
497+
if (sourceProperties.CanLabelBeReflected(label.Key))
498+
newResourceLabels[label.Key] = label.Value;
499+
}
500+
}
501+
490502
try
491503
{
492504
await OnResourceCreate(newResource, reflectionNsName.Namespace);
@@ -515,6 +527,19 @@ private async Task ResourceReflect(NamespacedName sourceNsName, NamespacedName r
515527
annotations[patchAnnotation.Key] = patchAnnotation.Value;
516528
patchDoc.Replace(e => e.Metadata.Annotations, annotations);
517529

530+
if (sourceProperties.Labels && source.Metadata.Labels != null)
531+
{
532+
var labels = new Dictionary<string, string>();
533+
if (reflectionObj.Metadata.Labels != null)
534+
labels = new Dictionary<string, string>(reflectionObj.Metadata.Labels);
535+
foreach (var label in source.Metadata.Labels)
536+
{
537+
if (sourceProperties.CanLabelBeReflected(label.Key))
538+
labels[label.Key] = label.Value;
539+
}
540+
patchDoc.Replace(e => e.Metadata.Labels, labels);
541+
}
542+
518543
await OnResourceConfigurePatch(source, patchDoc);
519544

520545
var patch = JsonConvert.SerializeObject(patchDoc, Formatting.Indented);

0 commit comments

Comments
 (0)