Skip to content

Commit 6776475

Browse files
julianvmodestok8s-publishing-bot
authored andcommitted
Add context and options to scale client
Kubernetes-commit: da3c3432d8361f315e7f562563e68316b10b9a2d
1 parent c078294 commit 6776475

File tree

4 files changed

+35
-16
lines changed

4 files changed

+35
-16
lines changed

scale/client.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"fmt"
2222

2323
autoscaling "k8s.io/api/autoscaling/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2425
"k8s.io/apimachinery/pkg/runtime"
2526
"k8s.io/apimachinery/pkg/runtime/schema"
2627
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
@@ -31,6 +32,14 @@ import (
3132

3233
var scaleConverter = NewScaleConverter()
3334
var codecs = serializer.NewCodecFactory(scaleConverter.Scheme())
35+
var parameterScheme = runtime.NewScheme()
36+
var dynamicParameterCodec = runtime.NewParameterCodec(parameterScheme)
37+
38+
var versionV1 = schema.GroupVersion{Version: "v1"}
39+
40+
func init() {
41+
metav1.AddToGroupVersion(parameterScheme, versionV1)
42+
}
3443

3544
// scaleClient is an implementation of ScalesGetter
3645
// which makes use of a RESTMapper and a generic REST
@@ -138,7 +147,7 @@ func (c *scaleClient) Scales(namespace string) ScaleInterface {
138147
}
139148
}
140149

141-
func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string) (*autoscaling.Scale, error) {
150+
func (c *namespacedScaleClient) Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscaling.Scale, error) {
142151
// Currently, a /scale endpoint can return different scale types.
143152
// Until we have support for the alternative API representations proposal,
144153
// we need to deal with accepting different API versions.
@@ -155,15 +164,16 @@ func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string)
155164
Resource(gvr.Resource).
156165
Name(name).
157166
SubResource("scale").
158-
Do(context.TODO())
167+
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
168+
Do(ctx)
159169
if err := result.Error(); err != nil {
160170
return nil, err
161171
}
162172

163173
return convertToScale(&result)
164174
}
165175

