Skip to content

Commit ba8b21c

Browse files
authored
Refactoring (#27)
1 parent f8b889c commit ba8b21c

26 files changed

+191
-126
lines changed

.github/workflows/build-publish.yml

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
name: Build
22
on:
33
push:
4+
branches:
5+
- main
6+
tags:
7+
- 'v*'
48
pull_request:
5-
release:
6-
types:
7-
- published
9+
branches:
10+
- main
811
env:
912
GITHUB_FEED: https://nuget.pkg.github.com/pmdevers/index.json
1013
GITHUB_USER: pmdevers
@@ -15,20 +18,27 @@ jobs:
1518
build:
1619
name: Build
1720
runs-on: ubuntu-latest
21+
permissions:
22+
packages: write
23+
contents: write
1824
steps:
1925
- uses: actions/checkout@v4
2026
with:
2127
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
28+
2229
- name: Setup dotnet
2330
uses: actions/setup-dotnet@v4
2431
with:
2532
dotnet-version: '8.x.x'
33+
2634
- name: Build
2735
run: dotnet build -c Release
36+
2837
- name: Test
2938
run: dotnet test -c Release
39+
3040
- name: DotNet Pack
31-
if: github.event_name == 'release'
41+
if: startsWith(github.ref, 'refs/tags/')
3242
run: |
3343
arrTag=(${GITHUB_REF//\// })
3444
VERSION="${arrTag[2]}"
@@ -38,13 +48,33 @@ jobs:
3848
dotnet pack -v normal --no-restore -p:PackageVersion=$VERSION --configuration Release --output packages
3949
4050
- name: Upload Artifact
41-
if: github.event_name == 'release'
51+
if: startsWith(github.ref, 'refs/tags/')
4252
uses: actions/upload-artifact@v4
4353
with:
4454
name: nupkg
4555
path: packages/*.nupkg
46-
56+
57+
- name: Create release notes
58+
uses: johnyherangi/create-release-notes@main
59+
if: startsWith(github.ref, 'refs/tags/')
60+
id: create-release-notes
61+
env:
62+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
63+
64+
- name: Create Release
65+
if: startsWith(github.ref, 'refs/tags/')
66+
id: create_release
67+
uses: actions/create-release@v1
68+
env:
69+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
70+
with:
71+
tag_name: ${{ github.ref }}
72+
release_name: Release ${{ github.ref }}
73+
body: ${{ steps.create-release-notes.outputs.release-notes }}
74+
draft: false
75+
prerelease: false
76+
4777
- name: DotNet NuGet Push
48-
if: github.event_name == 'release'
78+
if: startsWith(github.ref, 'refs/tags/')
4979
run: dotnet nuget push "packages/*.nupkg" --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NUGET_KEY }} --skip-duplicate --no-symbols
50-
80+

examples/SimpleOperator/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55

66

77
var builder = OperatorHost.CreateOperatorApplicationBuilder(args)
8-
.WithName("simple-operator");
8+
.WithName("simple-operator")
9+
.WithNamespace("simple-ops-system");
910

1011
builder.AddController<TestItemController>()
1112
.WithFinalizer("testitem.local.finalizer");

examples/SimpleOperator/Projects/ProjectController.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
using K8sOperator.NET;
2+
using K8sOperator.NET.Metadata;
23

34
namespace SimpleOperator.Projects;
45

5-
6+
[Namespace("default")]
67
public class ProjectController(ILoggerFactory logger) : Controller<Project>
78
{
89
private readonly ILogger _logger = logger.CreateLogger<ProjectController>();

examples/SimpleOperator/Projects/TestItem.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using k8s.Models;
2-
using K8sOperator.NET.Metadata;
32
using K8sOperator.NET.Models;
43
using static SimpleOperator.Projects.TestItem;
54

@@ -10,7 +9,6 @@ public class TestItem : CustomResource<TestItemSpec, TestItemStatus>
109
{
1110
public class TestItemSpec
1211
{
13-
public EntityScope Scope { get; set; }
1412
public string? String { get; set; }
1513
}
1614

examples/SimpleOperator/install.yaml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ spec:
99
listKind: TestItemList
1010
plural: testitems
1111
singular: testitem
12-
scope: Namespaced
12+
scope: Cluster
1313
versions:
1414
- name: v1alpha1
1515
schema:
@@ -21,16 +21,9 @@ spec:
2121
spec:
2222
nullable: false
2323
properties:
24-
scope:
25-
enum:
26-
- Namespaced
27-
- Cluster
28-
type: string
2924
string:
3025
nullable: false
3126
type: string
32-
required:
33-
- scope
3427
type: object
3528
type: object
3629
served: true
@@ -139,6 +132,7 @@ metadata:
139132
labels:
140133
operator-deployment: simple-operator
141134
name: simple-operator-deployment
135+
namespace: simple-ops-system
142136
spec:
143137
replicas: 1
144138
revisionHistoryLimit: 0

src/K8sOperator.NET.Generators/Builders/CustomResourceDefinitionBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace K8sOperator.NET.Generators.Builders;
1111
/// <summary>
1212
/// Provides extension methods for building Kubernetes CustomResourceDefinitions.
1313
/// </summary>
14-
public static class CustomResourceDefinitionBuilderExtensions
14+
public static partial class CustomResourceDefinitionBuilderExtensions
1515
{
1616

1717
/// <summary>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace K8sOperator.NET.Generators.Builders;
2+
3+
4+
/// <summary>
5+
///
6+
/// </summary>
7+
public enum EntityScope
8+
{
9+
/// <summary>
10+
/// The entity is scoped to a specific namespace.
11+
/// </summary>
12+
Namespaced = 0,
13+
14+
/// <summary>
15+
/// The entity is scoped to the entire cluster.
16+
/// </summary>
17+
Cluster = 1
18+
}

src/K8sOperator.NET.Generators/Builders/KubernetesObjectBuilderExtentions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ public static IKubernetesObjectBuilder<T> WithName<T>(this IKubernetesObjectBuil
2525
return builder;
2626
}
2727

28+
/// <summary>
29+
/// Sets the namespace of the kubernetes object
30+
/// </summary>
31+
/// <typeparam name="T">The type of the Kubernetes object.</typeparam>
32+
/// <param name="builder">The builder instance.</param>
33+
/// <param name="ns">The namespace to assign to the Kubernetes object.</param>
34+
/// <returns>The configured builder.</returns>
35+
public static IKubernetesObjectBuilder<T> WithNamespace<T>(this IKubernetesObjectBuilder<T> builder, string? ns)
36+
where T : IMetadata<V1ObjectMeta>
37+
{
38+
builder.Add(x =>
39+
{
40+
x.Metadata.SetNamespace(ns);
41+
});
42+
return builder;
43+
}
44+
2845
/// <summary>
2946
/// Adds a label to the Kubernetes object.
3047
/// </summary>

src/K8sOperator.NET.Generators/Install.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ private async Task Write(IKubernetesObject obj)
4949
private static V1CustomResourceDefinition CreateCustomResourceDefinition(IEventWatcher item)
5050
{
5151
var group = item.Metadata.OfType<KubernetesEntityAttribute>().First();
52-
var scope = item.Metadata.OfType<IEntityScopeMetadata>().First();
52+
var scope = item.Metadata.OfType<INamespaceMetadata>().FirstOrDefault();
5353

5454
var crdBuilder = new CustomResourceDefinitionBuilder();
55-
crdBuilder.WithName($"{group.PluralName}.{group.Group}")
55+
crdBuilder
56+
.WithName($"{group.PluralName}.{group.Group}")
5657
.WithSpec()
5758
.WithGroup(group.Group)
5859
.WithNames(
@@ -61,7 +62,7 @@ private static V1CustomResourceDefinition CreateCustomResourceDefinition(IEventW
6162
plural: group.PluralName,
6263
singular: group.Kind.ToLower()
6364
)
64-
.WithScope(scope.Scope)
65+
.WithScope(scope == null ? EntityScope.Cluster : EntityScope.Namespaced)
6566
.WithVersion(
6667
group.ApiVersion,
6768
schema =>
@@ -76,12 +77,15 @@ private static V1CustomResourceDefinition CreateCustomResourceDefinition(IEventW
7677

7778
private static V1Deployment CreateDeployment(IReadOnlyList<object> metadata)
7879
{
79-
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.Name)!;
80+
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.OperatorName)!;
8081
var image = metadata.TryGetValue<IImageMetadata, string>(x => x.GetImage())!;
82+
var ns = metadata.TryGetValue<INamespaceMetadata, string>(x => x.Namespace);
8183

8284
var deployment = DeploymentBuilder.Create();
8385

84-
deployment.WithName($"{name}-deployment")
86+
deployment
87+
.WithName($"{name}-deployment")
88+
.WithNamespace(ns)
8589
.WithLabel("operator-deployment", name)
8690
.WithSpec()
8791
.WithReplicas(1)
@@ -134,7 +138,7 @@ private static V1Deployment CreateDeployment(IReadOnlyList<object> metadata)
134138

135139
private static V1ClusterRoleBinding CreateClusterRoleBinding(IReadOnlyList<object> metadata)
136140
{
137-
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.Name);
141+
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.OperatorName);
138142

139143
var clusterrolebinding = new ClusterRoleBindingBuilder()
140144
.WithName($"{name}-role-binding")
@@ -146,7 +150,7 @@ private static V1ClusterRoleBinding CreateClusterRoleBinding(IReadOnlyList<objec
146150

147151
private static V1ClusterRole CreateClusterRole(IReadOnlyList<object> metadata, IEnumerable<IEventWatcher> watchers)
148152
{
149-
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.Name);
153+
var name = metadata.TryGetValue<IOperatorNameMetadata, string>(x => x.OperatorName);
150154

151155
var clusterrole = new ClusterRoleBuilder()
152156
.WithName($"{name}-role");

src/K8sOperator.NET/Builder/ControllerBuilder.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ internal class ControllerBuilder(IServiceProvider serviceProvider, Type controll
2525
public IController Build()
2626
{
2727
var controller = (IController)ActivatorUtilities.CreateInstance(_serviceProvider, _controllerType);
28-
var attribute = controller.ResourceType.GetCustomAttribute<KubernetesEntityAttribute>();
29-
30-
if (attribute is not null)
31-
{
32-
Metadata.Add(attribute);
33-
}
28+
Metadata.AddRange(_controllerType.GetCustomAttributes());
3429

30+
var attributes = controller.ResourceType.GetCustomAttributes();
31+
Metadata.AddRange(attributes);
3532
return controller;
3633
}
3734
}

0 commit comments

Comments
 (0)