@@ -7,9 +7,12 @@ resources on kubernetes and queueing of the events.
7
7
When you want to create a controller for your (or any) entity,
8
8
read the following instructions.
9
9
10
- When you have controllers, don't forget to register them with
11
- < xref:KubeOps.Operator.Builder.IOperatorBuilder.AddController``1 >
12
- to the DI system.
10
+ When you have controllers, they are automatically added to the
11
+ DI system via their < xref:KubeOps.Operator.Controller.IResourceController`1 > interface.
12
+
13
+ Controllers are registered as ** scoped** elements in the DI system.
14
+ Which means, they basically behave like asp.net api controllers.
15
+ You can use dependency injection with all types of dependencies.
13
16
14
17
## Controller instance
15
18
@@ -18,16 +21,14 @@ or you want to reconcile a given entity (from the `k8s.Models` namespace,
18
21
e.g. ` V1ConfigMap ` ) you need to create a controller class
19
22
as you would do for a MVC or API controller in asp.net.
20
23
21
- Make sure you have the correct baseclass
22
- (< xref:KubeOps.Operator.Controller.ResourceControllerBase`1 > )
23
- inherited.
24
+ Make sure you implement the < xref:KubeOps.Operator.Controller.IResourceController`1 > interface.
24
25
25
26
``` csharp
26
27
[EntityRbac (typeof (MyCustomEntity ), Verbs = RbacVerb .All )]
27
- public class FooCtrl : ResourceControllerBase <MyCustomEntity >
28
+ public class FooCtrl : IResourceController <MyCustomEntity >
28
29
{
29
- protected override async Task < TimeSpan ?> Created ( MyCustomEntity resource ){}
30
- // overwrite other methods here .
30
+ // Implement the needed methods here.
31
+ // The interface provides default implementation which do a NOOP .
31
32
// Possible overwrites:
32
33
// "Created" (i.e. when the operator sees the entity for the first time),
33
34
// "Updated" (i.e. when the operator knows the entity and it was updated),
@@ -86,27 +87,27 @@ which takes a list of api groups, resources, versions and a selection of
86
87
87
88
## Requeue
88
89
89
- The controller's methods have a return value of ` TimeSpan? ` . This means
90
- you can return a time span to automatically requeue the event for the
91
- given entity. If requeued and nothing changed, it will most likely fire
92
- a ` NotModified ` event.
90
+ The controller's methods have a return value of < xref:KubeOps.Operator.Controller.Results.ResourceControllerResult > .
91
+ There are multiple ways how a result of a controller can be created:
93
92
94
- This can be useful if you want to periodically check for a database
95
- connection for example and update the status of a given entity.
93
+ - ` null ` : The controller will not requeue your entity / event.
94
+ - < xref:KubeOps.Operator.Controller.Results.ResourceControllerResult.RequeueEvent(System.TimeSpan) > :
95
+ Return a result object with a < xref:System.TimeSpan > that will requeue
96
+ the event and the entity after the time has passed.
96
97
97
- If you return ` null ` in an event function, the event is not requeued.
98
- If you return a timespan, then the event is requeued after this delay .
98
+ The requeue mechanism can be useful if you want to periodically check for a database
99
+ connection for example and update the status of a given entity .
99
100
100
101
``` csharp
101
102
/* snip... */
102
- protected override async Task < TimeSpan ? > Created ( MyCustomEntity resource ){
103
- // do something useful.
104
- return TimeSpan .FromSeconds (15 ); // This will retrigger an event in 15 secs .
103
+ public Task < ResourceControllerResult > CreatedAsync ( V1TestEntity resource )
104
+ {
105
+ return Task . FromResult ( ResourceControllerResult . RequeueEvent ( TimeSpan .FromSeconds (15 )) ; // This will requeue the event in 15 seconds .
105
106
}
106
107
107
- protected override async Task < TimeSpan ? > Updated ( MyCustomEntity resource ){
108
- // do something useful.
109
- return null ; // This will not retrigger an event .
108
+ public Task < ResourceControllerResult > CreatedAsync ( V1TestEntity resource )
109
+ {
110
+ return Task . FromResult < ResourceControllerResult >( null ) ; // This wont trigger a requeue .
110
111
}
111
112
/* snip... */
112
113
```
@@ -117,22 +118,11 @@ If the function throws an error, the event is requeued with an exponential backo
117
118
118
119
```csharp
119
120
/* snip... */
120
- protected override async Task < TimeSpan ? > Created ( MyCustomEntity resource ){
121
+ public Task <ResourceControllerResult > CreatedAsync ( V1TestEntity resource )
121
122
// do something useful.
122
123
throw new Exception ("¯\\_ (ツ)_ / ¯" );
123
124
}
124
125
/* snip... */
125
126
```
126
127
127
- The backoff function is defined as follows:
128
-
129
- ``` csharp
130
- private const double MaxRetrySeconds = 64 ;
131
- private TimeSpan ExponentialBackoff (int retryCount ) => TimeSpan
132
- .FromSeconds (Math .Min (Math .Pow (2 , retryCount ), MaxRetrySeconds ))
133
- .Add (TimeSpan .FromMilliseconds (_rnd .Next (0 , 1000 )));
134
- ```
135
-
136
- Which means with each retry, it calculates the new backoff time
137
- to a max of 64. To each of those times a random number of milliseconds
138
- is added to add a certain fuzzying.
128
+ Each event that errors will be retried ** four times** .
0 commit comments