@@ -7,9 +7,12 @@ resources on kubernetes and queueing of the events.
77When you want to create a controller for your (or any) entity,
88read the following instructions.
99
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.
1316
1417## Controller instance
1518
@@ -18,16 +21,14 @@ or you want to reconcile a given entity (from the `k8s.Models` namespace,
1821e.g. ` V1ConfigMap ` ) you need to create a controller class
1922as you would do for a MVC or API controller in asp.net.
2023
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.
2425
2526``` csharp
2627[EntityRbac (typeof (MyCustomEntity ), Verbs = RbacVerb .All )]
27- public class FooCtrl : ResourceControllerBase <MyCustomEntity >
28+ public class FooCtrl : IResourceController <MyCustomEntity >
2829{
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 .
3132 // Possible overwrites:
3233 // "Created" (i.e. when the operator sees the entity for the first time),
3334 // "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
8687
8788## Requeue
8889
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:
9392
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.
9697
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 .
99100
100101``` csharp
101102/* 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 .
105106 }
106107
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 .
110111 }
111112/* snip... */
112113```
@@ -117,22 +118,11 @@ If the function throws an error, the event is requeued with an exponential backo
117118
118119```csharp
119120/* snip... */
120- protected override async Task < TimeSpan ? > Created ( MyCustomEntity resource ){
121+ public Task <ResourceControllerResult > CreatedAsync ( V1TestEntity resource )
121122 // do something useful.
122123 throw new Exception ("¯\\_ (ツ)_ / ¯" );
123124}
124125/* snip... */
125126```
126127
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