blob: 16b4199c2b3353fb3d00965ee01abe34bc6d462b [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2017 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package handlers
18
19import (
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
30type 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
50type ContextBasedNaming struct {
51 SelfLinker runtime.SelfLinker
52 ClusterScoped bool
53
54 SelfLinkPathPrefix string
55 SelfLinkPathSuffix string
56}
57
58// ContextBasedNaming implements ScopeNamer
59var _ ScopeNamer = ContextBasedNaming{}
60
61func (n ContextBasedNaming) SetSelfLink(obj runtime.Object, url string) error {
62 return n.SelfLinker.SetSelfLink(obj, url)
63}
64
65func (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
73func (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
89func (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
112func (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
119func (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.
135var errEmptyName = errors.NewBadRequest("name must be provided")