Skip to content

Commit 7b21bbc

Browse files
author
valtaroth
committed
Added ability to reflect whitelisted labels
1 parent f1e2183 commit 7b21bbc

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
@@ -409,7 +409,8 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
409409
{
410410
source = sourceResource;
411411
}
412-
412+
413+
var sourceProperties = source.GetReflectionProperties();
413414

414415
var patchAnnotations = new Dictionary<string, string>
415416
{
@@ -438,6 +439,17 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
438439
newResourceAnnotations[Annotations.Reflection.MetaReflectedAt] =
439440
JsonConvert.SerializeObject(DateTimeOffset.UtcNow);
440441

442+
if (sourceProperties.Labels)
443+
{
444+
newResource.Metadata.Labels ??= new Dictionary<string, string>();
445+
var newResourceLabels = newResource.Metadata.Labels;
446+
foreach (var label in source.Metadata.Labels)
447+
{
448+
if (sourceProperties.CanLabelBeReflected(label.Key))
449+
newResourceLabels[label.Key] = label.Value;
450+
}
451+
}
452+
441453
try
442454
{
443455
await OnResourceCreate(newResource, targetId.Namespace);
@@ -466,6 +478,19 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
466478
annotations[patchAnnotation.Key] = patchAnnotation.Value;
467479
patchDoc.Replace(e => e.Metadata.Annotations, annotations);
468480

481+
if (sourceProperties.Labels && source.Metadata.Labels != null)
482+
{
483+
var labels = new Dictionary<string, string>();
484+
if (targetResource.Metadata.Labels != null)
485+
labels = new Dictionary<string, string>(targetResource.Metadata.Labels);
486+
foreach (var label in source.Metadata.Labels)
487+
{
488+
if (sourceProperties.CanLabelBeReflected(label.Key))
489+
labels[label.Key] = label.Value;
490+
}
491+
patchDoc.Replace(e => e.Metadata.Labels, labels);
492+
}
493+
469494
await OnResourceConfigurePatch(source, patchDoc);
470495

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

0 commit comments

Comments
 (0)