How to know which custom controller handle a certain CRD - kubernetes

With CRD we can extend the functionality of kubernetes, but how could I know which controller handle a certain CRD, I mean I know there is a CRD registered in my kubernetes named foo but how could I know which controller/pod do the reconcile for it?

There is no way of knowing just by looking at the CRDs. Several different controllers could be watching the same CRD, it's not like there is a 1-1 relationship.
If you really need to know, there would be ways of figuring this out, like enabling the audit log and inspecting the calls received by the k8s api.

Related

How to create kubernetes custom resources or custom plugin?

Whenever the deployment has been created needs to trigger a custom function or webhook. Does Kubernetes provide any option to do this?
Custom Resources are an extension to the Kubernetes API. Just having them standalone is not going to do anything functionally for you. If you need to perform a specific action upon change or deployment or bare existence of a given custom resource, you will need a custom controller that does that.
One of the possible implementations is an Operator. I specifically mention that, as it is fairly easy to create the controller alongside the custom resource definition using Operator SDK. However you can just create a custom resource definition and deploy a custom controller.
On a closing note: there are other ways your question is very broadly formulated so there is a vast variety of ways to answer, and this is just one option.

Any way to import Kubernetes Object Schemas into CRD Schemas?

I am looking to create a CRD, which has some of the Specs of an existing k8s object. Is there a way of importing the schema and validation checks of the existing spec instead of manually repeating it again?
For reference, I am registering the CRD with the API like this - https://gist.github.com/tallclair/2491c8034f62629b224260fb8a1854d9#file-dynamic_crds-go-L56
And I would like to add a PodSpec into this CRD type.
CRD are managed by a controller specific to that CRD.
Validation of an object concerning the CRD is achieved through a service that takes a call from the API, in this case validation would work along these lines, admission controller validating webhook
More generally, your CRD does not need to concern itself with podspec per se. The CRD is just some declarative representation of the resource you want your controller to manage.
Extending the k8s api mostly works something like this;
think up some bundled functionality you would like to represent declaratively in one schema (the CRD)
create a controller that handles your CRD
add some validation to make sure the API will reject objects that will confuse the controller you made, and hook it up to the API by way of the Dynamic Admission Control
your controller manages the resources required to fullfil the functionality described
I'm sure you could use a podspec in your CRD, but I wouldn't. Generally that's an abstraction better left to the controller managing that specific resource.

Webhooks in kubernetes

I have a requirement to watch for PVC creation events and handle it before forwarding to actual Kubernetes controller for processing the PVC creation. I went through many docs and feel webhooks can be used for achieving this. But would like to get expert opinion on the approach and best practices.

Control Loop Implementation - Kubernetes Alternatives

We are in the process of designing a cloud native application that needs a control loop to keep its objects (few thousands) in desired state. Other than implementing the application as a set of Kubernetes CRDs, we are wondering whether there are any other open source alternatives. If you have developed your own custom implementation of control loop, can you please let us know the reasons behind that decision (as opposed to using Kubernetes CRDs)?
Your description seems to fit with the purpose of a CRD controller.
Check the Kubebuilder framework, you can bootstrap a controller quickly and you will just need to implement the reconcile loop

How to create a replicaset through a custom resource?

I want to create a custom resource that is able to create a replicaset under a certain event. What is the best way to accomplish this?
Note that I am aware of deployment, but deployment does not meet my intended use cases.
Seems like you might be looking into building something that would suit more or less the operator pattern.
https://coreos.com/operators/
https://coreos.com/blog/introducing-operators.html
https://github.com/coreos/prometheus-operator
Generaly you need to watch on some resources including your custom ones with kube client and act based on events propagated from kube API.