Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: apply object initializer shorthand and seal generator classes
- Updated object initializations across generator classes to use initializer shorthand for consistency.
- Marked generator classes as `sealed` for optimization and better design practices.
- Improved readability and alignment with modern C# coding standards.
  • Loading branch information
kimpenhaus committed Oct 31, 2025
commit 464730ad8ab746716bfca2c87490e97892001c59
45 changes: 27 additions & 18 deletions src/KubeOps.Cli/Generators/DeploymentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@

namespace KubeOps.Cli.Generators;

internal class DeploymentGenerator(OutputFormat format) : IConfigGenerator
internal sealed class DeploymentGenerator(OutputFormat format) : IConfigGenerator
{
public void Generate(ResultOutput output)
{
var deployment = new V1Deployment(metadata: new V1ObjectMeta(
labels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
name: "operator")).Initialize();
deployment.Spec = new V1DeploymentSpec
var deployment = new V1Deployment
{
Metadata = new()
{
Name = "operator",
Labels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
}.Initialize();
deployment.Spec = new()
{
Replicas = 1,
RevisionHistoryLimit = 0,
Selector = new V1LabelSelector(
matchLabels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } }),
Template = new V1PodTemplateSpec
Selector = new()
{
Metadata = new V1ObjectMeta(
labels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } }),
Spec = new V1PodSpec
MatchLabels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
Template = new()
{
Metadata = new()
{
Labels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
Spec = new()
{
TerminationGracePeriodSeconds = 10,
Containers = new List<V1Container>
Expand All @@ -41,26 +50,26 @@ public void Generate(ResultOutput output)
{
Name = "POD_NAMESPACE",
ValueFrom =
new V1EnvVarSource
new()
{
FieldRef = new V1ObjectFieldSelector
FieldRef = new()
{
FieldPath = "metadata.namespace",
},
},
},
},
Resources = new V1ResourceRequirements
Resources = new()
{
Requests = new Dictionary<string, ResourceQuantity>
{
{ "cpu", new ResourceQuantity("100m") },
{ "memory", new ResourceQuantity("64Mi") },
{ "cpu", new("100m") },
{ "memory", new("64Mi") },
},
Limits = new Dictionary<string, ResourceQuantity>
{
{ "cpu", new ResourceQuantity("100m") },
{ "memory", new ResourceQuantity("128Mi") },
{ "cpu", new("100m") },
{ "memory", new("128Mi") },
},
},
},
Expand Down
14 changes: 8 additions & 6 deletions src/KubeOps.Cli/Generators/MutationWebhookGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ public void Generate(ResultOutput output)
return;
}

var mutatorConfig = new V1MutatingWebhookConfiguration(
metadata: new V1ObjectMeta(name: "mutators"),
webhooks: new List<V1MutatingWebhook>()).Initialize();
var mutatorConfig = new V1MutatingWebhookConfiguration
{
Metadata = new() { Name = "mutators" },
Webhooks = new List<V1MutatingWebhook>(),
}.Initialize();

foreach (var hook in webhooks)
{
mutatorConfig.Webhooks.Add(new V1MutatingWebhook
mutatorConfig.Webhooks.Add(new()
{
Name = $"mutate.{hook.Metadata.SingularName}.{hook.Metadata.Group}.{hook.Metadata.Version}",
MatchPolicy = "Exact",
Expand All @@ -42,10 +44,10 @@ public void Generate(ResultOutput output)
ApiVersions = new[] { hook.Metadata.Version },
},
},
ClientConfig = new Admissionregistrationv1WebhookClientConfig
ClientConfig = new()
{
CaBundle = caBundle,
Service = new Admissionregistrationv1ServiceReference
Service = new()
{
Name = "operator",
Path = hook.WebhookPath,
Expand Down
26 changes: 19 additions & 7 deletions src/KubeOps.Cli/Generators/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace KubeOps.Cli.Generators;

internal class RbacGenerator(MetadataLoadContext parser,
internal sealed class RbacGenerator(MetadataLoadContext parser,
OutputFormat outputFormat) : IConfigGenerator
{
public void Generate(ResultOutput output)
Expand All @@ -24,16 +24,28 @@ public void Generate(ResultOutput output)
.Concat(parser.GetContextType<DefaultRbacAttributes>().GetCustomAttributesData<EntityRbacAttribute>())
.ToList();

var role = new V1ClusterRole(rules: parser.Transpile(attributes).ToList()).Initialize();
var role = new V1ClusterRole { Rules = parser.Transpile(attributes).ToList() }.Initialize();
role.Metadata.Name = "operator-role";
output.Add($"operator-role.{outputFormat.GetFileExtension()}", role);

var roleBinding = new V1ClusterRoleBinding(
roleRef: new V1RoleRef(V1ClusterRole.KubeGroup, V1ClusterRole.KubeKind, "operator-role"),
subjects: new List<Rbacv1Subject>
var roleBinding = new V1ClusterRoleBinding
{
RoleRef = new()
{
new(V1ServiceAccount.KubeKind, "default", namespaceProperty: "system"),
})
ApiGroup = V1ClusterRole.KubeGroup,
Kind = V1ClusterRole.KubeKind,
Name = "operator-role",
},
Subjects = new List<Rbacv1Subject>
{
new()
{
Kind = V1ServiceAccount.KubeKind,
Name = "default",
NamespaceProperty = "system",
},
},
}
.Initialize();
roleBinding.Metadata.Name = "operator-role-binding";
output.Add($"operator-role-binding.{outputFormat.GetFileExtension()}", roleBinding);
Expand Down
16 changes: 9 additions & 7 deletions src/KubeOps.Cli/Generators/ValidationWebhookGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace KubeOps.Cli.Generators;

internal class ValidationWebhookGenerator
internal sealed class ValidationWebhookGenerator
(List<ValidationWebhook> webhooks, byte[] caBundle, OutputFormat format) : IConfigGenerator
{
public void Generate(ResultOutput output)
Expand All @@ -20,13 +20,15 @@ public void Generate(ResultOutput output)
return;
}

var validatorConfig = new V1ValidatingWebhookConfiguration(
metadata: new V1ObjectMeta(name: "validators"),
webhooks: new List<V1ValidatingWebhook>()).Initialize();
var validatorConfig = new V1ValidatingWebhookConfiguration
{
Metadata = new() { Name = "validators" },
Webhooks = new List<V1ValidatingWebhook>(),
}.Initialize();

foreach (var hook in webhooks)
{
validatorConfig.Webhooks.Add(new V1ValidatingWebhook
validatorConfig.Webhooks.Add(new()
{
Name = $"validate.{hook.Metadata.SingularName}.{hook.Metadata.Group}.{hook.Metadata.Version}",
MatchPolicy = "Exact",
Expand All @@ -42,10 +44,10 @@ public void Generate(ResultOutput output)
ApiVersions = new[] { hook.Metadata.Version },
},
},
ClientConfig = new Admissionregistrationv1WebhookClientConfig
ClientConfig = new()
{
CaBundle = caBundle,
Service = new Admissionregistrationv1ServiceReference
Service = new()
{
Name = "operator",
Path = hook.WebhookPath,
Expand Down
78 changes: 47 additions & 31 deletions src/KubeOps.Cli/Generators/WebhookDeploymentGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.

using System.Reflection;

using k8s;
using k8s.Models;

using KubeOps.Cli.Output;
using KubeOps.Cli.Transpilation;
using KubeOps.Transpiler;

namespace KubeOps.Cli.Generators;

internal class WebhookDeploymentGenerator(OutputFormat format) : IConfigGenerator
internal sealed class WebhookDeploymentGenerator(OutputFormat format) : IConfigGenerator
{
public void Generate(ResultOutput output)
{
var deployment = new V1Deployment(metadata: new V1ObjectMeta(
labels: new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
name: "operator")).Initialize();
deployment.Spec = new V1DeploymentSpec
var deployment = new V1Deployment
{
Metadata = new()
{
Name = "operator",
Labels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
}.Initialize();
deployment.Spec = new()
{
Replicas = 1,
RevisionHistoryLimit = 0,
Selector = new V1LabelSelector(
matchLabels:
new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } }),
Template = new V1PodTemplateSpec
Selector = new()
{
Metadata = new V1ObjectMeta(
labels:
new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" }, }),
Spec = new V1PodSpec
MatchLabels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
Template = new()
{
Metadata = new()
{
Labels = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" } },
},
Spec = new()
{
TerminationGracePeriodSeconds = 10,
Volumes = new List<V1Volume>
Expand All @@ -57,9 +60,9 @@ public void Generate(ResultOutput output)
{
Name = "POD_NAMESPACE",
ValueFrom =
new V1EnvVarSource
new()
{
FieldRef = new V1ObjectFieldSelector
FieldRef = new()
{
FieldPath = "metadata.namespace",
},
Expand All @@ -71,18 +74,18 @@ public void Generate(ResultOutput output)
{
new() { ConfigMapRef = new() { Name = "webhook-config" } },
},
Ports = new List<V1ContainerPort> { new(5001, name: "https"), },
Resources = new V1ResourceRequirements
Ports = new List<V1ContainerPort> { new() { HostPort = 5001, Name = "https" } },
Resources = new()
{
Requests = new Dictionary<string, ResourceQuantity>
{
{ "cpu", new ResourceQuantity("100m") },
{ "memory", new ResourceQuantity("64Mi") },
{ "cpu", new("100m") },
{ "memory", new("64Mi") },
},
Limits = new Dictionary<string, ResourceQuantity>
{
{ "cpu", new ResourceQuantity("100m") },
{ "memory", new ResourceQuantity("128Mi") },
{ "cpu", new("100m") },
{ "memory", new("128Mi") },
},
},
},
Expand All @@ -94,13 +97,26 @@ public void Generate(ResultOutput output)

output.Add(
$"service.{format.GetFileExtension()}",
new V1Service(
metadata: new V1ObjectMeta(name: "operator"),
spec: new V1ServiceSpec
new V1Service
{
Metadata = new() { Name = "operator" },
Spec = new()
{
Ports =
new List<V1ServicePort> { new() { Name = "https", TargetPort = "https", Port = 443, }, },
Selector = new Dictionary<string, string> { { "operator-deployment", "kubernetes-operator" }, },
}).Initialize());
new List<V1ServicePort>
{
new()
{
Name = "https",
TargetPort = "https",
Port = 443,
},
},
Selector = new Dictionary<string, string>
{
{ "operator-deployment", "kubernetes-operator" },
},
},
}.Initialize());
}
}