installsupport.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ui
  14. import (
  15. "mime"
  16. "net/http"
  17. "k8s.io/kubernetes/pkg/ui/data/swagger"
  18. assetfs "github.com/elazarl/go-bindata-assetfs"
  19. )
  20. const dashboardPath = "/api/v1/proxy/namespaces/kube-system/services/kubernetes-dashboard"
  21. type MuxInterface interface {
  22. Handle(pattern string, handler http.Handler)
  23. HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
  24. }
  25. func InstallSupport(mux MuxInterface, enableSwaggerSupport bool) {
  26. // Send correct mime type for .svg files. TODO: remove when
  27. // https://github.com/golang/go/commit/21e47d831bafb59f22b1ea8098f709677ec8ce33
  28. // makes it into all of our supported go versions.
  29. mime.AddExtensionType(".svg", "image/svg+xml")
  30. // Redirect /ui to the kube-ui proxy path
  31. prefix := "/ui/"
  32. mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
  33. http.Redirect(w, r, dashboardPath, http.StatusTemporaryRedirect)
  34. })
  35. if enableSwaggerSupport {
  36. // Expose files in third_party/swagger-ui/ on <host>/swagger-ui
  37. fileServer := http.FileServer(&assetfs.AssetFS{
  38. Asset: swagger.Asset,
  39. AssetDir: swagger.AssetDir,
  40. Prefix: "third_party/swagger-ui",
  41. })
  42. prefix = "/swagger-ui/"
  43. mux.Handle(prefix, http.StripPrefix(prefix, fileServer))
  44. }
  45. }