-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSqlServerFinalizer.cs
More file actions
112 lines (95 loc) · 4.79 KB
/
Copy pathSqlServerFinalizer.cs
File metadata and controls
112 lines (95 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using k8s;
using k8s.Models;
using KubeOps.Abstractions.Reconciliation;
using KubeOps.Abstractions.Reconciliation.Finalizer;
using KubeOps.KubernetesClient;
using SqlServerOperator.Entities.V1Alpha1;
namespace SqlServerOperator.Finalizers.V1Alpha1;
public class SQLServerFinalizer(ILogger<SQLServerFinalizer> logger, IKubernetesClient kubernetesClient) : IEntityFinalizer<V1Alpha1SQLServer>
{
public async Task<ReconciliationResult<V1Alpha1SQLServer>> FinalizeAsync(V1Alpha1SQLServer entity, CancellationToken cancellationToken)
{
logger.LogInformation("Finalizing SQLServer: {Name}", entity.Metadata.Name);
var namespaceName = entity.Metadata.NamespaceProperty;
var statefulSetName = $"{entity.Metadata.Name}-statefulset";
var serviceName = $"{entity.Metadata.Name}-service";
var secretName = entity.Spec.SecretName ?? $"{entity.Metadata.Name}-secret";
var configMapName = $"{entity.Metadata.Name}-config";
try
{
// Delete the StatefulSet
await DeleteStatefulSetAsync(statefulSetName, namespaceName);
// Delete the headless service
await DeleteServiceAsync(serviceName, namespaceName);
// Delete the Secret
await DeleteSecretAsync(secretName, namespaceName);
// Delete the ConfigMap
await DeleteConfigMapAsync(configMapName, namespaceName);
logger.LogInformation("Finalization complete for SQLServer: {Name}", entity.Metadata.Name);
return ReconciliationResult<V1Alpha1SQLServer>.Success(entity);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred while finalizing SQLServer: {Name}", entity.Metadata.Name);
return ReconciliationResult<V1Alpha1SQLServer>.Failure(entity, ex.Message, ex);
}
}
private async Task DeleteStatefulSetAsync(string statefulSetName, string namespaceName)
{
try
{
logger.LogInformation("Deleting StatefulSet: {StatefulSetName} in namespace {Namespace}", statefulSetName, namespaceName);
await kubernetesClient.ApiClient.AppsV1.DeleteNamespacedStatefulSetAsync(statefulSetName, namespaceName);
logger.LogInformation("StatefulSet {StatefulSetName} deleted successfully.", statefulSetName);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete StatefulSet: {StatefulSetName} in namespace {Namespace}", statefulSetName, namespaceName);
}
}
private async Task DeleteServiceAsync(string serviceName, string namespaceName)
{
try
{
logger.LogInformation("Deleting Service: {ServiceName} in namespace {Namespace}", serviceName, namespaceName);
await kubernetesClient.ApiClient.CoreV1.DeleteNamespacedServiceAsync(serviceName, namespaceName);
logger.LogInformation("Service {ServiceName} deleted successfully.", serviceName);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete Service: {ServiceName} in namespace {Namespace}", serviceName, namespaceName);
}
}
private async Task DeleteSecretAsync(string secretName, string namespaceName)
{
try
{
logger.LogInformation("Deleting Secret: {SecretName} in namespace {Namespace}", secretName, namespaceName);
var secret = await kubernetesClient.GetAsync<V1Secret>(secretName, namespaceName);
if (secret is null)
{
logger.LogWarning("Secret {SecretName} not found in namespace {Namespace}. Skipping deletion.", secretName, namespaceName);
return;
}
await kubernetesClient.ApiClient.CoreV1.DeleteNamespacedSecretAsync(secretName, namespaceName);
logger.LogInformation("Secret {SecretName} deleted successfully.", secretName);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete Secret: {SecretName} in namespace {Namespace}", secretName, namespaceName);
}
}
private async Task DeleteConfigMapAsync(string configMapName, string namespaceName)
{
try
{
logger.LogInformation("Deleting ConfigMap: {ConfigMapName} in namespace {Namespace}", configMapName, namespaceName);
await kubernetesClient.ApiClient.CoreV1.DeleteNamespacedConfigMapAsync(configMapName, namespaceName);
logger.LogInformation("ConfigMap {ConfigMapName} deleted successfully.", configMapName);
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to delete ConfigMap: {ConfigMapName} in namespace {Namespace}", configMapName, namespaceName);
}
}
}