Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
0bb8823
Initial plan
Copilot Jul 19, 2025
efe54c6
Initial exploration and plan for modernizing exception patterns
Copilot Jul 19, 2025
f74ca06
Update core transform classes to use modern exception helpers
Copilot Jul 19, 2025
4e6297b
Update middleware and config classes to use modern exception helpers
Copilot Jul 19, 2025
f23e73a
Bulk update of exception patterns using automated script
Copilot Jul 19, 2025
a1e0f88
Update remaining complex exception patterns manually
Copilot Jul 19, 2025
770587c
Revert global.json SDK version to original state
Copilot Jul 19, 2025
37b06df
Remove redundant discard assignments after ThrowIfNull calls
Copilot Jul 19, 2025
c0c34cc
Place null checks before assignments in WebSocketsTelemetryMiddleware
Copilot Jul 19, 2025
ea19a7f
Group null checks before assignments in IngressCache constructor
Copilot Jul 19, 2025
807cf6b
Group null checks before assignments and remove unnecessary null prop…
Copilot Jul 19, 2025
15347e2
Group null checks before assignments in constructor patterns
Copilot Jul 19, 2025
cd29e51
Merge pull request #1 from WeihanLi/copilot/fix-22b218dd-f466-4c03-82…
WeihanLi Jul 19, 2025
a0b1337
adopt more argument exception throw helper
WeihanLi Jul 19, 2025
a3f34fb
fix test case
WeihanLi Jul 19, 2025
6f7a9fa
Update src/ReverseProxy/Delegation/HttpSysDelegator.cs
WeihanLi Jul 22, 2025
1eef853
Update src/TelemetryConsumption/EventListenerService.cs
WeihanLi Jul 22, 2025
71e484b
Update src/ReverseProxy/Forwarder/HttpForwarder.cs
WeihanLi Jul 22, 2025
374b605
Update src/ReverseProxy/WebSocketsTelemetry/HttpUpgradeFeatureWrapper.cs
WeihanLi Jul 22, 2025
7feba1c
Update src/ReverseProxy/WebSocketsTelemetry/HttpConnectFeatureWrapper.cs
WeihanLi Jul 22, 2025
93444f1
Update src/ReverseProxy/Utilities/ParsedMetadataEntry.cs
WeihanLi Jul 22, 2025
99cd18a
Update src/ReverseProxy/Transforms/PathTransformExtensions.cs
WeihanLi Jul 22, 2025
e44505a
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
8c56156
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
eddb232
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
3012010
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
a6fe6ec
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
cdca9ab
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
11d26c0
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
5112cce
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
153ee7f
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
c217983
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
433640e
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
144bd8f
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
26ed1fe
Apply suggestion from @MihaZupan
WeihanLi Jul 22, 2025
c19200b
apply more suggestions
WeihanLi Jul 22, 2025
f0799f4
refactor: apply suggestions for StreamCopyHttpContent
WeihanLi Jul 22, 2025
28a87f9
refactor: prefer ToArray directly according to the suggestion
WeihanLi Jul 22, 2025
65ecbbd
refactor: align null check style for configChangeListeners
WeihanLi Jul 22, 2025
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 @@ -13,7 +13,8 @@ public sealed class WebSocketsTelemetryConsumer : IWebSocketsTelemetryConsumer

public WebSocketsTelemetryConsumer(ILogger<WebSocketsTelemetryConsumer> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
ArgumentNullException.ThrowIfNull(logger);
_logger = logger;
}

public void OnWebSocketClosed(DateTime timestamp, DateTime establishedTime, WebSocketCloseReason closeReason, long messagesRead, long messagesWritten)
Expand Down
5 changes: 1 addition & 4 deletions src/Kubernetes.Controller/Caching/Endpoints.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ public struct Endpoints
{
public Endpoints(V1Endpoints endpoints)
{
if (endpoints is null)
{
throw new ArgumentNullException(nameof(endpoints));
}
ArgumentNullException.ThrowIfNull(endpoints);

Name = endpoints.Name();
Subsets = endpoints.Subsets;
Expand Down
28 changes: 12 additions & 16 deletions src/Kubernetes.Controller/Caching/IngressCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@ public class IngressCache : ICache

public IngressCache(IOptions<YarpOptions> options, IServerCertificateSelector certificateSelector, ICertificateHelper certificateHelper, ILogger<IngressCache> logger)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
_certificateSelector = certificateSelector ?? throw new ArgumentNullException(nameof(certificateSelector));
_certificateHelper = certificateHelper ?? throw new ArgumentNullException(nameof(certificateHelper));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
ArgumentNullException.ThrowIfNull(options?.Value);
ArgumentNullException.ThrowIfNull(certificateSelector);
ArgumentNullException.ThrowIfNull(certificateHelper);
ArgumentNullException.ThrowIfNull(logger);

_options = options.Value;
_certificateSelector = certificateSelector;
_certificateHelper = certificateHelper;
_logger = logger;
}

public void Update(WatchEventType eventType, V1IngressClass ingressClass)
{
if (ingressClass is null)
{
throw new ArgumentNullException(nameof(ingressClass));
}
ArgumentNullException.ThrowIfNull(ingressClass);

if (!string.Equals(_options.ControllerClass, ingressClass.Spec.Controller, StringComparison.OrdinalIgnoreCase))
{
Expand Down Expand Up @@ -72,10 +74,7 @@ public void Update(WatchEventType eventType, V1IngressClass ingressClass)

public bool Update(WatchEventType eventType, V1Ingress ingress)
{
if (ingress is null)
{
throw new ArgumentNullException(nameof(ingress));
}
ArgumentNullException.ThrowIfNull(ingress);

Namespace(ingress.Namespace()).Update(eventType, ingress);
return true;
Expand All @@ -84,10 +83,7 @@ public bool Update(WatchEventType eventType, V1Ingress ingress)

public ImmutableList<string> Update(WatchEventType eventType, V1Service service)
{
if (service is null)
{
throw new ArgumentNullException(nameof(service));
}
ArgumentNullException.ThrowIfNull(service);

return Namespace(service.Namespace()).Update(eventType, service);
}
Expand Down
5 changes: 1 addition & 4 deletions src/Kubernetes.Controller/Caching/IngressClassData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public struct IngressClassData
{
public IngressClassData(V1IngressClass ingressClass)
{
if (ingressClass is null)
{
throw new ArgumentNullException(nameof(ingressClass));
}
ArgumentNullException.ThrowIfNull(ingressClass);

IngressClass = ingressClass;
IsDefault = GetDefaultAnnotation(ingressClass);
Expand Down
5 changes: 1 addition & 4 deletions src/Kubernetes.Controller/Caching/IngressData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public struct IngressData
{
public IngressData(V1Ingress ingress)
{
if (ingress is null)
{
throw new ArgumentNullException(nameof(ingress));
}
ArgumentNullException.ThrowIfNull(ingress);

Spec = ingress.Spec;
Metadata = ingress.Metadata;
Expand Down
20 changes: 4 additions & 16 deletions src/Kubernetes.Controller/Caching/NamespaceCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ public class NamespaceCache

public void Update(WatchEventType eventType, V1Ingress ingress)
{
if (ingress is null)
{
throw new ArgumentNullException(nameof(ingress));
}
ArgumentNullException.ThrowIfNull(ingress);

var serviceNames = ImmutableList<string>.Empty;

Expand Down Expand Up @@ -121,10 +118,7 @@ public void Update(WatchEventType eventType, V1Ingress ingress)

public ImmutableList<string> Update(WatchEventType eventType, V1Service service)
{
if (service is null)
{
throw new ArgumentNullException(nameof(service));
}
ArgumentNullException.ThrowIfNull(service);

var serviceName = service.Name();
lock (_sync)
Expand All @@ -151,10 +145,7 @@ public ImmutableList<string> Update(WatchEventType eventType, V1Service service)

public void GetKeys(string ns, List<NamespacedName> keys)
{
if (keys is null)
{
throw new ArgumentNullException(nameof(keys));
}
ArgumentNullException.ThrowIfNull(keys);

lock (_sync)
{
Expand All @@ -167,10 +158,7 @@ public void GetKeys(string ns, List<NamespacedName> keys)

public ImmutableList<string> Update(WatchEventType eventType, V1Endpoints endpoints)
{
if (endpoints is null)
{
throw new ArgumentNullException(nameof(endpoints));
}
ArgumentNullException.ThrowIfNull(endpoints);

var serviceName = endpoints.Name();
lock (_sync)
Expand Down
5 changes: 1 addition & 4 deletions src/Kubernetes.Controller/Caching/ServiceData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ public struct ServiceData
{
public ServiceData(V1Service service)
{
if (service is null)
{
throw new ArgumentNullException(nameof(service));
}
ArgumentNullException.ThrowIfNull(service);

Spec = service.Spec;
Metadata = service.Metadata;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ public class CertificateHelper : ICertificateHelper

public CertificateHelper(ILogger<CertificateHelper> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
ArgumentNullException.ThrowIfNull(logger);
_logger = logger;
}

public X509Certificate2 ConvertCertificate(NamespacedName namespacedName, V1Secret secret)
Expand Down
5 changes: 4 additions & 1 deletion src/Kubernetes.Controller/Client/ResourceInformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ public ResourceInformer(
ILogger logger)
: base(hostApplicationLifetime, logger)
{
ArgumentNullException.ThrowIfNull(client);
ArgumentNullException.ThrowIfNull(selector);

Client = client;
_selector = selector ?? throw new ArgumentNullException(nameof(selector));
_selector = selector;
_names = GroupApiVersionKind.From<TResource>();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public V1IngressResourceStatusUpdater(
ICache cache,
ILogger<V1ServiceResourceInformer> logger)
{
_options = options?.Value ?? throw new ArgumentNullException(nameof(options));
ArgumentNullException.ThrowIfNull(options?.Value);
_options = options.Value;
_client = client;
_cache = cache;
_logger = logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public MessageConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterCon

public MessageConfig(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, string revisionId)
{
RevisionId = revisionId ?? throw new ArgumentNullException(nameof(revisionId));
ArgumentNullException.ThrowIfNull(revisionId);
RevisionId = revisionId;
Routes = routes;
Clusters = clusters;
ChangeToken = new CancellationChangeToken(_cts.Token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ protected BackgroundHostedService(
IHostApplicationLifetime hostApplicationLifetime,
ILogger logger)
{
_hostApplicationLifetime = hostApplicationLifetime ?? throw new ArgumentNullException(nameof(hostApplicationLifetime));
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
ArgumentNullException.ThrowIfNull(hostApplicationLifetime);
ArgumentNullException.ThrowIfNull(logger);
_hostApplicationLifetime = hostApplicationLifetime;
Logger = logger;

// register the stoppingToken to become cancelled as soon as the
// shutdown sequence is initiated.
Expand Down
9 changes: 3 additions & 6 deletions src/Kubernetes.Controller/NamespacedName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ public NamespacedName(string name)
/// <returns>NamespacedName.</returns>
public static NamespacedName From(IKubernetesObject<V1ObjectMeta> resource)
{
if (resource is null)
{
throw new ArgumentNullException(nameof(resource));
}
ArgumentNullException.ThrowIfNull(resource);

return new NamespacedName(resource.Namespace(), resource.Name());
}
Expand All @@ -97,8 +94,8 @@ public static NamespacedName From(IKubernetesObject<V1ObjectMeta> resource)
/// <returns>NamespacedName.</returns>
public static NamespacedName From(V1ObjectMeta metadata, [NotNull] V1OwnerReference ownerReference, bool? clusterScoped = null)
{
_ = metadata ?? throw new ArgumentNullException(nameof(metadata));
_ = ownerReference ?? throw new ArgumentNullException(nameof(ownerReference));
ArgumentNullException.ThrowIfNull(metadata);
ArgumentNullException.ThrowIfNull(ownerReference);

return new NamespacedName(
clusterScoped ?? false ? null : metadata.NamespaceProperty,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ public override string ToString()

public async Task ExecuteResultAsync(ActionContext context)
{
if (context is null)
{
throw new ArgumentNullException(nameof(context));
}
ArgumentNullException.ThrowIfNull(context);

var cancellationToken = context.HttpContext.RequestAborted;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class DispatchConfigProvider : IUpdateConfig

public DispatchConfigProvider(IDispatcher dispatcher)
{
_dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
ArgumentNullException.ThrowIfNull(dispatcher);
_dispatcher = dispatcher;
}

public async Task UpdateAsync(IReadOnlyList<RouteConfig> routes, IReadOnlyList<ClusterConfig> clusters, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ public static class MessageConfigProviderExtensions
{
public static IReverseProxyBuilder LoadFromMessages(this IReverseProxyBuilder builder)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
ArgumentNullException.ThrowIfNull(builder);

var provider = new KubernetesConfigProvider();
builder.Services.AddSingleton<IProxyConfigProvider>(provider);
Expand Down
5 changes: 1 addition & 4 deletions src/Kubernetes.Controller/Protocol/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public Receiver(
ILogger<Receiver> logger,
IUpdateConfig proxyConfigProvider) : base(hostApplicationLifetime, logger)
{
if (options is null)
{
throw new ArgumentNullException(nameof(options));
}
ArgumentNullException.ThrowIfNull(options);

_options = options.Value;

Expand Down
7 changes: 5 additions & 2 deletions src/Kubernetes.Controller/Services/IngressController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ public IngressController(

_queue = new ProcessingRateLimitedQueue<QueueItem>(perSecond: 0.5, burst: 1);

_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_reconciler = reconciler ?? throw new ArgumentNullException(nameof(reconciler));
ArgumentNullException.ThrowIfNull(cache);
ArgumentNullException.ThrowIfNull(reconciler);

_cache = cache;
_reconciler = reconciler;

_ingressChangeQueueItem = new QueueItem("Ingress Change");
}
Expand Down
10 changes: 7 additions & 3 deletions src/Kubernetes.Controller/Services/Reconciler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ public partial class Reconciler : IReconciler

public Reconciler(ICache cache, IUpdateConfig updateConfig, IIngressResourceStatusUpdater ingressResourceStatusUpdater, ILogger<Reconciler> logger)
{
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_updateConfig = updateConfig ?? throw new ArgumentNullException(nameof(updateConfig));
_ingressResourceStatusUpdater = ingressResourceStatusUpdater ?? throw new ArgumentNullException(nameof(ingressResourceStatusUpdater));
ArgumentNullException.ThrowIfNull(cache);
ArgumentNullException.ThrowIfNull(updateConfig);
ArgumentNullException.ThrowIfNull(ingressResourceStatusUpdater);

_cache = cache;
_updateConfig = updateConfig;
_ingressResourceStatusUpdater = ingressResourceStatusUpdater;
_logger = logger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ public HealthCheckValidator(IEnumerable<IAvailableDestinationsPolicy> availableD
IEnumerable<IActiveHealthCheckPolicy> activeHealthCheckPolicies,
IEnumerable<IPassiveHealthCheckPolicy> passiveHealthCheckPolicies)
{
_availableDestinationsPolicies = availableDestinationsPolicies?.ToDictionaryByUniqueId(p => p.Name) ?? throw new ArgumentNullException(nameof(availableDestinationsPolicies));
_activeHealthCheckPolicies = activeHealthCheckPolicies?.ToDictionaryByUniqueId(p => p.Name) ?? throw new ArgumentNullException(nameof(availableDestinationsPolicies));
_passiveHealthCheckPolicies = passiveHealthCheckPolicies?.ToDictionaryByUniqueId(p => p.Name) ?? throw new ArgumentNullException(nameof(availableDestinationsPolicies));
ArgumentNullException.ThrowIfNull(availableDestinationsPolicies);
ArgumentNullException.ThrowIfNull(activeHealthCheckPolicies);
ArgumentNullException.ThrowIfNull(passiveHealthCheckPolicies);

_availableDestinationsPolicies = availableDestinationsPolicies.ToDictionaryByUniqueId(p => p.Name);
_activeHealthCheckPolicies = activeHealthCheckPolicies.ToDictionaryByUniqueId(p => p.Name);
_passiveHealthCheckPolicies = passiveHealthCheckPolicies.ToDictionaryByUniqueId(p => p.Name);
}

public ValueTask ValidateAsync(ClusterConfig cluster, IList<Exception> errors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ internal sealed class LoadBalancingValidator : IClusterValidator
private readonly FrozenDictionary<string, ILoadBalancingPolicy> _loadBalancingPolicies;
public LoadBalancingValidator(IEnumerable<ILoadBalancingPolicy> loadBalancingPolicies)
{
_loadBalancingPolicies = loadBalancingPolicies?.ToDictionaryByUniqueId(p => p.Name) ?? throw new ArgumentNullException(nameof(loadBalancingPolicies));
ArgumentNullException.ThrowIfNull(loadBalancingPolicies);
_loadBalancingPolicies = loadBalancingPolicies.ToDictionaryByUniqueId(p => p.Name);
}

public ValueTask ValidateAsync(ClusterConfig cluster, IList<Exception> errors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ internal sealed class SessionAffinityValidator : IClusterValidator

public SessionAffinityValidator(IEnumerable<IAffinityFailurePolicy> affinityFailurePolicies)
{
_affinityFailurePolicies = affinityFailurePolicies?.ToDictionaryByUniqueId(p => p.Name) ?? throw new ArgumentNullException(nameof(affinityFailurePolicies));
ArgumentNullException.ThrowIfNull(affinityFailurePolicies);
_affinityFailurePolicies = affinityFailurePolicies.ToDictionaryByUniqueId(p => p.Name);
}

public ValueTask ValidateAsync(ClusterConfig cluster, IList<Exception> errors)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ public ConfigurationConfigProvider(
ILogger<ConfigurationConfigProvider> logger,
IConfiguration configuration)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
ArgumentNullException.ThrowIfNull(logger);
ArgumentNullException.ThrowIfNull(configuration);
_logger = logger;
_configuration = configuration;
}

public void Dispose()
Expand Down
15 changes: 10 additions & 5 deletions src/ReverseProxy/Configuration/ConfigValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ public ConfigValidator(ITransformBuilder transformBuilder,
IEnumerable<IRouteValidator> routeValidators,
IEnumerable<IClusterValidator> clusterValidators)
{
_transformBuilder = transformBuilder ?? throw new ArgumentNullException(nameof(transformBuilder));
_routeValidators = routeValidators?.ToArray() ?? throw new ArgumentNullException(nameof(routeValidators));
_clusterValidators = clusterValidators?.ToArray() ?? throw new ArgumentNullException(nameof(clusterValidators));
ArgumentNullException.ThrowIfNull(transformBuilder);
ArgumentNullException.ThrowIfNull(routeValidators);
ArgumentNullException.ThrowIfNull(clusterValidators);

_transformBuilder = transformBuilder;
_routeValidators = routeValidators.ToArray();
_clusterValidators = clusterValidators.ToArray();
}

// Note this performs all validation steps without short-circuiting in order to report all possible errors.
public async ValueTask<IList<Exception>> ValidateRouteAsync(RouteConfig route)
{
_ = route ?? throw new ArgumentNullException(nameof(route));
ArgumentNullException.ThrowIfNull(route);
var errors = new List<Exception>();

if (string.IsNullOrEmpty(route.RouteId))
Expand Down Expand Up @@ -61,7 +65,8 @@ public async ValueTask<IList<Exception>> ValidateRouteAsync(RouteConfig route)
// Note this performs all validation steps without short-circuiting in order to report all possible errors.
public async ValueTask<IList<Exception>> ValidateClusterAsync(ClusterConfig cluster)
{
_ = cluster ?? throw new ArgumentNullException(nameof(cluster));
ArgumentNullException.ThrowIfNull(cluster);

var errors = new List<Exception>();

if (string.IsNullOrEmpty(cluster.ClusterId))
Expand Down
Loading
Loading