kubernetes-sigs / kubebuilder-declarative-pattern

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

mockkubeapiserver: add test using SMD library #270

Closed justinsb closed 1 year ago

justinsb commented 1 year ago

Add a test before starting to use the SMD library to describe the built-in types.

k8s-ci-robot commented 1 year ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: justinsb

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[OWNERS](https://github.com/kubernetes-sigs/kubebuilder-declarative-pattern/blob/master/OWNERS)~~ [justinsb] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment
justinsb commented 1 year ago

/assign @apelisse

This library is awesome! I have two TODOs where I'm not quite sure I'm using it right (and I also would like to talk about whether I should be embedding the yaml form or if there's a better way)

atoato88 commented 1 year ago

Sorry for my less understandings.. What is the SMD library? SMD stands for what?

justinsb commented 1 year ago

Hi @atoato88 - SMD is the name for the sigs.k8s.io/structured-merge-diff/v4 library. It implements the core logic of server-side apply.

The goal of the mockkubeapiserver is to have something like fakeclient, but that supports server-side-apply. envtest might work, but it's harder to intercept all the requests, it's not possible to inject faults, and then users have to set up kube-apiserver & etcd in the KUBEBUILDER_ASSETS. I actually hope we can get to a place where we can run tests with either a "real" kube-apiserver, or envtest, or mockkubeapiserver, just by changing an env var.

SMD shouldn't get linked into controllers (except in tests), it's only a good way to implement server-side apply in our mocks.

Although one of the things that is sort of interesting is that we might be able to do a much better job of detecting when two controllers are both trying to change the same object by looking at the field manager, or when someone has made a conflicting vs a non-conflicting change. We probably don't need the full SMD library to do that though!

justinsb commented 1 year ago

Thanks for asking BTW - I might take this text and put it into the readme for mockkubeapiserver :-) (Particularly if you think it's useful!)