Skip to content

Commit 1de2160

Browse files
authored
Merge pull request #7908 from Preisschild/fix/capi-patch-instead-update
CA: Use Patch to Scale clusterapi nodepools
2 parents 05b97ef + ecb572a commit 1de2160

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_controller_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package clusterapi
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
23+
"k8s.io/apimachinery/pkg/types"
2224
"math/rand"
2325
"path"
2426
"reflect"
@@ -251,6 +253,39 @@ func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machin
251253
}
252254

253255
return true, s, nil
256+
case "patch":
257+
action, ok := action.(clientgotesting.PatchAction)
258+
if !ok {
259+
return true, nil, fmt.Errorf("failed to convert Action to PatchAction: %T", action)
260+
}
261+
262+
pt := action.GetPatchType()
263+
if pt != types.MergePatchType {
264+
return true, nil, fmt.Errorf("unexpected patch type: expected = %s, got = %s", types.MergePatchType, pt)
265+
}
266+
267+
var scale autoscalingv1.Scale
268+
err := json.Unmarshal(action.GetPatch(), &scale)
269+
if err != nil {
270+
return true, nil, fmt.Errorf("couldn't unmarshal patch: %w", err)
271+
}
272+
273+
_, err = dynamicClientset.Resource(gvr).Namespace(action.GetNamespace()).Patch(context.TODO(), action.GetName(), pt, action.GetPatch(), metav1.PatchOptions{})
274+
if err != nil {
275+
return true, nil, err
276+
}
277+
278+
newReplicas := scale.Spec.Replicas
279+
280+
return true, &autoscalingv1.Scale{
281+
ObjectMeta: metav1.ObjectMeta{
282+
Name: action.GetName(),
283+
Namespace: action.GetNamespace(),
284+
},
285+
Spec: autoscalingv1.ScaleSpec{
286+
Replicas: newReplicas,
287+
},
288+
}, nil
254289
default:
255290
return true, nil, fmt.Errorf("unknown verb: %v", action.GetVerb())
256291
}

cluster-autoscaler/cloudprovider/clusterapi/clusterapi_unstructured.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,21 @@ package clusterapi
1818

1919
import (
2020
"context"
21+
"encoding/json"
2122
"fmt"
2223
"path"
2324
"strings"
2425
"time"
2526

2627
"github.com/pkg/errors"
28+
autoscalingv1 "k8s.io/api/autoscaling/v1"
2729
apiv1 "k8s.io/api/core/v1"
2830
corev1 "k8s.io/api/core/v1"
2931
"k8s.io/apimachinery/pkg/api/resource"
3032
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3133
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
3234
"k8s.io/apimachinery/pkg/runtime/schema"
35+
"k8s.io/apimachinery/pkg/types"
3336
"k8s.io/apimachinery/pkg/util/validation"
3437
klog "k8s.io/klog/v2"
3538
)
@@ -118,17 +121,18 @@ func (r unstructuredScalableResource) SetSize(nreplicas int) error {
118121
return err
119122
}
120123

121-
s, err := r.controller.managementScaleClient.Scales(r.Namespace()).Get(context.TODO(), gvr.GroupResource(), r.Name(), metav1.GetOptions{})
122-
if err != nil {
123-
return err
124+
spec := autoscalingv1.Scale{
125+
Spec: autoscalingv1.ScaleSpec{
126+
Replicas: int32(nreplicas),
127+
},
124128
}
125129

126-
if s == nil {
127-
return fmt.Errorf("unknown %s %s/%s", r.Kind(), r.Namespace(), r.Name())
130+
patch, err := json.Marshal(spec)
131+
if err != nil {
132+
return fmt.Errorf("could not marshal json patch for scaling: %w", err)
128133
}
129134

130-
s.Spec.Replicas = int32(nreplicas)
131-
_, updateErr := r.controller.managementScaleClient.Scales(r.Namespace()).Update(context.TODO(), gvr.GroupResource(), s, metav1.UpdateOptions{})
135+
_, updateErr := r.controller.managementScaleClient.Scales(r.Namespace()).Patch(context.TODO(), gvr, r.Name(), types.MergePatchType, patch, metav1.PatchOptions{})
132136

133137
if updateErr == nil {
134138
updateErr = unstructured.SetNestedField(r.unstructured.UnstructuredContent(), int64(nreplicas), "spec", "replicas")

0 commit comments

Comments
 (0)