Skip to content

Commit fb06960

Browse files
committed
feat(cli): add generate docker file command and optimize it
Signed-off-by: Christoph Bühler <[email protected]>
1 parent 407d01a commit fb06960

File tree

4 files changed

+75
-82
lines changed

4 files changed

+75
-82
lines changed

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

Lines changed: 0 additions & 82 deletions
This file was deleted.
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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 DockerGenerator
15+
{
16+
public static Command Command
17+
{
18+
get
19+
{
20+
var cmd = new Command("docker", "Generates a dockerfile that builds and starts the operator.")
21+
{
22+
Options.OutputPath,
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+
34+
var result = new ResultOutput(console, OutputFormat.Plain);
35+
console.WriteLine("Generate operator Dockerfile.");
36+
37+
result.Add(
38+
"Dockerfile",
39+
"""
40+
FROM mcr.microsoft.com/dotnet/sdk:latest as build
41+
WORKDIR /operator
42+
43+
COPY ./ ./
44+
RUN dotnet publish -c Release /p:AssemblyName=operator -o out
45+
46+
# The runner for the application
47+
FROM mcr.microsoft.com/dotnet/runtime:latest as final
48+
49+
RUN addgroup k8s-operator && useradd -G k8s-operator operator-user
50+
51+
WORKDIR /operator
52+
COPY --from=build /operator/out/ ./
53+
RUN chown operator-user:k8s-operator -R .
54+
55+
USER operator-user
56+
57+
ENTRYPOINT [ "dotnet", "operator.dll" ]
58+
""");
59+
60+
if (outPath is not null)
61+
{
62+
await result.Write(outPath);
63+
}
64+
else
65+
{
66+
result.Write();
67+
}
68+
}
69+
}

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+
DockerGenerator.Command,
1617
OperatorGenerator.Command,
1718
RbacGenerator.Command,
1819
};

src/KubeOps.Cli/Properties/launchSettings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@
2121
"workingDirectory": "$(ProjectDir)",
2222
"commandLineArgs": "g op"
2323
},
24+
"CLI Generate Docker": {
25+
"commandName": "Project",
26+
"workingDirectory": "$(ProjectDir)",
27+
"commandLineArgs": "g docker"
28+
},
2429
"CLI Install": {
2530
"commandName": "Project",
2631
"workingDirectory": "$(ProjectDir)",

0 commit comments

Comments
 (0)