Skip to content

Commit

Permalink
Add CreateOrPatchE
Browse files Browse the repository at this point in the history
Signed-off-by: Tamal Saha <[email protected]>
  • Loading branch information
tamalsaha committed Nov 24, 2024
1 parent 16a1295 commit d84fac9
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ func NewUncachedClient(cfg *rest.Config, funcs ...func(*runtime.Scheme) error) (

type (
TransformFunc func(obj client.Object, createOp bool) client.Object
TransformFuncE func(obj client.Object, createOp bool) (client.Object, error)
TransformStatusFunc func(obj client.Object) client.Object
)

func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, transform TransformFunc, opts ...client.PatchOption) (kutil.VerbType, error) {
func CreateOrPatchE(ctx context.Context, c client.Client, obj client.Object, transform TransformFuncE, opts ...client.PatchOption) (kutil.VerbType, error) {
gvk, err := apiutil.GVKForObject(obj, c.Scheme())
if err != nil {
return kutil.VerbUnchanged, errors.Wrapf(err, "failed to get GVK for object %T", obj)
Expand All @@ -91,8 +92,11 @@ func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, tran
createOpts = append(createOpts, opt)
}
}
mod := transform(obj.DeepCopyObject().(client.Object), true)
err := c.Create(ctx, mod, createOpts...)
mod, err := transform(obj.DeepCopyObject().(client.Object), true)
if err != nil {
return kutil.VerbUnchanged, err
}
err = c.Create(ctx, mod, createOpts...)
if err != nil {
return kutil.VerbUnchanged, err
}
Expand All @@ -111,7 +115,10 @@ func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, tran
} else {
patch = client.MergeFrom(cur)
}
mod := transform(cur.DeepCopyObject().(client.Object), false)
mod, err := transform(cur.DeepCopyObject().(client.Object), false)
if err != nil {
return kutil.VerbUnchanged, err
}
err = c.Patch(ctx, mod, patch, opts...)
if err != nil {
return kutil.VerbUnchanged, err
Expand All @@ -132,6 +139,13 @@ func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, tran
return vt, nil
}

func CreateOrPatch(ctx context.Context, c client.Client, obj client.Object, transform TransformFunc, opts ...client.PatchOption) (kutil.VerbType, error) {
return CreateOrPatchE(ctx, c, obj, func(obj client.Object, createOp bool) (client.Object, error) {
transform(obj, createOp)
return obj, nil
}, opts...)
}

func assign(target, src any) {
srcValue := reflect.ValueOf(src)
if srcValue.Kind() == reflect.Pointer {
Expand Down

0 comments on commit d84fac9

Please sign in to comment.