Skip to content

Commit

Permalink
Merge pull request #1795 from hyschumi/refactor_same_code_style
Browse files Browse the repository at this point in the history
🌱 Refactor typed_client and unstructured_client to be consistent
  • Loading branch information
k8s-ci-robot authored Sep 26, 2022
2 parents 78b203b + 4ec75b4 commit 09e83d3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 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 @@ -152,8 +156,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 @@ -172,13 +178,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 @@ -196,13 +205,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)
}
23 changes: 17 additions & 6 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 All @@ -106,6 +108,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 @@ -128,6 +131,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 All @@ -154,11 +158,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 @@ -204,14 +210,14 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...
gvk := u.GroupVersionKind()
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")

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 @@ -230,13 +236,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 @@ -260,13 +269,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 09e83d3

Please sign in to comment.