Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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 k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 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 k8s;
using k8s.Models;

namespace KubeOps.Abstractions.Entities;

Expand Down
12 changes: 7 additions & 5 deletions src/KubeOps.Abstractions/Entities/KubernetesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,13 @@ public static TEntity WithOwnerReference<TEntity>(
/// <param name="kubernetesObject">The object that should be translated.</param>
/// <returns>The created <see cref="V1OwnerReference"/>.</returns>
public static V1OwnerReference MakeOwnerReference(this IKubernetesObject<V1ObjectMeta> kubernetesObject)
=> new(
kubernetesObject.ApiVersion,
kubernetesObject.Kind,
kubernetesObject.Metadata.Name,
kubernetesObject.Metadata.Uid);
=> new()
{
ApiVersion = kubernetesObject.ApiVersion,
Kind = kubernetesObject.Kind,
Name = kubernetesObject.Metadata.Name,
Uid = kubernetesObject.Metadata.Uid,
};

private static IList<V1OwnerReference> EnsureOwnerReferences(this V1ObjectMeta meta) =>
meta.OwnerReferences ??= new List<V1OwnerReference>();
Expand Down
2 changes: 1 addition & 1 deletion src/KubeOps.Abstractions/KubeOps.Abstractions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<ItemGroup>
<PackageReference Include="JsonPatch.Net" Version="3.3.0" />
<PackageReference Include="KubernetesClient" Version="17.0.14" />
<PackageReference Include="KubernetesClient" Version="18.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="9.0.10"/>
<PackageReference Include="ZiggyCreatures.FusionCache" Version="2.4.0" />
</ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions src/KubeOps.Cli/Commands/Generator/OperatorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// See the LICENSE file in the project root for more information.

using System.CommandLine;
using System.CommandLine.Invocation;
using System.Text;

using k8s;
Expand Down Expand Up @@ -116,15 +115,15 @@ internal static async Task<int> Handler(IAnsiConsole console, ParseResult parseR

result.Add(
$"namespace.{format.GetFileExtension()}",
new V1Namespace(metadata: new(name: "system")).Initialize());
new V1Namespace { Metadata = new() { Name = "system" } }.Initialize());

result.Add(
$"kustomization.{format.GetFileExtension()}",
new KustomizationConfig
{
NamePrefix = $"{name}-",
Namespace = $"{name}-system",
Labels = [new KustomizationCommonLabels(new Dictionary<string, string> { { "operator", name }, })],
Labels = [new(new Dictionary<string, string> { { "operator", name }, })],
Resources = result.DefaultFormatFiles.ToList(),
Images =
new List<KustomizationImage>
Expand Down
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
29 changes: 21 additions & 8 deletions src/KubeOps.Cli/Generators/RbacGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

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,17 +25,29 @@ 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()
{
ApiGroup = V1ClusterRole.KubeGroup,
Kind = V1ClusterRole.KubeKind,
Name = "operator-role",
},
Subjects = new List<Rbacv1Subject>
{
new()
{
new(V1ServiceAccount.KubeKind, "default", namespaceProperty: "system"),
})
.Initialize();
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
Loading