Merge pull request #7908 from Preisschild/fix/capi-patch-instead-update
CA: Use Patch to Scale clusterapi nodepools
This commit is contained in:
commit
1de2160986
|
@ -18,7 +18,9 @@ package clusterapi
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"math/rand"
|
||||
"path"
|
||||
"reflect"
|
||||
|
@ -251,6 +253,39 @@ func mustCreateTestController(t testing.TB, testConfigs ...*testConfig) (*machin
|
|||
}
|
||||
|
||||
return true, s, nil
|
||||
case "patch":
|
||||
action, ok := action.(clientgotesting.PatchAction)
|
||||
if !ok {
|
||||
return true, nil, fmt.Errorf("failed to convert Action to PatchAction: %T", action)
|
||||
}
|
||||
|
||||
pt := action.GetPatchType()
|
||||
if pt != types.MergePatchType {
|
||||
return true, nil, fmt.Errorf("unexpected patch type: expected = %s, got = %s", types.MergePatchType, pt)
|
||||
}
|
||||
|
||||
var scale autoscalingv1.Scale
|
||||
err := json.Unmarshal(action.GetPatch(), &scale)
|
||||
if err != nil {
|
||||
return true, nil, fmt.Errorf("couldn't unmarshal patch: %w", err)
|
||||
}
|
||||
|
||||
_, err = dynamicClientset.Resource(gvr).Namespace(action.GetNamespace()).Patch(context.TODO(), action.GetName(), pt, action.GetPatch(), metav1.PatchOptions{})
|
||||
if err != nil {
|
||||
return true, nil, err
|
||||
}
|
||||
|
||||
newReplicas := scale.Spec.Replicas
|
||||
|
||||
return true, &autoscalingv1.Scale{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: action.GetName(),
|
||||
Namespace: action.GetNamespace(),
|
||||
},
|
||||
Spec: autoscalingv1.ScaleSpec{
|
||||
Replicas: newReplicas,
|
||||
},
|
||||
}, nil
|
||||
default:
|
||||
return true, nil, fmt.Errorf("unknown verb: %v", action.GetVerb())
|
||||
}
|
||||
|
|
|
@ -18,18 +18,21 @@ package clusterapi
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"path"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||
apiv1 "k8s.io/api/core/v1"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/resource"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
klog "k8s.io/klog/v2"
|
||||
)
|
||||
|
@ -118,17 +121,18 @@ func (r unstructuredScalableResource) SetSize(nreplicas int) error {
|
|||
return err
|
||||
}
|
||||
|
||||
s, err := r.controller.managementScaleClient.Scales(r.Namespace()).Get(context.TODO(), gvr.GroupResource(), r.Name(), metav1.GetOptions{})
|
||||
spec := autoscalingv1.Scale{
|
||||
Spec: autoscalingv1.ScaleSpec{
|
||||
Replicas: int32(nreplicas),
|
||||
},
|
||||
}
|
||||
|
||||
patch, err := json.Marshal(spec)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("could not marshal json patch for scaling: %w", err)
|
||||
}
|
||||
|
||||
if s == nil {
|
||||
return fmt.Errorf("unknown %s %s/%s", r.Kind(), r.Namespace(), r.Name())
|
||||
}
|
||||
|
||||
s.Spec.Replicas = int32(nreplicas)
|
||||
_, updateErr := r.controller.managementScaleClient.Scales(r.Namespace()).Update(context.TODO(), gvr.GroupResource(), s, metav1.UpdateOptions{})
|
||||
_, updateErr := r.controller.managementScaleClient.Scales(r.Namespace()).Patch(context.TODO(), gvr, r.Name(), types.MergePatchType, patch, metav1.PatchOptions{})
|
||||
|
||||
if updateErr == nil {
|
||||
updateErr = unstructured.SetNestedField(r.unstructured.UnstructuredContent(), int64(nreplicas), "spec", "replicas")
|
||||
|
|
Loading…
Reference in New Issue