I am trying to create two k8s resources (namely Job and Secret) from a single CR. meaning the controller watches the primary resource (which is the CR) and create those two k8s resources. So when I delete my operator CRD, expecting all the resources created by operator also be deleted. But only one resource got deleted depending on the order the reference is set
controllerutil.SetControllerReference(myOperator, newSecret, r.scheme)
controllerutil.SetControllerReference(myOperator, newJob, r.scheme)
In this case only newJob got deleted while I delete the CRD.
I am not sure what is the best practice to create/manage multiple k8s resources using a single CRD/CR
In case if I have to create multiple controllers from a single CR then how to make sequential dependencies between resources creation. Because to create Job the Secret must exist.
@ffoysal You have it right the first time.
Your operator should be using 1 controller for 1 primary resource(your CRD) and creating secondary resources(Job and Secret) with OwnerReferences set to their respective CRs.
Having only one two secondary resources type being set with the correct ownerref or not being garbage collected might be a bug elsewhere in the reconcile loop.
Can you share more about your reconcile loop on how you're setting the owner refs and then creating each object. Just the above two lines seem to be the correct way to set ownerrefs.
Can you also get the YAML output for your CR and secondary resources to see if the ownerref for the secondary objects is not being correctly set to your CR instance? e.g kubectl get secret <secret-name> -o yaml
Hi @ffoysal,
Just to complement here, regards the following point:
In case if I have to create multiple controllers from a single CR then how to make sequential dependencies between resources creation. Because to create Job the Secret must exist.
Note that the reconcile is a loop and it will not stop until all conditions are as expected. So, you just need to impl the checks. See here an example. See the error returned in the reconcile here which will make it starts again. In this example the Backup service(represented by the Backup CR/CRD) needs the Pod and Service created for the Database(represented by Postgresql CR/CRD). So, if it is not found it will be re-queued(the loop will not stop) until it is satisfied.
Thank you @hasbro17 and @camilamacedo86 .
I was able to create multiple resources by following this instead of using controllerutil. now if I delete CRD or CR both resources also deleted which I wanted.
@hasbro17 my bad found a bug in my code. using contollerutil also works
Most helpful comment
@ffoysal You have it right the first time.
Your operator should be using 1 controller for 1 primary resource(your CRD) and creating secondary resources(Job and Secret) with OwnerReferences set to their respective CRs.
Having only one two secondary resources type being set with the correct ownerref or not being garbage collected might be a bug elsewhere in the reconcile loop.
Can you share more about your reconcile loop on how you're setting the owner refs and then creating each object. Just the above two lines seem to be the correct way to set ownerrefs.
Can you also get the YAML output for your CR and secondary resources to see if the ownerref for the secondary objects is not being correctly set to your CR instance? e.g
kubectl get secret <secret-name> -o yaml