Skip to content

Commit

Permalink
fix: invalid endpoint construction with K8s primitive entities. (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
Silvenga authored and buehler committed Apr 28, 2022
1 parent f7bed6e commit d83a1db
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 12 deletions.
7 changes: 1 addition & 6 deletions src/KubeOps/Operator/Webhooks/IMutationWebhook{TEntity}.cs
@@ -1,7 +1,6 @@
using System;
using System.Linq;
using System.Text;
using DotnetKubernetesClient.Entities;
using JsonDiffPatch;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -43,11 +42,7 @@ public interface IMutationWebhook<TEntity> : IAdmissionWebhook<TEntity, Mutation
/// <inheritdoc />
string IAdmissionWebhook<TEntity, MutationResult>.Endpoint
{
get
{
var crd = typeof(TEntity).CreateResourceDefinition();
return $"/{crd.Group}/{crd.Version}/{crd.Plural}/{GetType().Name}/mutate".ToLowerInvariant();
}
get => WebhookEndpointFactory.Create<TEntity>(GetType(), "/mutate");
}

/// <inheritdoc />
Expand Down
7 changes: 1 addition & 6 deletions src/KubeOps/Operator/Webhooks/IValidationWebhook{TEntity}.cs
@@ -1,5 +1,4 @@
using System.Linq;
using DotnetKubernetesClient.Entities;
using Newtonsoft.Json;

namespace KubeOps.Operator.Webhooks;
Expand Down Expand Up @@ -39,11 +38,7 @@ public interface IValidationWebhook<TEntity> : IAdmissionWebhook<TEntity, Valida
/// <inheritdoc />
string IAdmissionWebhook<TEntity, ValidationResult>.Endpoint
{
get
{
var crd = typeof(TEntity).CreateResourceDefinition();
return $"/{crd.Group}/{crd.Version}/{crd.Plural}/{GetType().Name}/validate".ToLowerInvariant();
}
get => WebhookEndpointFactory.Create<TEntity>(GetType(), "/validate");
}

/// <inheritdoc />
Expand Down
36 changes: 36 additions & 0 deletions src/KubeOps/Operator/Webhooks/WebhookEndpointFactory.cs
@@ -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 tests/KubeOps.Test/Operator/Webhook/WebhookEndpointFactory.Test.cs
@@ -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");
}
}
}

0 comments on commit d83a1db

Please sign in to comment.