|
| 1 | +using System.CommandLine; |
| 2 | +using System.CommandLine.Invocation; |
| 3 | + |
| 4 | +using k8s; |
| 5 | +using k8s.Autorest; |
| 6 | +using k8s.Models; |
| 7 | + |
| 8 | +using KubeOps.Cli.Roslyn; |
| 9 | +using KubeOps.Transpiler; |
| 10 | + |
| 11 | +using Spectre.Console; |
| 12 | + |
| 13 | +namespace KubeOps.Cli.Commands.Management; |
| 14 | + |
| 15 | +internal static class Install |
| 16 | +{ |
| 17 | + public static Command Command |
| 18 | + { |
| 19 | + get |
| 20 | + { |
| 21 | + var cmd = |
| 22 | + new Command("install", "Install CRDs into the cluster of the actually selected context.") |
| 23 | + { |
| 24 | + Options.Force, |
| 25 | + Options.SolutionProjectRegex, |
| 26 | + Options.TargetFramework, |
| 27 | + Arguments.SolutionOrProjectFile, |
| 28 | + }; |
| 29 | + cmd.AddAlias("i"); |
| 30 | + cmd.SetHandler(ctx => Handler( |
| 31 | + AnsiConsole.Console, |
| 32 | + new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()), |
| 33 | + ctx)); |
| 34 | + |
| 35 | + return cmd; |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + internal static async Task Handler(IAnsiConsole console, IKubernetes client, InvocationContext ctx) |
| 40 | + { |
| 41 | + var file = ctx.ParseResult.GetValueForArgument(Arguments.SolutionOrProjectFile); |
| 42 | + var force = ctx.ParseResult.GetValueForOption(Options.Force); |
| 43 | + |
| 44 | + var parser = file switch |
| 45 | + { |
| 46 | + { Extension: ".csproj", Exists: true } => await AssemblyParser.ForProject(console, file), |
| 47 | + { Extension: ".sln", Exists: true } => await AssemblyParser.ForSolution( |
| 48 | + console, |
| 49 | + file, |
| 50 | + ctx.ParseResult.GetValueForOption(Options.SolutionProjectRegex), |
| 51 | + ctx.ParseResult.GetValueForOption(Options.TargetFramework)), |
| 52 | + { Exists: false } => throw new FileNotFoundException($"The file {file.Name} does not exist."), |
| 53 | + _ => throw new NotSupportedException("Only *.csproj and *.sln files are supported."), |
| 54 | + }; |
| 55 | + |
| 56 | + console.WriteLine($"Install CRDs from {file.Name}."); |
| 57 | + var crds = Crds.Transpile(parser.Entities()).ToList(); |
| 58 | + if (crds.Count == 0) |
| 59 | + { |
| 60 | + console.WriteLine("No CRDs found. Exiting."); |
| 61 | + ctx.ExitCode = ExitCodes.Success; |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + console.WriteLine($"Found {crds.Count} CRDs."); |
| 66 | + console.WriteLine($"""Starting install into cluster with url "{client.BaseUri}"."""); |
| 67 | + |
| 68 | + foreach (var crd in crds) |
| 69 | + { |
| 70 | + console.MarkupLineInterpolated( |
| 71 | + $"""Install [cyan]"{crd.Spec.Group}/{crd.Spec.Names.Kind}"[/] into the cluster."""); |
| 72 | + |
| 73 | + try |
| 74 | + { |
| 75 | + switch (await client.ApiextensionsV1.ListCustomResourceDefinitionAsync( |
| 76 | + fieldSelector: $"metadata.name={crd.Name()}")) |
| 77 | + { |
| 78 | + case { Items: [var existing] }: |
| 79 | + console.MarkupLineInterpolated( |
| 80 | + $"""[yellow]CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}" already exists.[/]"""); |
| 81 | + if (!force && console.Confirm("[yellow]Should the CRD be overwritten?[/]")) |
| 82 | + { |
| 83 | + ctx.ExitCode = ExitCodes.Aborted; |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + crd.Metadata.ResourceVersion = existing.ResourceVersion(); |
| 88 | + await client.ApiextensionsV1.ReplaceCustomResourceDefinitionAsync(crd, crd.Name()); |
| 89 | + break; |
| 90 | + default: |
| 91 | + await client.ApiextensionsV1.CreateCustomResourceDefinitionAsync(crd); |
| 92 | + break; |
| 93 | + } |
| 94 | + |
| 95 | + console.MarkupLineInterpolated( |
| 96 | + $"""[green]Installed / Updated CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); |
| 97 | + } |
| 98 | + catch (HttpOperationException) |
| 99 | + { |
| 100 | + console.WriteLine( |
| 101 | + $"""[red]There was a http (api) error while installing "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); |
| 102 | + throw; |
| 103 | + } |
| 104 | + catch (Exception) |
| 105 | + { |
| 106 | + console.WriteLine( |
| 107 | + $"""[red]There was an error while installing "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); |
| 108 | + throw; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments