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 KubernetesProbesOptionsValidator to ensure different ports for probes #5400

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -47,6 +47,10 @@ public static IServiceCollection AddKubernetesProbes(this IServiceCollection ser
_ = Throw.IfNull(services);
_ = Throw.IfNull(configure);

_ = services
.AddOptionsWithValidateOnStart<KubernetesProbesOptions, KubernetesProbesOptionsValidator>()
.Configure(configure);

var wrapperOptions = new KubernetesProbesOptions();

return services
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.Diagnostics.Probes;

internal sealed class KubernetesProbesOptionsValidator : IValidateOptions<KubernetesProbesOptions>
{
public ValidateOptionsResult Validate(string? name, KubernetesProbesOptions options)
{
var builder = new ValidateOptionsResultBuilder();

if (options.LivenessProbe.TcpPort == options.StartupProbe.TcpPort
|| options.LivenessProbe.TcpPort == options.ReadinessProbe.TcpPort
|| options.StartupProbe.TcpPort == options.ReadinessProbe.TcpPort)
{
builder.AddError("Liveness, startup and readiness probes must use different ports.");
}

return builder.Build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Xunit;

namespace Microsoft.Extensions.Diagnostics.Probes.Test;

public class KubernetesProbesOptionsValidatorTests
makazeu marked this conversation as resolved.
Show resolved Hide resolved
{
[Fact]
public void Validator_DefaultValues_Succeeds()
{
var options = new KubernetesProbesOptions();
var result = new KubernetesProbesOptionsValidator().Validate(nameof(options), options);

Assert.True(result.Succeeded);
}

[Fact]
public void Validator_GivenValidOptions_Succeeds()
{
var options = new KubernetesProbesOptions();
options.LivenessProbe.TcpPort = 2305;
options.StartupProbe.TcpPort = 2306;
options.ReadinessProbe.TcpPort = 2307;

var result = new KubernetesProbesOptionsValidator().Validate(nameof(options), options);

Assert.True(result.Succeeded);
}

[Fact]
public void Validator_GivenInvalidOptions_Fails()
{
var options = new KubernetesProbesOptions();
options.LivenessProbe.TcpPort = 2305;
options.StartupProbe.TcpPort = 2305;
options.ReadinessProbe.TcpPort = 2307;

var validator = new KubernetesProbesOptionsValidator();
var result = validator.Validate(nameof(options), options);
Assert.True(result.Failed);

options.LivenessProbe.TcpPort = 2305;
options.StartupProbe.TcpPort = 2306;
options.ReadinessProbe.TcpPort = 2305;
result = validator.Validate(nameof(options), options);
Assert.True(result.Failed);

options.LivenessProbe.TcpPort = 2305;
options.StartupProbe.TcpPort = 2306;
options.ReadinessProbe.TcpPort = 2306;
result = validator.Validate(nameof(options), options);
Assert.True(result.Failed);

options.LivenessProbe.TcpPort = 2305;
options.StartupProbe.TcpPort = 2305;
options.ReadinessProbe.TcpPort = 2305;
result = validator.Validate(nameof(options), options);
Assert.True(result.Failed);
}
}
Loading