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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package nodeagent
import (
"cmp"
"fmt"
"net"
"path/filepath"
"regexp"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/kit/opt"
)
const (
DefaultNodeDir = "/etc/socialweb/node"
DefaultK3sDataDir = "/var/lib/rancher/k3s"
DefaultOpenBaoLinkAddress = "127.0.0.1:18200"
DefaultGuestLoginHelper = "/usr/local/bin/guest-login.sh"
DefaultK3sPath = "/usr/local/bin/k3s"
DefaultDumbpipePath = "/usr/local/bin/dumbpipe"
DefaultMeshdPath = "/usr/local/bin/socialweb-meshd"
DefaultNodeBinaryPath = "/usr/local/bin/socialweb-node"
DefaultCNIConfDir = "/etc/cni/net.d"
DefaultCNIBinDir = "/opt/cni/bin"
DefaultK3sNodePasswordPath = "/etc/rancher/node/password"
DefaultDoneStamp = "/var/lib/socialweb-node-secrets.done"
DefaultHostnamePath = "/etc/hostname"
DefaultLinkWaitAttempts = 120
DefaultLinkWaitInterval = 500 * time.Millisecond
DefaultCNIWaitAttempts = 90
DefaultCNIWaitInterval = 2 * time.Second
DefaultDiagnoseAttempts = 6
DefaultDiagnoseInterval = 20 * time.Second
DefaultGuestLoginWaitAttempts = 60
DefaultGuestLoginWaitInterval = 2 * time.Second
DefaultCNIRetryInterval = 2 * time.Second
DefaultUnitDir = "/etc/systemd/system"
PodClusterCIDR = "10.42.0.0/16"
PodMTU = 1160
PodBridge = "cni0"
PodEgressComment = "socialweb-pod-egress"
CNIConflistName = "10-socialweb-bridge.conflist"
NodeLocalDNSAddress = "169.254.20.10"
NodeLocalDNSLink = "swdns0"
)
var (
sha256Pattern = regexp.MustCompile(`^[0-9a-f]{64}$`)
originPattern = regexp.MustCompile(`^https?://[A-Za-z0-9.-]+(:[0-9]{1,5})?$`)
safeNamePattern = regexp.MustCompile(`^[a-z0-9]([a-z0-9.-]*[a-z0-9])?$`)
ipv4AddressPattern = regexp.MustCompile(`^([0-9]{1,3}\.){3}[0-9]{1,3}$`)
portPattern = regexp.MustCompile(`^[0-9]{1,5}$`)
ticketPattern = regexp.MustCompile(`^[a-z0-9]+$`)
)
type MeshdPin struct {
DownloadBase string `json:"downloadBase"`
Sha256 map[string]string `json:"sha256"`
}
type NodeAgentPin struct {
DownloadBase string `json:"downloadBase"`
Sha256 map[string]string `json:"sha256"`
}
func ValidateMeshdPin(pin *MeshdPin) error {
if pin == nil {
return nil
}
return ValidatePin("socialweb-meshd", pin.DownloadBase, pin.Sha256)
}
func ValidateNodeAgentPin(pin *NodeAgentPin) error {
if pin == nil {
return fmt.Errorf("nodeagent: the socialweb-node pin is missing")
}
return ValidatePin("socialweb-node", pin.DownloadBase, pin.Sha256)
}
func ValidatePin(kind, downloadBase string, sha map[string]string) error {
if !originPattern.MatchString(downloadBase) {
return fmt.Errorf("nodeagent: %s downloadBase %q is not an http(s) origin with no path", kind, downloadBase)
}
for _, arch := range []string{"x86_64", "aarch64"} {
if !sha256Pattern.MatchString(sha[arch]) {
return fmt.Errorf("nodeagent: %s sha256 for %s is not 64 lowercase hex characters", kind, arch)
}
}
return nil
}
type Config struct {
ClaimDocPath string `json:"claimDocPath"`
ClaimCAPath string `json:"claimCAPath"`
OpenBaoLinkAddress string `json:"openBaoLinkAddress"`
OpenBaoTicketPath string `json:"openBaoTicketPath"`
GuestLoginHelperPath string `json:"guestLoginHelperPath"`
K3sDataDir string `json:"k3sDataDir"`
NodeDir string `json:"nodeDir"`
CNIConfDir string `json:"cniConfDir"`
CNIBinDir string `json:"cniBinDir"`
KubeletKubeconfig string `json:"kubeletKubeconfig"`
APIServerEnvPath string `json:"apiserverEnvPath"`
TokenPath string `json:"tokenPath"`
NodePasswordPath string `json:"nodePasswordPath"`
ServerCAPath string `json:"serverCAPath"`
K3sNodePasswordPath string `json:"k3sNodePasswordPath"`
MeshdEnvPath string `json:"meshdEnvPath"`
DoneStamp string `json:"doneStamp"`
K3sPath string `json:"k3sPath"`
DumbpipePath string `json:"dumbpipePath"`
MeshdPath string `json:"meshdPath"`
HostnamePath string `json:"hostnamePath"`
NodeBinaryPath string `json:"nodeBinaryPath"`
ConfigPath string `json:"configPath"`
LinkWaitAttempts int `json:"linkWaitAttempts"`
LinkWaitIntervalMS int `json:"linkWaitIntervalMs"`
CNIWaitAttempts int `json:"cniWaitAttempts"`
CNIWaitIntervalMS int `json:"cniWaitIntervalMs"`
DiagnoseAttempts int `json:"diagnoseAttempts"`
DiagnoseIntervalSeconds int `json:"diagnoseIntervalSeconds"`
GuestLoginWaitAttempts int `json:"guestLoginWaitAttempts"`
GuestLoginWaitIntervalSeconds int `json:"guestLoginWaitIntervalSeconds"`
Meshd *MeshdPin `json:"meshd,omitempty"`
}
func (c Config) WithDefaults() Config {
c.NodeDir = cmp.Or(c.NodeDir, DefaultNodeDir)
inDir := func(name string) string { return filepath.Join(c.NodeDir, name) }
if c.ClaimDocPath == "" {
c.ClaimDocPath = inDir("claim.json")
}
if c.ClaimCAPath == "" {
c.ClaimCAPath = inDir("openbao-ca.pem")
}
if c.OpenBaoTicketPath == "" {
c.OpenBaoTicketPath = inDir("openbao-ticket")
}
c.OpenBaoLinkAddress = cmp.Or(c.OpenBaoLinkAddress, DefaultOpenBaoLinkAddress)
c.GuestLoginHelperPath = cmp.Or(c.GuestLoginHelperPath, DefaultGuestLoginHelper)
c.K3sDataDir = cmp.Or(c.K3sDataDir, DefaultK3sDataDir)
c.CNIConfDir = cmp.Or(c.CNIConfDir, DefaultCNIConfDir)
c.CNIBinDir = cmp.Or(c.CNIBinDir, DefaultCNIBinDir)
if c.KubeletKubeconfig == "" {
c.KubeletKubeconfig = filepath.Join(c.K3sDataDir, "agent", "kubelet.kubeconfig")
}
if c.APIServerEnvPath == "" {
c.APIServerEnvPath = inDir("apiserver.env")
}
if c.TokenPath == "" {
c.TokenPath = inDir("token")
}
if c.NodePasswordPath == "" {
c.NodePasswordPath = inDir("node-password")
}
if c.ServerCAPath == "" {
c.ServerCAPath = inDir("server-ca.pem")
}
c.K3sNodePasswordPath = cmp.Or(c.K3sNodePasswordPath, DefaultK3sNodePasswordPath)
if c.MeshdEnvPath == "" {
c.MeshdEnvPath = inDir("meshd.env")
}
c.DoneStamp = cmp.Or(c.DoneStamp, DefaultDoneStamp)
c.K3sPath = cmp.Or(c.K3sPath, DefaultK3sPath)
c.DumbpipePath = cmp.Or(c.DumbpipePath, DefaultDumbpipePath)
c.MeshdPath = cmp.Or(c.MeshdPath, DefaultMeshdPath)
c.HostnamePath = cmp.Or(c.HostnamePath, DefaultHostnamePath)
c.NodeBinaryPath = cmp.Or(c.NodeBinaryPath, DefaultNodeBinaryPath)
if c.ConfigPath == "" {
c.ConfigPath = inDir("join.json")
}
c.LinkWaitAttempts = opt.Positive(c.LinkWaitAttempts, DefaultLinkWaitAttempts)
if c.LinkWaitIntervalMS <= 0 {
c.LinkWaitIntervalMS = int(DefaultLinkWaitInterval / time.Millisecond)
}
c.CNIWaitAttempts = opt.Positive(c.CNIWaitAttempts, DefaultCNIWaitAttempts)
if c.CNIWaitIntervalMS <= 0 {
c.CNIWaitIntervalMS = int(DefaultCNIWaitInterval / time.Millisecond)
}
c.DiagnoseAttempts = opt.Positive(c.DiagnoseAttempts, DefaultDiagnoseAttempts)
if c.DiagnoseIntervalSeconds <= 0 {
c.DiagnoseIntervalSeconds = int(DefaultDiagnoseInterval / time.Second)
}
c.GuestLoginWaitAttempts = opt.Positive(c.GuestLoginWaitAttempts, DefaultGuestLoginWaitAttempts)
if c.GuestLoginWaitIntervalSeconds <= 0 {
c.GuestLoginWaitIntervalSeconds = int(DefaultGuestLoginWaitInterval / time.Second)
}
return c
}
func (c Config) Validate() error {
absolute := map[string]string{
"claimDocPath": c.ClaimDocPath, "claimCAPath": c.ClaimCAPath, "k3sDataDir": c.K3sDataDir,
"nodeDir": c.NodeDir, "cniConfDir": c.CNIConfDir, "cniBinDir": c.CNIBinDir,
"kubeletKubeconfig": c.KubeletKubeconfig, "apiserverEnvPath": c.APIServerEnvPath,
"tokenPath": c.TokenPath, "nodePasswordPath": c.NodePasswordPath, "serverCAPath": c.ServerCAPath,
"k3sNodePasswordPath": c.K3sNodePasswordPath, "meshdEnvPath": c.MeshdEnvPath,
"doneStamp": c.DoneStamp, "k3sPath": c.K3sPath, "dumbpipePath": c.DumbpipePath,
"meshdPath": c.MeshdPath, "hostnamePath": c.HostnamePath, "nodeBinaryPath": c.NodeBinaryPath,
"openBaoTicketPath": c.OpenBaoTicketPath, "configPath": c.ConfigPath,
}
for name, value := range absolute {
if value == "" || !filepath.IsAbs(value) {
return fmt.Errorf("nodeagent: %s %q is not an absolute path", name, value)
}
}
if host, port, err := net.SplitHostPort(c.OpenBaoLinkAddress); err != nil || !validIPv4(host) || !validPort(port) {
return fmt.Errorf("nodeagent: openBaoLinkAddress %q is not an IPv4 address and port", c.OpenBaoLinkAddress)
}
if err := ValidateMeshdPin(c.Meshd); err != nil {
return err
}
return nil
}
func (c Config) LinkWaitInterval() time.Duration {
return time.Duration(c.LinkWaitIntervalMS) * time.Millisecond
}
func (c Config) CNIWaitInterval() time.Duration {
return time.Duration(c.CNIWaitIntervalMS) * time.Millisecond
}
func (c Config) DiagnoseInterval() time.Duration {
return time.Duration(c.DiagnoseIntervalSeconds) * time.Second
}
func (c Config) GuestLoginWaitInterval() time.Duration {
return time.Duration(c.GuestLoginWaitIntervalSeconds) * time.Second
}
func (c Config) CNIPluginPath() string {
return filepath.Join(c.K3sDataDir, "data", "current", "bin", "cni")
}
func validName(name string) bool { return safeNamePattern.MatchString(name) }
func validIPv4(value string) bool { return ipv4AddressPattern.MatchString(value) }
func validPort(value string) bool { return portPattern.MatchString(value) }
func validTicket(value string) bool { return ticketPattern.MatchString(value) }
func valueString(value any) (string, bool) {
switch v := value.(type) {
case string:
if v == "" {
return "", false
}
return v, true
case float64:
return fmt.Sprintf("%.0f", v), true
case float32:
return fmt.Sprintf("%.0f", v), true
case int:
return fmt.Sprintf("%d", v), true
case int64:
return fmt.Sprintf("%d", v), true
default:
return "", false
}
}