mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-28 05:57:25 +00:00
Pass RequestInfo to GenerateLink
This commit is contained in:
parent
25f0fe1adb
commit
b4018f7da1
@ -45,7 +45,7 @@ type ScopeNamer interface {
|
||||
SetSelfLink(obj runtime.Object, url string) error
|
||||
// GenerateLink creates an encoded URI for a given runtime object that represents the canonical path
|
||||
// and query.
|
||||
GenerateLink(ctx request.Context, obj runtime.Object) (uri string, err error)
|
||||
GenerateLink(requestInfo *request.RequestInfo, obj runtime.Object) (uri string, err error)
|
||||
// GenerateListLink creates an encoded URI for a list that represents the canonical path and query.
|
||||
GenerateListLink(req *http.Request) (uri string, err error)
|
||||
}
|
||||
@ -90,12 +90,7 @@ func (n ContextBasedNaming) Name(req *http.Request) (namespace, name string, err
|
||||
return ns, requestInfo.Name, nil
|
||||
}
|
||||
|
||||
func (n ContextBasedNaming) GenerateLink(ctx request.Context, obj runtime.Object) (uri string, err error) {
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("missing requestInfo")
|
||||
}
|
||||
|
||||
func (n ContextBasedNaming) GenerateLink(requestInfo *request.RequestInfo, obj runtime.Object) (uri string, err error) {
|
||||
namespace, name, err := n.ObjectName(obj)
|
||||
if err == errEmptyName && len(requestInfo.Name) > 0 {
|
||||
name = requestInfo.Name
|
||||
|
@ -102,7 +102,12 @@ func getResourceHandler(scope RequestScope, getter getterFunc) http.HandlerFunc
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
if err := setSelfLink(result, ctx, scope.Namer); err != nil {
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
if err := setSelfLink(result, requestInfo, scope.Namer); err != nil {
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
@ -419,7 +424,12 @@ func createHandler(r rest.NamedCreater, scope RequestScope, typer runtime.Object
|
||||
}
|
||||
trace.Step("Object stored in database")
|
||||
|
||||
if err := setSelfLink(result, ctx, scope.Namer); err != nil {
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
if err := setSelfLink(result, requestInfo, scope.Namer); err != nil {
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
@ -512,7 +522,12 @@ func PatchResource(r rest.Patcher, scope RequestScope, admit admission.Interface
|
||||
return
|
||||
}
|
||||
|
||||
if err := setSelfLink(result, ctx, scope.Namer); err != nil {
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
if err := setSelfLink(result, requestInfo, scope.Namer); err != nil {
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
@ -829,7 +844,12 @@ func UpdateResource(r rest.Updater, scope RequestScope, typer runtime.ObjectType
|
||||
}
|
||||
trace.Step("Object stored in database")
|
||||
|
||||
if err := setSelfLink(result, ctx, scope.Namer); err != nil {
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
if err := setSelfLink(result, requestInfo, scope.Namer); err != nil {
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
@ -942,8 +962,13 @@ func DeleteResource(r rest.GracefulDeleter, allowsOptions bool, scope RequestSco
|
||||
}
|
||||
} else {
|
||||
// when a non-status response is returned, set the self link
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
if _, ok := result.(*metav1.Status); !ok {
|
||||
if err := setSelfLink(result, ctx, scope.Namer); err != nil {
|
||||
if err := setSelfLink(result, requestInfo, scope.Namer); err != nil {
|
||||
scope.err(err, w, req)
|
||||
return
|
||||
}
|
||||
@ -1113,9 +1138,9 @@ func transformDecodeError(typer runtime.ObjectTyper, baseErr error, into runtime
|
||||
|
||||
// setSelfLink sets the self link of an object (or the child items in a list) to the base URL of the request
|
||||
// plus the path and query generated by the provided linkFunc
|
||||
func setSelfLink(obj runtime.Object, ctx request.Context, namer ScopeNamer) error {
|
||||
func setSelfLink(obj runtime.Object, requestInfo *request.RequestInfo, namer ScopeNamer) error {
|
||||
// TODO: SelfLink generation should return a full URL?
|
||||
uri, err := namer.GenerateLink(ctx, obj)
|
||||
uri, err := namer.GenerateLink(requestInfo, obj)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
@ -1172,11 +1197,15 @@ func setListSelfLink(obj runtime.Object, ctx request.Context, req *http.Request,
|
||||
if err := namer.SetSelfLink(obj, uri); err != nil {
|
||||
glog.V(4).Infof("Unable to set self link on object: %v", err)
|
||||
}
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
return 0, fmt.Errorf("missing requestInfo")
|
||||
}
|
||||
|
||||
count := 0
|
||||
err = meta.EachListItem(obj, func(obj runtime.Object) error {
|
||||
count++
|
||||
return setSelfLink(obj, ctx, namer)
|
||||
return setSelfLink(obj, requestInfo, namer)
|
||||
})
|
||||
return count, err
|
||||
}
|
||||
|
@ -160,7 +160,7 @@ func (p *testNamer) SetSelfLink(obj runtime.Object, url string) error {
|
||||
}
|
||||
|
||||
// GenerateLink creates a path and query for a given runtime object that represents the canonical path.
|
||||
func (p *testNamer) GenerateLink(ctx request.Context, obj runtime.Object) (uri string, err error) {
|
||||
func (p *testNamer) GenerateLink(requestInfo *request.RequestInfo, obj runtime.Object) (uri string, err error) {
|
||||
return "", errors.New("not implemented")
|
||||
}
|
||||
|
||||
|
@ -89,13 +89,12 @@ func serveWatch(watcher watch.Interface, scope RequestScope, req *http.Request,
|
||||
mediaType += ";stream=watch"
|
||||
}
|
||||
|
||||
namespace, _, err := scope.Namer.Name(req)
|
||||
if err != nil {
|
||||
scope.err(fmt.Errorf("unexpected error from Namer.Name: %v", err), w, req)
|
||||
ctx := scope.ContextFunc(req)
|
||||
requestInfo, ok := request.RequestInfoFrom(ctx)
|
||||
if !ok {
|
||||
scope.err(fmt.Errorf("missing requestInfo"), w, req)
|
||||
return
|
||||
}
|
||||
ctx := scope.ContextFunc(req)
|
||||
ctx = request.WithNamespace(ctx, namespace)
|
||||
|
||||
server := &WatchServer{
|
||||
Watching: watcher,
|
||||
@ -107,7 +106,7 @@ func serveWatch(watcher watch.Interface, scope RequestScope, req *http.Request,
|
||||
Encoder: encoder,
|
||||
EmbeddedEncoder: embeddedEncoder,
|
||||
Fixup: func(obj runtime.Object) {
|
||||
if err := setSelfLink(obj, ctx, scope.Namer); err != nil {
|
||||
if err := setSelfLink(obj, requestInfo, scope.Namer); err != nil {
|
||||
utilruntime.HandleError(fmt.Errorf("failed to set link for object %v: %v", reflect.TypeOf(obj), err))
|
||||
}
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user