mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 01:06:27 +00:00
Add WrapUpdatedObjectInfo helper
This commit is contained in:
parent
1ef1906209
commit
10c1234c98
@ -173,3 +173,44 @@ func (i *defaultUpdatedObjectInfo) UpdatedObject(ctx api.Context, oldObj runtime
|
|||||||
|
|
||||||
return newObj, nil
|
return newObj, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// wrappedUpdatedObjectInfo allows wrapping an existing objInfo and
|
||||||
|
// chaining additional transformations/checks on the result of UpdatedObject()
|
||||||
|
type wrappedUpdatedObjectInfo struct {
|
||||||
|
// obj is the updated object
|
||||||
|
objInfo UpdatedObjectInfo
|
||||||
|
|
||||||
|
// transformers is an optional list of transforming functions that modify or
|
||||||
|
// replace obj using information from the context, old object, or other sources.
|
||||||
|
transformers []TransformFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
// WrapUpdatedObjectInfo returns an UpdatedObjectInfo impl that delegates to
|
||||||
|
// the specified objInfo, then calls the passed transformers
|
||||||
|
func WrapUpdatedObjectInfo(objInfo UpdatedObjectInfo, transformers ...TransformFunc) UpdatedObjectInfo {
|
||||||
|
return &wrappedUpdatedObjectInfo{objInfo, transformers}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Preconditions satisfies the UpdatedObjectInfo interface.
|
||||||
|
func (i *wrappedUpdatedObjectInfo) Preconditions() *api.Preconditions {
|
||||||
|
return i.objInfo.Preconditions()
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdatedObject satisfies the UpdatedObjectInfo interface.
|
||||||
|
// It delegates to the wrapped objInfo and passes the result through any configured transformers.
|
||||||
|
func (i *wrappedUpdatedObjectInfo) UpdatedObject(ctx api.Context, oldObj runtime.Object) (runtime.Object, error) {
|
||||||
|
newObj, err := i.objInfo.UpdatedObject(ctx, oldObj)
|
||||||
|
if err != nil {
|
||||||
|
return newObj, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow any configured transformers to update the new object or error
|
||||||
|
for _, transformer := range i.transformers {
|
||||||
|
newObj, err = transformer(ctx, newObj, oldObj)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return newObj, nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user