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

Cast parent resource type #51

Merged
merged 3 commits into from
Dec 7, 2020
Merged

Cast parent resource type #51

merged 3 commits into from
Dec 7, 2020

Conversation

scothis
Copy link
Contributor

@scothis scothis commented Dec 7, 2020

A CastParent reconciler casts the ParentReconciler's type by projecting the
resource data onto a new struct. Casting the parent resource is
useful to create cross cutting reconcilers that can operate on common
portion of multiple parent resources, commonly referred to as a duck type.

JSON encoding is used as the intermediate representation. Operations on
a cast parent are read-only. Attempts to mutate the parent will result
in the reconciler erring.

func FunctionReconciler(c reconcilers.Config) *reconcilers.ParentReconciler {
	c.Log = c.Log.WithName("Function")

	return &reconcilers.ParentReconciler{
		Type: &buildv1alpha1.Function{},
		Reconciler: reconcilers.Sequence{
			&reconcilers.CastParent{
				Type: &duckv1alpha1.ImageRef{},
				Reconciler: &reconcilers.SyncReconciler{
					Sync: func(ctx context.Context, parent *duckv1alpha1.ImageRef) error {
						// do something with the duckv1alpha1.ImageRef instead of a buildv1alpha1.Function
						return nil
					},
					Config: c,
				},
			},
			FunctionChildImageReconciler(c),
		},

		Config: c,
	}
}

Signed-off-by: Scott Andrews andrewssc@vmware.com

A CastParent reconciler casts the type of the ParentReconciler's type by
projecting the data onto a new resource. Casting the parent resource is
useful to create cross cutting reconcilers that can operate on common
portion of multiple parent resources, common referred to as a duck type.

JSON encoding is used as the intermediate representation. Operations on
a cast parent are read-only. Attempts to mutate the parent will result
in the reconciler erring.

```
func FunctionReconciler(c reconcilers.Config) *reconcilers.ParentReconciler {
	c.Log = c.Log.WithName("Function")

	return &reconcilers.ParentReconciler{
		Type: &buildv1alpha1.Function{},
		Reconciler: reconcilers.Sequence{
			&reconcilers.CastParent{
				Type: &duckv1alpha1.ImageRef{},
				Reconciler: &reconcilers.SyncReconciler{
					Sync: func(ctx context.Context, parent *duckv1alpha1.ImageRef) error {
						// do something with the duckv1alpha1.ImageRef instead of a buildv1alpha1.Function
						return nil
					},
					Config: c,
				},
			},
			FunctionChildImageReconciler(c),
		},

		Config: c,
	}
}
```

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
@scothis scothis requested a review from glyn December 7, 2020 15:32
@codecov
Copy link

codecov bot commented Dec 7, 2020

Codecov Report

Merging #51 (d566213) into main (2db5e1f) will increase coverage by 1.27%.
The diff coverage is 88.09%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #51      +/-   ##
==========================================
+ Coverage   61.38%   62.65%   +1.27%     
==========================================
  Files          11       11              
  Lines         606      640      +34     
==========================================
+ Hits          372      401      +29     
- Misses        226      231       +5     
  Partials        8        8              
Impacted Files Coverage Δ
reconcilers/reconcilers.go 85.36% <88.09%> (-0.01%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2db5e1f...d566213. Read the comment docs.

Signed-off-by: Scott Andrews <andrewssc@vmware.com>
Copy link
Contributor

@glyn glyn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, just some commentary suggestions.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
reconcilers/reconcilers.go Outdated Show resolved Hide resolved
Co-authored-by: Glyn Normington <glyn.normington.work@gmail.com>
@scothis scothis merged commit c3f7a25 into vmware-labs:main Dec 7, 2020
@scothis scothis deleted the cast-parent branch December 7, 2020 16:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants