Skip to content

Commit

Permalink
Fix k8s name validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmelsayed committed May 4, 2019
1 parent 94a4840 commit 476884e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/Azure.Functions.Cli/Kubernetes/KubernetesHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static class KubernetesHelper
"mutatingwebhookconfiguration.admissionregistration.k8s.io/osiris-osiris-edge-proxy-injector"
};

internal static void ValidateKubernetesName(string name)
public static void ValidateKubernetesName(string name)
{
var regExValue = "^[a-z0-9\\-\\.]*$";
var regEx = new Regex(regExValue);
Expand All @@ -59,10 +59,6 @@ internal static void ValidateKubernetesName(string name)
{
throw new CliException("Kubernetes name must be < 253 characters.\n" + kNameDoc);
}
else if (!name.All(Char.IsLower))
{
throw new CliException("Kubernetes name must all lowercase.\n" + kNameDoc);
}
else if (!regEx.IsMatch(name))
{
throw new CliException($"Kubernetes namemust match {regExValue}.\n" + kNameDoc);
Expand Down
32 changes: 32 additions & 0 deletions test/Azure.Functions.Cli.Tests/KubernetesHelperUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Azure.Functions.Cli.Common;
using Azure.Functions.Cli.Kubernetes;
using FluentAssertions;
using Xunit;

namespace Azure.Functions.Cli.Tests
{
public class KubernetesHelperUnitTests
{
[Theory]
[InlineData("normalname", true)]
[InlineData("name-with-dashes", true)]
[InlineData("name-with-numbers-938-234", true)]
[InlineData("name with spaces", false)]
[InlineData("NameWithCapital", false)]
[InlineData("name@something", false)]
public void ValidateKubernetesNames(string name, bool isValid)
{
try
{
KubernetesHelper.ValidateKubernetesName(name);
}
catch
{
if (isValid)
{
throw;
}
}
}
}
}

0 comments on commit 476884e

Please sign in to comment.