Skip to content

Commit 7df5d48

Browse files
committed
feat(operator): add build targets extension for automatic resource generation
BREAKING CHANGE: The targets file contains other properties than before. Refer to the documentation for explicit details.
1 parent b967f37 commit 7df5d48

File tree

15 files changed

+174
-201
lines changed

15 files changed

+174
-201
lines changed

.config/dotnet-tools.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
"tools": {
55
"docfx": {
66
"version": "2.71.0",
7-
"commands": [
8-
"docfx"
9-
]
7+
"commands": ["docfx"]
108
}
119
}
1210
}

_old/src/KubeOps/Build/KubeOps.targets

Lines changed: 0 additions & 111 deletions
This file was deleted.

_old/src/KubeOps/Operator/Commands/Generators/InstallerGenerator.cs

Lines changed: 0 additions & 75 deletions
This file was deleted.

examples/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config/
2+
Dockerfile

examples/Operator/Controller/V1SecondEntityController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public V1SecondEntityController(
2323
_logger = logger;
2424
}
2525

26-
public async Task ReconcileAsync(V1SecondEntity entity)
26+
public Task ReconcileAsync(V1SecondEntity entity)
2727
{
2828
_logger.LogInformation("Reconciling entity {Entity}.", entity);
29+
return Task.CompletedTask;
2930
}
3031
}

examples/Operator/todos.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
todo:
2-
- other CLI commands
3-
- build targets
42
- error handling
53
- web: webhooks
64
- docs

src/KubeOps.Cli/Arguments.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ var slnFile
4141
"namespace",
4242
() => "default",
4343
"The Kubernetes namespace that the operator will be run.");
44+
45+
public static readonly Argument<string> OperatorName = new(
46+
"name",
47+
"Name of the operator.");
4448
}

src/KubeOps.Cli/Commands/Generator/CrdGenerator.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@ public static Command Command
1616
{
1717
get
1818
{
19-
var cmd = new Command("crd", "Generates CRDs for Kubernetes based on a solution or project.")
19+
var cmd = new Command("crds", "Generates CRDs for Kubernetes based on a solution or project.")
2020
{
2121
Options.OutputFormat,
2222
Options.OutputPath,
2323
Options.SolutionProjectRegex,
2424
Options.TargetFramework,
2525
Arguments.SolutionOrProjectFile,
2626
};
27-
cmd.AddAlias("crds");
28-
cmd.AddAlias("c");
2927
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));
3028

3129
return cmd;

src/KubeOps.Cli/Commands/Generator/Generator.cs renamed to src/KubeOps.Cli/Commands/Generator/Generate.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33

44
namespace KubeOps.Cli.Commands.Generator;
55

6-
internal static class Generator
6+
internal static class Generate
77
{
88
public static Command Command
99
{
1010
get
1111
{
12-
var cmd = new Command("generator", "Generates elements related to an operator.")
12+
var cmd = new Command("generate", "Generates elements related to an operator.")
1313
{
1414
CertificateGenerator.Command,
1515
CrdGenerator.Command,
1616
DockerGenerator.Command,
17+
InstallerGenerator.Command,
1718
OperatorGenerator.Command,
1819
RbacGenerator.Command,
1920
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System.CommandLine;
2+
using System.CommandLine.Invocation;
3+
4+
using k8s;
5+
using k8s.Models;
6+
7+
using KubeOps.Abstractions.Kustomize;
8+
using KubeOps.Cli.Output;
9+
10+
using Spectre.Console;
11+
12+
namespace KubeOps.Cli.Commands.Generator;
13+
14+
internal static class InstallerGenerator
15+
{
16+
public static Command Command
17+
{
18+
get
19+
{
20+
var cmd = new Command("installer", "Generates Kustomization YAML to install the entire operator.")
21+
{
22+
Options.OutputPath, Options.OutputFormat, Arguments.OperatorName,
23+
};
24+
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));
25+
26+
return cmd;
27+
}
28+
}
29+
30+
internal static async Task Handler(IAnsiConsole console, InvocationContext ctx)
31+
{
32+
var outPath = ctx.ParseResult.GetValueForOption(Options.OutputPath);
33+
var format = ctx.ParseResult.GetValueForOption(Options.OutputFormat);
34+
var name = ctx.ParseResult.GetValueForArgument(Arguments.OperatorName);
35+
36+
var result = new ResultOutput(console, format);
37+
console.WriteLine("Generate operator installer.");
38+
39+
result.Add(
40+
$"namespace.{format.ToString().ToLowerInvariant()}",
41+
new V1Namespace(metadata: new(name: "system")).Initialize());
42+
result.Add(
43+
$"kustomization.{format.ToString().ToLowerInvariant()}",
44+
new KustomizationConfig
45+
{
46+
NamePrefix = $"{name}-",
47+
Namespace = $"{name}-system",
48+
CommonLabels = new Dictionary<string, string> { { "operator", name }, },
49+
Resources = new List<string>
50+
{
51+
$"./namespace.{format.ToString().ToLowerInvariant()}", "./rbac", "./operator", "./crds",
52+
},
53+
Images = new List<KustomizationImage>
54+
{
55+
new() { Name = "operator", NewName = "accessible-docker-image", NewTag = "latest", },
56+
},
57+
});
58+
59+
if (outPath is not null)
60+
{
61+
await result.Write(outPath);
62+
}
63+
else
64+
{
65+
result.Write();
66+
}
67+
}
68+
}

0 commit comments

Comments
 (0)