feat(scale): add Patch method to ScaleInterface

Signed-off-by: knight42 <anonymousknight96@gmail.com>

Kubernetes-commit: f020c9159869918c63e0aad56abd01e655e61e78
This commit is contained in:
knight42
2019-07-29 12:33:24 +08:00
committed by Kubernetes Publisher
parent 396a06da3b
commit 70681df7b9
4 changed files with 160 additions and 50 deletions

View File

@@ -25,9 +25,11 @@ import (
"net/http"
"testing"
jsonpatch "github.com/evanphx/json-patch"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
fakedisco "k8s.io/client-go/discovery/fake"
"k8s.io/client-go/dynamic"
fakerest "k8s.io/client-go/rest/fake"
@@ -197,6 +199,37 @@ func fakeScaleClient(t *testing.T) (ScalesGetter, []schema.GroupResource) {
return nil, err
}
return &http.Response{StatusCode: 200, Header: defaultHeaders(), Body: bytesBody(res)}, nil
case "PATCH":
body, err := ioutil.ReadAll(req.Body)
if err != nil {
return nil, err
}
originScale, err := json.Marshal(scale)
if err != nil {
return nil, err
}
var res []byte
contentType := req.Header.Get("Content-Type")
pt := types.PatchType(contentType)
switch pt {
case types.MergePatchType:
res, err = jsonpatch.MergePatch(originScale, body)
if err != nil {
return nil, err
}
case types.JSONPatchType:
patch, err := jsonpatch.DecodePatch(body)
if err != nil {
return nil, err
}
res, err = patch.Apply(originScale)
if err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid patch type")
}
return &http.Response{StatusCode: 200, Header: defaultHeaders(), Body: bytesBody(res)}, nil
default:
return nil, fmt.Errorf("unexpected request for URL %q with method %q", req.URL.String(), req.Method)
}
@@ -213,10 +246,10 @@ func fakeScaleClient(t *testing.T) (ScalesGetter, []schema.GroupResource) {
client := New(fakeClient, restMapper, dynamic.LegacyAPIPathResolverFunc, resolver)
groupResources := []schema.GroupResource{
{Group: corev1.GroupName, Resource: "replicationcontroller"},
{Group: extv1beta1.GroupName, Resource: "replicaset"},
{Group: appsv1beta2.GroupName, Resource: "deployment"},
{Group: "cheese.testing.k8s.io", Resource: "cheddar"},
{Group: corev1.GroupName, Resource: "replicationcontrollers"},
{Group: extv1beta1.GroupName, Resource: "replicasets"},
{Group: appsv1beta2.GroupName, Resource: "deployments"},
{Group: "cheese.testing.k8s.io", Resource: "cheddars"},
}
return client, groupResources
@@ -277,3 +310,55 @@ func TestUpdateScale(t *testing.T) {
assert.Equal(t, expectedScale, scale, "should have returned the expected scale for %s", groupResource.String())
}
}
func TestPatchScale(t *testing.T) {
scaleClient, groupResources := fakeScaleClient(t)
expectedScale := &autoscalingv1.Scale{
TypeMeta: metav1.TypeMeta{
Kind: "Scale",
APIVersion: autoscalingv1.SchemeGroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: autoscalingv1.ScaleSpec{Replicas: 5},
Status: autoscalingv1.ScaleStatus{
Replicas: 10,
Selector: "foo=bar",
},
}
gvrs := make([]schema.GroupVersionResource, 0, len(groupResources))
for _, gr := range groupResources {
switch gr.Group {
case corev1.GroupName:
gvrs = append(gvrs, gr.WithVersion(corev1.SchemeGroupVersion.Version))
case extv1beta1.GroupName:
gvrs = append(gvrs, gr.WithVersion(extv1beta1.SchemeGroupVersion.Version))
case appsv1beta2.GroupName:
gvrs = append(gvrs, gr.WithVersion(appsv1beta2.SchemeGroupVersion.Version))
default:
// Group cheese.testing.k8s.io
gvrs = append(gvrs, gr.WithVersion("v27alpha15"))
}
}
patch := []byte(`{"spec":{"replicas":5}}`)
for _, gvr := range gvrs {
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.MergePatchType, patch)
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
continue
}
assert.NotNil(t, scale, "should have returned a non-nil scale for %s", gvr.String())
assert.Equal(t, expectedScale, scale, "should have returned the expected scale for %s", gvr.String())
}
patch = []byte(`[{"op":"replace","path":"/spec/replicas","value":5}]`)
for _, gvr := range gvrs {
scale, err := scaleClient.Scales("default").Patch(gvr, "foo", types.JSONPatchType, patch)
if !assert.NoError(t, err, "should have been able to fetch a scale for %s", gvr.String()) {
continue
}
assert.NotNil(t, scale, "should have returned a non-nil scale for %s", gvr.String())
assert.Equal(t, expectedScale, scale, "should have returned the expected scale for %s", gvr.String())
}
}