1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/sdk/go/arvados"
14 "github.com/prometheus/client_golang/prometheus"
15 "github.com/sirupsen/logrus"
16 "golang.org/x/crypto/ssh"
19 // A RateLimitError should be returned by an InstanceSet when the
20 // cloud service indicates it is rejecting all API calls for some time
22 type RateLimitError interface {
23 // Time before which the caller should expect requests to
25 EarliestRetry() time.Time
29 // A QuotaError should be returned by an InstanceSet when the cloud
30 // service indicates the account cannot create more VMs than already
32 type QuotaError interface {
33 // If true, don't create more instances until some existing
34 // instances are destroyed. If false, don't handle the error
40 // A CapacityError should be returned by an InstanceSet's Create
41 // method when the cloud service indicates it has insufficient
42 // capacity to create new instances -- i.e., we shouldn't retry right
44 type CapacityError interface {
45 // If true, wait before trying to create more instances.
46 IsCapacityError() bool
47 // If true, the condition is specific to the requested
48 // instance types. Wait before trying to create more
49 // instances of that same type.
50 IsInstanceTypeSpecific() bool
54 type SharedResourceTags map[string]string
55 type InstanceSetID string
56 type InstanceTags map[string]string
57 type InstanceID string
60 // An Executor executes commands on an ExecutorTarget.
61 type Executor interface {
62 // Update the set of private keys used to authenticate to
64 SetSigners(...ssh.Signer)
66 // Set the target used for subsequent command executions.
67 SetTarget(ExecutorTarget)
69 // Return the current target.
70 Target() ExecutorTarget
72 // Execute a shell command and return the resulting stdout and
73 // stderr. stdin can be nil.
74 Execute(cmd string, stdin io.Reader) (stdout, stderr []byte, err error)
77 var ErrNotImplemented = errors.New("not implemented")
79 // An ExecutorTarget is a remote command execution service.
80 type ExecutorTarget interface {
81 // SSH server hostname or IP address, or empty string if
82 // unknown while instance is booting.
85 // Remote username to send during SSH authentication.
88 // Return nil if the given public key matches the instance's
89 // SSH server key. If the provided Dialer is not nil,
90 // VerifyHostKey can use it to make outgoing network
91 // connections from the instance -- e.g., to use the cloud's
92 // "this instance's metadata" API.
94 // Return ErrNotImplemented if no verification mechanism is
96 VerifyHostKey(ssh.PublicKey, *ssh.Client) error
99 // Instance is implemented by the provider-specific instance types.
100 type Instance interface {
103 // ID returns the provider's instance ID. It must be stable
104 // for the life of the instance.
107 // String typically returns the cloud-provided instance ID.
110 // Cloud provider's "instance type" ID. Matches a ProviderType
111 // in the cluster's InstanceTypes configuration.
112 ProviderType() string
117 // Replace tags with the given tags
118 SetTags(InstanceTags) error
120 // Get recent price history, if available. The InstanceType is
121 // supplied as an argument so the driver implementation can
122 // account for AddedScratch cost without requesting the volume
123 // attachment information from the provider's API.
124 PriceHistory(arvados.InstanceType) []InstancePrice
126 // Shut down the node
130 // An InstanceSet manages a set of VM instances created by an elastic
131 // cloud provider like AWS, GCE, or Azure.
133 // All public methods of an InstanceSet, and all public methods of the
134 // instances it returns, are goroutine safe.
135 type InstanceSet interface {
136 // Create a new instance with the given type, image, and
137 // initial set of tags. If supported by the driver, add the
138 // provided public key to /root/.ssh/authorized_keys.
140 // The given InitCommand should be executed on the newly
141 // created instance. This is optional for a driver whose
142 // instances' VerifyHostKey() method never returns
143 // ErrNotImplemented. InitCommand will be under 1 KiB.
145 // The returned error should implement RateLimitError and
146 // QuotaError where applicable.
147 Create(arvados.InstanceType, ImageID, InstanceTags, InitCommand, ssh.PublicKey) (Instance, error)
149 // Return all instances, including ones that are booting or
150 // shutting down. Optionally, filter out nodes that don't have
151 // all of the given InstanceTags (the caller will ignore these
154 // An instance returned by successive calls to Instances() may
155 // -- but does not need to -- be represented by the same
156 // Instance object each time. Thus, the caller is responsible
157 // for de-duplicating the returned instances by comparing the
158 // InstanceIDs returned by the instances' ID() methods.
159 Instances(InstanceTags) ([]Instance, error)
161 // Stop any background tasks and release other resources.
165 type InstancePrice struct {
170 type InitCommand string
172 // A Driver returns an InstanceSet that uses the given InstanceSetID
173 // and driver-dependent configuration parameters.
175 // If the driver creates cloud resources that aren't attached to a
176 // single VM instance (like SSH key pairs on AWS) and support tagging,
177 // they should be tagged with the provided SharedResourceTags.
179 // The supplied id will be of the form "zzzzz-zzzzz-zzzzzzzzzzzzzzz"
180 // where each z can be any alphanum. The returned InstanceSet must use
181 // this id to tag long-lived cloud resources that it creates, and must
182 // assume control of any existing resources that are tagged with the
183 // same id. Tagging can be accomplished by including the ID in
184 // resource names, using the cloud provider's tagging feature, or any
185 // other mechanism. The tags must be visible to another instance of
186 // the same driver running on a different host.
188 // The returned InstanceSet must not modify or delete cloud resources
189 // unless they are tagged with the given InstanceSetID or the caller
190 // (dispatcher) calls Destroy() on them. It may log a summary of
191 // untagged resources once at startup, though. Thus, two identically
192 // configured InstanceSets running on different hosts with different
193 // ids should log about the existence of each other's resources at
194 // startup, but will not interfere with each other.
196 // The dispatcher always passes the InstanceSetID as a tag when
197 // calling Create() and Instances(), so the driver does not need to
198 // tag/filter VMs by InstanceSetID itself.
202 // type exampleInstanceSet struct {
207 // type exampleDriver struct {}
209 // func (*exampleDriver) InstanceSet(config json.RawMessage, id cloud.InstanceSetID, tags cloud.SharedResourceTags, logger logrus.FieldLogger, reg *prometheus.Registry) (cloud.InstanceSet, error) {
210 // var is exampleInstanceSet
211 // if err := json.Unmarshal(config, &is); err != nil {
217 type Driver interface {
218 InstanceSet(config json.RawMessage, id InstanceSetID, tags SharedResourceTags, logger logrus.FieldLogger, reg *prometheus.Registry) (InstanceSet, error)
221 // DriverFunc makes a Driver using the provided function as its
222 // InstanceSet method. This is similar to http.HandlerFunc.
223 func DriverFunc(fn func(config json.RawMessage, id InstanceSetID, tags SharedResourceTags, logger logrus.FieldLogger, reg *prometheus.Registry) (InstanceSet, error)) Driver {
224 return driverFunc(fn)
227 type driverFunc func(config json.RawMessage, id InstanceSetID, tags SharedResourceTags, logger logrus.FieldLogger, reg *prometheus.Registry) (InstanceSet, error)
229 func (df driverFunc) InstanceSet(config json.RawMessage, id InstanceSetID, tags SharedResourceTags, logger logrus.FieldLogger, reg *prometheus.Registry) (InstanceSet, error) {
230 return df(config, id, tags, logger, reg)