|
| 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 | +} |
0 commit comments