1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
17 "git.arvados.org/arvados.git/lib/cloud"
18 "git.arvados.org/arvados.git/lib/dispatchcloud/container"
19 "git.arvados.org/arvados.git/lib/dispatchcloud/scheduler"
20 "git.arvados.org/arvados.git/lib/dispatchcloud/sshexecutor"
21 "git.arvados.org/arvados.git/lib/dispatchcloud/worker"
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 "git.arvados.org/arvados.git/sdk/go/auth"
24 "git.arvados.org/arvados.git/sdk/go/ctxlog"
25 "git.arvados.org/arvados.git/sdk/go/health"
26 "git.arvados.org/arvados.git/sdk/go/httpserver"
27 "github.com/julienschmidt/httprouter"
28 "github.com/prometheus/client_golang/prometheus"
29 "github.com/prometheus/client_golang/prometheus/promhttp"
30 "github.com/sirupsen/logrus"
31 "golang.org/x/crypto/ssh"
35 defaultPollInterval = time.Second
36 defaultStaleLockTimeout = time.Minute
42 Instances() []worker.InstanceView
43 SetIdleBehavior(cloud.InstanceID, worker.IdleBehavior) error
44 KillInstance(id cloud.InstanceID, reason string) error
48 type dispatcher struct {
49 Cluster *arvados.Cluster
50 Context context.Context
51 ArvClient *arvados.Client
53 Registry *prometheus.Registry
54 InstanceSetID cloud.InstanceSetID
56 logger logrus.FieldLogger
57 instanceSet cloud.InstanceSet
59 queue scheduler.ContainerQueue
60 httpHandler http.Handler
68 // Start starts the dispatcher. Start can be called multiple times
69 // with no ill effect.
70 func (disp *dispatcher) Start() {
71 disp.setupOnce.Do(disp.setup)
74 // ServeHTTP implements service.Handler.
75 func (disp *dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
77 disp.httpHandler.ServeHTTP(w, r)
80 // CheckHealth implements service.Handler.
81 func (disp *dispatcher) CheckHealth() error {
83 return disp.pool.CheckHealth()
86 // Done implements service.Handler.
87 func (disp *dispatcher) Done() <-chan struct{} {
91 // Stop dispatching containers and release resources. Typically used
93 func (disp *dispatcher) Close() {
96 case disp.stop <- struct{}{}:
102 // Make a worker.Executor for the given instance.
103 func (disp *dispatcher) newExecutor(inst cloud.Instance) worker.Executor {
104 exr := sshexecutor.New(inst)
105 exr.SetTargetPort(disp.Cluster.Containers.CloudVMs.SSHPort)
106 exr.SetSigners(disp.sshKey)
110 func (disp *dispatcher) typeChooser(ctr *arvados.Container) (arvados.InstanceType, error) {
111 return ChooseInstanceType(disp.Cluster, ctr)
114 func (disp *dispatcher) setup() {
119 func (disp *dispatcher) initialize() {
120 disp.logger = ctxlog.FromContext(disp.Context)
122 disp.ArvClient.AuthToken = disp.AuthToken
124 if disp.InstanceSetID == "" {
125 if strings.HasPrefix(disp.AuthToken, "v2/") {
126 disp.InstanceSetID = cloud.InstanceSetID(strings.Split(disp.AuthToken, "/")[1])
128 // Use some other string unique to this token
129 // that doesn't reveal the token itself.
130 disp.InstanceSetID = cloud.InstanceSetID(fmt.Sprintf("%x", md5.Sum([]byte(disp.AuthToken))))
133 disp.stop = make(chan struct{}, 1)
134 disp.stopped = make(chan struct{})
136 if key, err := ssh.ParsePrivateKey([]byte(disp.Cluster.Containers.DispatchPrivateKey)); err != nil {
137 disp.logger.Fatalf("error parsing configured Containers.DispatchPrivateKey: %s", err)
142 instanceSet, err := newInstanceSet(disp.Cluster, disp.InstanceSetID, disp.logger, disp.Registry)
144 disp.logger.Fatalf("error initializing driver: %s", err)
146 disp.instanceSet = instanceSet
147 disp.pool = worker.NewPool(disp.logger, disp.ArvClient, disp.Registry, disp.InstanceSetID, disp.instanceSet, disp.newExecutor, disp.sshKey.PublicKey(), disp.Cluster)
148 disp.queue = container.NewQueue(disp.logger, disp.Registry, disp.typeChooser, disp.ArvClient)
150 if disp.Cluster.ManagementToken == "" {
151 disp.httpHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
152 http.Error(w, "Management API authentication is not configured", http.StatusForbidden)
155 mux := httprouter.New()
156 mux.HandlerFunc("GET", "/arvados/v1/dispatch/containers", disp.apiContainers)
157 mux.HandlerFunc("POST", "/arvados/v1/dispatch/containers/kill", disp.apiContainerKill)
158 mux.HandlerFunc("GET", "/arvados/v1/dispatch/instances", disp.apiInstances)
159 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/hold", disp.apiInstanceHold)
160 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/drain", disp.apiInstanceDrain)
161 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/run", disp.apiInstanceRun)
162 mux.HandlerFunc("POST", "/arvados/v1/dispatch/instances/kill", disp.apiInstanceKill)
163 metricsH := promhttp.HandlerFor(disp.Registry, promhttp.HandlerOpts{
164 ErrorLog: disp.logger,
166 mux.Handler("GET", "/metrics", metricsH)
167 mux.Handler("GET", "/metrics.json", metricsH)
168 mux.Handler("GET", "/_health/:check", &health.Handler{
169 Token: disp.Cluster.ManagementToken,
171 Routes: health.Routes{"ping": disp.CheckHealth},
173 disp.httpHandler = auth.RequireLiteralToken(disp.Cluster.ManagementToken, mux)
177 func (disp *dispatcher) run() {
178 defer close(disp.stopped)
179 defer disp.instanceSet.Stop()
180 defer disp.pool.Stop()
182 staleLockTimeout := time.Duration(disp.Cluster.Containers.StaleLockTimeout)
183 if staleLockTimeout == 0 {
184 staleLockTimeout = defaultStaleLockTimeout
186 pollInterval := time.Duration(disp.Cluster.Containers.CloudVMs.PollInterval)
187 if pollInterval <= 0 {
188 pollInterval = defaultPollInterval
190 sched := scheduler.New(disp.Context, disp.queue, disp.pool, disp.Registry, staleLockTimeout, pollInterval)
197 // Management API: all active and queued containers.
198 func (disp *dispatcher) apiContainers(w http.ResponseWriter, r *http.Request) {
200 Items []container.QueueEnt `json:"items"`
202 qEntries, _ := disp.queue.Entries()
203 for _, ent := range qEntries {
204 resp.Items = append(resp.Items, ent)
206 json.NewEncoder(w).Encode(resp)
209 // Management API: all active instances (cloud VMs).
210 func (disp *dispatcher) apiInstances(w http.ResponseWriter, r *http.Request) {
212 Items []worker.InstanceView `json:"items"`
214 resp.Items = disp.pool.Instances()
215 json.NewEncoder(w).Encode(resp)
218 // Management API: set idle behavior to "hold" for specified instance.
219 func (disp *dispatcher) apiInstanceHold(w http.ResponseWriter, r *http.Request) {
220 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorHold)
223 // Management API: set idle behavior to "drain" for specified instance.
224 func (disp *dispatcher) apiInstanceDrain(w http.ResponseWriter, r *http.Request) {
225 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorDrain)
228 // Management API: set idle behavior to "run" for specified instance.
229 func (disp *dispatcher) apiInstanceRun(w http.ResponseWriter, r *http.Request) {
230 disp.apiInstanceIdleBehavior(w, r, worker.IdleBehaviorRun)
233 // Management API: shutdown/destroy specified instance now.
234 func (disp *dispatcher) apiInstanceKill(w http.ResponseWriter, r *http.Request) {
235 id := cloud.InstanceID(r.FormValue("instance_id"))
237 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
240 err := disp.pool.KillInstance(id, "via management API: "+r.FormValue("reason"))
242 httpserver.Error(w, err.Error(), http.StatusNotFound)
247 // Management API: send SIGTERM to specified container's crunch-run
249 func (disp *dispatcher) apiContainerKill(w http.ResponseWriter, r *http.Request) {
250 uuid := r.FormValue("container_uuid")
252 httpserver.Error(w, "container_uuid parameter not provided", http.StatusBadRequest)
255 if !disp.pool.KillContainer(uuid, "via management API: "+r.FormValue("reason")) {
256 httpserver.Error(w, "container not found", http.StatusNotFound)
261 func (disp *dispatcher) apiInstanceIdleBehavior(w http.ResponseWriter, r *http.Request, want worker.IdleBehavior) {
262 id := cloud.InstanceID(r.FormValue("instance_id"))
264 httpserver.Error(w, "instance_id parameter not provided", http.StatusBadRequest)
267 err := disp.pool.SetIdleBehavior(id, want)
269 httpserver.Error(w, err.Error(), http.StatusNotFound)