1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/arvados"
16 "git.curoverse.com/arvados.git/sdk/go/health"
17 "git.curoverse.com/arvados.git/sdk/go/httpserver"
21 Cluster *arvados.Cluster
22 NodeProfile *arvados.NodeProfile
25 handlerStack http.Handler
27 secureClient *http.Client
28 insecureClient *http.Client
31 func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
32 h.setupOnce.Do(h.setup)
33 h.handlerStack.ServeHTTP(w, req)
36 func (h *Handler) CheckHealth() error {
37 h.setupOnce.Do(h.setup)
38 _, _, err := findRailsAPI(h.Cluster, h.NodeProfile)
42 func (h *Handler) setup() {
43 mux := http.NewServeMux()
44 mux.Handle("/_health/", &health.Handler{
45 Token: h.Cluster.ManagementToken,
48 hs := http.NotFoundHandler()
49 hs = prepend(hs, h.proxyRailsAPI)
50 hs = prepend(hs, h.proxyRemoteCluster)
54 sc := *arvados.DefaultSecureClient
55 sc.Timeout = time.Duration(h.Cluster.HTTPRequestTimeout)
58 ic := *arvados.InsecureHTTPClient
59 ic.Timeout = time.Duration(h.Cluster.HTTPRequestTimeout)
60 h.insecureClient = &ic
63 Name: "arvados-controller",
64 RequestTimeout: time.Duration(h.Cluster.HTTPRequestTimeout),
68 type middlewareFunc func(http.ResponseWriter, *http.Request, http.Handler)
70 func prepend(next http.Handler, middleware middlewareFunc) http.Handler {
71 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
72 middleware(w, req, next)
76 func (h *Handler) proxyRailsAPI(w http.ResponseWriter, req *http.Request, next http.Handler) {
77 urlOut, insecure, err := findRailsAPI(h.Cluster, h.NodeProfile)
79 httpserver.Error(w, err.Error(), http.StatusInternalServerError)
83 Scheme: urlOut.Scheme,
86 RawPath: req.URL.RawPath,
87 RawQuery: req.URL.RawQuery,
89 client := h.secureClient
91 client = h.insecureClient
93 h.proxy.Do(w, req, urlOut, client)
96 // For now, findRailsAPI always uses the rails API running on this
98 func findRailsAPI(cluster *arvados.Cluster, np *arvados.NodeProfile) (*url.URL, bool, error) {
99 hostport := np.RailsAPI.Listen
100 if len(hostport) > 1 && hostport[0] == ':' && strings.TrimRight(hostport[1:], "0123456789") == "" {
101 // ":12345" => connect to indicated port on localhost
102 hostport = "localhost" + hostport
103 } else if _, _, err := net.SplitHostPort(hostport); err == nil {
104 // "[::1]:12345" => connect to indicated address & port
106 return nil, false, err
112 url, err := url.Parse(proto + "://" + hostport)
113 return url, np.RailsAPI.Insecure, err