Skip to content

Commit 2274a31

Browse files
committed
feat(cli): add generate operator command
1 parent 5e91856 commit 2274a31

File tree

7 files changed

+289
-312
lines changed

7 files changed

+289
-312
lines changed

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

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

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

Lines changed: 170 additions & 217 deletions
Large diffs are not rendered by default.

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

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

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

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public static Command Command
1313
{
1414
CertificateGenerator.Command,
1515
CrdGenerator.Command,
16+
OperatorGenerator.Command,
1617
RbacGenerator.Command,
1718
};
1819
cmd.AddAlias("gen");
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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 OperatorGenerator
15+
{
16+
public static Command Command
17+
{
18+
get
19+
{
20+
var cmd = new Command("operator", "Generates deployments and other resources for the operator to run.")
21+
{
22+
Options.OutputFormat, Options.OutputPath,
23+
};
24+
cmd.AddAlias("op");
25+
cmd.SetHandler(ctx => Handler(AnsiConsole.Console, ctx));
26+
27+
return cmd;
28+
}
29+
}
30+
31+
internal static async Task Handler(IAnsiConsole console, InvocationContext ctx)
32+
{
33+
var outPath = ctx.ParseResult.GetValueForOption(Options.OutputPath);
34+
var format = ctx.ParseResult.GetValueForOption(Options.OutputFormat);
35+
36+
var result = new ResultOutput(console, format);
37+
console.WriteLine("Generate operator resources.");
38+
39+
var deployment = new V1Deployment(metadata: new V1ObjectMeta(
40+
labels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
41+
name: "operator")).Initialize();
42+
deployment.Spec = new V1DeploymentSpec
43+
{
44+
Replicas = 1,
45+
RevisionHistoryLimit = 0,
46+
Selector = new V1LabelSelector(
47+
matchLabels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } }),
48+
Template = new V1PodTemplateSpec
49+
{
50+
Metadata = new V1ObjectMeta(
51+
labels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } }),
52+
Spec = new V1PodSpec
53+
{
54+
TerminationGracePeriodSeconds = 10,
55+
Containers = new List<V1Container>
56+
{
57+
new()
58+
{
59+
Image = "operator",
60+
Name = "operator",
61+
Env = new List<V1EnvVar>
62+
{
63+
new()
64+
{
65+
Name = "POD_NAMESPACE",
66+
ValueFrom =
67+
new V1EnvVarSource
68+
{
69+
FieldRef = new V1ObjectFieldSelector
70+
{
71+
FieldPath = "metadata.namespace",
72+
},
73+
},
74+
},
75+
},
76+
Resources = new V1ResourceRequirements
77+
{
78+
Requests = new Dictionary<string, ResourceQuantity>
79+
{
80+
{ "cpu", new ResourceQuantity("100m") },
81+
{ "memory", new ResourceQuantity("64Mi") },
82+
},
83+
Limits = new Dictionary<string, ResourceQuantity>
84+
{
85+
{ "cpu", new ResourceQuantity("100m") },
86+
{ "memory", new ResourceQuantity("128Mi") },
87+
},
88+
},
89+
},
90+
},
91+
},
92+
},
93+
};
94+
result.Add($"deployment.{format.ToString().ToLowerInvariant()}", deployment);
95+
96+
result.Add(
97+
$"kustomization.{format.ToString().ToLowerInvariant()}",
98+
new KustomizationConfig
99+
{
100+
Resources = new List<string> { $"deployment.{format.ToString().ToLowerInvariant()}", },
101+
CommonLabels = new Dictionary<string, string> { { "operator-element", "operator-instance" }, },
102+
});
103+
104+
if (outPath is not null)
105+
{
106+
await result.Write(outPath);
107+
}
108+
else
109+
{
110+
result.Write();
111+
}
112+
}
113+
}

src/KubeOps.Cli/Properties/launchSettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
"workingDirectory": "$(ProjectDir)",
1717
"commandLineArgs": "g c ../../examples/Operator/Operator.csproj"
1818
},
19+
"CLI Generate Operator": {
20+
"commandName": "Project",
21+
"workingDirectory": "$(ProjectDir)",
22+
"commandLineArgs": "g op"
23+
},
1924
"CLI Install": {
2025
"commandName": "Project",
2126
"workingDirectory": "$(ProjectDir)",

0 commit comments

Comments
 (0)