Skip to content

Commit

Permalink
fix(webhook builder): Deepclone the service object on install to prev…
Browse files Browse the repository at this point in the history
…ent duplicate paths.
  • Loading branch information
buehler committed Aug 27, 2021
1 parent eb4d583 commit 67c52d7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 14 deletions.
18 changes: 7 additions & 11 deletions src/KubeOps/Operator/Commands/Management/Webhooks/Install.cs
Expand Up @@ -90,7 +90,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
new V1Service(
V1Service.KubeApiVersion,
V1Service.KubeKind,
new V1ObjectMeta(
new(
name: _settings.Name,
namespaceProperty: @namespace,
ownerReferences: deployment != null
Expand All @@ -104,7 +104,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
{ "operator", _settings.Name },
{ "usage", "webhook-service" },
}),
new V1ServiceSpec
new()
{
Ports = new List<V1ServicePort>
{
Expand All @@ -126,17 +126,14 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
_settings.Name,
null,
caBundle,
new Admissionregistrationv1ServiceReference
new()
{
Name = _settings.Name,
NamespaceProperty = @namespace,
});

await app.Out.WriteLineAsync("Create validator definition.");
var validatorConfig = _validatingWebhookConfigurationBuilder.BuildWebhookConfiguration(webhookConfig);

await _client.Delete<V1ValidatingWebhookConfiguration>(validatorConfig.Name());

if (deployment != null)
{
validatorConfig.Metadata.OwnerReferences = new List<V1OwnerReference>
Expand All @@ -145,13 +142,10 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
};
}

await _client.Create(validatorConfig);
await _client.Save(validatorConfig);

await app.Out.WriteLineAsync("Create mutator definition.");
var mutatorConfig = _mutatingWebhookConfigurationBuilder.BuildWebhookConfiguration(webhookConfig);

await _client.Delete<V1MutatingWebhookConfiguration>(mutatorConfig.Name());

if (deployment != null)
{
mutatorConfig.Metadata.OwnerReferences = new List<V1OwnerReference>
Expand All @@ -160,7 +154,9 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
};
}

await _client.Create(mutatorConfig);
await _client.Save(mutatorConfig);

await app.Out.WriteLineAsync("Installed webhook service and admission configurations.");

return ExitCodes.Success;
}
Expand Down
3 changes: 2 additions & 1 deletion src/KubeOps/Operator/Webhooks/MutatingWebhookBuilder.cs
Expand Up @@ -5,6 +5,7 @@
using DotnetKubernetesClient.Entities;
using k8s.Models;
using KubeOps.Operator.Builder;
using KubeOps.Operator.Entities.Extensions;
using KubeOps.Operator.Util;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -47,7 +48,7 @@ public List<V1MutatingWebhook> BuildWebhooks(WebhookConfig webhookConfig)
}
else
{
clientConfig.Service = webhookConfig.Service;
clientConfig.Service = webhookConfig.Service?.DeepClone();
if (clientConfig.Service != null)
{
clientConfig.Service.Path = endpoint;
Expand Down
3 changes: 2 additions & 1 deletion src/KubeOps/Operator/Webhooks/ValidatingWebhookBuilder.cs
Expand Up @@ -5,6 +5,7 @@
using DotnetKubernetesClient.Entities;
using k8s.Models;
using KubeOps.Operator.Builder;
using KubeOps.Operator.Entities.Extensions;
using KubeOps.Operator.Util;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -47,7 +48,7 @@ public List<V1ValidatingWebhook> BuildWebhooks(WebhookConfig webhookConfig)
}
else
{
clientConfig.Service = webhookConfig.Service;
clientConfig.Service = webhookConfig.Service?.DeepClone();
if (clientConfig.Service != null)
{
clientConfig.Service.Path = endpoint;
Expand Down
Expand Up @@ -18,7 +18,7 @@ public ValidatingWebhookConfigurationBuilder(ValidatingWebhookBuilder webhookBui
Kind = V1ValidatingWebhookConfiguration.KubeKind,
ApiVersion =
$"{V1ValidatingWebhookConfiguration.KubeGroup}/{V1ValidatingWebhookConfiguration.KubeApiVersion}",
Metadata = new V1ObjectMeta
Metadata = new()
{
Name = webhookConfig.OperatorName.TrimWebhookName("validators."),
},
Expand Down

0 comments on commit 67c52d7

Please sign in to comment.