Skip to content

Commit

Permalink
fix(generators): Remove direct dependency to IKubernetesClient.
Browse files Browse the repository at this point in the history
This fixes #346. For non interactive commands like generators
the (in-)direct dependency to IKubernetesClient is removed.
Hence, no more exceptions when no context is available.
  • Loading branch information
buehler committed Jan 2, 2022
1 parent 6a19596 commit cde5ef8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 34 deletions.
12 changes: 6 additions & 6 deletions src/KubeOps/Operator/Commands/Management/Install.cs
Expand Up @@ -4,6 +4,7 @@
using DotnetKubernetesClient;
using KubeOps.Operator.Entities;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Rest;

namespace KubeOps.Operator.Commands.Management;
Expand All @@ -15,22 +16,21 @@ namespace KubeOps.Operator.Commands.Management;
"Install the current custom resources (crd's) into the cluster of the actually selected context.")]
internal class Install
{
private readonly IKubernetesClient _client;

private readonly ICrdBuilder _crdBuilder;

public Install(IKubernetesClient client, ICrdBuilder crdBuilder)
public Install(ICrdBuilder crdBuilder)
{
_client = client;
_crdBuilder = crdBuilder;
}

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var client = app.GetRequiredService<IKubernetesClient>();

var error = false;
var crds = _crdBuilder.BuildCrds().ToList();
await app.Out.WriteLineAsync($"Found {crds.Count} CRD's.");
await app.Out.WriteLineAsync($@"Starting install into cluster with url ""{_client.ApiClient.BaseUri}"".");
await app.Out.WriteLineAsync($@"Starting install into cluster with url ""{client.ApiClient.BaseUri}"".");

foreach (var crd in crds)
{
Expand All @@ -39,7 +39,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)

try
{
await _client.Save(crd);
await client.Save(crd);
}
catch (HttpOperationException e)
{
Expand Down
12 changes: 6 additions & 6 deletions src/KubeOps/Operator/Commands/Management/Uninstall.cs
Expand Up @@ -4,6 +4,7 @@
using DotnetKubernetesClient;
using KubeOps.Operator.Entities;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Rest;

namespace KubeOps.Operator.Commands.Management;
Expand All @@ -15,13 +16,10 @@ namespace KubeOps.Operator.Commands.Management;
"Uninstall the current custom resources (crd's) from the cluster of the actually selected context.")]
internal class Uninstall
{
private readonly IKubernetesClient _client;

private readonly ICrdBuilder _crdBuilder;

public Uninstall(IKubernetesClient client, ICrdBuilder crdBuilder)
public Uninstall(ICrdBuilder crdBuilder)
{
_client = client;
_crdBuilder = crdBuilder;
}

Expand All @@ -30,6 +28,8 @@ public Uninstall(IKubernetesClient client, ICrdBuilder crdBuilder)

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var client = app.GetRequiredService<IKubernetesClient>();

var error = false;
var crds = _crdBuilder.BuildCrds().ToList();
await app.Out.WriteLineAsync($"Found {crds.Count} CRD's.");
Expand All @@ -40,15 +40,15 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
}

await app.Out.WriteLineAsync(
$@"Starting uninstall from the cluster with url ""{_client.ApiClient.BaseUri}"".");
$@"Starting uninstall from the cluster with url ""{client.ApiClient.BaseUri}"".");

