Skip to content

Commit

Permalink
fix(cluster): scale sub-resources of workload cannot be updated using…
Browse files Browse the repository at this point in the history
… patch request
  • Loading branch information
choujimmy authored and QianChenglong committed May 20, 2020
1 parent a96f057 commit c4ce3fc
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 39 deletions.
22 changes: 22 additions & 0 deletions pkg/platform/proxy/apps/deployment/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"tkestack.io/tke/pkg/platform/util"
)

Expand Down Expand Up @@ -289,6 +291,26 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
return nil, false, err
}

if requestInfo.Verb == "patch" {
requestBody, ok := filter.RequestBodyFrom(ctx)
if !ok {
return nil, false, errors.NewBadRequest("request body is required")
}
result := &autoscalingv1.Scale{}
if err := client.
Patch(types.PatchType(requestBody.ContentType)).
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Resource(requestInfo.Resource).
SubResource(requestInfo.Subresource).
Name(name).
Body(requestBody.Data).
Do(ctx).
Into(result); err != nil {
return nil, false, err
}
return result, true, nil
}

obj, err := objInfo.UpdatedObject(ctx, nil)
if err != nil {
return nil, false, errors.NewInternalError(err)
Expand Down
54 changes: 38 additions & 16 deletions pkg/platform/proxy/apps/replicaset/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ package storage
import (
"context"

appsV1 "k8s.io/api/apps/v1"
appsV1Beta1 "k8s.io/api/apps/v1beta1"
appsV1Beta2 "k8s.io/api/apps/v1beta2"
autoscalingV1API "k8s.io/api/autoscaling/v1"
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
autoscalingv1 "k8s.io/api/autoscaling/v1"
extensionsv1beta1 "k8s.io/api/extensions/v1beta1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"tkestack.io/tke/pkg/platform/util"
)

Expand All @@ -55,8 +57,8 @@ type REST struct {
// NewStorageV1 returns a Storage object that will work against resources.
func NewStorageV1(_ genericregistry.RESTOptionsGetter, platformClient platforminternalclient.PlatformInterface) *Storage {
replicaSetStore := &util.Store{
NewFunc: func() runtime.Object { return &appsV1.ReplicaSet{} },
NewListFunc: func() runtime.Object { return &appsV1.ReplicaSetList{} },
NewFunc: func() runtime.Object { return &appsv1.ReplicaSet{} },
NewListFunc: func() runtime.Object { return &appsv1.ReplicaSetList{} },
Namespaced: true,
PlatformClient: platformClient,
}
Expand All @@ -83,8 +85,8 @@ func NewStorageV1(_ genericregistry.RESTOptionsGetter, platformClient platformin
// NewStorageV1Beta2 returns a Storage object that will work against resources.
func NewStorageV1Beta2(_ genericregistry.RESTOptionsGetter, platformClient platforminternalclient.PlatformInterface) *Storage {
replicaSetStore := &util.Store{
NewFunc: func() runtime.Object { return &appsV1Beta2.ReplicaSet{} },
NewListFunc: func() runtime.Object { return &appsV1Beta2.ReplicaSetList{} },
NewFunc: func() runtime.Object { return &appsv1beta2.ReplicaSet{} },
NewListFunc: func() runtime.Object { return &appsv1beta2.ReplicaSetList{} },
Namespaced: true,
PlatformClient: platformClient,
}
Expand Down Expand Up @@ -189,18 +191,18 @@ var _ = rest.GroupVersionKindProvider(&ScaleREST{})
// GroupVersionKind is used to specify a particular GroupVersionKind to discovery.
func (r *ScaleREST) GroupVersionKind(containingGV schema.GroupVersion) schema.GroupVersionKind {
switch containingGV {
case appsV1Beta1.SchemeGroupVersion:
return appsV1Beta1.SchemeGroupVersion.WithKind("Scale")
case appsV1Beta2.SchemeGroupVersion:
return appsV1Beta2.SchemeGroupVersion.WithKind("Scale")
case appsv1beta1.SchemeGroupVersion:
return appsv1beta1.SchemeGroupVersion.WithKind("Scale")
case appsv1beta2.SchemeGroupVersion:
return appsv1beta2.SchemeGroupVersion.WithKind("Scale")
default:
return autoscalingV1API.SchemeGroupVersion.WithKind("Scale")
return autoscalingv1.SchemeGroupVersion.WithKind("Scale")
}
}

// New creates a new Scale object
func (r *ScaleREST) New() runtime.Object {
return &autoscalingV1API.Scale{}
return &autoscalingv1.Scale{}
}

// Get finds a resource in the storage by name and returns it.
Expand All @@ -210,7 +212,7 @@ func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOpt
return nil, err
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Get().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand All @@ -232,12 +234,32 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
return nil, false, err
}

if requestInfo.Verb == "patch" {
requestBody, ok := filter.RequestBodyFrom(ctx)
if !ok {
return nil, false, errors.NewBadRequest("request body is required")
}
result := &autoscalingv1.Scale{}
if err := client.
Patch(types.PatchType(requestBody.ContentType)).
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Resource(requestInfo.Resource).
SubResource(requestInfo.Subresource).
Name(name).
Body(requestBody.Data).
Do(ctx).
Into(result); err != nil {
return nil, false, err
}
return result, true, nil
}

obj, err := objInfo.UpdatedObject(ctx, nil)
if err != nil {
return nil, false, errors.NewInternalError(err)
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Put().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand Down
58 changes: 40 additions & 18 deletions pkg/platform/proxy/apps/statefulset/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ package storage
import (
"context"

appsV1 "k8s.io/api/apps/v1"
appsV1Beta1 "k8s.io/api/apps/v1beta1"
appsV1Beta2 "k8s.io/api/apps/v1beta2"
autoscalingV1API "k8s.io/api/autoscaling/v1"
appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
appsv1beta2 "k8s.io/api/apps/v1beta2"
autoscalingv1 "k8s.io/api/autoscaling/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"tkestack.io/tke/pkg/platform/util"
)

Expand All @@ -52,8 +54,8 @@ type REST struct {
// NewStorageV1 returns a Storage object that will work against resources.
func NewStorageV1(_ genericregistry.RESTOptionsGetter, platformClient platforminternalclient.PlatformInterface) *Storage {
statefulSetStore := &util.Store{
NewFunc: func() runtime.Object { return &appsV1.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsV1.StatefulSetList{} },
NewFunc: func() runtime.Object { return &appsv1.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsv1.StatefulSetList{} },
Namespaced: true,
PlatformClient: platformClient,
}
Expand All @@ -80,8 +82,8 @@ func NewStorageV1(_ genericregistry.RESTOptionsGetter, platformClient platformin
// NewStorageV1Beta1 returns a Storage object that will work against resources.
func NewStorageV1Beta1(_ genericregistry.RESTOptionsGetter, platformClient platforminternalclient.PlatformInterface) *Storage {
statefulSetStore := &util.Store{
NewFunc: func() runtime.Object { return &appsV1Beta1.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsV1Beta1.StatefulSetList{} },
NewFunc: func() runtime.Object { return &appsv1beta1.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsv1beta1.StatefulSetList{} },
Namespaced: true,
PlatformClient: platformClient,
}
Expand All @@ -108,8 +110,8 @@ func NewStorageV1Beta1(_ genericregistry.RESTOptionsGetter, platformClient platf
// NewStorageV1Beta2 returns a Storage object that will work against resources.
func NewStorageV1Beta2(_ genericregistry.RESTOptionsGetter, platformClient platforminternalclient.PlatformInterface) *Storage {
statefulSetStore := &util.Store{
NewFunc: func() runtime.Object { return &appsV1Beta2.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsV1Beta2.StatefulSetList{} },
NewFunc: func() runtime.Object { return &appsv1beta2.StatefulSet{} },
NewListFunc: func() runtime.Object { return &appsv1beta2.StatefulSetList{} },
Namespaced: true,
PlatformClient: platformClient,
}
Expand Down Expand Up @@ -189,18 +191,18 @@ var _ = rest.GroupVersionKindProvider(&ScaleREST{})
// GroupVersionKind is used to specify a particular GroupVersionKind to discovery.
func (r *ScaleREST) GroupVersionKind(containingGV schema.GroupVersion) schema.GroupVersionKind {
switch containingGV {
case appsV1Beta1.SchemeGroupVersion:
return appsV1Beta1.SchemeGroupVersion.WithKind("Scale")
case appsV1Beta2.SchemeGroupVersion:
return appsV1Beta2.SchemeGroupVersion.WithKind("Scale")
case appsv1beta1.SchemeGroupVersion:
return appsv1beta1.SchemeGroupVersion.WithKind("Scale")
case appsv1beta2.SchemeGroupVersion:
return appsv1beta2.SchemeGroupVersion.WithKind("Scale")
default:
return autoscalingV1API.SchemeGroupVersion.WithKind("Scale")
return autoscalingv1.SchemeGroupVersion.WithKind("Scale")
}
}

// New creates a new Scale object.
func (r *ScaleREST) New() runtime.Object {
return &autoscalingV1API.Scale{}
return &autoscalingv1.Scale{}
}

// Get finds a resource in the storage by name and returns it.
Expand All @@ -210,7 +212,7 @@ func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOpt
return nil, err
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Get().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand All @@ -232,12 +234,32 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
return nil, false, err
}

if requestInfo.Verb == "patch" {
requestBody, ok := filter.RequestBodyFrom(ctx)
if !ok {
return nil, false, errors.NewBadRequest("request body is required")
}
result := &autoscalingv1.Scale{}
if err := client.
Patch(types.PatchType(requestBody.ContentType)).
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Resource(requestInfo.Resource).
SubResource(requestInfo.Subresource).
Name(name).
Body(requestBody.Data).
Do(ctx).
Into(result); err != nil {
return nil, false, err
}
return result, true, nil
}

obj, err := objInfo.UpdatedObject(ctx, nil)
if err != nil {
return nil, false, errors.NewInternalError(err)
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Put().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand Down
32 changes: 27 additions & 5 deletions pkg/platform/proxy/core/replicationcontroller/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,18 @@ package storage
import (
"context"

autoscalingV1API "k8s.io/api/autoscaling/v1"
autoscalingv1 "k8s.io/api/autoscaling/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
genericregistry "k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/client-go/scale/scheme/extensionsv1beta1"
platforminternalclient "tkestack.io/tke/api/client/clientset/internalversion/typed/platform/internalversion"
"tkestack.io/tke/pkg/platform/apiserver/filter"
"tkestack.io/tke/pkg/platform/util"
)

Expand Down Expand Up @@ -135,13 +137,13 @@ func (r *ScaleREST) GroupVersionKind(containingGV schema.GroupVersion) schema.Gr
case extensionsv1beta1.SchemeGroupVersion:
return extensionsv1beta1.SchemeGroupVersion.WithKind("Scale")
default:
return autoscalingV1API.SchemeGroupVersion.WithKind("Scale")
return autoscalingv1.SchemeGroupVersion.WithKind("Scale")
}
}

// New creates a new Scale object
func (r *ScaleREST) New() runtime.Object {
return &autoscalingV1API.Scale{}
return &autoscalingv1.Scale{}
}

// Get finds a resource in the storage by name and returns it.
Expand All @@ -151,7 +153,7 @@ func (r *ScaleREST) Get(ctx context.Context, name string, options *metav1.GetOpt
return nil, err
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Get().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand All @@ -173,12 +175,32 @@ func (r *ScaleREST) Update(ctx context.Context, name string, objInfo rest.Update
return nil, false, err
}

if requestInfo.Verb == "patch" {
requestBody, ok := filter.RequestBodyFrom(ctx)
if !ok {
return nil, false, errors.NewBadRequest("request body is required")
}
result := &autoscalingv1.Scale{}
if err := client.
Patch(types.PatchType(requestBody.ContentType)).
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Resource(requestInfo.Resource).
SubResource(requestInfo.Subresource).
Name(name).
Body(requestBody.Data).
Do(ctx).
Into(result); err != nil {
return nil, false, err
}
return result, true, nil
}

obj, err := objInfo.UpdatedObject(ctx, nil)
if err != nil {
return nil, false, errors.NewInternalError(err)
}

result := &autoscalingV1API.Scale{}
result := &autoscalingv1.Scale{}
if err := client.
Put().
NamespaceIfScoped(requestInfo.Namespace, requestInfo.Namespace != "").
Expand Down

0 comments on commit c4ce3fc

Please sign in to comment.