mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-27 21:47:07 +00:00
Merge pull request #88599 from julianvmodesto/scale-ctx-opts
Add context and options to scale client
This commit is contained in:
commit
179fe40d06
@ -292,7 +292,7 @@ func (dc *DisruptionController) getScaleController(controllerRef *metav1.OwnerRe
|
|||||||
}
|
}
|
||||||
gr := mapping.Resource.GroupResource()
|
gr := mapping.Resource.GroupResource()
|
||||||
|
|
||||||
scale, err := dc.scaleNamespacer.Scales(namespace).Get(gr, controllerRef.Name)
|
scale, err := dc.scaleNamespacer.Scales(namespace).Get(context.TODO(), gr, controllerRef.Name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.IsNotFound(err) {
|
if errors.IsNotFound(err) {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -648,7 +648,7 @@ func (a *HorizontalController) reconcileAutoscaler(hpav1Shared *autoscalingv1.Ho
|
|||||||
|
|
||||||
if rescale {
|
if rescale {
|
||||||
scale.Spec.Replicas = desiredReplicas
|
scale.Spec.Replicas = desiredReplicas
|
||||||
_, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(targetGR, scale)
|
_, err = a.scaleNamespacer.Scales(hpa.Namespace).Update(context.TODO(), targetGR, scale, metav1.UpdateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.eventRecorder.Eventf(hpa, v1.EventTypeWarning, "FailedRescale", "New size: %d; reason: %s; error: %v", desiredReplicas, rescaleReason, err.Error())
|
a.eventRecorder.Eventf(hpa, v1.EventTypeWarning, "FailedRescale", "New size: %d; reason: %s; error: %v", desiredReplicas, rescaleReason, err.Error())
|
||||||
setCondition(hpa, autoscalingv2.AbleToScale, v1.ConditionFalse, "FailedUpdateScale", "the HPA controller was unable to update the target scale: %v", err)
|
setCondition(hpa, autoscalingv2.AbleToScale, v1.ConditionFalse, "FailedUpdateScale", "the HPA controller was unable to update the target scale: %v", err)
|
||||||
@ -1050,7 +1050,7 @@ func (a *HorizontalController) scaleForResourceMappings(namespace, name string,
|
|||||||
var firstErr error
|
var firstErr error
|
||||||
for i, mapping := range mappings {
|
for i, mapping := range mappings {
|
||||||
targetGR := mapping.Resource.GroupResource()
|
targetGR := mapping.Resource.GroupResource()
|
||||||
scale, err := a.scaleNamespacer.Scales(namespace).Get(targetGR, name)
|
scale, err := a.scaleNamespacer.Scales(namespace).Get(context.TODO(), targetGR, name, metav1.GetOptions{})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return scale, targetGR, nil
|
return scale, targetGR, nil
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package integration
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -328,7 +329,7 @@ func TestScaleSubresource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get the scale object
|
// get the scale object
|
||||||
gottenScale, err := scaleClient.Scales("not-the-default").Get(groupResource, "foo")
|
gottenScale, err := scaleClient.Scales("not-the-default").Get(context.TODO(), groupResource, "foo", metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -349,7 +350,7 @@ func TestScaleSubresource(t *testing.T) {
|
|||||||
// check that spec is updated, but status is not
|
// check that spec is updated, but status is not
|
||||||
gottenScale.Spec.Replicas = 5
|
gottenScale.Spec.Replicas = 5
|
||||||
gottenScale.Status.Selector = "baz"
|
gottenScale.Status.Selector = "baz"
|
||||||
updatedScale, err := scaleClient.Scales("not-the-default").Update(groupResource, gottenScale)
|
updatedScale, err := scaleClient.Scales("not-the-default").Update(context.TODO(), groupResource, gottenScale, metav1.UpdateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -842,7 +843,7 @@ func TestSubresourcePatch(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Scale.Spec.Replicas = 7 but Scale.Status.Replicas should remain 0
|
// Scale.Spec.Replicas = 7 but Scale.Status.Replicas should remain 0
|
||||||
gottenScale, err := scaleClient.Scales("not-the-default").Get(groupResource, "foo")
|
gottenScale, err := scaleClient.Scales("not-the-default").Get(context.TODO(), groupResource, "foo", metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ go_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/meta:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime/serializer:go_default_library",
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
autoscaling "k8s.io/api/autoscaling/v1"
|
autoscaling "k8s.io/api/autoscaling/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
serializer "k8s.io/apimachinery/pkg/runtime/serializer"
|
||||||
@ -31,6 +32,14 @@ import (
|
|||||||
|
|
||||||
var scaleConverter = NewScaleConverter()
|
var scaleConverter = NewScaleConverter()
|
||||||
var codecs = serializer.NewCodecFactory(scaleConverter.Scheme())
|
var codecs = serializer.NewCodecFactory(scaleConverter.Scheme())
|
||||||
|
var parameterScheme = runtime.NewScheme()
|
||||||
|
var dynamicParameterCodec = runtime.NewParameterCodec(parameterScheme)
|
||||||
|
|
||||||
|
var versionV1 = schema.GroupVersion{Version: "v1"}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
metav1.AddToGroupVersion(parameterScheme, versionV1)
|
||||||
|
}
|
||||||
|
|
||||||
// scaleClient is an implementation of ScalesGetter
|
// scaleClient is an implementation of ScalesGetter
|
||||||
// which makes use of a RESTMapper and a generic REST
|
// which makes use of a RESTMapper and a generic REST
|
||||||
@ -138,7 +147,7 @@ func (c *scaleClient) Scales(namespace string) ScaleInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string) (*autoscaling.Scale, error) {
|
func (c *namespacedScaleClient) Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscaling.Scale, error) {
|
||||||
// Currently, a /scale endpoint can return different scale types.
|
// Currently, a /scale endpoint can return different scale types.
|
||||||
// Until we have support for the alternative API representations proposal,
|
// Until we have support for the alternative API representations proposal,
|
||||||
// we need to deal with accepting different API versions.
|
// we need to deal with accepting different API versions.
|
||||||
@ -155,7 +164,8 @@ func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string)
|
|||||||
Resource(gvr.Resource).
|
Resource(gvr.Resource).
|
||||||
Name(name).
|
Name(name).
|
||||||
SubResource("scale").
|
SubResource("scale").
|
||||||
Do(context.TODO())
|
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||||
|
Do(ctx)
|
||||||
if err := result.Error(); err != nil {
|
if err := result.Error(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -163,7 +173,7 @@ func (c *namespacedScaleClient) Get(resource schema.GroupResource, name string)
|
|||||||
return convertToScale(&result)
|
return convertToScale(&result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *autoscaling.Scale) (*autoscaling.Scale, error) {
|
func (c *namespacedScaleClient) Update(ctx context.Context, resource schema.GroupResource, scale *autoscaling.Scale, opts metav1.UpdateOptions) (*autoscaling.Scale, error) {
|
||||||
path, gvr, err := c.client.pathAndVersionFor(resource)
|
path, gvr, err := c.client.pathAndVersionFor(resource)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to get client for %s: %v", resource.String(), err)
|
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
|
|||||||
Resource(gvr.Resource).
|
Resource(gvr.Resource).
|
||||||
Name(scale.Name).
|
Name(scale.Name).
|
||||||
SubResource("scale").
|
SubResource("scale").
|
||||||
|
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||||
Body(scaleUpdateBytes).
|
Body(scaleUpdateBytes).
|
||||||
Do(context.TODO())
|
Do(ctx)
|
||||||
if err := result.Error(); err != nil {
|
if err := result.Error(); err != nil {
|
||||||
// propagate "raw" error from the API
|
// propagate "raw" error from the API
|
||||||
// this allows callers to interpret underlying Reason field
|
// this allows callers to interpret underlying Reason field
|
||||||
@ -208,7 +219,7 @@ func (c *namespacedScaleClient) Update(resource schema.GroupResource, scale *aut
|
|||||||
return convertToScale(&result)
|
return convertToScale(&result)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *namespacedScaleClient) Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte) (*autoscaling.Scale, error) {
|
func (c *namespacedScaleClient) Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*autoscaling.Scale, error) {
|
||||||
groupVersion := gvr.GroupVersion()
|
groupVersion := gvr.GroupVersion()
|
||||||
result := c.client.clientBase.Patch(pt).
|
result := c.client.clientBase.Patch(pt).
|
||||||
AbsPath(c.client.apiPathFor(groupVersion)).
|
AbsPath(c.client.apiPathFor(groupVersion)).
|
||||||
@ -216,8 +227,9 @@ func (c *namespacedScaleClient) Patch(gvr schema.GroupVersionResource, name stri
|
|||||||
Resource(gvr.Resource).
|
Resource(gvr.Resource).
|
||||||
Name(name).
|
Name(name).
|
||||||
SubResource("scale").
|
SubResource("scale").
|
||||||
|
SpecificallyVersionedParams(&opts, dynamicParameterCodec, versionV1).
|
||||||
Body(data).
|
Body(data).
|
||||||
Do(context.TODO())
|
Do(ctx)
|
||||||
if err := result.Error(); err != nil {
|
if err := result.Error(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package scale
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
@ -273,7 +274,7 @@ func TestGetScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, groupResource := range groupResources {
|
for _, groupResource := range groupResources {
|
||||||
scale, err := scaleClient.Scales("default").Get(groupResource, "foo")
|
scale, err := scaleClient.Scales("default").Get(context.TODO(), groupResource, "foo", metav1.GetOptions{})
|
||||||
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
|
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -301,7 +302,7 @@ func TestUpdateScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, groupResource := range groupResources {
|
for _, groupResource := range groupResources {
|
||||||
scale, err := scaleClient.Scales("default").Update(groupResource, expectedScale)
|
scale, err := scaleClient.Scales("default").Update(context.TODO(), groupResource, expectedScale, metav1.UpdateOptions{})
|
||||||
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
|
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", groupResource.String()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -344,7 +345,7 @@ func TestPatchScale(t *testing.T) {
|
|||||||
|
|
||||||
patch := []byte(`{"spec":{"replicas":5}}`)
|
patch := []byte(`{"spec":{"replicas":5}}`)
|
||||||
for _, gvr := range gvrs {
|
for _, gvr := range gvrs {
|
||||||
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.MergePatchType, patch)
|
scale, err := scaleClient.Scales("default").Patch(context.TODO(), gvr, "foo", types.MergePatchType, patch, metav1.PatchOptions{})
|
||||||
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
|
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -354,7 +355,7 @@ func TestPatchScale(t *testing.T) {
|
|||||||
|
|
||||||
patch = []byte(`[{"op":"replace","path":"/spec/replicas","value":5}]`)
|
patch = []byte(`[{"op":"replace","path":"/spec/replicas","value":5}]`)
|
||||||
for _, gvr := range gvrs {
|
for _, gvr := range gvrs {
|
||||||
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.JSONPatchType, patch)
|
scale, err := scaleClient.Scales("default").Patch(context.TODO(), gvr, "foo", types.JSONPatchType, patch, metav1.PatchOptions{})
|
||||||
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
|
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ go_library(
|
|||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
deps = [
|
deps = [
|
||||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//staging/src/k8s.io/client-go/scale:go_default_library",
|
"//staging/src/k8s.io/client-go/scale:go_default_library",
|
||||||
|
@ -20,7 +20,10 @@ limitations under the License.
|
|||||||
package fake
|
package fake
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
autoscalingapi "k8s.io/api/autoscaling/v1"
|
autoscalingapi "k8s.io/api/autoscaling/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/client-go/scale"
|
"k8s.io/client-go/scale"
|
||||||
@ -44,7 +47,7 @@ type fakeNamespacedScaleClient struct {
|
|||||||
fake *testing.Fake
|
fake *testing.Fake
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeNamespacedScaleClient) Get(resource schema.GroupResource, name string) (*autoscalingapi.Scale, error) {
|
func (f *fakeNamespacedScaleClient) Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscalingapi.Scale, error) {
|
||||||
obj, err := f.fake.
|
obj, err := f.fake.
|
||||||
Invokes(testing.NewGetSubresourceAction(resource.WithVersion(""), f.namespace, "scale", name), &autoscalingapi.Scale{})
|
Invokes(testing.NewGetSubresourceAction(resource.WithVersion(""), f.namespace, "scale", name), &autoscalingapi.Scale{})
|
||||||
|
|
||||||
@ -55,7 +58,7 @@ func (f *fakeNamespacedScaleClient) Get(resource schema.GroupResource, name stri
|
|||||||
return obj.(*autoscalingapi.Scale), err
|
return obj.(*autoscalingapi.Scale), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeNamespacedScaleClient) Update(resource schema.GroupResource, scale *autoscalingapi.Scale) (*autoscalingapi.Scale, error) {
|
func (f *fakeNamespacedScaleClient) Update(ctx context.Context, resource schema.GroupResource, scale *autoscalingapi.Scale, opts metav1.UpdateOptions) (*autoscalingapi.Scale, error) {
|
||||||
obj, err := f.fake.
|
obj, err := f.fake.
|
||||||
Invokes(testing.NewUpdateSubresourceAction(resource.WithVersion(""), f.namespace, "scale", scale), &autoscalingapi.Scale{})
|
Invokes(testing.NewUpdateSubresourceAction(resource.WithVersion(""), f.namespace, "scale", scale), &autoscalingapi.Scale{})
|
||||||
|
|
||||||
@ -66,7 +69,7 @@ func (f *fakeNamespacedScaleClient) Update(resource schema.GroupResource, scale
|
|||||||
return obj.(*autoscalingapi.Scale), err
|
return obj.(*autoscalingapi.Scale), err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeNamespacedScaleClient) Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, patch []byte) (*autoscalingapi.Scale, error) {
|
func (f *fakeNamespacedScaleClient) Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, patch []byte, opts metav1.PatchOptions) (*autoscalingapi.Scale, error) {
|
||||||
obj, err := f.fake.
|
obj, err := f.fake.
|
||||||
Invokes(testing.NewPatchSubresourceAction(gvr, f.namespace, name, pt, patch, "scale"), &autoscalingapi.Scale{})
|
Invokes(testing.NewPatchSubresourceAction(gvr, f.namespace, name, pt, patch, "scale"), &autoscalingapi.Scale{})
|
||||||
|
|
||||||
|
@ -17,7 +17,10 @@ limitations under the License.
|
|||||||
package scale
|
package scale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
autoscalingapi "k8s.io/api/autoscaling/v1"
|
autoscalingapi "k8s.io/api/autoscaling/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
)
|
)
|
||||||
@ -34,11 +37,11 @@ type ScalesGetter interface {
|
|||||||
// the scale subresource.
|
// the scale subresource.
|
||||||
type ScaleInterface interface {
|
type ScaleInterface interface {
|
||||||
// Get fetches the scale of the given scalable resource.
|
// Get fetches the scale of the given scalable resource.
|
||||||
Get(resource schema.GroupResource, name string) (*autoscalingapi.Scale, error)
|
Get(ctx context.Context, resource schema.GroupResource, name string, opts metav1.GetOptions) (*autoscalingapi.Scale, error)
|
||||||
|
|
||||||
// Update updates the scale of the given scalable resource.
|
// Update updates the scale of the given scalable resource.
|
||||||
Update(resource schema.GroupResource, scale *autoscalingapi.Scale) (*autoscalingapi.Scale, error)
|
Update(ctx context.Context, resource schema.GroupResource, scale *autoscalingapi.Scale, opts metav1.UpdateOptions) (*autoscalingapi.Scale, error)
|
||||||
|
|
||||||
// Patch patches the scale of the given scalable resource.
|
// Patch patches the scale of the given scalable resource.
|
||||||
Patch(gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte) (*autoscalingapi.Scale, error)
|
Patch(ctx context.Context, gvr schema.GroupVersionResource, name string, pt types.PatchType, data []byte, opts metav1.PatchOptions) (*autoscalingapi.Scale, error)
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ go_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
"//staging/src/k8s.io/api/autoscaling/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
|
||||||
|
@ -17,12 +17,14 @@ limitations under the License.
|
|||||||
package scale
|
package scale
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||||
"k8s.io/apimachinery/pkg/types"
|
"k8s.io/apimachinery/pkg/types"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
@ -115,7 +117,7 @@ var _ Scaler = &genericScaler{}
|
|||||||
// ScaleSimple updates a scale of a given resource. It returns the resourceVersion of the scale if the update was successful.
|
// ScaleSimple updates a scale of a given resource. It returns the resourceVersion of the scale if the update was successful.
|
||||||
func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint, gvr schema.GroupVersionResource) (updatedResourceVersion string, err error) {
|
func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *ScalePrecondition, newSize uint, gvr schema.GroupVersionResource) (updatedResourceVersion string, err error) {
|
||||||
if preconditions != nil {
|
if preconditions != nil {
|
||||||
scale, err := s.scaleNamespacer.Scales(namespace).Get(gvr.GroupResource(), name)
|
scale, err := s.scaleNamespacer.Scales(namespace).Get(context.TODO(), gvr.GroupResource(), name, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -123,7 +125,7 @@ func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *Scale
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
scale.Spec.Replicas = int32(newSize)
|
scale.Spec.Replicas = int32(newSize)
|
||||||
updatedScale, err := s.scaleNamespacer.Scales(namespace).Update(gvr.GroupResource(), scale)
|
updatedScale, err := s.scaleNamespacer.Scales(namespace).Update(context.TODO(), gvr.GroupResource(), scale, metav1.UpdateOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -131,7 +133,7 @@ func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *Scale
|
|||||||
}
|
}
|
||||||
|
|
||||||
patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, newSize))
|
patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, newSize))
|
||||||
updatedScale, err := s.scaleNamespacer.Scales(namespace).Patch(gvr, name, types.MergePatchType, patch)
|
updatedScale, err := s.scaleNamespacer.Scales(namespace).Patch(context.TODO(), gvr, name, types.MergePatchType, patch, metav1.PatchOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -159,7 +161,7 @@ func (s *genericScaler) Scale(namespace, resourceName string, newSize uint, prec
|
|||||||
// count for a scale (Spec) equals its updated replicas count (Status)
|
// count for a scale (Spec) equals its updated replicas count (Status)
|
||||||
func scaleHasDesiredReplicas(sClient scaleclient.ScalesGetter, gr schema.GroupResource, resourceName string, namespace string, desiredReplicas int32) wait.ConditionFunc {
|
func scaleHasDesiredReplicas(sClient scaleclient.ScalesGetter, gr schema.GroupResource, resourceName string, namespace string, desiredReplicas int32) wait.ConditionFunc {
|
||||||
return func() (bool, error) {
|
return func() (bool, error) {
|
||||||
actualScale, err := sClient.Scales(namespace).Get(gr, resourceName)
|
actualScale, err := sClient.Scales(namespace).Get(context.TODO(), gr, resourceName, metav1.GetOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user