blob: 14075798867cce551613dda78a5d0c83f8c8a0b2 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2014 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 routes
18
19import (
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 /.
29type 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
35type ListedPathProviders []ListedPathProvider
36
37// ListedPaths unions and sorts the included paths.
38func (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.
50type Index struct{}
51
52// Install adds the Index webservice to the given mux.
53func (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
61type IndexLister struct {
62 StatusCode int
63 PathProvider ListedPathProvider
64}
65
66// ServeHTTP serves the available paths.
67func (i IndexLister) ServeHTTP(w http.ResponseWriter, r *http.Request) {
68 responsewriters.WriteRawJSON(i.StatusCode, metav1.RootPaths{Paths: i.PathProvider.ListedPaths()}, w)
69}