Skip to content

Commit

Permalink
feat(operator): add build targets extension for automatic resource ge…
Browse files Browse the repository at this point in the history
…neration

BREAKING CHANGE: The targets file contains
other properties than before. Refer to the
documentation for explicit details.
  • Loading branch information
buehler committed Oct 6, 2023
1 parent c1345c3 commit edaed46
Show file tree
Hide file tree
Showing 15 changed files with 174 additions and 201 deletions.
4 changes: 1 addition & 3 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
"tools": {
"docfx": {
"version": "2.71.0",
"commands": [
"docfx"
]
"commands": ["docfx"]
}
}
}
111 changes: 0 additions & 111 deletions _old/src/KubeOps/Build/KubeOps.targets

This file was deleted.

This file was deleted.

2 changes: 2 additions & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config/
Dockerfile
3 changes: 2 additions & 1 deletion examples/Operator/Controller/V1SecondEntityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public V1SecondEntityController(
_logger = logger;
}

public async Task ReconcileAsync(V1SecondEntity entity)
public Task ReconcileAsync(V1SecondEntity entity)
{
_logger.LogInformation("Reconciling entity {Entity}.", entity);
return Task.CompletedTask;
}
}
2 changes: 0 additions & 2 deletions examples/Operator/todos.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
todo:
- other CLI commands
- build targets
- error handling
- web: webhooks
- docs
Expand Down
4 changes: 4 additions & 0 deletions src/KubeOps.Cli/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ var slnFile
"namespace",
() => "default",
"The Kubernetes namespace that the operator will be run.");

public static readonly Argument<string> OperatorName = new(
"name",
"Name of the operator.");
}
4 changes: 1 addition & 3 deletions src/KubeOps.Cli/Commands/Generator/CrdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ public static Command Command
{
get
{
var cmd = new Command("crd", "Generates CRDs for Kubernetes based on a solution or project.")
var cmd = new Command("crds", "Generates CRDs for Kubernetes based on a solution or project.")
{
Options.OutputFormat,
Options.OutputPath,
Options.SolutionProjectRegex,
Options.TargetFramework,
Arguments.SolutionOrProjectFile,
};
cmd.AddAlias("crds");
cmd.AddAlias("c");
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));

return cmd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

namespace KubeOps.Cli.Commands.Generator;

internal static class Generator
internal static class Generate
{
public static Command Command
{
get
{
var cmd = new Command("generator", "Generates elements related to an operator.")
var cmd = new Command("generate", "Generates elements related to an operator.")
{
CertificateGenerator.Command,
CrdGenerator.Command,
DockerGenerator.Command,
InstallerGenerator.Command,
OperatorGenerator.Command,
RbacGenerator.Command,
};
Expand Down
68 changes: 68 additions & 0 deletions src/KubeOps.Cli/Commands/Generator/InstallerGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.CommandLine;
using System.CommandLine.Invocation;

using k8s;
using k8s.Models;

using KubeOps.Abstractions.Kustomize;
using KubeOps.Cli.Output;

using Spectre.Console;

namespace KubeOps.Cli.Commands.Generator;

internal static class InstallerGenerator
{
public static Command Command
{
get
{
var cmd = new Command("installer", "Generates Kustomization YAML to install the entire operator.")
{
Options.OutputPath, Options.OutputFormat, Arguments.OperatorName,
};
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));

return cmd;
}
}

internal static async Task Handler(IAnsiConsole console, InvocationContext ctx)
{
var outPath = ctx.ParseResult.GetValueForOption(Options.OutputPath);
var format = ctx.ParseResult.GetValueForOption(Options.OutputFormat);
var name = ctx.ParseResult.GetValueForArgument(Arguments.OperatorName);

var result = new ResultOutput(console, format);
console.WriteLine("Generate operator installer.");

result.Add(
$"namespace.{format.ToString().ToLowerInvariant()}",
new V1Namespace(metadata: new(name: "system")).Initialize());
result.Add(
$"kustomization.{format.ToString().ToLowerInvariant()}",
new KustomizationConfig
{
NamePrefix = $"{name}-",
Namespace = $"{name}-system",
CommonLabels = new Dictionary<string, string> { { "operator", name }, },
Resources = new List<string>
{
$"./namespace.{format.ToString().ToLowerInvariant()}", "./rbac", "./operator", "./crds",
},
Images = new List<KustomizationImage>
{
new() { Name = "operator", NewName = "accessible-docker-image", NewTag = "latest", },
},
});

if (outPath is not null)
{
await result.Write(outPath);
}
else
{
result.Write();
}
}
}
1 change: 0 additions & 1 deletion src/KubeOps.Cli/Commands/Generator/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public static Command Command
Options.TargetFramework,
Arguments.SolutionOrProjectFile,
};
cmd.AddAlias("r");
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));

return cmd;
Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps.Cli/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
return await new CommandLineBuilder(new RootCommand(
"CLI for KubeOps. Commandline tool to help with management tasks such as generating or installing CRDs.")
{
Generator.Command, Version.Command, Install.Command, Uninstall.Command,
Generate.Command, Version.Command, Install.Command, Uninstall.Command,
})
.UseDefaults()
.UseParseErrorReporting(ExitCodes.UsageError)
Expand Down
9 changes: 7 additions & 2 deletions src/KubeOps.Cli/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"CLI Generate RBAC": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "gen rbac ../../examples/Operator/Operator.csproj"
"commandLineArgs": "g rbac ../../examples/Operator/Operator.csproj"
},
"CLI Generate Cert": {
"commandName": "Project",
Expand All @@ -14,7 +14,7 @@
"CLI Generate CRDs": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "g c ../../examples/Operator/Operator.csproj"
"commandLineArgs": "g crds ../../examples/Operator/Operator.csproj"
},
"CLI Generate Operator": {
"commandName": "Project",
Expand All @@ -26,6 +26,11 @@
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "g docker"
},
"CLI Generate Installer": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)",
"commandLineArgs": "g installer demo-operator"
},
"CLI Install": {
"commandName": "Project",
"workingDirectory": "$(ProjectDir)",
Expand Down
Loading

0 comments on commit edaed46

Please sign in to comment.