-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): management install/uninstall commands (#618)
Adding install / uninstall commands to the CLI. BREAKING CHANGE: The install / uninstall commands now search for a project or solution file to parse the CRDs from a solution or a project.
- Loading branch information
Showing
14 changed files
with
300 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
|
||
using k8s; | ||
using k8s.Autorest; | ||
using k8s.Models; | ||
|
||
using KubeOps.Cli.Roslyn; | ||
using KubeOps.Transpiler; | ||
|
||
using Spectre.Console; | ||
|
||
namespace KubeOps.Cli.Commands.Management; | ||
|
||
internal static class Install | ||
{ | ||
public static Command Command | ||
{ | ||
get | ||
{ | ||
var cmd = | ||
new Command("install", "Install CRDs into the cluster of the actually selected context.") | ||
{ | ||
Options.Force, | ||
Options.SolutionProjectRegex, | ||
Options.TargetFramework, | ||
Arguments.SolutionOrProjectFile, | ||
}; | ||
cmd.AddAlias("i"); | ||
cmd.SetHandler(ctx => Handler( | ||
AnsiConsole.Console, | ||
new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()), | ||
ctx)); | ||
|
||
return cmd; | ||
} | ||
} | ||
|
||
internal static async Task Handler(IAnsiConsole console, IKubernetes client, InvocationContext ctx) | ||
{ | ||
var file = ctx.ParseResult.GetValueForArgument(Arguments.SolutionOrProjectFile); | ||
var force = ctx.ParseResult.GetValueForOption(Options.Force); | ||
|
||
var parser = file switch | ||
{ | ||
{ Extension: ".csproj", Exists: true } => await AssemblyParser.ForProject(console, file), | ||
{ Extension: ".sln", Exists: true } => await AssemblyParser.ForSolution( | ||
console, | ||
file, | ||
ctx.ParseResult.GetValueForOption(Options.SolutionProjectRegex), | ||
ctx.ParseResult.GetValueForOption(Options.TargetFramework)), | ||
{ Exists: false } => throw new FileNotFoundException($"The file {file.Name} does not exist."), | ||
_ => throw new NotSupportedException("Only *.csproj and *.sln files are supported."), | ||
}; | ||
|
||
console.WriteLine($"Install CRDs from {file.Name}."); | ||
var crds = Crds.Transpile(parser.Entities()).ToList(); | ||
if (crds.Count == 0) | ||
{ | ||
console.WriteLine("No CRDs found. Exiting."); | ||
ctx.ExitCode = ExitCodes.Success; | ||
return; | ||
} | ||
|
||
console.WriteLine($"Found {crds.Count} CRDs."); | ||
console.WriteLine($"""Starting install into cluster with url "{client.BaseUri}"."""); | ||
|
||
foreach (var crd in crds) | ||
{ | ||
console.MarkupLineInterpolated( | ||
$"""Install [cyan]"{crd.Spec.Group}/{crd.Spec.Names.Kind}"[/] into the cluster."""); | ||
|
||
try | ||
{ | ||
switch (await client.ApiextensionsV1.ListCustomResourceDefinitionAsync( | ||
fieldSelector: $"metadata.name={crd.Name()}")) | ||
{ | ||
case { Items: [var existing] }: | ||
console.MarkupLineInterpolated( | ||
$"""[yellow]CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}" already exists.[/]"""); | ||
if (!force && console.Confirm("[yellow]Should the CRD be overwritten?[/]")) | ||
{ | ||
ctx.ExitCode = ExitCodes.Aborted; | ||
return; | ||
} | ||
|
||
crd.Metadata.ResourceVersion = existing.ResourceVersion(); | ||
await client.ApiextensionsV1.ReplaceCustomResourceDefinitionAsync(crd, crd.Name()); | ||
break; | ||
default: | ||
await client.ApiextensionsV1.CreateCustomResourceDefinitionAsync(crd); | ||
break; | ||
} | ||
|
||
console.MarkupLineInterpolated( | ||
$"""[green]Installed / Updated CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); | ||
} | ||
catch (HttpOperationException) | ||
{ | ||
console.WriteLine( | ||
$"""[red]There was a http (api) error while installing "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); | ||
throw; | ||
} | ||
catch (Exception) | ||
{ | ||
console.WriteLine( | ||
$"""[red]There was an error while installing "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
using System.CommandLine; | ||
using System.CommandLine.Invocation; | ||
|
||
using k8s; | ||
using k8s.Autorest; | ||
using k8s.Models; | ||
|
||
using KubeOps.Cli.Roslyn; | ||
using KubeOps.Transpiler; | ||
|
||
using Spectre.Console; | ||
|
||
namespace KubeOps.Cli.Commands.Management; | ||
|
||
internal static class Uninstall | ||
{ | ||
public static Command Command | ||
{ | ||
get | ||
{ | ||
var cmd = | ||
new Command("uninstall", "Uninstall CRDs from the cluster of the actually selected context.") | ||
{ | ||
Options.Force, | ||
Options.SolutionProjectRegex, | ||
Options.TargetFramework, | ||
Arguments.SolutionOrProjectFile, | ||
}; | ||
cmd.AddAlias("u"); | ||
cmd.SetHandler(ctx => Handler( | ||
AnsiConsole.Console, | ||
new Kubernetes(KubernetesClientConfiguration.BuildDefaultConfig()), | ||
ctx)); | ||
|
||
return cmd; | ||
} | ||
} | ||
|
||
internal static async Task Handler(IAnsiConsole console, IKubernetes client, InvocationContext ctx) | ||
{ | ||
var file = ctx.ParseResult.GetValueForArgument(Arguments.SolutionOrProjectFile); | ||
var force = ctx.ParseResult.GetValueForOption(Options.Force); | ||
|
||
var parser = file switch | ||
{ | ||
{ Extension: ".csproj", Exists: true } => await AssemblyParser.ForProject(console, file), | ||
{ Extension: ".sln", Exists: true } => await AssemblyParser.ForSolution( | ||
console, | ||
file, | ||
ctx.ParseResult.GetValueForOption(Options.SolutionProjectRegex), | ||
ctx.ParseResult.GetValueForOption(Options.TargetFramework)), | ||
{ Exists: false } => throw new FileNotFoundException($"The file {file.Name} does not exist."), | ||
_ => throw new NotSupportedException("Only *.csproj and *.sln files are supported."), | ||
}; | ||
|
||
console.WriteLine($"Uninstall CRDs from {file.Name}."); | ||
var crds = Crds.Transpile(parser.Entities()).ToList(); | ||
if (crds.Count == 0) | ||
{ | ||
console.WriteLine("No CRDs found. Exiting."); | ||
ctx.ExitCode = ExitCodes.Success; | ||
return; | ||
} | ||
|
||
console.WriteLine($"Found {crds.Count} CRDs."); | ||
if (!force && !console.Confirm("[red]Should the CRDs be uninstalled?[/]", false)) | ||
{ | ||
ctx.ExitCode = ExitCodes.Aborted; | ||
return; | ||
} | ||
|
||
console.WriteLine($"""Starting uninstall from cluster with url "{client.BaseUri}"."""); | ||
|
||
foreach (var crd in crds) | ||
{ | ||
console.MarkupLineInterpolated( | ||
$"""Uninstall [cyan]"{crd.Spec.Group}/{crd.Spec.Names.Kind}"[/] from the cluster."""); | ||
|
||
try | ||
{ | ||
switch (await client.ApiextensionsV1.ListCustomResourceDefinitionAsync( | ||
fieldSelector: $"metadata.name={crd.Name()}")) | ||
{ | ||
case { Items: [var existing] }: | ||
await client.ApiextensionsV1.DeleteCustomResourceDefinitionAsync(existing.Name()); | ||
console.MarkupLineInterpolated( | ||
$"""[green]CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}" deleted.[/]"""); | ||
break; | ||
default: | ||
console.MarkupLineInterpolated( | ||
$"""[green]CRD "{crd.Spec.Group}/{crd.Spec.Names.Kind}" did not exist.[/]"""); | ||
break; | ||
} | ||
} | ||
catch (HttpOperationException) | ||
{ | ||
console.WriteLine( | ||
$"""[red]There was a http (api) error while uninstalling "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); | ||
throw; | ||
} | ||
catch (Exception) | ||
{ | ||
console.WriteLine( | ||
$"""[red]There was an error while uninstalling "{crd.Spec.Group}/{crd.Spec.Names.Kind}".[/]"""); | ||
throw; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.