Skip to content

Commit f3e95de

Browse files
authored
Merge pull request #6326 from ikaven1024/fix-proxy-pb
fix proxy request error with pb content type
2 parents 4e651a0 + 7835614 commit f3e95de

File tree

6 files changed

+241
-67
lines changed

6 files changed

+241
-67
lines changed

pkg/search/proxy/controller.go

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ func (ctl *Controller) Connect(ctx context.Context, proxyPath string, responder
291291
}
292292

293293
h, err := ctl.proxy.Connect(newCtx, framework.ProxyRequest{
294+
RestMapper: ctl.restMapper,
294295
RequestInfo: requestInfo,
295296
GroupVersionResource: gvr,
296297
ProxyPath: proxyPath,

pkg/search/proxy/framework/interface.go

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"net/http"
2222

23+
"k8s.io/apimachinery/pkg/api/meta"
2324
"k8s.io/apimachinery/pkg/runtime/schema"
2425
"k8s.io/apiserver/pkg/endpoints/request"
2526
"k8s.io/apiserver/pkg/registry/rest"
@@ -74,6 +75,7 @@ type Plugin interface {
7475

7576
// ProxyRequest holds parameter for Proxy.Connect()
7677
type ProxyRequest struct {
78+
RestMapper meta.RESTMapper
7779
RequestInfo *request.RequestInfo
7880
GroupVersionResource schema.GroupVersionResource
7981
ProxyPath string

pkg/search/proxy/framework/plugins/cache/cache.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructuredscheme"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
"k8s.io/apimachinery/pkg/runtime/schema"
3031
"k8s.io/apimachinery/pkg/watch"
@@ -109,7 +110,7 @@ func (c *Cache) Connect(_ context.Context, request framework.ProxyRequest) (http
109110
Namer: meta.NewAccessor(),
110111
ClusterScoped: mapping.Scope.Name() == meta.RESTScopeNameRoot,
111112
},
112-
Serializer: scheme.Codecs.WithoutConversion(),
113+
Serializer: cacheSerializer,
113114
Convertor: cacheScheme,
114115
Subresource: requestInfo.Subresource,
115116
MetaGroupVersion: metav1.SchemeGroupVersion,
@@ -126,6 +127,22 @@ func (c *Cache) Connect(_ context.Context, request framework.ProxyRequest) (http
126127
return h, nil
127128
}
128129

130+
var (
131+
cacheSerializer = &customSerializer{
132+
NegotiatedSerializer: scheme.Codecs.WithoutConversion(),
133+
supportedMediaTypes: unstructuredscheme.NewUnstructuredNegotiatedSerializer().SupportedMediaTypes(),
134+
}
135+
)
136+
137+
type customSerializer struct {
138+
runtime.NegotiatedSerializer
139+
supportedMediaTypes []runtime.SerializerInfo
140+
}
141+
142+
func (u *customSerializer) SupportedMediaTypes() []runtime.SerializerInfo {
143+
return u.supportedMediaTypes
144+
}
145+
129146
type rester struct {
130147
store store.Store
131148
gvr schema.GroupVersionResource

pkg/search/proxy/framework/plugins/cluster/cluster.go

+30-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
apierrors "k8s.io/apimachinery/pkg/api/errors"
2727
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
28+
"k8s.io/apimachinery/pkg/runtime"
29+
"k8s.io/client-go/kubernetes/scheme"
2830
listcorev1 "k8s.io/client-go/listers/core/v1"
31+
"k8s.io/klog/v2"
2932

3033
clusterapis "github.com/karmada-io/karmada/pkg/apis/cluster"
3134
clusterv1alpha1 "github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
@@ -118,15 +121,15 @@ func (c *Cluster) Connect(ctx context.Context, request framework.ProxyRequest) (
118121
// Objects get by client via proxy are edited some fields, different from objects in member clusters.
119122
// So before update, we shall recover these fields.
120123
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
121-
if err = modifyRequest(req, clusterName); err != nil {
124+
if err = modifyRequest(req, request, clusterName); err != nil {
122125
request.Responder.Error(err)
123126
return
124127
}
125128
h.ServeHTTP(rw, req)
126129
}), nil
127130
}
128131

129-
func modifyRequest(req *http.Request, cluster string) error {
132+
func modifyRequest(req *http.Request, requestInfo framework.ProxyRequest, cluster string) error {
130133
if req.ContentLength == 0 {
131134
return nil
132135
}
@@ -143,9 +146,16 @@ func modifyRequest(req *http.Request, cluster string) error {
143146
req.ContentLength = int64(body.Len())
144147
}()
145148

146-
obj := &unstructured.Unstructured{}
147-
_, _, err = unstructured.UnstructuredJSONScheme.Decode(body.Bytes(), nil, obj)
149+
encoder, decoder, obj, err := getSerializer(req, requestInfo)
148150
if err != nil {
151+
klog.Warningf("modifyRequest getSerializer error for request %s: %v", req.RequestURI, err)
152+
// ignore error
153+
return nil
154+
}
155+
156+
_, _, err = decoder.Decode(body.Bytes(), nil, obj)
157+
if err != nil {
158+
klog.Warningf("modifyRequest decode error for request %s: %v", req.RequestURI, err)
149159
// ignore error
150160
return nil
151161
}
@@ -156,8 +166,23 @@ func modifyRequest(req *http.Request, cluster string) error {
156166
if changed {
157167
// write changed object into body
158168
body.Reset()
159-
return unstructured.UnstructuredJSONScheme.Encode(obj, body)
169+
return encoder.Encode(obj, body)
160170
}
161171

162172
return nil
163173
}
174+
175+
func getSerializer(req *http.Request, requestInfo framework.ProxyRequest) (runtime.Encoder, runtime.Decoder, runtime.Object, error) {
176+
gvk, _ := requestInfo.RestMapper.KindFor(requestInfo.GroupVersionResource)
177+
if scheme.Scheme.Recognizes(gvk) {
178+
negotiator := runtime.NewClientNegotiator(scheme.Codecs.WithoutConversion(), requestInfo.GroupVersionResource.GroupVersion())
179+
contentType := req.Header.Get("Content-Type")
180+
encoder, _ := negotiator.Encoder(contentType, nil)
181+
decoder, _ := negotiator.Decoder(contentType, nil)
182+
183+
obj, err := scheme.Scheme.New(gvk)
184+
return encoder, decoder, obj, err
185+
}
186+
187+
return unstructured.UnstructuredJSONScheme, unstructured.UnstructuredJSONScheme, &unstructured.Unstructured{}, nil
188+
}

0 commit comments

Comments
 (0)