@@ -23,8 +23,8 @@ import (
23
23
24
24
autoscalingv1 "k8s.io/api/autoscaling/v1"
25
25
"k8s.io/apimachinery/pkg/api/errors"
26
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
26
"k8s.io/apimachinery/pkg/runtime/schema"
27
+ "k8s.io/apimachinery/pkg/types"
28
28
"k8s.io/apimachinery/pkg/util/wait"
29
29
scaleclient "k8s.io/client-go/scale"
30
30
)
@@ -35,10 +35,10 @@ type Scaler interface {
35
35
// retries in the event of resource version mismatch (if retry is not nil),
36
36
// and optionally waits until the status of the resource matches newSize (if wait is not nil)
37
37
// TODO: Make the implementation of this watch-based (#56075) once #31345 is fixed.
38
- Scale (namespace , name string , newSize uint , preconditions * ScalePrecondition , retry , wait * RetryParams , gr schema.GroupResource ) error
38
+ Scale (namespace , name string , newSize uint , preconditions * ScalePrecondition , retry , wait * RetryParams , gvr schema.GroupVersionResource ) error
39
39
// ScaleSimple does a simple one-shot attempt at scaling - not useful on its own, but
40
40
// a necessary building block for Scale
41
- ScaleSimple (namespace , name string , preconditions * ScalePrecondition , newSize uint , gr schema.GroupResource ) (updatedResourceVersion string , err error )
41
+ ScaleSimple (namespace , name string , preconditions * ScalePrecondition , newSize uint , gvr schema.GroupVersionResource ) (updatedResourceVersion string , err error )
42
42
}
43
43
44
44
// NewScaler get a scaler for a given resource
@@ -77,9 +77,9 @@ func NewRetryParams(interval, timeout time.Duration) *RetryParams {
77
77
}
78
78
79
79
// ScaleCondition is a closure around Scale that facilitates retries via util.wait
80
- func ScaleCondition (r Scaler , precondition * ScalePrecondition , namespace , name string , count uint , updatedResourceVersion * string , gr schema.GroupResource ) wait.ConditionFunc {
80
+ func ScaleCondition (r Scaler , precondition * ScalePrecondition , namespace , name string , count uint , updatedResourceVersion * string , gvr schema.GroupVersionResource ) wait.ConditionFunc {
81
81
return func () (bool , error ) {
82
- rv , err := r .ScaleSimple (namespace , name , precondition , count , gr )
82
+ rv , err := r .ScaleSimple (namespace , name , precondition , count , gvr )
83
83
if updatedResourceVersion != nil {
84
84
* updatedResourceVersion = rv
85
85
}
@@ -113,23 +113,25 @@ type genericScaler struct {
113
113
var _ Scaler = & genericScaler {}
114
114
115
115
// ScaleSimple updates a scale of a given resource. It returns the resourceVersion of the scale if the update was successful.
116
- func (s * genericScaler ) ScaleSimple (namespace , name string , preconditions * ScalePrecondition , newSize uint , gr schema.GroupResource ) (updatedResourceVersion string , err error ) {
117
- scale := & autoscalingv1.Scale {
118
- ObjectMeta : metav1.ObjectMeta {Namespace : namespace , Name : name },
119
- }
116
+ func (s * genericScaler ) ScaleSimple (namespace , name string , preconditions * ScalePrecondition , newSize uint , gvr schema.GroupVersionResource ) (updatedResourceVersion string , err error ) {
120
117
if preconditions != nil {
121
- var err error
122
- scale , err = s .scaleNamespacer .Scales (namespace ).Get (gr , name )
118
+ scale , err := s .scaleNamespacer .Scales (namespace ).Get (gvr .GroupResource (), name )
123
119
if err != nil {
124
120
return "" , err
125
121
}
126
- if err := preconditions .validate (scale ); err != nil {
122
+ if err = preconditions .validate (scale ); err != nil {
123
+ return "" , err
124
+ }
125
+ scale .Spec .Replicas = int32 (newSize )
126
+ updatedScale , err := s .scaleNamespacer .Scales (namespace ).Update (gvr .GroupResource (), scale )
127
+ if err != nil {
127
128
return "" , err
128
129
}
130
+ return updatedScale .ResourceVersion , nil
129
131
}
130
132
131
- scale . Spec . Replicas = int32 ( newSize )
132
- updatedScale , err := s .scaleNamespacer .Scales (namespace ).Update ( gr , scale )
133
+ patch := [] byte ( fmt . Sprintf ( `{"spec":{"replicas":%d}}` , newSize ) )
134
+ updatedScale , err := s .scaleNamespacer .Scales (namespace ).Patch ( gvr , name , types . MergePatchType , patch )
133
135
if err != nil {
134
136
return "" , err
135
137
}
@@ -138,17 +140,17 @@ func (s *genericScaler) ScaleSimple(namespace, name string, preconditions *Scale
138
140
139
141
// Scale updates a scale of a given resource to a new size, with optional precondition check (if preconditions is not nil),
140
142
// optional retries (if retry is not nil), and then optionally waits for the status to reach desired count.
141
- func (s * genericScaler ) Scale (namespace , resourceName string , newSize uint , preconditions * ScalePrecondition , retry , waitForReplicas * RetryParams , gr schema.GroupResource ) error {
143
+ func (s * genericScaler ) Scale (namespace , resourceName string , newSize uint , preconditions * ScalePrecondition , retry , waitForReplicas * RetryParams , gvr schema.GroupVersionResource ) error {
142
144
if retry == nil {
143
145
// make it try only once, immediately
144
146
retry = & RetryParams {Interval : time .Millisecond , Timeout : time .Millisecond }
145
147
}
146
- cond := ScaleCondition (s , preconditions , namespace , resourceName , newSize , nil , gr )
148
+ cond := ScaleCondition (s , preconditions , namespace , resourceName , newSize , nil , gvr )
147
149
if err := wait .PollImmediate (retry .Interval , retry .Timeout , cond ); err != nil {
148
150
return err
149
151
}
150
152
if waitForReplicas != nil {
151
- return WaitForScaleHasDesiredReplicas (s .scaleNamespacer , gr , resourceName , namespace , newSize , waitForReplicas )
153
+ return WaitForScaleHasDesiredReplicas (s .scaleNamespacer , gvr . GroupResource () , resourceName , namespace , newSize , waitForReplicas )
152
154
}
153
155
return nil
154
156
}
0 commit comments