Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to reflect whitelisted labels #464

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public static class Reflection
public static string AllowedNamespaces => $"{Prefix}/reflection-allowed-namespaces";
public static string AutoEnabled => $"{Prefix}/reflection-auto-enabled";
public static string AutoNamespaces => $"{Prefix}/reflection-auto-namespaces";
public static string Labels => $"{Prefix}/reflection-labels";
public static string LabelsIncluded => $"{Prefix}/reflection-labels-included";
public static string Reflects => $"{Prefix}/reflects";


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public static bool CanBeAutoReflectedToNamespace(this ReflectorProperties proper
}


public static bool CanLabelBeReflected(this ReflectorProperties properties, string label)
{
return properties.Labels && PatternListMatch(properties.LabelsIncluded, label);
}


private static bool PatternListMatch(string patternList, string value)
{
if (string.IsNullOrEmpty(patternList)) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ public static ReflectorProperties GetReflectionProperties(this V1ObjectMeta meta
? autoNamespaces ?? string.Empty
: string.Empty,

Labels = metadata.SafeAnnotations()
.TryGet(Annotations.Reflection.Labels, out bool labels) && labels,

LabelsIncluded = metadata.SafeAnnotations()
.TryGet(Annotations.Reflection.LabelsIncluded, out string? labelsIncluded)
? labelsIncluded ?? string.Empty
: string.Empty,

Reflects = metadata.SafeAnnotations()
.TryGet(Annotations.Reflection.Reflects, out string? metaReflects)
? string.IsNullOrWhiteSpace(metaReflects) ? KubeRef.Empty :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class ReflectorProperties
public string AllowedNamespaces { get; set; } = string.Empty;
public bool AutoEnabled { get; set; }
public string AutoNamespaces { get; set; } = string.Empty;
public bool Labels { get; set; }
public string LabelsIncluded { get; set; } = string.Empty;
public KubeRef Reflects { get; set; } = KubeRef.Empty;

public string Version { get; set; } = string.Empty;
Expand Down
27 changes: 26 additions & 1 deletion src/ES.Kubernetes.Reflector/Core/Mirroring/ResourceMirror.cs
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,8 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
{
source = sourceResource;
}


var sourceProperties = source.GetReflectionProperties();

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

if (sourceProperties.Labels)
{
newResource.Metadata.Labels ??= new Dictionary<string, string>();
var newResourceLabels = newResource.Metadata.Labels;
foreach (var label in source.Metadata.Labels)
{
if (sourceProperties.CanLabelBeReflected(label.Key))
newResourceLabels[label.Key] = label.Value;
}
}

try
{
await OnResourceCreate(newResource, targetId.Namespace);
Expand Down Expand Up @@ -476,6 +488,19 @@ private async Task ResourceReflect(KubeRef sourceId, KubeRef targetId, TResource
annotations[patchAnnotation.Key] = patchAnnotation.Value;
patchDoc.Replace(e => e.Metadata.Annotations, annotations);

if (sourceProperties.Labels && source.Metadata.Labels != null)
{
var labels = new Dictionary<string, string>();
if (targetResource.Metadata.Labels != null)
labels = new Dictionary<string, string>(targetResource.Metadata.Labels);
foreach (var label in source.Metadata.Labels)
{
if (sourceProperties.CanLabelBeReflected(label.Key))
labels[label.Key] = label.Value;
}
patchDoc.Replace(e => e.Metadata.Labels, labels);
}

await OnResourceConfigurePatch(source, patchDoc);

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