kubebuilder icon indicating copy to clipboard operation
kubebuilder copied to clipboard

Give more explanation and examples about using SetControllerReference

Open yashbhutwala opened this issue 6 years ago • 4 comments
trafficstars

/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?

yashbhutwala avatar Jul 25 '19 10:07 yashbhutwala

.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?

DirectXMan12 avatar Aug 06 '19 00:08 DirectXMan12

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

fejta-bot avatar Nov 04 '19 00:11 fejta-bot

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)
}

cargaona avatar Sep 23 '20 23:09 cargaona

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.

camilamacedo86 avatar Sep 12 '22 06:09 camilamacedo86

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/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was 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

k8s-triage-robot avatar Feb 08 '23 03:02 k8s-triage-robot

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

camilamacedo86 avatar Feb 11 '23 15:02 camilamacedo86