14807: Load API host/token from cluster config if present.
[arvados.git] / lib / dispatchcloud / dispatcher.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package dispatchcloud
6
7 import (
8         "context"
9         "crypto/md5"
10         "encoding/json"
11         "fmt"
12         "net/http"
13         "strings"
14         "sync"
15         "time"
16
17         "git.curoverse.com/arvados.git/lib/cloud"
18         "git.curoverse.com/arvados.git/lib/dispatchcloud/container"
19         "git.curoverse.com/arvados.git/lib/dispatchcloud/scheduler"
20         "git.curoverse.com/arvados.git/lib/dispatchcloud/ssh_executor"
21         "git.curoverse.com/arvados.git/lib/dispatchcloud/worker"
22         "git.curoverse.com/arvados.git/sdk/go/arvados"
23         "git.curoverse.com/arvados.git/sdk/go/auth"
24         "git.curoverse.com/arvados.git/sdk/go/ctxlog"
25         "git.curoverse.com/arvados.git/sdk/go/httpserver"
26         "github.com/julienschmidt/httprouter"
27         "github.com/prometheus/client_golang/prometheus"
28         "github.com/prometheus/client_golang/prometheus/promhttp"
29         "github.com/sirupsen/logrus"
30         "golang.org/x/crypto/ssh"
31 )
32
33 const (
34         defaultPollInterval     = time.Second
35         defaultStaleLockTimeout = time.Minute
36 )
37
38 type pool interface {
39         scheduler.WorkerPool
40         Instances() []worker.InstanceView
41         SetIdleBehavior(cloud.InstanceID, worker.IdleBehavior) error
42         KillInstance(id cloud.InstanceID, reason string) error
43         Stop()
44 }
45
46 type dispatcher struct {
47         Cluster       *arvados.Cluster
48         Context       context.Context
49         AuthToken     string
50         InstanceSetID cloud.InstanceSetID
51
52         logger      logrus.FieldLogger
53         reg         *prometheus.Registry
54         instanceSet cloud.InstanceSet
55         pool        pool
56         queue       scheduler.ContainerQueue
57         httpHandler http.Handler
58         sshKey      ssh.Signer
59
60         setupOnce sync.Once
61         stop      chan struct{}
62         stopped   chan struct{}
63 }
64
65 // Start starts the dispatcher. Start can be called multiple times
66 // with no ill effect.
67 func (disp *dispatcher) Start() {
68         disp.setupOnce.Do(disp.setup)
69 }
70
71 // ServeHTTP implements service.Handler.
72 func (disp *dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
73         disp.Start()
74         disp.httpHandler.ServeHTTP(w, r)
75 }
76
77 // CheckHealth implements service.Handler.
78 func (disp *dispatcher) CheckHealth() error {
79         disp.Start()
80         return nil
81 }
82
83 // Stop dispatching containers and release resources. Typically used
84 // in tests.
85 func (disp *dispatcher) Close() {
86         disp.Start()
87         select {
88         case disp.stop <- struct{}{}:
89         default:
90         }
91         <-disp.stopped
92 }
93
94 // Make a worker.Executor for the given instance.
95 func (disp *dispatcher) newExecutor(inst cloud.Instance) worker.Executor {
96         exr := ssh_executor.New(inst)
97         exr.SetTargetPort(disp.Cluster.CloudVMs.SSHPort)
98         exr.SetSigners(disp.sshKey)
99         return exr
100 }
101
102 func (disp *dispatcher) typeChooser(ctr *arvados.Container) (arvados.InstanceType, error) {
103         return ChooseInstanceType(disp.Cluster, ctr)
104 }
105
106 func (disp *dispatcher) setup() {
107         disp.initialize()
108         go disp.run()
109 }
110
111 func (disp *dispatcher) initialize() {
112         disp.logger = ctxlog.FromContext(disp.Context)
113
114         arvClient, err := arvados.NewClientFromConfig(disp.Cluster)
115         if err != nil {
116                 disp.logger.WithError(err).Warn("error initializing client from cluster config, falling back to ARVADOS_API_HOST(_INSECURE) environment variables")
117                 arvClient = arvados.NewClientFromEnv()
118         }
119         arvClient.AuthToken = disp.AuthToken
120
121         if disp.InstanceSetID == "" {
122                 if strings.HasPrefix(arvClient.AuthToken, "v2/") {
123                         disp.InstanceSetID = cloud.InstanceSetID(strings.Split(arvClient.AuthToken, "/")[1])
124                 } else {
125                         // Use some other string unique to this token
126                         // that doesn't reveal the token itself.
127                         disp.InstanceSetID = cloud.InstanceSetID(fmt.Sprintf("%x", md5.Sum([]byte(arvClient.AuthToken))))
128                 }
129         }
130         disp.stop = make(chan struct{}, 1)
131         disp.stopped = make(chan struct{})
132
133         if key, err := ssh.ParsePrivateKey([]byte(disp.Cluster.Dispatch.PrivateKey)); err != nil {
134                 disp.logger.Fatalf("error parsing configured Dispatch.PrivateKey: %s", err)
135         } else {
136                 disp.sshKey = key
137         }
138
139         instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID, disp.logger)
140         if err != nil {
141                 disp.logger.Fatalf("error initializing driver: %s", err)
142         }
143         disp.instanceSet = instanceSet
144         disp.reg = prometheus.NewRegistry()
145         disp.pool = worker.NewPool(disp.logger, arvClient, disp.reg, disp.instanceSet, disp.newExecutor, disp.sshKey.PublicKey(), disp.Cluster)
146         disp.queue = container.NewQueue(disp.logger, disp.reg, disp.typeChooser, arvClient)
147
148         if disp.Cluster.ManagementToken == "" {
149                 disp.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
150                         http.Error(w, "Management API authentication is not configured", http.StatusForbidden)
151                 })
152         } else {
153                 mux := httprouter.New()
154                 mux.HandlerFunc("GET", "/arvados/v1/dispatch/containers", disp.apiContainers)
155                 mux.HandlerFunc("GET", "/arvados/v1/dispatch/instances", disp.apiInstances)
156                 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/hold", disp.apiInstanceHold)
157                 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/drain", disp.apiInstanceDrain)
158                 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/run", disp.apiInstanceRun)
159                 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/kill", disp.apiInstanceKill)
160                 metricsH := promhttp.HandlerFor(disp.reg, promhttp.HandlerOpts{
161                         ErrorLog: disp.logger,
162                 })
163                 mux.Handler("GET", "/metrics", metricsH)
164                 mux.Handler("GET", "/metrics.json", metricsH)
165                 disp.httpHandler = auth.RequireLiteralToken(disp.Cluster.ManagementToken, mux)
166         }
167 }
168
169 func (disp *dispatcher) run() {
170         defer close(disp.stopped)
171         defer disp.instanceSet.Stop()
172         defer disp.pool.Stop()
173
174         staleLockTimeout := time.Duration(disp.Cluster.Dispatch.StaleLockTimeout)
175         if staleLockTimeout == 0 {
176                 staleLockTimeout = defaultStaleLockTimeout
177         }
178         pollInterval := time.Duration(disp.Cluster.Dispatch.PollInterval)
179         if pollInterval <= 0 {
180                 pollInterval = defaultPollInterval
181         }
182         sched := scheduler.New(disp.Context, disp.queue, disp.pool, staleLockTimeout, pollInterval)
183         sched.Start()
184         defer sched.Stop()
185
186         <-disp.stop
187 }
188
189 // Management API: all active and queued containers.
190 func (disp *dispatcher) apiContainers(w http.ResponseWriter, r *http.Request) {
191         var resp struct {
192                 Items []container.QueueEnt `json:"items"`
193         }
194         qEntries, _ := disp.queue.Entries()
195         for _, ent := range qEntries {
196                 resp.Items = append(resp.Items, ent)
197         }
198         json.NewEncoder(w).Encode(resp)
199 }
200
201 // Management API: all active instances (cloud VMs).
202 func (disp *dispatcher) apiInstances(w http.ResponseWriter, r *http.Request) {
203         var resp struct {
204                 Items []worker.InstanceView `json:"items"`
205         }
206         resp.Items = disp.pool.Instances()
207         json.NewEncoder(w).Encode(resp)
208 }
209
210 // Management API: set idle behavior to "hold" for specified instance.
211 func (disp *dispatcher) apiInstanceHold(w http.ResponseWriter, r *http.Request) {
212         disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorHold)
213 }
214
215 // Management API: set idle behavior to "drain" for specified instance.
216 func (disp *dispatcher) apiInstanceDrain(w http.ResponseWriter, r *http.Request) {
217         disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorDrain)
218 }
219
220 // Management API: set idle behavior to "run" for specified instance.
221 func (disp *dispatcher) apiInstanceRun(w http.ResponseWriter, r *http.Request) {
222         disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorRun)
223 }
224
225 // Management API: shutdown/destroy specified instance now.
226 func (disp *dispatcher) apiInstanceKill(w http.ResponseWriter, r *http.Request) {
227         id := cloud.InstanceID(r.FormValue("instance_id"))
228         if id == "" {
229                 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
230                 return
231         }
232         err := disp.pool.KillInstance(id, "via management API: "+r.FormValue("reason"))
233         if err != nil {
234                 httpserver.Error(w, err.Error(), http.StatusNotFound)
235                 return
236         }
237 }
238
239 func (disp *dispatcher) apiInstanceIdleBehavior(w http.ResponseWriter, r *http.Request, want worker.IdleBehavior) {
240         id := cloud.InstanceID(r.FormValue("instance_id"))
241         if id == "" {
242                 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
243                 return
244         }
245         err := disp.pool.SetIdleBehavior(id, want)
246         if err != nil {
247                 httpserver.Error(w, err.Error(), http.StatusNotFound)
248                 return
249         }
250 }