kubebuilder
kubebuilder copied to clipboard
Give more explanation and examples about using SetControllerReference
/kind feature
Currently, there is not a clear explanation/section on using SetControllerReference for reconciling on child objects. The example here https://book.kubebuilder.io/cronjob-tutorial/controller-implementation.html?highlight=owner#setup is only setting up one type of resource. What if one controller is managing more than one resource: pods, services, deployments, configmaps, secrets, etc?
.Owns(&corev1.Pod{}).Owns(&corev1.Service{}).Owns(&appsv1.Deployment{}) and so forth.
See also https://godoc.org/sigs.k8s.io/controller-runtime/pkg/builder#Builder.Owns.
It's collapsed, but we talk about it a bit in the constructJobForCronjob section, which me might want to un-collapse, and also in the godocs in various places.
Can you give an example of what was unclear to you? Was it not clear how Owns interacted with SetControllerReference?
Issues go stale after 90d of inactivity.
Mark the issue as fresh with /remove-lifecycle stale.
Stale issues rot after an additional 30d of inactivity and eventually close.
If this issue is safe to close now please do so with /close.
Send feedback to sig-testing, kubernetes/test-infra and/or fejta. /lifecycle stale
I was able to get SetControllerReference working using it within an anonymous function as a parameter for the CreateOrUpdate method. Btw, i think some examples could be added to the book.
redisUrl := "exampleurl.com"
// Service definition
service := corev1.Service{
TypeMeta: metav1.TypeMeta{
Kind: "Service",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: instance.Status.Name,
Namespace: instance.GetNamespace(),
},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeExternalName,
ExternalName: redisUrl,
},
},
}
ctrl.CreateOrUpdate(context.Background(), r, &service, func() error {
if err := ctrl.SetControllerReference(instance, &service, r.Scheme); err != nil {
return err
}
return nil
})
Also, my controller Owns the Service Kind in this case:
func (r *RedisReconciler) SetupWithManager(mgr ctrl.Manager) error {
pred := predicate.GenerationChangedPredicate{}
return ctrl.NewControllerManagedBy(mgr).For(&redisv1beta1.Redis{}).Owns(&corev1.Service{}).WithEventFilter(pred).Complete(r)
}
The new Deploy Image plugin allows users to scaffold API/Controllers to deploy and manage an Operand (image) on the cluster following the guidelines and best practices. It abstracts the complexities of achieving this goal while allowing users to customize the generated code. (More info). You can check also this demo in: https://www.youtube.com/watch?v=UwPuRjjnMjY
Therefore, you can check that the code generated by it provides an example and info about it to help out see:
https://github.com/kubernetes-sigs/kubebuilder/blob/e7c871e8efff01644527c283ba4fb20e8d3dc0d7/testdata/project-v3-with-deploy-image/controllers/busybox_controller.go#L363-L368
We are also clarifying why set the ownerRef is important in the finalizer, see:
https://github.com/kubernetes-sigs/kubebuilder/blob/e7c871e8efff01644527c283ba4fb20e8d3dc0d7/testdata/project-v3-with-deploy-image/controllers/busybox_controller.go#L287-L304
In this way, it would be great if we create a FAQ section and add a question/answer to provide this info and add provide the new plugin to let users be able to check the code.
The Kubernetes project currently lacks enough contributors to adequately respond to all PRs.
This bot triages PRs according to the following rules:
- After 90d of inactivity,
lifecycle/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas applied, the PR is closed
You can:
- Mark this PR as fresh with
/remove-lifecycle stale - Close this PR with
/close - Offer to help out with Issue Triage
Please send feedback to sig-contributor-experience at kubernetes/community.
/lifecycle stale
Closing this one since the examples are provided now in the code scaffold by the plugin : https://book.kubebuilder.io/plugins/deploy-image-plugin-v1-alpha.html