-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
330 lines (271 loc) · 9.52 KB
/
main.go
File metadata and controls
330 lines (271 loc) · 9.52 KB
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
330
package main
import (
"context"
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
"strings"
"time"
slicer "github.com/slicervm/sdk"
)
const setupUserdata = `#!/bin/bash
set -euo pipefail
exec > >(tee -a /var/log/slicer-k3s-userdata.log) 2>&1
echo "phase=userdata_start ts=$(date -Is)"
echo "phase=install_tools_start ts=$(date -Is)"
arkade get k3sup kubectl --path /usr/local/bin
chmod +x /usr/local/bin/*
echo "phase=install_tools_done ts=$(date -Is)"
if [ -x /usr/local/bin/k3sup ]; then
export PATH="/usr/local/bin:${PATH}"
fi
echo "phase=k3sup_install_start ts=$(date -Is)"
k3sup install --local
echo "phase=k3sup_install_done ts=$(date -Is)"
echo "phase=kubeconfig_start ts=$(date -Is)"
mkdir -p /home/ubuntu/.kube
cp kubeconfig /home/ubuntu/.kube/config
chown -R ubuntu:ubuntu /home/ubuntu/
echo "phase=kubeconfig_done ts=$(date -Is)"
echo "phase=k3sup_ready_start ts=$(date -Is)"
k3sup ready --kubeconfig ./kubeconfig --pause 500ms --attempts 120
echo "phase=userdata_done ts=$(date -Is)"
`
func main() {
totalStart := time.Now()
baseURL := resolveBaseURL() // Override via SLICER_URL if needed.
token := os.Getenv("SLICER_TOKEN")
hostGroup := envOrDefault("SLICER_HOST_GROUP", "vm")
tag := envOrDefault("K3S_TAG", fmt.Sprintf("example=k3s-%d", time.Now().Unix()))
if token == "" && !isUnixSocket(baseURL) {
fmt.Println("SLICER_TOKEN is required")
os.Exit(1)
}
client := slicer.NewSlicerClient(baseURL, token, "slicer-k3s-userdata/1.0", nil)
log.Printf("configured base_url=%s host_group=%s tag=%s", baseURL, hostGroup, tag)
infoCtx, infoCancel := context.WithTimeout(context.Background(), 10*time.Second)
defer infoCancel()
log.Printf("Resolving hostgroup")
hostGroup, err := resolveHostGroup(infoCtx, client, hostGroup)
if err != nil {
fmt.Printf("failed to resolve hostgroup from /info: %v\n", err)
fmt.Printf("using configured host group: %s\n", hostGroup)
} else {
log.Printf("resolved host_group=%s", hostGroup)
}
log.Printf("host_group=%s", hostGroup)
ctx, cancel := context.WithTimeout(context.Background(), 13*time.Minute)
defer cancel()
log.Printf("creating VM wait=userdata timeout=12m host_group=%s tag=%s cpus=2 ram_gb=4", hostGroup, tag)
createStart := time.Now()
node, err := client.CreateVMWithOptions(ctx, hostGroup, slicer.SlicerCreateNodeRequest{
CPUs: 2,
RamBytes: slicer.GiB(4),
Userdata: setupUserdata,
Tags: []string{tag},
}, slicer.SlicerCreateNodeOptions{
Wait: slicer.SlicerCreateNodeWaitUserdata,
Timeout: 12 * time.Minute,
})
if err != nil {
fmt.Printf("create VM failed: %v\n", err)
os.Exit(1)
}
log.Printf("created ready VM hostname=%s ip=%s elapsed=%s", node.Hostname, node.IP, time.Since(createStart).Round(time.Millisecond))
fmt.Printf("phase=create_userdata_ready_vm_ms elapsed=%d\n", time.Since(createStart).Milliseconds())
nodeIP, err := parseNodeIP(node.IP)
if err != nil {
fmt.Printf("warning: could not parse VM ip (%s): %v\n", node.IP, err)
}
fmt.Printf("created ready VM: hostname=%s ip=%s tag=%s\n", node.Hostname, node.IP, tag)
execCtx, execCancel := context.WithTimeout(context.Background(), 12*time.Minute)
defer execCancel()
kubeStart := time.Now()
log.Printf("verifying k3s after server-side userdata wait timeout=12m hostname=%s", node.Hostname)
out, err := waitForKubectlNodes(execCtx, client, hostGroup, node.Hostname, tag, 1000)
if err != nil {
fmt.Printf("kubectl check failed: %v\n", err)
printVMLogs(execCtx, client, node.Hostname, 80)
os.Exit(1)
}
log.Printf("kubectl is ready elapsed=%s", time.Since(kubeStart).Round(time.Millisecond))
fmt.Printf("phase=kubectl_get_nodes_ms elapsed=%d\n", time.Since(kubeStart).Milliseconds())
fmt.Printf("kubectl get nodes output:\n%s\n", strings.TrimSpace(out))
if nodeIP == "" {
nodeIP = envOrDefault("SLICER_NODE_IP", "")
}
if nodeIP == "" {
fmt.Printf("warning: no IP available for kubeconfig rewrite\n")
} else {
kubeCopyStart := time.Now()
log.Printf("copying kubeconfig hostname=%s node_ip=%s", node.Hostname, nodeIP)
localConfig, err := copyAndRewriteKubeconfig(execCtx, client, node.Hostname, nodeIP)
if err != nil {
fmt.Printf("kubeconfig copy failed: %v\n", err)
printVMLogs(execCtx, client, node.Hostname, 80)
os.Exit(1)
}
log.Printf("copied kubeconfig path=%s elapsed=%s", localConfig, time.Since(kubeCopyStart).Round(time.Millisecond))
fmt.Printf("phase=copy_kubeconfig_ms elapsed=%d\n", time.Since(kubeCopyStart).Milliseconds())
fmt.Printf("kubeconfig saved and updated for direct use: %s\n", localConfig)
fmt.Printf("try it now:\n")
fmt.Printf("KUBECONFIG=%s kubectl get nodes\n", localConfig)
}
fmt.Printf("phase=total_ms elapsed=%d\n", time.Since(totalStart).Milliseconds())
}
func waitForKubectlNodes(ctx context.Context, client *slicer.SlicerClient, hostGroup, nodeName, tag string, uid uint32) (string, error) {
retryDelay := 1 * time.Second
notFoundAttempts := 0
for attempt := 1; ; attempt++ {
if ctx.Err() != nil {
return "", ctx.Err()
}
out, err := runKubectlNodes(ctx, client, nodeName, uid)
if err == nil {
return out, nil
}
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return out, err
}
if isNotFound(err) {
notFoundAttempts++
if notFoundAttempts >= 3 {
nodes, listErr := client.GetHostGroupNodes(ctx, hostGroup, slicer.ListOptions{Tag: tag})
if listErr != nil {
return out, fmt.Errorf("guest endpoint returned 404 for %s and node lookup failed: %w", nodeName, listErr)
}
if len(nodes) == 0 {
return out, fmt.Errorf("guest endpoint returned 404 for %s and no nodes with tag %q remain in host group %s", nodeName, tag, hostGroup)
}
return out, fmt.Errorf("guest endpoint returned 404 for %s; nodes with tag %q still present: %s", nodeName, tag, formatNodeNames(nodes))
}
} else {
notFoundAttempts = 0
}
if attempt == 1 || attempt%10 == 0 {
log.Printf("kubectl not ready attempt=%d err=%v", attempt, err)
}
if attempt%30 == 0 {
printVMLogs(ctx, client, nodeName, 20)
}
select {
case <-time.After(retryDelay):
case <-ctx.Done():
return "", ctx.Err()
}
}
}
func isNotFound(err error) bool {
return strings.Contains(err.Error(), "404 Not Found")
}
func formatNodeNames(nodes []slicer.SlicerNode) string {
names := make([]string, 0, len(nodes))
for _, node := range nodes {
names = append(names, node.Hostname)
}
return strings.Join(names, ",")
}
func printVMLogs(ctx context.Context, client *slicer.SlicerClient, nodeName string, lines int) {
logs, err := client.GetVMLogs(ctx, nodeName, lines)
if err != nil {
log.Printf("unable to fetch VM logs hostname=%s err=%v", nodeName, err)
return
}
content := strings.TrimSpace(logs.Content)
if content == "" {
log.Printf("VM logs are empty hostname=%s", nodeName)
return
}
log.Printf("last %d VM log lines for %s:\n%s", lines, nodeName, content)
}
func runKubectlNodes(ctx context.Context, client *slicer.SlicerClient, nodeName string, uid uint32) (string, error) {
cmd := client.CommandContext(ctx, nodeName, "kubectl", "get", "nodes")
cmd.UID = uid
cmd.GID = uid
stdout, err := cmd.Output()
if err != nil {
if exitErr := new(slicer.ExitError); errors.As(err, &exitErr) && len(exitErr.Stderr) > 0 {
return string(stdout) + string(exitErr.Stderr), err
}
return string(stdout), err
}
text := string(stdout)
if strings.TrimSpace(text) == "" {
return "", fmt.Errorf("no output from kubectl command")
}
return text, nil
}
func copyAndRewriteKubeconfig(ctx context.Context, client *slicer.SlicerClient, nodeName, nodeIP string) (string, error) {
if strings.TrimSpace(nodeName) == "" {
return "", fmt.Errorf("empty node name")
}
if strings.TrimSpace(nodeIP) == "" {
return "", fmt.Errorf("empty node ip")
}
localFile := "./kubeconfig-" + nodeName + ".yaml"
if err := client.CpFromVM(ctx, nodeName, "/home/ubuntu/.kube/config", localFile, "", "binary"); err != nil {
return "", fmt.Errorf("cp kubeconfig from VM failed: %w", err)
}
raw, err := os.ReadFile(localFile)
if err != nil {
return "", fmt.Errorf("read local kubeconfig failed: %w", err)
}
rewritten := strings.ReplaceAll(string(raw), "127.0.0.1", nodeIP)
rewritten = strings.ReplaceAll(rewritten, "localhost", nodeIP)
if err := os.WriteFile(localFile, []byte(rewritten), 0o600); err != nil {
return "", fmt.Errorf("write updated kubeconfig failed: %w", err)
}
return localFile, nil
}
func parseNodeIP(raw string) (string, error) {
trimmed := strings.TrimSpace(raw)
if trimmed == "" {
return "", fmt.Errorf("empty ip")
}
if strings.Contains(trimmed, "/") {
ip, _, err := net.ParseCIDR(trimmed)
if err != nil {
return "", err
}
return ip.String(), nil
}
ip := net.ParseIP(trimmed)
if ip == nil {
return "", fmt.Errorf("invalid ip")
}
return ip.String(), nil
}
func isUnixSocket(baseURL string) bool {
return strings.HasPrefix(baseURL, "/") || strings.HasPrefix(baseURL, "./")
}
func resolveBaseURL() string {
baseURL := envOrDefault("SLICER_URL", "~/slicer-mac/slicer.sock")
if strings.HasPrefix(baseURL, "~/") {
home, err := os.UserHomeDir()
if err != nil {
fmt.Printf("resolve SLICER_URL home directory: %v\n", err)
os.Exit(1)
}
baseURL = filepath.Join(home, baseURL[2:])
}
return baseURL
}
func resolveHostGroup(ctx context.Context, client *slicer.SlicerClient, configured string) (string, error) {
info, err := client.GetInfo(ctx)
if err != nil {
return configured, err
}
if strings.EqualFold(info.Platform, "darwin") {
return "sbox", nil
}
return configured, nil
}
func envOrDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}