Skip to content

Commit

Permalink
chore: watch-processor support watching generic kind (#1873)
Browse files Browse the repository at this point in the history
## Description

We support doing mutates/validates on GenericKinds by allowing the user
to add in the GVK manually as the second argument to the `When()`. The
problem occurs when the user then tries to watch a resource through
`.Watch() or .Reconcile()` then the KFC does not know how to lookup the
generic kind because the second argument is not passed to the [k8s
function](https://github.com/defenseunicorns/kubernetes-fluent-client/blob/00a7be6c71fb39855e1e2329850c5fa868979576/src/fluent/index.ts#L26).

The second point here is that the same GVK look up happening twice,
[once](https://github.com/defenseunicorns/pepr/blob/fe7f40df5e062d6b64fd65418a310ce35e2eb975/src/lib/core/capability.ts#L184)
in the Capability class when the modules are being transformed into
capability exports, and then again when Pepr calls the KFC watch (and
KFC watch does a second
[lookup](https://github.com/defenseunicorns/kubernetes-fluent-client/blob/00a7be6c71fb39855e1e2329850c5fa868979576/src/fluent/index.ts#L29)).
This is an important backstop for KFC since it is its own project and
does not rely on Pepr, but in this case, we already have all of the
information.

The K8s KFC function takes filters that account for `kindOverride`, by
adding kindOverride as the kind, then we pass the information from the
first GVK lookup OR the GVK from the binding directly to the K8s
function. There is no need to consider the kind of the Kubernetes
object, whether it is a GenericKind or well known, since the
kindOverrides filter will always have the appropriate information,
whether from the GVK map or from the GVK argument in `When()`.

(Capability adds correct types to binding kind
[here](https://github.com/defenseunicorns/pepr/blob/fe7f40df5e062d6b64fd65418a310ce35e2eb975/src/lib/core/capability.ts#L194))

```ts
When(a.GenericKind, {
  group: "pepr.dev",
  version: "v1",
  kind: "Unicorn",
})
```

- [x] **Needs excellent example coverage**
[PEXEX](defenseunicorns/pepr-excellent-examples#237)

```bash
 PASS  capabilities/genericKind.e2e.test.ts (46.82 s)
  genericKind.ts
    ✓ watches resource creates (542 ms)
    ✓ watches resource create-or-updates (544 ms)
    ✓ watches resource updates (568 ms)
    ✓ watches resource deletes (527 ms)
 ```

![image](https://github.com/user-attachments/assets/4d7ece44-7936-46db-a2a8-18a36509690b)
![image](https://github.com/user-attachments/assets/1b3b40a4-8eea-4997-bb78-ac7a90e348b2)


## Related Issue

Fixes #1838 
<!-- or -->
Relates to #

## Type of change

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Other (security config, docs update, etc)

## Checklist before merging
- [x] Unit, [Journey](https://github.com/defenseunicorns/pepr/tree/main/journey), [E2E Tests](https://github.com/defenseunicorns/pepr-excellent-examples), [docs](https://github.com/defenseunicorns/pepr/tree/main/docs), [adr](https://github.com/defenseunicorns/pepr/tree/main/adr) added or updated as needed
- [x] [Contributor Guide Steps](https://docs.pepr.dev/main/contribute/#submitting-a-pull-request) followed

Signed-off-by: Case Wylie <[email protected]>
  • Loading branch information
cmwylie19 authored Feb 25, 2025
1 parent 2c6eeae commit ca4ab1a
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/lib/processors/watch-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async function runBinding(
};

// Setup the resource watch
const watcher = K8s(binding.model, binding.filters).Watch(async (obj, phase) => {
const watcher = K8s(binding.model, { ...binding.filters, kindOverride: binding.kind }).Watch(async (obj, phase) => {
Log.debug(obj, `Watch event ${phase} received`);

if (binding.isQueue) {
Expand Down

0 comments on commit ca4ab1a

Please sign in to comment.