166-
func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *autoscaling.Scale) (*autoscaling.Scale, error) {
176+
func (c *namespacedScaleClient) Update(ctx context.Context, resource schema.GroupResource, scale *autoscaling.Scale, opts metav1.UpdateOptions) (*autoscaling.Scale, error) {
167177
path, gvr, err := c.client.pathAndVersionFor(resource)
168178
if err != nil {
169179
return nil, fmt.Errorf("unable to get client for %s: %v", resource.String(), err)
@@ -196,8 +206,9 @@ func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *aut
196206
Resource(gvr.Resource).
197207
Name(scale.Name).
198208
SubResource("scale").
209+
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
199210
Body(scaleUpdateBytes).
200-
Do(context.TODO())
211+
Do(ctx)
201212
if err := result.Error(); err != nil {
202213
// propagate "raw" error from the API
203214
// this allows callers to interpret underlying Reason field
@@ -208,16 +219,17 @@ func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *aut
208219
return convertToScale(&result)
209220
}
210221

211-
func (c *namespacedScaleClient) Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte) (*autoscaling.Scale, error) {
222+
func (c *namespacedScaleClient) Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*autoscaling.Scale, error) {
212223
groupVersion := gvr.GroupVersion()
213224
result := c.client.clientBase.Patch(pt).
214225
AbsPath(c.client.apiPathFor(groupVersion)).
215226
NamespaceIfScoped(c.namespace, c.namespace != "").
216227
Resource(gvr.Resource).
217228
Name(name).
218229
SubResource("scale").
230+
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
219231
Body(data).
220-
Do(context.TODO())
232+
Do(ctx)
221233
if err := result.Error(); err != nil {
222234
return nil, err
223235
}

scale/client_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scale
1818

1919
import (
2020
"bytes"
21+
"context"
2122
"encoding/json"
2223
"fmt"
2324
"io"
@@ -273,7 +274,7 @@ func TestGetScale(t *testing.T) {
273274
}
274275

275276
for _, groupResource := range groupResources {
276-
scale, err := scaleClient.Scales("default").Get(groupResource, "foo")
277+
scale, err := scaleClient.Scales("default").Get(context.TODO(), groupResource, "foo", metav1.GetOptions{})
277278
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
278279
continue
279280
}
@@ -301,7 +302,7 @@ func TestUpdateScale(t *testing.T) {
301302
}
302303

303304
for _, groupResource := range groupResources {
304-
scale, err := scaleClient.Scales("default").Update(groupResource, expectedScale)
305+
scale, err := scaleClient.Scales("default").Update(context.TODO(), groupResource, expectedScale, metav1.UpdateOptions{})
305306
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
306307
continue
307308
}
@@ -344,7 +345,7 @@ func TestPatchScale(t *testing.T) {
344345

345346
patch := []byte(`{"spec":{"replicas":5}}`)
346347
for _, gvr := range gvrs {
347-
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.MergePatchType, patch)
348+
scale, err := scaleClient.Scales("default").Patch(context.TODO(), gvr, "foo", types.MergePatchType, patch, metav1.PatchOptions{})
348349
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
349350
continue
350351
}
@@ -354,7 +355,7 @@ func TestPatchScale(t *testing.T) {
354355

355356
patch = []byte(`[{"op":"replace","path":"/spec/replicas","value":5}]`)
356357
for _, gvr := range gvrs {
357-
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.JSONPatchType, patch)
358+
scale, err := scaleClient.Scales("default").Patch(context.TODO(), gvr, "foo", types.JSONPatchType, patch, metav1.PatchOptions{})
358359
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
359360
continue
360361
}

scale/fake/client.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ limitations under the License.
2020
package fake
2121

2222
import (
23+
"context"
24+
2325
autoscalingapi "k8s.io/api/autoscaling/v1"
26+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2427
"k8s.io/apimachinery/pkg/runtime/schema"
2528
"k8s.io/apimachinery/pkg/types"
2629
"k8s.io/client-go/scale"
@@ -44,7 +47,7 @@ type fakeNamespacedScaleClient struct {
4447
fake *testing.Fake
4548
}
4649

47-
func (f *fakeNamespacedScaleClient) Get(resource schema.GroupResource, name string) (*autoscalingapi.Scale, error) {
50+
func (f *fakeNamespacedScaleClient) Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscalingapi.Scale, error) {
4851
obj, err := f.fake.
4952
Invokes(testing.NewGetSubresourceAction(resource.WithVersion(""), f.namespace, "scale", name), &autoscalingapi.Scale{})
5053

@@ -55,7 +58,7 @@ func (f *fakeNamespacedScaleClient) Get(resource schema.GroupResource, name stri
5558
return obj.(*autoscalingapi.Scale), err
5659
}
5760

58-
func (f *fakeNamespacedScaleClient) Update(resource schema.GroupResource, scale *autoscalingapi.Scale) (*autoscalingapi.Scale, error) {
61+
func (f *fakeNamespacedScaleClient) Update(ctx context.Context, resource schema.GroupResource, scale *autoscalingapi.Scale, opts metav1.UpdateOptions) (*autoscalingapi.Scale, error) {
5962
obj, err := f.fake.
6063
Invokes(testing.NewUpdateSubresourceAction(resource.WithVersion(""), f.namespace, "scale", scale), &autoscalingapi.Scale{})
6164

@@ -66,7 +69,7 @@ func (f *fakeNamespacedScaleClient) Update(resource schema.GroupResource, scale
6669
return obj.(*autoscalingapi.Scale), err
6770
}
6871

69-
func (f *fakeNamespacedScaleClient) Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, patch []byte) (*autoscalingapi.Scale, error) {
72+
func (f *fakeNamespacedScaleClient) Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, patch []byte, opts metav1.PatchOptions) (*autoscalingapi.Scale, error) {
7073
obj, err := f.fake.
7174
Invokes(testing.NewPatchSubresourceAction(gvr, f.namespace, name, pt, patch, "scale"), &autoscalingapi.Scale{})
7275

scale/interfaces.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ limitations under the License.
1717
package scale
1818

1919
import (
20+
"context"
21+
2022
autoscalingapi "k8s.io/api/autoscaling/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2124
"k8s.io/apimachinery/pkg/runtime/schema"
2225
"k8s.io/apimachinery/pkg/types"
2326
)
@@ -34,11 +37,11 @@ type ScalesGetter interface {
3437
// the scale subresource.
3538
type ScaleInterface interface {
3639
// Get fetches the scale of the given scalable resource.
37-
Get(resource schema.GroupResource, name string) (*autoscalingapi.Scale, error)
40+
Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscalingapi.Scale, error)
3841

3942
// Update updates the scale of the given scalable resource.
40-
Update(resource schema.GroupResource, scale *autoscalingapi.Scale) (*autoscalingapi.Scale, error)
43+
Update(ctx context.Context, resource schema.GroupResource, scale *autoscalingapi.Scale, opts metav1.UpdateOptions) (*autoscalingapi.Scale, error)
4144

4245
// Patch patches the scale of the given scalable resource.
43-
Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte) (*autoscalingapi.Scale, error)
46+
Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*autoscalingapi.Scale, error)
4447
}

0 commit comments

Comments
 (0)