Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package handlers |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "net/http" |
| 22 | "net/url" |
| 23 | |
| 24 | "k8s.io/apimachinery/pkg/api/errors" |
| 25 | "k8s.io/apimachinery/pkg/runtime" |
| 26 | "k8s.io/apiserver/pkg/endpoints/request" |
| 27 | ) |
| 28 | |
| 29 | // ScopeNamer handles accessing names from requests and objects |
| 30 | type ScopeNamer interface { |
| 31 | // Namespace returns the appropriate namespace value from the request (may be empty) or an |
| 32 | // error. |
| 33 | Namespace(req *http.Request) (namespace string, err error) |
| 34 | // Name returns the name from the request, and an optional namespace value if this is a namespace |
| 35 | // scoped call. An error is returned if the name is not available. |
| 36 | Name(req *http.Request) (namespace, name string, err error) |
| 37 | // ObjectName returns the namespace and name from an object if they exist, or an error if the object |
| 38 | // does not support names. |
| 39 | ObjectName(obj runtime.Object) (namespace, name string, err error) |
| 40 | // SetSelfLink sets the provided URL onto the object. The method should return nil if the object |
| 41 | // does not support selfLinks. |
| 42 | SetSelfLink(obj runtime.Object, url string) error |
| 43 | // GenerateLink creates an encoded URI for a given runtime object that represents the canonical path |
| 44 | // and query. |
| 45 | GenerateLink(requestInfo *request.RequestInfo, obj runtime.Object) (uri string, err error) |
| 46 | // GenerateListLink creates an encoded URI for a list that represents the canonical path and query. |
| 47 | GenerateListLink(req *http.Request) (uri string, err error) |
| 48 | } |
| 49 | |
| 50 | type ContextBasedNaming struct { |
| 51 | SelfLinker runtime.SelfLinker |
| 52 | ClusterScoped bool |
| 53 | |
| 54 | SelfLinkPathPrefix string |
| 55 | SelfLinkPathSuffix string |
| 56 | } |
| 57 | |
| 58 | // ContextBasedNaming implements ScopeNamer |
| 59 | var _ ScopeNamer = ContextBasedNaming{} |
| 60 | |
| 61 | func (n ContextBasedNaming) SetSelfLink(obj runtime.Object, url string) error { |
| 62 | return n.SelfLinker.SetSelfLink(obj, url) |
| 63 | } |
| 64 | |
| 65 | func (n ContextBasedNaming) Namespace(req *http.Request) (namespace string, err error) { |
| 66 | requestInfo, ok := request.RequestInfoFrom(req.Context()) |
| 67 | if !ok { |
| 68 | return "", fmt.Errorf("missing requestInfo") |
| 69 | } |
| 70 | return requestInfo.Namespace, nil |
| 71 | } |
| 72 | |
| 73 | func (n ContextBasedNaming) Name(req *http.Request) (namespace, name string, err error) { |
| 74 | requestInfo, ok := request.RequestInfoFrom(req.Context()) |
| 75 | if !ok { |
| 76 | return "", "", fmt.Errorf("missing requestInfo") |
| 77 | } |
| 78 | ns, err := n.Namespace(req) |
| 79 | if err != nil { |
| 80 | return "", "", err |
| 81 | } |
| 82 | |
| 83 | if len(requestInfo.Name) == 0 { |
| 84 | return "", "", errEmptyName |
| 85 | } |
| 86 | return ns, requestInfo.Name, nil |
| 87 | } |
| 88 | |
| 89 | func (n ContextBasedNaming) GenerateLink(requestInfo *request.RequestInfo, obj runtime.Object) (uri string, err error) { |
| 90 | namespace, name, err := n.ObjectName(obj) |
| 91 | if err == errEmptyName && len(requestInfo.Name) > 0 { |
| 92 | name = requestInfo.Name |
| 93 | } else if err != nil { |
| 94 | return "", err |
| 95 | } |
| 96 | if len(namespace) == 0 && len(requestInfo.Namespace) > 0 { |
| 97 | namespace = requestInfo.Namespace |
| 98 | } |
| 99 | |
| 100 | if n.ClusterScoped { |
| 101 | return n.SelfLinkPathPrefix + url.QueryEscape(name) + n.SelfLinkPathSuffix, nil |
| 102 | } |
| 103 | |
| 104 | return n.SelfLinkPathPrefix + |
| 105 | url.QueryEscape(namespace) + |
| 106 | "/" + url.QueryEscape(requestInfo.Resource) + "/" + |
| 107 | url.QueryEscape(name) + |
| 108 | n.SelfLinkPathSuffix, |
| 109 | nil |
| 110 | } |
| 111 | |
| 112 | func (n ContextBasedNaming) GenerateListLink(req *http.Request) (uri string, err error) { |
| 113 | if len(req.URL.RawPath) > 0 { |
| 114 | return req.URL.RawPath, nil |
| 115 | } |
| 116 | return req.URL.EscapedPath(), nil |
| 117 | } |
| 118 | |
| 119 | func (n ContextBasedNaming) ObjectName(obj runtime.Object) (namespace, name string, err error) { |
| 120 | name, err = n.SelfLinker.Name(obj) |
| 121 | if err != nil { |
| 122 | return "", "", err |
| 123 | } |
| 124 | if len(name) == 0 { |
| 125 | return "", "", errEmptyName |
| 126 | } |
| 127 | namespace, err = n.SelfLinker.Namespace(obj) |
| 128 | if err != nil { |
| 129 | return "", "", err |
| 130 | } |
| 131 | return namespace, name, err |
| 132 | } |
| 133 | |
| 134 | // errEmptyName is returned when API requests do not fill the name section of the path. |
| 135 | var errEmptyName = errors.NewBadRequest("name must be provided") |