Skip to content

Commit c5877e2

Browse files
authored
Add reconcile details (#2612)
1 parent cc11ac4 commit c5877e2

File tree

2 files changed

+168
-2
lines changed

2 files changed

+168
-2
lines changed

doc/user-guide.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,89 @@ return reconcile.Result{RequeueAfter: time.Second*5}, nil
249249

250250
**Note:** Returning `Result` with `RequeueAfter` set is how you can periodically reconcile a CR.
251251

252-
For a guide on Reconcilers, Clients, and interacting with resource Events, see the [Client API doc][doc_client_api].
252+
#### Reconcile Result Use Cases
253+
**The following are possible reconcile loop return options.**
254+
255+
#### 1. With the error:
256+
257+
If an error is encountered during processing the appropriate return option is to return an error.
258+
This results in the reconcile loop being re-triggered to run again.
259+
260+
**Usage**
261+
```Go
262+
return reconcile.Result{}, err
263+
```
264+
265+
**Example:**
266+
267+
In the example below a `reconcile.Result{}, err` is used when there is an error reading the object.
268+
As a result the request is requeued for another try.
269+
```Go
270+
// Fetch the Memcached instance
271+
memcached := &cachev1alpha1.Memcached{}
272+
err := r.client.Get(context.TODO(), request.NamespacedName, memcached)
273+
if err != nil {
274+
if errors.IsNotFound(err) {
275+
...
276+
}
277+
// Error reading the object - requeue the request.
278+
reqLogger.Error(err, "Failed to get Memcached")
279+
return reconcile.Result{}, err
280+
}
281+
```
282+
283+
#### 2. Without an error:
284+
285+
There are several situations where although no error occured, the reconcile loop should signify
286+
during its return that it needs to run again.
287+
288+
**Usage**
289+
```Go
290+
return reconcile.Result{Requeue: true}, nil
291+
```
292+
293+
**Example:**
294+
295+
In the example below a `reconcile.Result{Requeue: true}, nil` is used because a new resource is being created and as such there is the potential that further processing is required. Thus, the reconcile loop needs to trigger a requeue but there is no error associated with this requeue.
296+
As a result the request is requeued for another try.
297+
```Go
298+
// Define a new deployment
299+
dep := r.deploymentForMemcached(memcached)
300+
...
301+
302+
// Deployment created successfully - return and requeue
303+
return reconcile.Result{Requeue: true}, nil
304+
```
305+
306+
#### 3. Without an error and no need to requeue the request:
307+
308+
In some situations, such as when the primary resource has been deleted, there is no need to
309+
requeue the request for another attempt
310+
311+
**Usage**
312+
```Go
313+
return reconcile.Result{}, nil
314+
```
315+
316+
**Example:**
317+
318+
In the example below a `reconcile.Result{}, nil` is used because the Memcached resource was not found, and no further processing is required.
319+
```Go
320+
// Fetch the Memcached instance
321+
memcached := &cachev1alpha1.Memcached{}
322+
err := r.client.Get(context.TODO(), request.NamespacedName, memcached)
323+
if err != nil {
324+
if errors.IsNotFound(err) {
325+
// Request object not found, could have been deleted after reconcile request.
326+
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
327+
// Return and don't requeue
328+
reqLogger.Info("Memcached resource not found. Ignoring since object must be deleted")
329+
return reconcile.Result{}, nil
330+
}
331+
...
332+
```
333+
334+
For a guide on Reconcilers, Clients, and interacting with resource Events, see the [Client API doc][doc_client_api] and the [controller-runtime documentation over reconcile][controller-runtime-reconcile-godoc].
253335
254336
## Build and run the operator
255337
@@ -760,6 +842,7 @@ When the operator is not running in a cluster, the Manager will return an error
760842
[event_filtering]:./user/event-filtering.md
761843
[controller_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/controller#Options
762844
[controller_godocs]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/controller
845+
[controller-runtime-reconcile-godoc]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/reconcile#Reconciler
763846
[operator_scope]:./operator-scope.md
764847
[install_guide]: ./user/install-operator-sdk.md
765848
[pod_eviction_timeout]: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-controller-manager/#options

website/content/en/docs/golang/quickstart.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,89 @@ return reconcile.Result{RequeueAfter: time.Second*5}, nil
238238

239239
**Note:** Returning `Result` with `RequeueAfter` set is how you can periodically reconcile a CR.
240240

241-
For a guide on Reconcilers, Clients, and interacting with resource Events, see the [Client API doc][doc_client_api].
241+
#### Reconcile Result Use Cases
242+
**The following are possible reconcile loop return options.**
243+
244+
#### 1. With the error:
245+
246+
If an error is encountered during processing the appropriate return option is to return an error.
247+
This results in the reconcile loop being re-triggered to run again.
248+
249+
**Usage**
250+
```Go
251+
return reconcile.Result{}, err
252+
```
253+
254+
**Example:**
255+
256+
In the example below a `reconcile.Result{}, err` is used when there is an error reading the object.
257+
As a result the request is requeued for another try.
258+
```Go
259+
// Fetch the Memcached instance
260+
memcached := &cachev1alpha1.Memcached{}
261+
err := r.client.Get(context.TODO(), request.NamespacedName, memcached)
262+
if err != nil {
263+
if errors.IsNotFound(err) {
264+
...
265+
}
266+
// Error reading the object - requeue the request.
267+
reqLogger.Error(err, "Failed to get Memcached")
268+
return reconcile.Result{}, err
269+
}
270+
```
271+
272+
#### 2. Without an error:
273+
274+
There are several situations where although no error occured, the reconcile loop should signify
275+
during its return that it needs to run again.
276+
277+
**Usage**
278+
```Go
279+
return reconcile.Result{Requeue: true}, nil
280+
```
281+
282+
**Example:**
283+
284+
In the example below a `reconcile.Result{Requeue: true}, nil` is used because a new resource is being created and as such there is the potential that further processing is required. Thus, the reconcile loop needs to trigger a requeue but there is no error associated with this requeue.
285+
As a result the request is requeued for another try.
286+
```Go
287+
// Define a new deployment
288+
dep := r.deploymentForMemcached(memcached)
289+
...
290+
291+
// Deployment created successfully - return and requeue
292+
return reconcile.Result{Requeue: true}, nil
293+
```
294+
295+
#### 3. Without an error and no need to requeue the request:
296+
297+
In some situations, such as when the primary resource has been deleted, there is no need to
298+
requeue the request for another attempt
299+
300+
**Usage**
301+
```Go
302+
return reconcile.Result{}, nil
303+
```
304+
305+
**Example:**
306+
307+
In the example below a `reconcile.Result{}, nil` is used because the Memcached resource was not found, and no further processing is required.
308+
```Go
309+
// Fetch the Memcached instance
310+
memcached := &cachev1alpha1.Memcached{}
311+
err := r.client.Get(context.TODO(), request.NamespacedName, memcached)
312+
if err != nil {
313+
if errors.IsNotFound(err) {
314+
// Request object not found, could have been deleted after reconcile request.
315+
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
316+
// Return and don't requeue
317+
reqLogger.Info("Memcached resource not found. Ignoring since object must be deleted")
318+
return reconcile.Result{}, nil
319+
}
320+
...
321+
```
322+
323+
For a guide on Reconcilers, Clients, and interacting with resource Events, see the [Client API doc][doc_client_api] and the [controller-runtime documentation over reconcile][controller-runtime-reconcile-godoc].
242324
243325
## Build and run the operator
244326
@@ -751,6 +833,7 @@ When the operator is not running in a cluster, the Manager will return an error
751833
[event_filtering]:/docs/golang/references/event-filtering/
752834
[controller_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/controller#Options
753835
[controller_godocs]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/controller
836+
[controller-runtime-reconcile-godoc]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/reconcile#Reconciler
754837
[operator_scope]:/docs/operator-scope/
755838
[pod_eviction_timeout]: https://kubernetes.io/docs/reference/command-line-tools-reference/kube-controller-manager/#options
756839
[manager_options]: https://godoc.org/github.com/kubernetes-sigs/controller-runtime/pkg/manager#Options

0 commit comments

Comments
 (0)