Skip to content

Commit bb8ec38

Browse files
✨ (deploy-image/v1-alpha1): Add comments to clarify resource watching and reconciliation logic
Added detailed comments in the SetupManager scaffold within the controller to explain how the resource watching and reconciliation process works. These comments clarify how the controller watches for changes in Custom Resources and Deployments, ensuring that the cluster maintains the desired state.
1 parent d47b048 commit bb8ec38

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,22 @@ func imageForMemcached() (string, error) {
427427
}
428428

429429
// SetupWithManager sets up the controller with the Manager.
430-
// Note that the Deployment will be also watched in order to ensure its
431-
// desirable state on the cluster
430+
// The whole idea is to be watching the resources that matter for the controller.
431+
// When a resource that the controller is interested in changes, the Watch triggers
432+
// the controller’s reconciliation loop, ensuring that the actual state of the resource
433+
// matches the desired state as defined in the controller’s logic.
434+
//
435+
// Notice how we configured the Manager to monitor events such as the creation, update,
436+
// or deletion of a Custom Resource (CR) of the Memcached kind, as well as any changes
437+
// to the Deployment that the controller manages and owns.
432438
func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error {
433439
return ctrl.NewControllerManagedBy(mgr).
440+
// Watch the Memcached CR(s) and trigger reconciliation whenever it
441+
// is created, updated, or deleted
434442
For(&cachev1alpha1.Memcached{}).
443+
// Watch the Deployment managed by the MemcachedReconciler. If any changes occur to the Deployment
444+
// owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster
445+
// state aligns with the desired state. See that the ownerRef was set when the Deployment was created.
435446
Owns(&appsv1.Deployment{}).
436447
Complete(r)
437448
}

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,16 +460,27 @@ func imageFor{{ .Resource.Kind }}() (string, error) {
460460
}
461461
462462
// SetupWithManager sets up the controller with the Manager.
463-
// Note that the Deployment will be also watched in order to ensure its
464-
// desirable state on the cluster
463+
// The whole idea is to be watching the resources that matter for the controller.
464+
// When a resource that the controller is interested in changes, the Watch triggers
465+
// the controller’s reconciliation loop, ensuring that the actual state of the resource
466+
// matches the desired state as defined in the controller’s logic.
467+
//
468+
// Notice how we configured the Manager to monitor events such as the creation, update,
469+
// or deletion of a Custom Resource (CR) of the {{ .Resource.Kind }} kind, as well as any changes
470+
// to the Deployment that the controller manages and owns.
465471
func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) error {
466472
return ctrl.NewControllerManagedBy(mgr).
467473
{{ if not (isEmptyStr .Resource.Path) -}}
474+
// Watch the {{ .Resource.Kind }} CR(s) and trigger reconciliation whenever it
475+
// is created, updated, or deleted
468476
For(&{{ .Resource.ImportAlias }}.{{ .Resource.Kind }}{}).
469477
{{- else -}}
470478
// Uncomment the following line adding a pointer to an instance of the controlled resource as an argument
471479
// For().
472480
{{- end }}
481+
// Watch the Deployment managed by the {{ .Resource.Kind }}Reconciler. If any changes occur to the Deployment
482+
// owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster
483+
// state aligns with the desired state. See that the ownerRef was set when the Deployment was created.
473484
Owns(&appsv1.Deployment{}).
474485
Complete(r)
475486
}

testdata/project-v4-with-deploy-image/internal/controller/busybox_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,11 +421,22 @@ func imageForBusybox() (string, error) {
421421
}
422422

423423
// SetupWithManager sets up the controller with the Manager.
424-
// Note that the Deployment will be also watched in order to ensure its
425-
// desirable state on the cluster
424+
// The whole idea is to be watching the resources that matter for the controller.
425+
// When a resource that the controller is interested in changes, the Watch triggers
426+
// the controller’s reconciliation loop, ensuring that the actual state of the resource
427+
// matches the desired state as defined in the controller’s logic.
428+
//
429+
// Notice how we configured the Manager to monitor events such as the creation, update,
430+
// or deletion of a Custom Resource (CR) of the Busybox kind, as well as any changes
431+
// to the Deployment that the controller manages and owns.
426432
func (r *BusyboxReconciler) SetupWithManager(mgr ctrl.Manager) error {
427433
return ctrl.NewControllerManagedBy(mgr).
434+
// Watch the Busybox CR(s) and trigger reconciliation whenever it
435+
// is created, updated, or deleted
428436
For(&examplecomv1alpha1.Busybox{}).
437+
// Watch the Deployment managed by the BusyboxReconciler. If any changes occur to the Deployment
438+
// owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster
439+
// state aligns with the desired state. See that the ownerRef was set when the Deployment was created.
429440
Owns(&appsv1.Deployment{}).
430441
Complete(r)
431442
}

testdata/project-v4-with-deploy-image/internal/controller/memcached_controller.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,22 @@ func imageForMemcached() (string, error) {
427427
}
428428

429429
// SetupWithManager sets up the controller with the Manager.
430-
// Note that the Deployment will be also watched in order to ensure its
431-
// desirable state on the cluster
430+
// The whole idea is to be watching the resources that matter for the controller.
431+
// When a resource that the controller is interested in changes, the Watch triggers
432+
// the controller’s reconciliation loop, ensuring that the actual state of the resource
433+
// matches the desired state as defined in the controller’s logic.
434+
//
435+
// Notice how we configured the Manager to monitor events such as the creation, update,
436+
// or deletion of a Custom Resource (CR) of the Memcached kind, as well as any changes
437+
// to the Deployment that the controller manages and owns.
432438
func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error {
433439
return ctrl.NewControllerManagedBy(mgr).
440+
// Watch the Memcached CR(s) and trigger reconciliation whenever it
441+
// is created, updated, or deleted
434442
For(&examplecomv1alpha1.Memcached{}).
443+
// Watch the Deployment managed by the MemcachedReconciler. If any changes occur to the Deployment
444+
// owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster
445+
// state aligns with the desired state. See that the ownerRef was set when the Deployment was created.
435446
Owns(&appsv1.Deployment{}).
436447
Complete(r)
437448
}

0 commit comments

Comments
 (0)