Skip to content

Commit 09e83d3

Browse files
authored
Merge pull request #1795 from hyschumi/refactor_same_code_style
🌱 Refactor typed_client and unstructured_client to be consistent
2 parents 78b203b + 4ec75b4 commit 09e83d3

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

pkg/client/typed_client.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (c *typedClient) Create(ctx context.Context, obj Object, opts ...CreateOpti
4242

4343
createOpts := &CreateOptions{}
4444
createOpts.ApplyOptions(opts)
45+
4546
return o.Post().
4647
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
4748
Resource(o.resource()).
@@ -60,6 +61,7 @@ func (c *typedClient) Update(ctx context.Context, obj Object, opts ...UpdateOpti
6061

6162
updateOpts := &UpdateOptions{}
6263
updateOpts.ApplyOptions(opts)
64+
6365
return o.Put().
6466
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
6567
Resource(o.resource()).
@@ -121,11 +123,13 @@ func (c *typedClient) Patch(ctx context.Context, obj Object, patch Patch, opts .
121123
}
122124

123125
patchOpts := &PatchOptions{}
126+
patchOpts.ApplyOptions(opts)
127+
124128
return o.Patch(patch.Type()).
125129
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
126130
Resource(o.resource()).
127131
Name(o.GetName()).
128-
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
132+
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
129133
Body(data).
130134
Do(ctx).
131135
Into(obj)
@@ -152,8 +156,10 @@ func (c *typedClient) List(ctx context.Context, obj ObjectList, opts ...ListOpti
152156
if err != nil {
153157
return err
154158
}
159+
155160
listOpts := ListOptions{}
156161
listOpts.ApplyOptions(opts)
162+
157163
return r.Get().
158164
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
159165
Resource(r.resource()).
@@ -172,13 +178,16 @@ func (c *typedClient) UpdateStatus(ctx context.Context, obj Object, opts ...Upda
172178
// wrapped to improve the UX ?
173179
// It will be nice to receive an error saying the object doesn't implement
174180
// status subresource and check CRD definition
181+
updateOpts := &UpdateOptions{}
182+
updateOpts.ApplyOptions(opts)
183+
175184
return o.Put().
176185
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
177186
Resource(o.resource()).
178187
Name(o.GetName()).
179188
SubResource("status").
180189
Body(obj).
181-
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), c.paramCodec).
190+
VersionedParams(updateOpts.AsUpdateOptions(), c.paramCodec).
182191
Do(ctx).
183192
Into(obj)
184193
}
@@ -196,13 +205,15 @@ func (c *typedClient) PatchStatus(ctx context.Context, obj Object, patch Patch,
196205
}
197206

198207
patchOpts := &PatchOptions{}
208+
patchOpts.ApplyOptions(opts)
209+
199210
return o.Patch(patch.Type()).
200211
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
201212
Resource(o.resource()).
202213
Name(o.GetName()).
203214
SubResource("status").
204215
Body(data).
205-
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), c.paramCodec).
216+
VersionedParams(patchOpts.AsPatchOptions(), c.paramCodec).
206217
Do(ctx).
207218
Into(obj)
208219
}

pkg/client/unstructured_client.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func (uc *unstructuredClient) Create(ctx context.Context, obj Object, opts ...Cr
5252

5353
createOpts := &CreateOptions{}
5454
createOpts.ApplyOptions(opts)
55+
5556
result := o.Post().
5657
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
5758
Resource(o.resource()).
@@ -80,6 +81,7 @@ func (uc *unstructuredClient) Update(ctx context.Context, obj Object, opts ...Up
8081

8182
updateOpts := UpdateOptions{}
8283
updateOpts.ApplyOptions(opts)
84+
8385
result := o.Put().
8486
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
8587
Resource(o.resource()).
@@ -106,6 +108,7 @@ func (uc *unstructuredClient) Delete(ctx context.Context, obj Object, opts ...De
106108

107109
deleteOpts := DeleteOptions{}
108110
deleteOpts.ApplyOptions(opts)
111+
109112
return o.Delete().
110113
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
111114
Resource(o.resource()).
@@ -128,6 +131,7 @@ func (uc *unstructuredClient) DeleteAllOf(ctx context.Context, obj Object, opts
128131

129132
deleteAllOfOpts := DeleteAllOfOptions{}
130133
deleteAllOfOpts.ApplyOptions(opts)
134+
131135
return o.Delete().
132136
NamespaceIfScoped(deleteAllOfOpts.ListOptions.Namespace, o.isNamespaced()).
133137
Resource(o.resource()).
@@ -154,11 +158,13 @@ func (uc *unstructuredClient) Patch(ctx context.Context, obj Object, patch Patch
154158
}
155159

156160
patchOpts := &PatchOptions{}
161+
patchOpts.ApplyOptions(opts)
162+
157163
return o.Patch(patch.Type()).
158164
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
159165
Resource(o.resource()).
160166
Name(o.GetName()).
161-
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
167+
VersionedParams(patchOpts.AsPatchOptions(), uc.paramCodec).
162168
Body(data).
163169
Do(ctx).
164170
Into(obj)
@@ -204,14 +210,14 @@ func (uc *unstructuredClient) List(ctx context.Context, obj ObjectList, opts ...
204210
gvk := u.GroupVersionKind()
205211
gvk.Kind = strings.TrimSuffix(gvk.Kind, "List")
206212

207-
listOpts := ListOptions{}
208-
listOpts.ApplyOptions(opts)
209-
210213
r, err := uc.cache.getResource(obj)
211214
if err != nil {
212215
return err
213216
}
214217

218+
listOpts := ListOptions{}
219+
listOpts.ApplyOptions(opts)
220+
215221
return r.Get().
216222
NamespaceIfScoped(listOpts.Namespace, r.isNamespaced()).
217223
Resource(r.resource()).
@@ -230,13 +236,16 @@ func (uc *unstructuredClient) UpdateStatus(ctx context.Context, obj Object, opts
230236
return err
231237
}
232238

239+
updateOpts := UpdateOptions{}
240+
updateOpts.ApplyOptions(opts)
241+
233242
return o.Put().
234243
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
235244
Resource(o.resource()).
236245
Name(o.GetName()).
237246
SubResource("status").
238247
Body(obj).
239-
VersionedParams((&UpdateOptions{}).ApplyOptions(opts).AsUpdateOptions(), uc.paramCodec).
248+
VersionedParams(updateOpts.AsUpdateOptions(), uc.paramCodec).
240249
Do(ctx).
241250
Into(obj)
242251
}
@@ -260,13 +269,15 @@ func (uc *unstructuredClient) PatchStatus(ctx context.Context, obj Object, patch
260269
}
261270

262271
patchOpts := &PatchOptions{}
272+
patchOpts.ApplyOptions(opts)
273+
263274
result := o.Patch(patch.Type()).
264275
NamespaceIfScoped(o.GetNamespace(), o.isNamespaced()).
265276
Resource(o.resource()).
266277
Name(o.GetName()).
267278
SubResource("status").
268279
Body(data).
269-
VersionedParams(patchOpts.ApplyOptions(opts).AsPatchOptions(), uc.paramCodec).
280+
VersionedParams(patchOpts.AsPatchOptions(), uc.paramCodec).
270281
Do(ctx).
271282
Into(u)
272283

0 commit comments

Comments
 (0)