Skip to content

Commit db6c915

Browse files
authored
Replace Resource AfterAddOrModified (#17)
1 parent 8acb511 commit db6c915

File tree

6 files changed

+196
-3
lines changed

6 files changed

+196
-3
lines changed

examples/SimpleOperator/Projects/Project.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace SimpleOperator.Projects;
55

6-
[KubernetesEntity(Group = "sonarcloud.io", ApiVersion = "v1alpha1", Kind = "Project", PluralName = "projects")]
6+
[KubernetesEntity(Group = "operator.io", ApiVersion = "v1alpha1", Kind = "Project", PluralName = "projects")]
77
public class Project : CustomResource<Project.Specs, Project.ProjectStatus>
88
{
99
public class Specs

examples/SimpleOperator/Projects/ProjectController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ public class ProjectController(ILoggerFactory logger) : Controller<Project>
1010
public override Task AddOrModifyAsync(Project resource, CancellationToken cancellationToken)
1111
{
1212
_logger.LogDebug("Controller AddOrModify received.");
13+
14+
resource.Metadata.Labels = new Dictionary<string, string>
15+
{
16+
{ "created", "created" }
17+
};
18+
19+
resource.Status.Result = "HEHE";
20+
1321
return base.AddOrModifyAsync(resource, cancellationToken);
1422
}
1523

examples/SimpleOperator/Projects/TestItem.cs

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

66
namespace SimpleOperator.Projects;
77

8-
[KubernetesEntity(Group = "sonarcloud.io", ApiVersion = "v1alpha1", Kind = "TestItem", PluralName = "testitems")]
8+
[KubernetesEntity(Group = "operator.io", ApiVersion = "v1alpha1", Kind = "TestItem", PluralName = "testitems")]
99
public class TestItem : CustomResource<TestItemSpec, TestItemStatus>
1010
{
1111
public class TestItemSpec

examples/SimpleOperator/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
"Install": {
1212
"commandName": "Project",
13-
"commandLineArgs": "install",
13+
"commandLineArgs": "install > ./install.yaml",
1414
"environmentVariables": {
1515
"ASPNETCORE_ENVIRONMENT": "Development"
1616
},

examples/SimpleOperator/install.yaml

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
apiVersion: apiextensions.k8s.io/v1
2+
kind: CustomResourceDefinition
3+
metadata:
4+
name: testitems.operator.io
5+
spec:
6+
group: operator.io
7+
names:
8+
kind: TestItem
9+
listKind: TestItemList
10+
plural: testitems
11+
singular: testitem
12+
scope: Namespaced
13+
versions:
14+
- name: v1alpha1
15+
schema:
16+
openAPIV3Schema:
17+
properties:
18+
status:
19+
nullable: false
20+
type: object
21+
spec:
22+
nullable: false
23+
properties:
24+
scope:
25+
enum:
26+
- Namespaced
27+
- Cluster
28+
type: string
29+
string:
30+
nullable: false
31+
type: string
32+
required:
33+
- scope
34+
type: object
35+
type: object
36+
served: true
37+
storage: true
38+
---
39+
apiVersion: apiextensions.k8s.io/v1
40+
kind: CustomResourceDefinition
41+
metadata:
42+
name: projects.operator.io
43+
spec:
44+
group: operator.io
45+
names:
46+
kind: Project
47+
listKind: ProjectList
48+
plural: projects
49+
singular: project
50+
scope: Namespaced
51+
versions:
52+
- name: v1alpha1
53+
schema:
54+
openAPIV3Schema:
55+
properties:
56+
status:
57+
nullable: false
58+
properties:
59+
result:
60+
nullable: false
61+
type: string
62+
required:
63+
- result
64+
type: object
65+
spec:
66+
nullable: false
67+
properties:
68+
name:
69+
nullable: false
70+
type: string
71+
organization:
72+
nullable: false
73+
type: string
74+
project:
75+
nullable: false
76+
type: string
77+
required:
78+
- name
79+
- organization
80+
- project
81+
type: object
82+
type: object
83+
served: true
84+
storage: true
85+
---
86+
apiVersion: rbac.authorization.k8s.io/v1
87+
kind: ClusterRole
88+
metadata:
89+
name: simple-operator-role
90+
rules:
91+
- apiGroups:
92+
- ""
93+
resources:
94+
- events
95+
verbs:
96+
- get
97+
- list
98+
- create
99+
- update
100+
- apiGroups:
101+
- coordination.k8s.io
102+
resources:
103+
- leases
104+
verbs:
105+
- '*'
106+
- apiGroups:
107+
- operator.io
108+
resources:
109+
- testitems
110+
- projects
111+
verbs:
112+
- '*'
113+
- apiGroups:
114+
- operator.io
115+
resources:
116+
- testitems/status
117+
- projects/status
118+
verbs:
119+
- get
120+
- update
121+
- patch
122+
---
123+
apiVersion: rbac.authorization.k8s.io/v1
124+
kind: ClusterRoleBinding
125+
metadata:
126+
name: simple-operator-role-binding
127+
roleRef:
128+
apiGroup: rbac.authorization.k8s.io
129+
kind: ClusterRole
130+
name: simple-operator-role
131+
subjects:
132+
- kind: ServiceAccount
133+
name: default
134+
namespace: system
135+
---
136+
apiVersion: apps/v1
137+
kind: Deployment
138+
metadata:
139+
labels:
140+
operator-deployment: simple-operator
141+
name: simple-operator-deployment
142+
spec:
143+
replicas: 1
144+
revisionHistoryLimit: 0
145+
selector:
146+
matchLabels:
147+
operator-deployment: simple-operator
148+
template:
149+
metadata:
150+
labels:
151+
operator-deployment: simple-operator
152+
spec:
153+
containers:
154+
- env:
155+
- name: test
156+
valueFrom:
157+
fieldRef:
158+
fieldPath: metadata.namespace
159+
image: ghcr.io/operator/operator:latest
160+
name: simple-operator
161+
resources:
162+
limits:
163+
cpu: 100m
164+
memory: 128Mi
165+
requests:
166+
cpu: 100m
167+
memory: 64Mi
168+
securityContext:
169+
allowPrivilegeEscalation: false
170+
capabilities:
171+
drop:
172+
- ALL
173+
runAsGroup: 2024
174+
runAsNonRoot: true
175+
runAsUser: 2024
176+
securityContext:
177+
runAsNonRoot: true
178+
seccompProfile:
179+
type: RuntimeDefault
180+
terminationGracePeriodSeconds: 10
181+
---
182+

src/K8sOperator.NET/EventWatcher.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ private async Task HandleAddOrModifyAsync(T resource, CancellationToken cancella
240240
}
241241

242242
await _controller.AddOrModifyAsync(resource, cancellationToken);
243+
244+
resource = await ReplaceAsync(resource, _cancellationToken);
245+
243246
_changeTracker.TrackResourceGenerationAsHandled(resource);
244247

245248
Logger.EndAddOrModify(resource);

0 commit comments

Comments
 (0)