-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.go
181 lines (157 loc) · 6.46 KB
/
config.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
package models
import (
"encoding/json"
"errors"
"net/netip"
"os"
"path/filepath"
"github.com/nats-io/nats-server/v2/server"
"github.com/splode/fname"
agentapi "github.com/synadia-io/nex/internal/agent-api"
)
const (
DefaultCNINetworkName = "fcnet"
DefaultCNIInterfaceName = "veth0"
DefaultCNISubnet = "192.168.127.0/24"
DefaultInternalNodeHost = "192.168.127.1"
DefaultInternalNodePort = 9222
DefaultNodeMemSizeMib = 256
DefaultNodeVcpuCount = 1
DefaultOtelExporterUrl = "127.0.0.1:14532"
DefaultAgentHandshakeTimeoutMillisecond = 5000
)
var (
// docker/OCI needs to be explicitly enabled in node configuration
DefaultWorkloadTypes = []string{"elf", "v8", "wasm"}
DefaultBinPath = append([]string{"/usr/local/bin"}, filepath.SplitList(os.Getenv("PATH"))...)
// check the default cni bin path first, otherwise look in the rest of the PATH
DefaultCNIBinPath = append([]string{"/opt/cni/bin"}, filepath.SplitList(os.Getenv("PATH"))...)
)
// Node configuration is used to configure the node process as well
// as the virtual machines it produces
type NodeConfiguration struct {
AgentHandshakeTimeoutMillisecond int `json:"agent_handshake_timeout_ms,omitempty"`
BinPath []string `json:"bin_path"`
CNI CNIDefinition `json:"cni"`
DefaultResourceDir string `json:"default_resource_dir"`
ForceDepInstall bool `json:"-"`
InternalNodeHost *string `json:"internal_node_host,omitempty"`
InternalNodePort *int `json:"internal_node_port"`
KernelFilepath string `json:"kernel_filepath"`
MachinePoolSize int `json:"machine_pool_size"`
MachineTemplate MachineTemplate `json:"machine_template"`
NoSandbox bool `json:"no_sandbox,omitempty"`
OtlpExporterUrl string `json:"otlp_exporter_url,omitempty"`
OtelMetrics bool `json:"otel_metrics"`
OtelMetricsPort int `json:"otel_metrics_port"`
OtelMetricsExporter string `json:"otel_metrics_exporter"`
OtelTraces bool `json:"otel_traces"`
OtelTracesExporter string `json:"otel_traces_exporter"`
PreserveNetwork bool `json:"preserve_network,omitempty"`
RateLimiters *Limiters `json:"rate_limiters,omitempty"`
RootFsFilepath string `json:"rootfs_filepath"`
Tags map[string]string `json:"tags,omitempty"`
ValidIssuers []string `json:"valid_issuers,omitempty"`
WorkloadTypes []string `json:"workload_types,omitempty"`
HostServicesConfiguration *HostServicesConfig `json:"host_services,omitempty"`
// Public NATS server options; when non-nil, a public "userland" NATS server is started during node init
PublicNATSServer *server.Options `json:"public_nats_server,omitempty"`
Errors []error `json:"errors,omitempty"`
}
type HostServicesConfig struct {
NatsUrl string `json:"nats_url"`
NatsUserJwt string `json:"nats_user_jwt"`
NatsUserSeed string `json:"nats_user_seed"`
Services map[string]ServiceConfig `json:"services"`
}
type ServiceConfig struct {
Enabled bool `json:"enabled"`
Configuration json.RawMessage `json:"config"`
}
func (c *NodeConfiguration) Validate() bool {
c.Errors = make([]error, 0)
if c.MachinePoolSize < 1 {
c.Errors = append(c.Errors, errors.New("machine pool size must be >= 1"))
}
if !c.NoSandbox {
if _, err := os.Stat(c.KernelFilepath); errors.Is(err, os.ErrNotExist) {
c.Errors = append(c.Errors, err)
}
if _, err := os.Stat(c.RootFsFilepath); errors.Is(err, os.ErrNotExist) {
c.Errors = append(c.Errors, err)
}
cniSubnet, err := netip.ParsePrefix(*c.CNI.Subnet)
if err != nil {
c.Errors = append(c.Errors, err)
}
internalNodeHost, err := netip.ParseAddr(*c.InternalNodeHost)
if err != nil {
c.Errors = append(c.Errors, err)
}
hostInSubnet := cniSubnet.Contains(internalNodeHost)
if !hostInSubnet {
c.Errors = append(c.Errors, errors.New("internal node host must be in the CNI subnet"))
}
}
return len(c.Errors) == 0
}
func DefaultNodeConfiguration() NodeConfiguration {
defaultNodePort := DefaultInternalNodePort
defaultVcpuCount := DefaultNodeVcpuCount
defaultMemSizeMib := DefaultNodeMemSizeMib
tags := make(map[string]string)
rng := fname.NewGenerator()
nodeName, err := rng.Generate()
if err == nil {
tags["node_name"] = nodeName
}
config := NodeConfiguration{
AgentHandshakeTimeoutMillisecond: DefaultAgentHandshakeTimeoutMillisecond,
BinPath: DefaultBinPath,
// CAUTION: This needs to be the IP of the node server's internal NATS --as visible to the agent.
// This is not necessarily the address on which the internal NATS server is actually listening inside the node.
InternalNodeHost: agentapi.StringOrNil(DefaultInternalNodeHost),
InternalNodePort: &defaultNodePort,
MachinePoolSize: 1,
MachineTemplate: MachineTemplate{
VcpuCount: &defaultVcpuCount,
MemSizeMib: &defaultMemSizeMib,
},
OtlpExporterUrl: DefaultOtelExporterUrl,
RateLimiters: nil,
Tags: tags,
WorkloadTypes: DefaultWorkloadTypes,
HostServicesConfiguration: &HostServicesConfig{
NatsUrl: "", // this will trigger logic to re-use the main connection
NatsUserJwt: "",
NatsUserSeed: "",
Services: map[string]ServiceConfig{
"http": {
Enabled: true,
Configuration: nil,
},
"kv": {
Enabled: true,
Configuration: nil,
},
"messaging": {
Enabled: true,
Configuration: nil,
},
"objectstore": {
Enabled: true,
Configuration: nil,
},
},
},
}
if !config.NoSandbox {
config.CNI = CNIDefinition{
BinPath: DefaultCNIBinPath,
NetworkName: agentapi.StringOrNil(DefaultCNINetworkName),
InterfaceName: agentapi.StringOrNil(DefaultCNIInterfaceName),
Subnet: agentapi.StringOrNil(DefaultCNISubnet),
}
}
return config
}