1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.curoverse.com/arvados.git/sdk/go/arvados"
18 "git.curoverse.com/arvados.git/sdk/go/health"
19 "git.curoverse.com/arvados.git/sdk/go/httpserver"
24 Cluster *arvados.Cluster
25 NodeProfile *arvados.NodeProfile
28 handlerStack http.Handler
30 secureClient *http.Client
31 insecureClient *http.Client
36 func (h *Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
37 h.setupOnce.Do(h.setup)
38 h.handlerStack.ServeHTTP(w, req)
41 func (h *Handler) CheckHealth() error {
42 h.setupOnce.Do(h.setup)
43 _, _, err := findRailsAPI(h.Cluster, h.NodeProfile)
47 func (h *Handler) setup() {
48 mux := http.NewServeMux()
49 mux.Handle("/_health/", &health.Handler{
50 Token: h.Cluster.ManagementToken,
53 hs := http.NotFoundHandler()
54 hs = prepend(hs, h.proxyRailsAPI)
55 hs = prepend(hs, h.proxyRemoteCluster)
59 sc := *arvados.DefaultSecureClient
60 sc.Timeout = time.Duration(h.Cluster.HTTPRequestTimeout)
63 ic := *arvados.InsecureHTTPClient
64 ic.Timeout = time.Duration(h.Cluster.HTTPRequestTimeout)
65 h.insecureClient = &ic
68 Name: "arvados-controller",
69 RequestTimeout: time.Duration(h.Cluster.HTTPRequestTimeout),
73 var errDBConnection = errors.New("database connection error")
75 func (h *Handler) db(req *http.Request) (*sql.DB, error) {
77 defer h.pgdbMtx.Unlock()
82 db, err := sql.Open("postgres", h.Cluster.PostgreSQL.Connection.String())
84 httpserver.Logger(req).WithError(err).Error("postgresql connect failed")
85 return nil, errDBConnection
87 if p := h.Cluster.PostgreSQL.ConnectionPool; p > 0 {
90 if err := db.Ping(); err != nil {
91 httpserver.Logger(req).WithError(err).Error("postgresql connect succeeded but ping failed")
92 return nil, errDBConnection
98 type middlewareFunc func(http.ResponseWriter, *http.Request, http.Handler)
100 func prepend(next http.Handler, middleware middlewareFunc) http.Handler {
101 return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
102 middleware(w, req, next)
106 func (h *Handler) proxyRailsAPI(w http.ResponseWriter, req *http.Request, next http.Handler) {
107 urlOut, insecure, err := findRailsAPI(h.Cluster, h.NodeProfile)
109 httpserver.Error(w, err.Error(), http.StatusInternalServerError)
113 Scheme: urlOut.Scheme,
116 RawPath: req.URL.RawPath,
117 RawQuery: req.URL.RawQuery,
119 client := h.secureClient
121 client = h.insecureClient
123 h.proxy.Do(w, req, urlOut, client)
126 // For now, findRailsAPI always uses the rails API running on this
128 func findRailsAPI(cluster *arvados.Cluster, np *arvados.NodeProfile) (*url.URL, bool, error) {
129 hostport := np.RailsAPI.Listen
130 if len(hostport) > 1 && hostport[0] == ':' && strings.TrimRight(hostport[1:], "0123456789") == "" {
131 // ":12345" => connect to indicated port on localhost
132 hostport = "localhost" + hostport
133 } else if _, _, err := net.SplitHostPort(hostport); err == nil {
134 // "[::1]:12345" => connect to indicated address & port
136 return nil, false, err
142 url, err := url.Parse(proto + "://" + hostport)
143 return url, np.RailsAPI.Insecure, err