[metadata.ownerReferences] Error to create the deployment and service with custom CRDs #3160
-
Hi guys, I'm trying to create a CRDs but I'm having errors when creating the deployment and service. the error is: My parameters are:
I changed the parameters OwnerReferences to variable and not worked. I'm using this kubebuilder version: How can I set the Owner reference in the deployment/service? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, I managed to adjust the code. The problem was I didn't call the function "r.Get(ctx, req, d)" to read the information from my Kind. I adjust it and worked. Thank you! |
Beta Was this translation helpful? Give feedback.
-
Hi @Tomelin , The error you're encountering seems came from the fact that the Here's how you can do it:
import (
...
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
)
// Assuming you've fetched the actual Discovery CR into a variable named 'd'
svc := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "sc-monogodb",
Namespace: "service-discovery",
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{{
Name: "sc-mongodb",
Protocol: corev1.ProtocolTCP,
Port: 27017,
}},
},
}
// Set the owner reference
if err := controllerutil.SetControllerReference(d, svc, mgr.GetScheme()); err != nil {
return err
} I hope that answer your question.
I am closing this one as sorted out |
Beta Was this translation helpful? Give feedback.
Hi @Tomelin ,
The error you're encountering seems came from the fact that the
name
anduid
fields in theOwnerReferences
shouldn't be empty. Using thecontroller-runtime
library, which comes bundled withkubebuilder
, there's a more straightforward way to set the owner reference correctly.Here's how you can do it:
Fetch the actual
Discovery
custom resource (CR) instance that you want to set as the owner of theService
object.Use the
controllerutil.SetOwnerReference
method to correctly set the owner reference.