Adding ChildSetReconciler to manage a set of child resources#382
Adding ChildSetReconciler to manage a set of child resources#382LittleBaiBai wants to merge 3 commits intovmware-labs:mainfrom
Conversation
c4df50a to
0787ec4
Compare
1b62191 to
bc1d6f2
Compare
| }, | ||
| }, | ||
| }, | ||
| "sanitize each child before logging": { //TODO: what's different? |
There was a problem hiding this comment.
I copied this test from the ChildReconciler test suite but I don't fully understand how we are testing the functionality here. There seems to be no assertion on logs, and changing the return value of SanitizeForEachChild, or SanitizeForChild in ChildReconciler suite, doesn't seem to affect the test runs.
bc1d6f2 to
3599f72
Compare
scothis
left a comment
There was a problem hiding this comment.
initial comments, will need to dig deeper tomorrow
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| utilErrs "k8s.io/apimachinery/pkg/util/errors" |
There was a problem hiding this comment.
this package is already imported as apierrs
There was a problem hiding this comment.
apierrs "k8s.io/apimachinery/pkg/api/errors" is a different package from this.
| return Result{}, err | ||
| } | ||
| r.ReflectChildStatusOnParent(ctx, resource, child, err) | ||
| r.ReflectChildStatusOnParent(ctx, resource, child, nil) |
There was a problem hiding this comment.
Yes it was intended. The if block before this already handled the case when err != nil so the err here will be nil and it reads better to me. Happy to revert it if that's preferred.
reconcilers/reconcilers.go
Outdated
| return Result{}, aggregateErr | ||
| } | ||
| if aggregateErr != nil { | ||
| for _, eachErr := range aggregateErr.(utilErrs.Aggregate).Errors() { |
There was a problem hiding this comment.
this cast appears to be redundant since aggregateErr is already an Aggregate as defined by the reconcile signature.
There was a problem hiding this comment.
Good point. That was there before I change the signature of the method. Updated in latest commits.
| r.ReflectChildSetStatusOnParent(ctx, resource, childSet, aggregateErr) | ||
| return Result{}, nil |
There was a problem hiding this comment.
this return can obscure other errors that exist in the aggregate
There was a problem hiding this comment.
Good catch. Could you share more on why you chose to return Result{}, nil for ChildReconciler here? Is it for not triggering the rateLimiter in the requeue? I'm tempted to move the ReflectChildSetStatusOnParent call outside of the for loop and always return return Result{}, aggregateErr when something goes wrong.
| return defaultValue | ||
| } | ||
|
|
||
| func (r *ChildSetReconciler[T, CT, CLT]) reconcile(ctx context.Context, resource T) ([]CT, utilErrs.Aggregate) { |
There was a problem hiding this comment.
Aggregate errors strip out nil values, so the errors are separated from the specific child, is this is ok? Switch to []error if linking the error with a child resource is important.
There was a problem hiding this comment.
Oh interesting! I'll experiment with that.
There was a problem hiding this comment.
As I try to rewrite it, there are errors that could happen before the actual child resource reconciliation, e.g. listing actual children. Also, when error occurs, the result returned by r.stamp.Manage is usually nilT so I still don't see how we can link the error to the child resource even if we switch it to []Error.
| // on the reconciled resource as being not ready. | ||
| apierr := eachErr.(apierrs.APIStatus) | ||
| conflicted := r.ChildType.DeepCopyObject().(CT) | ||
| _ = c.APIReader.Get(ctx, types.NamespacedName{Namespace: resource.GetNamespace(), Name: apierr.Status().Details.Name}, conflicted) |
There was a problem hiding this comment.
this line comes from an earlier era of ChildReconciler where children were forced to exist in the same namespace at the parent resource. This restriction was relaxed, but this line wasn't updated... I'll need to think about how to resolve this since the namespace is not on the err object.
reconcilers/reconcilers.go
Outdated
| } | ||
|
|
||
| actualChildrenMap, err1 := r.childListToMap(actualChildren) | ||
| if err1 != nil { |
There was a problem hiding this comment.
avoid non-semantic names for errors. Either reassign err, or if you need to avoid shadowing the existing value, use a semantic name.
There was a problem hiding this comment.
Updated in latest commits
reconcilers/reconcilers.go
Outdated
|
|
||
| func (r *ChildSetReconciler[T, CT, CLT]) hasChild(childSet []CT) bool { | ||
| hasChild := false | ||
| if !internal.IsNil(childSet) { |
There was a problem hiding this comment.
checking the length is a more common way to nil check a slice, unless nil and length 0 have different semantic values.
There was a problem hiding this comment.
Updated in latest commits
|
Folded concepts into #396 |
Relates to #75