k8s.io/apiserver@v0.31.1/pkg/server/routes/index.go (about) 1 /* 2 Copyright 2014 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 routes 18 19 import ( 20 "net/http" 21 22 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 23 "k8s.io/apimachinery/pkg/util/sets" 24 "k8s.io/apiserver/pkg/endpoints/handlers/responsewriters" 25 "k8s.io/apiserver/pkg/server/mux" 26 ) 27 28 // ListedPathProvider is an interface for providing paths that should be reported at /. 29 type ListedPathProvider interface { 30 // ListedPaths is an alphabetically sorted list of paths to be reported at /. 31 ListedPaths() []string 32 } 33 34 // ListedPathProviders is a convenient way to combine multiple ListedPathProviders 35 type ListedPathProviders []ListedPathProvider 36 37 // ListedPaths unions and sorts the included paths. 38 func (p ListedPathProviders) ListedPaths() []string { 39 ret := sets.String{} 40 for _, provider := range p { 41 for _, path := range provider.ListedPaths() { 42 ret.Insert(path) 43 } 44 } 45 46 return ret.List() 47 } 48 49 // Index provides a webservice for the http root / listing all known paths. 50 type Index struct{} 51 52 // Install adds the Index webservice to the given mux. 53 func (i Index) Install(pathProvider ListedPathProvider, mux *mux.PathRecorderMux) { 54 handler := IndexLister{StatusCode: http.StatusOK, PathProvider: pathProvider} 55 56 mux.UnlistedHandle("/", handler) 57 mux.UnlistedHandle("/index.html", handler) 58 } 59 60 // IndexLister lists the available indexes with the status code provided 61 type IndexLister struct { 62 StatusCode int 63 PathProvider ListedPathProvider 64 } 65 66 // ServeHTTP serves the available paths. 67 func (i IndexLister) ServeHTTP(w http.ResponseWriter, r *http.Request) { 68 responsewriters.WriteRawJSON(i.StatusCode, metav1.RootPaths{Paths: i.PathProvider.ListedPaths()}, w) 69 }