-
Notifications
You must be signed in to change notification settings - Fork 12
/
types.go
227 lines (175 loc) · 6.47 KB
/
types.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package agentapi
import (
"encoding/json"
"errors"
"io"
"net/url"
"strings"
"time"
"github.com/nats-io/jwt/v2"
"github.com/nats-io/nats.go"
)
// Executable Linkable Format execution provider
const NexExecutionProviderELF = "elf"
// V8 execution provider
const NexExecutionProviderV8 = "v8"
// OCI execution provider
const NexExecutionProviderOCI = "oci"
// Wasm execution provider
const NexExecutionProviderWasm = "wasm"
// Name of the internal, non-public bucket for sharing files between host and agent
const WorkloadCacheBucket = "NEXCACHE"
// DefaultRunloopSleepTimeoutMillis default number of milliseconds to sleep during execution runloops
const DefaultRunloopSleepTimeoutMillis = 25
// ExecutionProviderParams parameters for initializing a specific execution provider
type ExecutionProviderParams struct {
DeployRequest
TriggerSubjects []string `json:"trigger_subjects"`
// Fail channel receives bool upon command failing to start
Fail chan bool `json:"-"`
// Run channel receives bool upon command successfully starting
Run chan bool `json:"-"`
// Exit channel receives int exit code upon command exit
Exit chan int `json:"-"`
Stderr io.Writer `json:"-"`
Stdout io.Writer `json:"-"`
TmpFilename *string `json:"-"`
VmID string `json:"-"`
// NATS connection which be injected into the execution provider
NATSConn *nats.Conn `json:"-"`
}
// DeployRequest processed by the agent
type DeployRequest struct {
Argv []string `json:"argv,omitempty"`
DecodedClaims jwt.GenericClaims `json:"-"`
Description *string `json:"description"`
Environment map[string]string `json:"environment"`
Essential *bool `json:"essential,omitempty"`
Hash string `json:"hash,omitempty"`
Namespace *string `json:"namespace,omitempty"`
RetriedAt *time.Time `json:"retried_at,omitempty"`
RetryCount *uint `json:"retry_count,omitempty"`
TotalBytes int64 `json:"total_bytes,omitempty"`
TriggerSubjects []string `json:"trigger_subjects"`
WorkloadName *string `json:"workload_name,omitempty"`
WorkloadType *string `json:"workload_type,omitempty"`
Stderr io.Writer `json:"-"`
Stdout io.Writer `json:"-"`
TmpFilename *string `json:"-"`
EncryptedEnvironment *string `json:"-"`
JsDomain *string `json:"-"`
Location *url.URL `json:"-"`
SenderPublicKey *string `json:"-"`
TargetNode *string `json:"-"`
WorkloadJwt *string `json:"-"`
Errors []error `json:"errors,omitempty"`
}
func (request *DeployRequest) IsEssential() bool {
return request.Essential != nil && *request.Essential
}
// Returns true if the run request supports essential flag
func (request *DeployRequest) SupportsEssential() bool {
return strings.EqualFold(*request.WorkloadType, "elf") ||
strings.EqualFold(*request.WorkloadType, "oci")
}
// Returns true if the run request supports trigger subjects
func (request *DeployRequest) SupportsTriggerSubjects() bool {
return (strings.EqualFold(*request.WorkloadType, "v8") ||
strings.EqualFold(*request.WorkloadType, "wasm")) &&
len(request.TriggerSubjects) > 0
}
func (r *DeployRequest) Validate() error {
var err error
if r.Namespace == nil {
err = errors.Join(err, errors.New("namespace is required"))
}
if r.WorkloadName == nil {
err = errors.Join(err, errors.New("workload name is required"))
}
if r.Essential != nil && *r.Essential && !r.SupportsEssential() {
err = errors.Join(err, errors.New("essential flag is not supported for workload type"))
}
if r.Hash == "" { // FIXME--- this should probably be checked against *string
err = errors.Join(err, errors.New("hash is required"))
}
if r.TotalBytes == 0 { // FIXME--- this should probably be checked against *string
err = errors.Join(err, errors.New("total bytes is required"))
}
if r.WorkloadType == nil {
err = errors.Join(err, errors.New("workload type is required"))
} else if (strings.EqualFold(*r.WorkloadType, NexExecutionProviderV8) ||
strings.EqualFold(*r.WorkloadType, NexExecutionProviderWasm)) &&
len(r.TriggerSubjects) == 0 {
err = errors.Join(err, errors.New("at least one trigger subject is required for this workload type"))
}
return err
}
type DeployResponse struct {
Accepted bool `json:"accepted"`
Message *string `json:"message"`
}
type HandshakeRequest struct {
ID *string `json:"id"`
StartTime time.Time `json:"start_time"`
Message *string `json:"message,omitempty"`
}
type HandshakeResponse struct {
}
type HostServicesHTTPRequest struct {
Method string `json:"method"`
URL string `json:"url"`
Body *string `json:"body,omitempty"`
Headers *json.RawMessage `json:"headers,omitempty"`
// FIXME-- this is very poorly named currently...
//these params are parsed as an object and serialized as part of the query string
Params *json.RawMessage `json:"params,omitempty"`
}
type HostServicesHTTPResponse struct {
Status int `json:"status"`
Headers *json.RawMessage `json:"headers,omitempty"`
Body string `json:"body"`
Error *string `json:"error,omitempty"`
}
type HostServicesKeyValueResponse struct {
Revision int64 `json:"revision,omitempty"`
Success *bool `json:"success,omitempty"`
Errors []string `json:"errors,omitempty"`
}
type HostServicesObjectStoreResponse struct {
Errors []string `json:"errors,omitempty"`
Success bool `json:"success,omitempty"`
}
type HostServicesMessagingRequest struct {
Subject *string `json:"key"`
Payload *json.RawMessage `json:"payload,omitempty"`
}
type HostServicesMessagingResponse struct {
Errors []string `json:"errors,omitempty"`
Success bool `json:"success,omitempty"`
}
type MachineMetadata struct {
VmID *string `json:"vmid"`
NodeNatsHost *string `json:"node_nats_host"`
NodeNatsPort *int `json:"node_nats_port"`
Message *string `json:"message"`
Errors []error `json:"errors,omitempty"`
}
func (m *MachineMetadata) Validate() bool {
var err error
if m.VmID == nil {
err = errors.Join(err, errors.New("vm id is required"))
}
if m.NodeNatsHost == nil {
err = errors.Join(err, errors.New("node NATS host is required"))
}
if m.NodeNatsPort == nil {
err = errors.Join(err, errors.New("node NATS port is required"))
}
return err == nil
}
type LogEntry struct {
Source string `json:"source,omitempty"`
Level LogLevel `json:"level,omitempty"`
Text string `json:"text,omitempty"`
}
type LogLevel int32