Skip to content
Merged
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ You can customize the values of the helm deployment by using the following Value
| `image.pullPolicy` | Container image pull policy | `IfNotPresent` |
| `configuration.logging.minimumLevel` | Logging minimum level | `Information` |
| `configuration.watcher.timeout` | Maximum watcher lifetime in seconds | `` |
| `configuration.watcher.excludedNamespaces` | Comma-separated list of namespace glob patterns to exclude from watching. Supports `*` (any characters) and `?` (single character). Example: `"ephie-*,kube-system,*-temp"` | `` |
Comment thread
komapa marked this conversation as resolved.
Outdated
| `configuration.kubernetes.skipTlsVerify` | Skip TLS verify when connecting the the cluster | `false` |
| `rbac.enabled` | Create and use RBAC resources | `true` |
| `serviceAccount.create` | Create ServiceAccount | `true` |
Expand Down
7 changes: 7 additions & 0 deletions src/ES.Kubernetes.Reflector/Configuration/WatcherOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
public class WatcherOptions
{
public int? Timeout { get; set; }

/// <summary>
/// Comma-separated list of namespace patterns to exclude from watching.
/// Supports glob wildcards: * (any characters), ? (single character).
/// Example: "ephie-*,kube-system,*-temp"
/// </summary>
public string? ExcludedNamespaces { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading.Channels;
using ES.Kubernetes.Reflector.Configuration;
using ES.Kubernetes.Reflector.Watchers.Core.Events;
Expand Down Expand Up @@ -36,9 +37,16 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
FullMode = BoundedChannelFullMode.Wait
});

var excludedNamespacePatterns = ParseGlobPatterns(options.CurrentValue.Watcher?.ExcludedNamespaces);
long namespaceExcludedCount = 0;
try
{
logger.LogInformation("Requesting {type} resources", typeof(TResource).Name);
if (excludedNamespacePatterns.Length > 0)
logger.LogInformation(
"Requesting {type} resources (excluding namespaces matching: {patterns})",
typeof(TResource).Name, options.CurrentValue.Watcher?.ExcludedNamespaces);
else
logger.LogInformation("Requesting {type} resources", typeof(TResource).Name);

//Read using a separate task so the watcher doesn't get stuck waiting on subscribers to handle the event
_ = Task.Run(async () =>
Expand All @@ -62,6 +70,12 @@ await watcherEventHandler.Handle(new WatcherEvent
{
await foreach (var (type, item) in watchList)
{
if (IsNamespaceExcluded(item.Metadata?.NamespaceProperty, excludedNamespacePatterns))
Comment thread
komapa marked this conversation as resolved.
Outdated
{
namespaceExcludedCount++;
continue;
}

if (await OnResourceIgnoreCheck(item)) continue;
await eventChannel.Writer.WriteAsync(new WatcherEvent
{
Expand Down Expand Up @@ -91,8 +105,13 @@ await eventChannel.Writer.WriteAsync(new WatcherEvent

var sessionElapsed = sessionStopwatch.Elapsed;
sessionStopwatch.Stop();
logger.LogInformation("Session closed. Duration: {duration}. Faulted: {faulted}.", sessionElapsed,
sessionFaulted);
if (namespaceExcludedCount > 0)
logger.LogInformation(
"Session closed. Duration: {duration}. Faulted: {faulted}. Namespace-excluded events: {excluded}.",
sessionElapsed, sessionFaulted, namespaceExcludedCount);
else
logger.LogInformation("Session closed. Duration: {duration}. Faulted: {faulted}.",
sessionElapsed, sessionFaulted);

foreach (var handler in watcherClosedHandlers)
await handler.Handle(new WatcherClosed
Expand All @@ -107,4 +126,25 @@ await handler.Handle(new WatcherClosed
protected abstract IAsyncEnumerable<(WatchEventType, TResource)> OnGetWatcher(CancellationToken cancellationToken);

protected virtual Task<bool> OnResourceIgnoreCheck(TResource item) => Task.FromResult(false);

/// <summary>
/// Parses a comma-separated list of glob patterns into compiled Regex objects.
/// Supports * (any characters) and ? (single character).
/// </summary>
private static Regex[] ParseGlobPatterns(string? patterns)
{
if (string.IsNullOrWhiteSpace(patterns)) return [];
return patterns.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
.Where(p => !string.IsNullOrWhiteSpace(p))
Comment thread
komapa marked this conversation as resolved.
Outdated
.Select(p => new Regex(
"^" + Regex.Escape(p).Replace("\\*", ".*").Replace("\\?", ".") + "$",
RegexOptions.Compiled))
.ToArray();
}

private static bool IsNamespaceExcluded(string? ns, Regex[] patterns)
{
if (patterns.Length == 0 || string.IsNullOrEmpty(ns)) return false;
return patterns.Any(p => p.IsMatch(ns));
}
}
2 changes: 2 additions & 0 deletions src/helm/reflector/templates/cron.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ spec:
value: {{ .Values.configuration.logging.minimumLevel | quote }}
- name: ES_Reflector__Watcher__Timeout
value: {{ .Values.configuration.watcher.timeout | quote }}
- name: ES_Reflector__Watcher__ExcludedNamespaces
value: {{ .Values.configuration.watcher.excludedNamespaces | quote }}
- name: ES_Ignite__KubernetesClient__SkipTlsVerify
value: {{ .Values.configuration.kubernetes.skipTlsVerify | quote }}
{{- with .Values.extraEnv }}
Expand Down
2 changes: 2 additions & 0 deletions src/helm/reflector/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ spec:
value: {{ .Values.configuration.logging.minimumLevel | quote }}
- name: ES_Reflector__Watcher__Timeout
value: {{ .Values.configuration.watcher.timeout | quote }}
- name: ES_Reflector__Watcher__ExcludedNamespaces
value: {{ .Values.configuration.watcher.excludedNamespaces | quote }}
- name: ES_Ignite__KubernetesClient__SkipTlsVerify
value: {{ .Values.configuration.kubernetes.skipTlsVerify | quote }}
{{- with .Values.extraEnv }}
Expand Down
4 changes: 4 additions & 0 deletions src/helm/reflector/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ configuration:
minimumLevel: Information
watcher:
timeout: ""
# Comma-separated list of namespace glob patterns to exclude from watching.
Comment thread
komapa marked this conversation as resolved.
Outdated
# Supports wildcards: * (any characters), ? (single character).
# Example: "kube-system,*-suffix,prefix-*"
excludedNamespaces: ""
Comment thread
komapa marked this conversation as resolved.
kubernetes:
skipTlsVerify: false

Expand Down
Loading