kubernetes-sigs / kubebuilder-declarative-pattern

A toolkit for building declarative operators with kubebuilder
Apache License 2.0
254 stars 84 forks source link

[Question] How to make some changes to a generated reconclier #235

Closed djdongjin closed 2 years ago

djdongjin commented 2 years ago

Hi, I have a question about reconcilers built based on kubebuild-declarative-pattern. Suppose I want that in the reconcile loop, the reconciler should read an annotation from a namespace and based on that annotation value, add a node-affinity (or any other deployment change) to the deployment it creates. Is it possible? Thanks! For example:

  1. Reconclier: https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/examples/guestbook-operator/controllers/guestbook_controller.go#L55
  2. Deployment: https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/examples/guestbook-operator/channels/packages/guestbook/0.1.0/manifest.yaml#L19

Basically, the question is how to make some customization to the generated reconclier.

atoato88 commented 2 years ago

I think we can do that operation by multiple options.

I'm not sure that which one is better for your requirement, but maybe option 2 is better because your requirement seems to have some logics depending on complicated condtion.

djdongjin commented 2 years ago

@atoato88 thanks for the suggestion! I finally figured it out using the WithObjectTransform and MutatePodSpec here

atoato88 commented 2 years ago

You're welcome, and thank you for sharing your solution. 😉