Skip to content

Commit

Permalink
refactor: client keep same code style
Browse files Browse the repository at this point in the history
  • Loading branch information
hyschumi authored and liudongsheng committed Feb 8, 2022
1 parent 273e608 commit d5ae002
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
17 changes: 14 additions & 3 deletions pkg/client/typed_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOpti

createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)

return o.Post().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Expand All @@ -60,6 +61,7 @@ func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOpti

updateOpts := &UpdateOptions{}
updateOpts.ApplyOptions(opts)

return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Expand Down Expand Up @@ -121,11 +123,13 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .
}

patchOpts := &PatchOptions{}
patchOpts.ApplyOptions(opts)

return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
Body(data).
Do(ctx).
Into(obj)
Expand All @@ -149,8 +153,10 @@ func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOpti
if err != nil {
return err
}

listOpts := ListOptions{}
listOpts.ApplyOptions(opts)

return r.Get().
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
Resource(r.resource()).
Expand All @@ -169,13 +175,16 @@ func (c *typedClient) UpdateStatus(ctx context.Context, obj Object, opts ...Upda
// wrapped to improve the UX ?
// It will be nice to receive an error saying the object doesn't implement
// status subresource and check CRD definition
updateOpts := &UpdateOptions{}
updateOpts.ApplyOptions(opts)

return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource("status").
Body(obj).
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
Expand All @@ -193,13 +202,15 @@ func (c *typedClient) PatchStatus(ctx context.Context, obj Object, patch Patch,
}

patchOpts := &PatchOptions{}
patchOpts.ApplyOptions(opts)

return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource("status").
Body(data).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
Do(ctx).
Into(obj)
}
22 changes: 17 additions & 5 deletions pkg/client/unstructured_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...Cr

createOpts := &CreateOptions{}
createOpts.ApplyOptions(opts)

result := o.Post().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Expand Down Expand Up @@ -80,6 +81,7 @@ func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...Up

updateOpts := UpdateOptions{}
updateOpts.ApplyOptions(opts)

result := o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Expand Down Expand Up @@ -107,6 +109,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De

deleteOpts := DeleteOptions{}
deleteOpts.ApplyOptions(opts)

return o.Delete().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Expand All @@ -130,6 +133,7 @@ func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts

deleteAllOfOpts := DeleteAllOfOptions{}
deleteAllOfOpts.ApplyOptions(opts)

return o.Delete().
NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.isNamespaced()).
Resource(o.resource()).
Expand Down Expand Up @@ -157,11 +161,13 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch
}

patchOpts := &PatchOptions{}
patchOpts.ApplyOptions(opts)

return o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
VersionedParams(patchOpts.AsPatchOptions(), uc.paramCodec).
Body(data).
Do(ctx).
Into(obj)
Expand Down Expand Up @@ -205,14 +211,15 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]
}

listOpts := ListOptions{}
listOpts.ApplyOptions(opts)

r, err := uc.cache.getResource(obj)
if err != nil {
return err
}

listOpts := ListOptions{}
listOpts.ApplyOptions(opts)

return r.Get().
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
Resource(r.resource()).
Expand All @@ -232,13 +239,16 @@ func (uc *unstructuredClient) UpdateStatus(ctx context.Context, obj Object, opts
return err
}

updateOpts := UpdateOptions{}
updateOpts.ApplyOptions(opts)

return o.Put().
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource("status").
Body(obj).
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), uc.paramCodec).
VersionedParams(updateOpts.AsUpdateOptions(), uc.paramCodec).
Do(ctx).
Into(obj)
}
Expand All @@ -262,13 +272,15 @@ func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj Object, patch
}

patchOpts := &PatchOptions{}
patchOpts.ApplyOptions(opts)

result := o.Patch(patch.Type()).
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
Resource(o.resource()).
Name(o.GetName()).
SubResource("status").
Body(data).
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
VersionedParams(patchOpts.AsPatchOptions(), uc.paramCodec).
Do(ctx).
Into(u)

Expand Down

0 comments on commit d5ae002

Please sign in to comment.