k8s.io/apiserver@v0.31.1/pkg/endpoints/handlers/namer.go (about) 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 23 "k8s.io/apimachinery/pkg/api/errors" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apiserver/pkg/endpoints/request" 26 ) 27 28 // ScopeNamer handles accessing names from requests and objects 29 type ScopeNamer interface { 30 // Namespace returns the appropriate namespace value from the request (may be empty) or an 31 // error. 32 Namespace(req *http.Request) (namespace string, err error) 33 // Name returns the name from the request, and an optional namespace value if this is a namespace 34 // scoped call. An error is returned if the name is not available. 35 Name(req *http.Request) (namespace, name string, err error) 36 // ObjectName returns the namespace and name from an object if they exist, or an error if the object 37 // does not support names. 38 ObjectName(obj runtime.Object) (namespace, name string, err error) 39 } 40 41 type ContextBasedNaming struct { 42 Namer runtime.Namer 43 ClusterScoped bool 44 } 45 46 // ContextBasedNaming implements ScopeNamer 47 var _ ScopeNamer = ContextBasedNaming{} 48 49 func (n ContextBasedNaming) Namespace(req *http.Request) (namespace string, err error) { 50 requestInfo, ok := request.RequestInfoFrom(req.Context()) 51 if !ok { 52 return "", fmt.Errorf("missing requestInfo") 53 } 54 return requestInfo.Namespace, nil 55 } 56 57 func (n ContextBasedNaming) Name(req *http.Request) (namespace, name string, err error) { 58 requestInfo, ok := request.RequestInfoFrom(req.Context()) 59 if !ok { 60 return "", "", fmt.Errorf("missing requestInfo") 61 } 62 63 if len(requestInfo.Name) == 0 { 64 return "", "", errEmptyName 65 } 66 return requestInfo.Namespace, requestInfo.Name, nil 67 } 68 69 func (n ContextBasedNaming) ObjectName(obj runtime.Object) (namespace, name string, err error) { 70 name, err = n.Namer.Name(obj) 71 if err != nil { 72 return "", "", err 73 } 74 if len(name) == 0 { 75 return "", "", errEmptyName 76 } 77 namespace, err = n.Namer.Namespace(obj) 78 if err != nil { 79 return "", "", err 80 } 81 return namespace, name, err 82 } 83 84 // errEmptyName is returned when API requests do not fill the name section of the path. 85 var errEmptyName = errors.NewBadRequest("name must be provided")