Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
fix: some fixes on events spamming (#61)
Browse files Browse the repository at this point in the history
* fix: some fixes on events spamming

* bump version

---------

Co-authored-by: Pumba98 <[email protected]>
  • Loading branch information
BlowaXD and Pumba98 authored Jul 16, 2023
1 parent 3d72d89 commit 5d8ccff
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 19 deletions.
4 changes: 2 additions & 2 deletions charts/bitwarden-secret-operator/Chart.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ description: Deploy the Bitwarden Secret Operator

type: application

version: "0.11.0"
version: "0.12.0"

appVersion: "0.11.0"
appVersion: "0.12.0"

keywords:
- operator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public class BitwardenSecretController : ControllerBase, IResourceController<Bit
private readonly BitwardenCliWrapper _cliWrapper;
private readonly IEventManager _eventManager;

public BitwardenSecretController(ILogger<BitwardenSecretController> logger, KubernetesClient kubernetesClient, BitwardenCliWrapper cliWrapper, IOptions<BitwardenOperatorOptions> operatorOptions, IEventManager eventManager)
public BitwardenSecretController(ILogger<BitwardenSecretController> logger, KubernetesClient kubernetesClient, BitwardenCliWrapper cliWrapper, IOptions<BitwardenOperatorOptions> operatorOptions,
IEventManager eventManager)
{
_logger = logger;
_kubernetesClient = kubernetesClient;
Expand All @@ -31,38 +32,38 @@ public BitwardenSecretController(ILogger<BitwardenSecretController> logger, Kube

public async Task<ResourceControllerResult?> ReconcileAsync(BitwardenSecretCrd entity)
{
BitwardenSecretSpec spec = entity.Spec;
BitwardenSecretSpec spec = entity.Spec;

string? destinationName = spec.Name ?? entity.Name();
string? destinationNamespace = spec.Namespace ?? entity.Namespace();
string? destinationName = spec.Name ?? entity.Name();
string? destinationNamespace = spec.Namespace ?? entity.Namespace();
try
{
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", "Received reconcile");
var secret = await _kubernetesClient.Get<V1Secret>(destinationName, destinationNamespace);
if (secret == null)
{
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", $"Couldn't find secret {destinationName} in namespace {destinationNamespace}, creating it...");
_logger.LogInformation("Secret: {SecretName} in namespace: {Namespace} couldn't be found, creating it", destinationName, destinationNamespace);

// create
secret = await entity.GetSecretAsync(_cliWrapper);
secret = await _kubernetesClient.Create<V1Secret>(secret);

// created events
await _eventManager.PublishAsync(secret, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, created");
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, creation successful");
await _eventManager.PublishAsync(secret, "Created", $"Secret {destinationName} in namespace {destinationNamespace}, created");
_logger.LogInformation("Secret: {SecretName} in namespace: {Namespace} created", destinationName, destinationNamespace);
}
else
{
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, updating it...");
await _eventManager.PublishAsync(secret, "Updating", $"Secret {destinationName} in namespace {destinationNamespace}, updating it...");
_logger.LogInformation("Secret: {SecretName} in namespace: {Namespace} exists, updating it", destinationName, destinationNamespace);

// update
secret = await entity.GetSecretAsync(_cliWrapper);
await _kubernetesClient.Update<V1Secret>(secret);

// updated events
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, updated!");
await _eventManager.PublishAsync(secret, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, updated");

await _eventManager.PublishAsync(secret, "Updated", $"Secret {destinationName} in namespace {destinationNamespace}, updated!");

_logger.LogInformation("Secret: {SecretName} in namespace: {Namespace} updated", destinationName, destinationNamespace);
}

Expand All @@ -71,21 +72,21 @@ public BitwardenSecretController(ILogger<BitwardenSecretController> logger, Kube
}
catch (Exception e)
{
await _eventManager.PublishAsync(entity, "bitwarden-secret-operator", $"Secret {destinationName} in namespace {destinationNamespace}, failed to create, check operator logs");
await _eventManager.PublishAsync(entity, "Failed", $"Secret {destinationName} in namespace {destinationNamespace}, failed to create, check operator logs", EventType.Warning);
_logger.LogError(e, "[{Method}] Failed", nameof(ReconcileAsync));
// requeue the event in 15 seconds
if (_operatorOptions.DelayAfterFailedWebhook is null)
{
throw;
}

return ResourceControllerResult.RequeueEvent(_operatorOptions.DelayAfterFailedWebhook.Value);
}
}

public async Task StatusModifiedAsync(BitwardenSecretCrd entity)
public Task StatusModifiedAsync(BitwardenSecretCrd entity)
{
return;
return Task.CompletedTask;
}

public async Task DeletedAsync(BitwardenSecretCrd entity)
Expand Down

0 comments on commit 5d8ccff

Please sign in to comment.