foreach (var crd in crds)
{
await app.Out.WriteLineAsync(
$@"Uninstall ""{crd.Spec.Group}/{crd.Spec.Names.Kind}"" from the cluster");
try
{
await _client.Delete(crd);
await client.Delete(crd);
}
catch (HttpOperationException e)
{
Expand Down
18 changes: 9 additions & 9 deletions src/KubeOps/Operator/Commands/Management/Webhooks/Install.cs
Expand Up @@ -9,6 +9,7 @@
using KubeOps.Operator.Entities.Extensions;
using KubeOps.Operator.Webhooks;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;

namespace KubeOps.Operator.Commands.Management.Webhooks;

Expand All @@ -19,18 +20,15 @@ namespace KubeOps.Operator.Commands.Management.Webhooks;
"First, certificates will be generated, then a service is created and the validation config is created.")]
internal class Install
{
private readonly IKubernetesClient _client;
private readonly OperatorSettings _settings;
private readonly ValidatingWebhookConfigurationBuilder _validatingWebhookConfigurationBuilder;
private readonly MutatingWebhookConfigurationBuilder _mutatingWebhookConfigurationBuilder;

public Install(
IKubernetesClient client,
OperatorSettings settings,
ValidatingWebhookConfigurationBuilder validatingWebhookConfigurationBuilder,
MutatingWebhookConfigurationBuilder mutatingWebhookConfigurationBuilder)
{
_client = client;
_settings = settings;
_validatingWebhookConfigurationBuilder = validatingWebhookConfigurationBuilder;
_mutatingWebhookConfigurationBuilder = mutatingWebhookConfigurationBuilder;
Expand All @@ -52,7 +50,9 @@ internal class Install

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var @namespace = await _client.GetCurrentNamespace();
var client = app.GetRequiredService<IKubernetesClient>();

var @namespace = await client.GetCurrentNamespace();
using var certManager = new CertificateGenerator(app.Out);

#if DEBUG
Expand All @@ -75,7 +75,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
Path.Join(CaCertificatesPath, "ca.pem"),
Path.Join(CaCertificatesPath, "ca-key.pem"));

var deployment = (await _client.List<V1Deployment>(
var deployment = (await client.List<V1Deployment>(
@namespace,
new EqualsSelector("operator-deployment", _settings.Name))).FirstOrDefault();
if (deployment != null)
Expand All @@ -85,8 +85,8 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
}

await app.Out.WriteLineAsync("Create service.");
await _client.Delete<V1Service>(_settings.Name, @namespace);
await _client.Create(
await client.Delete<V1Service>(_settings.Name, @namespace);
await client.Create(
new V1Service(
V1Service.KubeApiVersion,
V1Service.KubeKind,
Expand Down Expand Up @@ -121,7 +121,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
validatorConfig.Metadata.OwnerReferences = new List<V1OwnerReference> { deployment.MakeOwnerReference(), };
}

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

await app.Out.WriteLineAsync("Create mutator definition.");
var mutatorConfig = _mutatingWebhookConfigurationBuilder.BuildWebhookConfiguration(webhookConfig);
Expand All @@ -130,7 +130,7 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)
mutatorConfig.Metadata.OwnerReferences = new List<V1OwnerReference> { deployment.MakeOwnerReference(), };
}

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

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

Expand Down
9 changes: 4 additions & 5 deletions src/KubeOps/Operator/Commands/Management/Webhooks/Register.cs
Expand Up @@ -5,6 +5,7 @@
using KubeOps.Operator.Builder;
using KubeOps.Operator.Webhooks;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;

namespace KubeOps.Operator.Commands.Management.Webhooks;

Expand All @@ -16,20 +17,17 @@ namespace KubeOps.Operator.Commands.Management.Webhooks;
internal class Register
{
private readonly OperatorSettings _settings;
private readonly IKubernetesClient _client;
private readonly IComponentRegistrar _componentRegistrar;
private readonly ValidatingWebhookConfigurationBuilder _validatingWebhookConfigurationBuilder;
private readonly MutatingWebhookConfigurationBuilder _mutatingWebhookConfigurationBuilder;

public Register(
OperatorSettings settings,
IKubernetesClient client,
IComponentRegistrar componentRegistrar,
MutatingWebhookConfigurationBuilder mutatingWebhookConfigurationBuilder,
ValidatingWebhookConfigurationBuilder validatingWebhookConfigurationBuilder)
{
_settings = settings;
_client = client;
_componentRegistrar = componentRegistrar;
_mutatingWebhookConfigurationBuilder = mutatingWebhookConfigurationBuilder;
_validatingWebhookConfigurationBuilder = validatingWebhookConfigurationBuilder;
Expand Down Expand Up @@ -78,6 +76,7 @@ internal class Register

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var client = app.GetRequiredService<IKubernetesClient>();
await app.Out.WriteLineAsync(
$"Found {_componentRegistrar.ValidatorRegistrations.Count} validator registrations.");
await app.Out.WriteLineAsync(
Expand All @@ -100,8 +99,8 @@ public async Task<int> OnExecuteAsync(CommandLineApplication app)

await app.Out.WriteLineAsync($@"Install ""{validatorConfig.Metadata.Name}"" validator on cluster.");
await app.Out.WriteLineAsync($@"Install ""{mutatorConfig.Metadata.Name}"" mutator on cluster.");
await _client.Save(validatorConfig);
await _client.Save(mutatorConfig);
await client.Save(validatorConfig);
await client.Save(mutatorConfig);

return ExitCodes.Success;
}
Expand Down
11 changes: 3 additions & 8 deletions src/KubeOps/Operator/Commands/Utilities/Version.cs
@@ -1,6 +1,7 @@
using System.Threading.Tasks;
using DotnetKubernetesClient;
using McMaster.Extensions.CommandLineUtils;
using Microsoft.Extensions.DependencyInjection;

namespace KubeOps.Operator.Commands.Utilities;

Expand All @@ -10,16 +11,10 @@ namespace KubeOps.Operator.Commands.Utilities;
Description = "Prints the actual server version of the connected kubernetes cluster.")]
internal class Version
{
private readonly IKubernetesClient _client;

public Version(IKubernetesClient client)
{
_client = client;
}

public async Task<int> OnExecuteAsync(CommandLineApplication app)
{
var version = await _client.GetServerVersion();
var client = app.GetRequiredService<IKubernetesClient>();
var version = await client.GetServerVersion();
await app.Out.WriteLineAsync(
$@"The kubernetes api reported the following version:
Git-Version: {version.GitVersion}
Expand Down

0 comments on commit cde5ef8

Please sign in to comment.