...

Source file src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_discovery.go

Documentation: k8s.io/apiextensions-apiserver/pkg/apiserver

     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 apiserver
    18  
    19  import (
    20  	"net/http"
    21  	"strings"
    22  	"sync"
    23  
    24  	"k8s.io/apimachinery/pkg/runtime/schema"
    25  	"k8s.io/apiserver/pkg/endpoints/discovery"
    26  )
    27  
    28  type versionDiscoveryHandler struct {
    29  	// TODO, writing is infrequent, optimize this
    30  	discoveryLock sync.RWMutex
    31  	discovery     map[schema.GroupVersion]*discovery.APIVersionHandler
    32  
    33  	delegate http.Handler
    34  }
    35  
    36  func (r *versionDiscoveryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    37  	pathParts := splitPath(req.URL.Path)
    38  	// only match /apis/<group>/<version>
    39  	if len(pathParts) != 3 || pathParts[0] != "apis" {
    40  		r.delegate.ServeHTTP(w, req)
    41  		return
    42  	}
    43  	discovery, ok := r.getDiscovery(schema.GroupVersion{Group: pathParts[1], Version: pathParts[2]})
    44  	if !ok {
    45  		r.delegate.ServeHTTP(w, req)
    46  		return
    47  	}
    48  
    49  	discovery.ServeHTTP(w, req)
    50  }
    51  
    52  func (r *versionDiscoveryHandler) getDiscovery(gv schema.GroupVersion) (*discovery.APIVersionHandler, bool) {
    53  	r.discoveryLock.RLock()
    54  	defer r.discoveryLock.RUnlock()
    55  
    56  	ret, ok := r.discovery[gv]
    57  	return ret, ok
    58  }
    59  
    60  func (r *versionDiscoveryHandler) setDiscovery(gv schema.GroupVersion, discovery *discovery.APIVersionHandler) {
    61  	r.discoveryLock.Lock()
    62  	defer r.discoveryLock.Unlock()
    63  
    64  	r.discovery[gv] = discovery
    65  }
    66  
    67  func (r *versionDiscoveryHandler) unsetDiscovery(gv schema.GroupVersion) {
    68  	r.discoveryLock.Lock()
    69  	defer r.discoveryLock.Unlock()
    70  
    71  	delete(r.discovery, gv)
    72  }
    73  
    74  type groupDiscoveryHandler struct {
    75  	// TODO, writing is infrequent, optimize this
    76  	discoveryLock sync.RWMutex
    77  	discovery     map[string]*discovery.APIGroupHandler
    78  
    79  	delegate http.Handler
    80  }
    81  
    82  func (r *groupDiscoveryHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
    83  	pathParts := splitPath(req.URL.Path)
    84  	// only match /apis/<group>
    85  	if len(pathParts) != 2 || pathParts[0] != "apis" {
    86  		r.delegate.ServeHTTP(w, req)
    87  		return
    88  	}
    89  	discovery, ok := r.getDiscovery(pathParts[1])
    90  	if !ok {
    91  		r.delegate.ServeHTTP(w, req)
    92  		return
    93  	}
    94  
    95  	discovery.ServeHTTP(w, req)
    96  }
    97  
    98  func (r *groupDiscoveryHandler) getDiscovery(group string) (*discovery.APIGroupHandler, bool) {
    99  	r.discoveryLock.RLock()
   100  	defer r.discoveryLock.RUnlock()
   101  
   102  	ret, ok := r.discovery[group]
   103  	return ret, ok
   104  }
   105  
   106  func (r *groupDiscoveryHandler) setDiscovery(group string, discovery *discovery.APIGroupHandler) {
   107  	r.discoveryLock.Lock()
   108  	defer r.discoveryLock.Unlock()
   109  
   110  	r.discovery[group] = discovery
   111  }
   112  
   113  func (r *groupDiscoveryHandler) unsetDiscovery(group string) {
   114  	r.discoveryLock.Lock()
   115  	defer r.discoveryLock.Unlock()
   116  
   117  	delete(r.discovery, group)
   118  }
   119  
   120  // splitPath returns the segments for a URL path.
   121  func splitPath(path string) []string {
   122  	path = strings.Trim(path, "/")
   123  	if path == "" {
   124  		return []string{}
   125  	}
   126  	return strings.Split(path, "/")
   127  }
   128  

View as plain text