Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Duck type clients and reconcilers #402

Merged
merged 5 commits into from
Aug 10, 2023
Merged

Duck type clients and reconcilers #402

merged 5 commits into from
Aug 10, 2023

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Aug 9, 2023

The clients used by reconciler runtime are now able to interact with duck typed resources, with some limitations. The support extends to working inside reconcilers, including with the ResourceReconciler.

Duck typed resources are structured types that implement the client.Object interface but are not registered in the scheme for a GVK. The APIVersion and Kind fields must be set on the object.

Using ResourceReconciler with a duck type is similar to using an unstructured type and then casting to the duck type, however, starting with a duck type will allow the resource to participate in common structured operations like setting the status observed generation, initializing conditions, and coalescing condition timestamps when the condition did not otherwise change.

Known limitations include:

  • Create client methods are not supported. Resources always need to be created with a concrete type.
  • Update client methods are not supported. Use Patch methods instead.
  • ResourceReconciler will patch status if a mutation is detected instead of update. The bytes of the patch must be defined on the ReconcilerTestCase for tests that result in a patch request. Ephemeral values, like LastTransitionTime timestamps, need to be pinned to known values. Use ReconcilerTestCase#Now for timestamp values.

To help solve the ephemeral nature of time, this change also introduces a mechanism to retrieve the current time off the context via rtime.RetrieveNow. Each root reconciler stashes the current time. Additionally, each test case can define the Now field with a custom timestamp that is stashed as the current time.

The condition manager Manage method is deprecated in favor of ManageWithContext, which will use the stashed time when constructing conditions. The status InitializeConditions method should be updated to accept a context when it is defined, the no arg variant is deprecated. When using either deprecated method without a context, time.Now is used instead of the stashed time.

Resolves #53

The clients used by reconciler runtime are now able to interact with
duck typed resources, with some limitations. The support extends to
working inside reconcilers, including with the ResourceReconciler.

Duck typed resources are structured types that implement the
client.Object interface but are not registered in the scheme for a GVK.
The APIVersion and Kind fields must be set on the object.

Using ResourceReconciler with a duck type is similar to using an
unstructured type and then casting to the duck type, however, starting
with a duck type will allow the resource to participate in common
structured operations like setting the status observed generation,
initializing conditions, and coalescing condition timestamps when the
condition did not otherwise change.

Known limitations include:
- Create client methods are not supported. Resources always need to be
  created with a concrete type.
- Update client methods are not supported. Use Patch methods instead.
- ResourceReconciler will patch status if a mutation is detected instead
  of update. The bytes of the patch must be defined on the
  ReconcilerTestCase for tests that result in a patch request.
  Ephemeral values, like LastTransitionTime timestamps, need to be
  pinned to known values. Use ReconcilerTestCase#Now for timestamp
  values.

To help solve the ephemeral nature of time, this change also introduces
a mechanism to retrieve the current time off the context via
rtime.RetrieveNow. Each root reconciler stashes the current time.
Additionally, each test case can define the Now field with a custom
timestamp that is stashed as the current time.

The condition manager Manage method is deprecated in favor of
ManageWithContext, which will use the stashed time when constructing
conditions. The status InitializeConditions method should be updated
to accept a context when it is defined, the no arg variant is
deprecated. When using either deprecated method without a context,
time.Now is used instead of the stashed time.

Signed-off-by: Scott Andrews <[email protected]>
@codecov
Copy link

codecov bot commented Aug 9, 2023

Codecov Report

Patch coverage: 39.24% and project coverage change: -0.74% ⚠️

Comparison is base (4dff503) 61.36% compared to head (d3cd0cc) 60.62%.

❗ Current head d3cd0cc differs from pull request most recent head 7256938. Consider uploading reports for the commit 7256938 to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #402      +/-   ##
==========================================
- Coverage   61.36%   60.62%   -0.74%     
==========================================
  Files          24       24              
  Lines        2275     2334      +59     
==========================================
+ Hits         1396     1415      +19     
- Misses        796      835      +39     
- Partials       83       84       +1     
Files Changed Coverage Δ
apis/conditionset.go 3.72% <0.00%> (-0.17%) ⬇️
reconcilers/childset.go 87.87% <ø> (ø)
reconcilers/config.go 60.86% <0.00%> (ø)
testing/reconciler.go 0.00% <0.00%> (ø)
testing/subreconciler.go 0.00% <0.00%> (ø)
testing/webhook.go 0.00% <0.00%> (ø)
reconcilers/aggregate.go 60.83% <7.69%> (-5.84%) ⬇️
reconcilers/resource.go 74.09% <62.22%> (-4.44%) ⬇️
reconcilers/webhook.go 90.38% <100.00%> (+0.09%) ⬆️
testing/config.go 87.55% <100.00%> (ø)

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Scott Andrews <[email protected]>
@scothis scothis marked this pull request as ready for review August 10, 2023 19:42
@scothis scothis merged commit 376b743 into vmware-labs:main Aug 10, 2023
@scothis scothis deleted the duck-client branch August 10, 2023 19:55
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enhance client to read resources via a duck type
3 participants