|
| 1 | +using System.Reflection; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | + |
| 4 | +using k8s.Models; |
| 5 | + |
| 6 | +using KubeOps.Abstractions.Crds; |
| 7 | +using KubeOps.Abstractions.Entities.Attributes; |
| 8 | +using KubeOps.KubernetesClient; |
| 9 | +using KubeOps.Transpiler; |
| 10 | + |
| 11 | +using Microsoft.Extensions.Hosting; |
| 12 | +using Microsoft.Extensions.Logging; |
| 13 | + |
| 14 | +namespace KubeOps.Operator.Crds; |
| 15 | + |
| 16 | +internal class CrdInstaller(ILogger<CrdInstaller> logger, CrdInstallerSettings settings, IKubernetesClient client) |
| 17 | + : IHostedService |
| 18 | +{ |
| 19 | + private List<V1CustomResourceDefinition> _crds = []; |
| 20 | + |
| 21 | + public async Task StartAsync(CancellationToken cancellationToken) |
| 22 | + { |
| 23 | + logger.LogInformation("Execute CRD installer with overwrite: {Overwrite}", settings.OverwriteExisting); |
| 24 | + var assembly = Assembly.GetEntryAssembly(); |
| 25 | + if (assembly is null) |
| 26 | + { |
| 27 | + logger.LogError("No entry assembly found, cannot install CRDs."); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + var entities = assembly |
| 32 | + .DefinedTypes |
| 33 | + .Where(t => t is { IsInterface: false, IsAbstract: false, IsGenericType: false }) |
| 34 | + .Select(t => (t, attrs: CustomAttributeData.GetCustomAttributes(t))) |
| 35 | + .Where(e => e.attrs.Any(a => a.AttributeType.Name == nameof(KubernetesEntityAttribute)) && |
| 36 | + e.attrs.All(a => a.AttributeType.Name != nameof(IgnoreAttribute))) |
| 37 | + .Select(e => e.t); |
| 38 | + |
| 39 | + using var mlc = ContextCreator.Create( |
| 40 | + Directory |
| 41 | + .GetFiles(RuntimeEnvironment.GetRuntimeDirectory(), "*.dll") |
| 42 | + .Concat( |
| 43 | + Directory.GetFiles(Path.GetDirectoryName(assembly.Location)!, "*.dll")) |
| 44 | + .Distinct(), |
| 45 | + coreAssemblyName: typeof(object).Assembly.GetName().Name); |
| 46 | + _crds = mlc.Transpile(entities).ToList(); |
| 47 | + |
| 48 | + foreach (var crd in _crds) |
| 49 | + { |
| 50 | + var existing = |
| 51 | + await client.GetAsync<V1CustomResourceDefinition>(crd.Name(), cancellationToken: cancellationToken); |
| 52 | + if (existing is not null && !settings.OverwriteExisting) |
| 53 | + { |
| 54 | + logger.LogDebug("CRD {Name} already exists, skipping installation.", crd.Name()); |
| 55 | + } |
| 56 | + else if (existing is not null) |
| 57 | + { |
| 58 | + logger.LogDebug("CRD {Name} already exists.", crd.Name()); |
| 59 | + logger.LogInformation("Overwriting existing CRD {Name}.", crd.Name()); |
| 60 | + crd.Metadata.ResourceVersion = existing.ResourceVersion(); |
| 61 | + await client.UpdateAsync(crd, cancellationToken); |
| 62 | + } |
| 63 | + else |
| 64 | + { |
| 65 | + logger.LogInformation("Installing CRD {Name}.", crd.Name()); |
| 66 | + await client.CreateAsync(crd, cancellationToken); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public async Task StopAsync(CancellationToken cancellationToken) |
| 72 | + { |
| 73 | + if (!settings.DeleteOnShutdown) |
| 74 | + { |
| 75 | + logger.LogDebug("Skipping CRD deletion on shutdown as per settings."); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + logger.LogInformation("Deleting CRDs on shutdown."); |
| 80 | + foreach (var crd in _crds) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + logger.LogInformation("Deleting CRD {Name}.", crd.Name()); |
| 85 | + await client.DeleteAsync(crd, cancellationToken); |
| 86 | + } |
| 87 | + catch (Exception ex) |
| 88 | + { |
| 89 | + logger.LogError(ex, "Failed to delete CRD {Name}.", crd.Name()); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments