Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: invalid endpoint construction with K8s primitive entities. (#426)
- Loading branch information
Showing
4 changed files
with
55 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.Text; | ||
using DotnetKubernetesClient.Entities; | ||
|
||
namespace KubeOps.Operator.Webhooks | ||
{ | ||
internal static class WebhookEndpointFactory | ||
{ | ||
internal static string Create<TEntity>(Type owningType, string suffix) | ||
{ | ||
var crd = typeof(TEntity).CreateResourceDefinition(); | ||
|
||
var builder = new StringBuilder(); | ||
|
||
if (!string.IsNullOrEmpty(crd.Group)) | ||
{ | ||
builder.Append($"/{crd.Group}"); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(crd.Version)) | ||
{ | ||
builder.Append($"/{crd.Version}"); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(crd.Plural)) | ||
{ | ||
builder.Append($"/{crd.Plural}"); | ||
} | ||
|
||
builder.Append($"/{owningType.Name}"); | ||
builder.Append(suffix); | ||
|
||
return builder.ToString().ToLowerInvariant(); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/KubeOps.Test/Operator/Webhook/WebhookEndpointFactory.Test.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using FluentAssertions; | ||
using k8s.Models; | ||
using KubeOps.Operator.Webhooks; | ||
using Xunit; | ||
|
||
namespace KubeOps.Test.Operator.Webhook | ||
{ | ||
public class WebhookEndpointFactoryTest | ||
{ | ||
[Fact] | ||
public void When_group_is_empty_then_WebhookEndpointFactory_should_not_create_empty_path_segments() | ||
{ | ||
var result = WebhookEndpointFactory.Create<V1Pod>(typeof(WebhookEndpointFactoryTest), "/mutator"); | ||
result.Should().Be("/v1/pods/webhookendpointfactorytest/mutator"); | ||
} | ||
} | ||
} |