Skip to content

Commit f72f0b1

Browse files
author
valtaroth
committed
Added ability to reflect whitelisted labels
1 parent a9571d3 commit f72f0b1

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -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/Core/Mirroring/Extensions/ReflectionStatusExtensions.cs

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@ public static bool CanBeAutoReflectedToNamespace(this ReflectorProperties proper
1717
}
1818

1919

20+
public static bool CanLabelBeReflected(this ReflectorProperties properties, string label)
21+
{
22+
return properties.Labels && PatternListMatch(properties.LabelsIncluded, label);
23+
}
24+
25+
2026
private static bool PatternListMatch(string patternList, string value)
2127
{
2228
if (string.IsNullOrEmpty(patternList)) return true;

src/ES.Kubernetes.Reflector/Core/Mirroring/Extensions/ReflectorExtensions.cs

+8
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ public static ReflectorProperties GetReflectionProperties(this V1ObjectMeta meta
3535
? autoNamespaces ?? string.Empty
3636
: string.Empty,
3737

38+
Labels = metadata.SafeAnnotations()
39+
.TryGet(Annotations.Reflection.Labels, out bool labels) && labels,
40+
41+
LabelsIncluded = metadata.SafeAnnotations()
42+
.TryGet(Annotations.Reflection.LabelsIncluded, out string? labelsIncluded)
43+
? labelsIncluded ?? string.Empty
44+
: string.Empty,
45+
3846
Reflects = metadata.SafeAnnotations()
3947
.TryGet(Annotations.Reflection.Reflects, out string? metaReflects)
4048
? string.IsNullOrWhiteSpace(metaReflects) ? KubeRef.Empty :

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

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class ReflectorProperties
88
public string AllowedNamespaces { get; set; } = string.Empty;
99
public bool AutoEnabled { get; set; }
1010
public string AutoNamespaces { get; set; } = string.Empty;
11+
public bool Labels { get; set; }
12+
public string LabelsIncluded { get; set; } = string.Empty;
1113
public KubeRef Reflects { get; set; } = KubeRef.Empty;
1214

1315
public string Version { get; set; } = string.Empty;

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

+26-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
419419
{
420420
source = sourceResource;
421421
}
422-
422+
423+
var sourceProperties = source.GetReflectionProperties();
423424

424425
var patchAnnotations = new Dictionary<string, string>
425426
{
@@ -448,6 +449,17 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
448449
newResourceAnnotations[Annotations.Reflection.MetaReflectedAt] =
449450
JsonConvert.SerializeObject(DateTimeOffset.UtcNow);
450451

452+
if (sourceProperties.Labels)
453+
{
454+
newResource.Metadata.Labels ??= new Dictionary<string, string>();
455+
var newResourceLabels = newResource.Metadata.Labels;
456+
foreach (var label in source.Metadata.Labels)
457+
{
458+
if (sourceProperties.CanLabelBeReflected(label.Key))
459+
newResourceLabels[label.Key] = label.Value;
460+
}
461+
}
462+
451463
try
452464
{
453465
await OnResourceCreate(newResource, targetId.Namespace);
@@ -476,6 +488,19 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
476488
annotations[patchAnnotation.Key] = patchAnnotation.Value;
477489
patchDoc.Replace(e => e.Metadata.Annotations, annotations);
478490

491+
if (sourceProperties.Labels && source.Metadata.Labels != null)
492+
{
493+
var labels = new Dictionary<string, string>();
494+
if (targetResource.Metadata.Labels != null)
495+
labels = new Dictionary<string, string>(targetResource.Metadata.Labels);
496+
foreach (var label in source.Metadata.Labels)
497+
{
498+
if (sourceProperties.CanLabelBeReflected(label.Key))
499+
labels[label.Key] = label.Value;
500+
}
501+
patchDoc.Replace(e => e.Metadata.Labels, labels);
502+
}
503+
479504
await OnResourceConfigurePatch(source, patchDoc);
480505

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

0 commit comments

Comments
 (0)