-
Notifications
You must be signed in to change notification settings - Fork 64
/
ipxe_server.go
465 lines (375 loc) · 12.7 KB
/
ipxe_server.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package ipxe
import (
"bytes"
"context"
"errors"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"text/template"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
"github.com/talos-systems/go-procfs/procfs"
talosconstants "github.com/talos-systems/talos/pkg/machinery/constants"
infrav1 "github.com/talos-systems/sidero/app/caps-controller-manager/api/v1alpha3"
metalv1alpha1 "github.com/talos-systems/sidero/app/sidero-controller-manager/api/v1alpha1"
"github.com/talos-systems/sidero/app/sidero-controller-manager/internal/siderolink"
"github.com/talos-systems/sidero/app/sidero-controller-manager/pkg/constants"
)
var ErrBootFromDisk = errors.New("boot from disk")
// bootFile is used when iPXE is booted without embedded script via iPXE request http://endpoint:8081/boot.ipxe.
const bootFile = `#!ipxe
chain ipxe?uuid=${uuid}&mac=${mac:hexhyp}&domain=${domain}&hostname=${hostname}&serial=${serial}&arch=${buildarch}
`
// bootTemplate is embedded into iPXE binary when that binary is sent to the node.
//
// bootTemplate should be kept in sync with the bootFile above.
var bootTemplate = template.Must(template.New("iPXE embedded").Parse(`dhcp
chain http://{{ .Endpoint }}:{{ .Port }}/ipxe?uuid=${uuid}&mac=${mac:hexhyp}&domain=${domain}&hostname=${hostname}&serial=${serial}&arch=${buildarch}
`))
// ipxeTemplate is returned as response to `chain` request from the bootFile/bootTemplate to boot actual OS (or Sidero agent).
var ipxeTemplate = template.Must(template.New("iPXE config").Parse(`#!ipxe
kernel /env/{{ .Env.Name }}/{{ .KernelAsset }} {{range $arg := .Env.Spec.Kernel.Args}} {{$arg}}{{end}}
initrd /env/{{ .Env.Name }}/{{ .InitrdAsset }}
boot
`))
// ipxeBootFromDiskExit script is used to skip PXE booting and boot from disk via exit.
const ipxeBootFromDiskExit = `#!ipxe
exit
`
// ipxeBootFromDiskSanboot script is used to skip PXE booting and boot from disk via sanboot.
const ipxeBootFromDiskSanboot = `#!ipxe
sanboot --no-describe --drive 0x80
`
// BootFromDisk defines a way to boot from disk.
type BootFromDisk string
const (
BootIPXEExit BootFromDisk = "ipxe-exit" // Use iPXE script with `exit` command.
Boot404 BootFromDisk = "http-404" // Return HTTP 404 response to iPXE.
BootSANDisk BootFromDisk = "ipxe-sanboot" // Use iPXE script with `sanboot` command.
)
var (
apiEndpoint string
apiPort int
extraAgentKernelArgs string
defaultBootFromDiskMethod BootFromDisk
c client.Client
)
func bootFileHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, bootFile)
}
//nolint:unparam
func bootFromDiskHandler(method BootFromDisk, w http.ResponseWriter, r *http.Request) {
switch method { //nolint:exhaustive
case Boot404:
w.WriteHeader(http.StatusNotFound)
case BootSANDisk:
fmt.Fprint(w, ipxeBootFromDiskSanboot)
case BootIPXEExit:
fallthrough
default:
fmt.Fprint(w, ipxeBootFromDiskExit)
}
}
func ipxeHandler(w http.ResponseWriter, r *http.Request) {
var err error
labels := labelsFromRequest(r)
uuid := labels["uuid"]
var arch string
switch labels["arch"] {
case "arm64":
arch = "arm64"
default:
arch = "amd64"
}
server, serverBinding, err := lookupServer(uuid)
if err != nil {
log.Printf("Error looking up server: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
env, err := newEnvironment(server, serverBinding, arch)
if err != nil {
if errors.Is(err, ErrBootFromDisk) {
log.Printf("Server %q booting from disk", uuid)
bootFromDiskHandler(defaultBootFromDiskMethod, w, r)
return
}
if apierrors.IsNotFound(err) {
log.Printf("Environment not found: %v", err)
w.WriteHeader(http.StatusNotFound)
return
}
log.Printf("%v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if server != nil {
log.Printf("Using %q environment for %q", env.Name, server.Name)
} else {
log.Printf("Using %q environment", env.Name)
}
args := struct {
Env *metalv1alpha1.Environment
KernelAsset string
InitrdAsset string
}{
Env: env,
KernelAsset: constants.KernelAsset,
InitrdAsset: constants.InitrdAsset,
}
var buf bytes.Buffer
err = ipxeTemplate.Execute(&buf, args)
if err != nil {
log.Printf("error rendering template: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := buf.WriteTo(w); err != nil {
log.Printf("error writing to response: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if !strings.HasPrefix(env.ObjectMeta.Name, "agent") {
if err = markAsPXEBooted(server); err != nil {
log.Printf("error marking server as PXE booted: %s", err)
}
}
}
func RegisterIPXE(mux *http.ServeMux, endpoint string, port int, args string, bootMethod BootFromDisk, iPXEPort int, mgrClient client.Client) error {
apiEndpoint = endpoint
apiPort = port
extraAgentKernelArgs = args
defaultBootFromDiskMethod = bootMethod
c = mgrClient
var embeddedScriptBuf bytes.Buffer
if err := bootTemplate.Execute(&embeddedScriptBuf, map[string]string{
"Endpoint": apiEndpoint,
"Port": strconv.Itoa(iPXEPort),
}); err != nil {
return err
}
if err := PatchBinaries(embeddedScriptBuf.Bytes()); err != nil {
return err
}
mux.Handle("/boot.ipxe", logRequest(http.HandlerFunc(bootFileHandler)))
mux.Handle("/ipxe", logRequest(http.HandlerFunc(ipxeHandler)))
mux.Handle("/env/", logRequest(http.StripPrefix("/env/", http.FileServer(http.Dir("/var/lib/sidero/env")))))
mux.Handle("/tftp/", logRequest(http.StripPrefix("/tftp/", http.FileServer(http.Dir("/var/lib/sidero/tftp")))))
return nil
}
func logRequest(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
log.Printf("HTTP %s %v %s", r.Method, r.URL, r.RemoteAddr)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func labelsFromRequest(req *http.Request) map[string]string {
values := req.URL.Query()
labels := map[string]string{}
for key := range values {
switch strings.ToLower(key) {
case "mac":
// set mac if and only if it parses
if hw, err := parseMAC(values.Get(key)); err == nil {
labels[key] = hw.String()
}
default:
// matchers don't use multi-value keys, drop later values
labels[key] = values.Get(key)
}
}
return labels
}
func parseMAC(s string) (net.HardwareAddr, error) {
macAddr, err := net.ParseMAC(s)
if err != nil {
return nil, err
}
return macAddr, err
}
func lookupServer(uuid string) (*metalv1alpha1.Server, *infrav1.ServerBinding, error) {
key := client.ObjectKey{
Name: uuid,
}
s := &metalv1alpha1.Server{}
if err := c.Get(context.Background(), key, s); err != nil {
if apierrors.IsNotFound(err) {
return nil, nil, nil
}
return nil, nil, err
}
b := &infrav1.ServerBinding{}
if err := c.Get(context.Background(), key, b); err != nil {
if apierrors.IsNotFound(err) {
return s, nil, nil
}
return nil, nil, err
}
return s, b, nil
}
// newEnvironment handles which env CRD we'll respect for a given server.
// specied in the server spec overrides everything, specified in the server class overrides default, default is default :).
func newEnvironment(server *metalv1alpha1.Server, serverBinding *infrav1.ServerBinding, arch string) (env *metalv1alpha1.Environment, err error) {
// NB: The order of this switch statement is important. It defines the
// precedence of which environment to boot.
switch {
case server == nil:
return newAgentEnvironment(arch), nil
case serverBinding == nil:
return newAgentEnvironment(arch), nil
case conditions.Has(server, metalv1alpha1.ConditionPXEBooted) && !server.Spec.PXEBootAlways:
return nil, ErrBootFromDisk
case server.Spec.EnvironmentRef != nil:
env, err = newEnvironmentFromServer(server)
if err != nil {
return nil, err
}
case serverBinding.Spec.ServerClassRef != nil:
env, err = newEnvironmentFromServerClass(serverBinding)
if err != nil {
return nil, err
}
}
if env == nil {
env, err = newDefaultEnvironment()
if err != nil {
return nil, err
}
}
if env == nil {
return nil, fmt.Errorf("could not find environment for %q", server.Name)
}
return env, nil
}
func newAgentEnvironment(arch string) *metalv1alpha1.Environment {
args := []string{
"console=tty0",
"console=ttyS0",
"ima_appraise=fix",
"ima_hash=sha512",
"ima_template=ima-ng",
"initrd=initramfs.xz",
"ip=dhcp",
"page_poison=1",
"panic=30",
"printk.devkmsg=on",
"pti=on",
"random.trust_cpu=on",
"slab_nomerge=",
"slub_debug=P",
fmt.Sprintf("%s=%s:%d", constants.AgentEndpointArg, apiEndpoint, apiPort),
}
cmdline := procfs.NewCmdline(strings.Join(args, " "))
extra := procfs.NewCmdline(extraAgentKernelArgs)
// override defaults with extra kernel agent params
for _, p := range extra.Parameters {
cmdline.Set(p.Key(), p)
}
env := &metalv1alpha1.Environment{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("agent-%s", arch),
},
Spec: metalv1alpha1.EnvironmentSpec{
Kernel: metalv1alpha1.Kernel{
Args: cmdline.Strings(),
},
},
}
return env
}
func newDefaultEnvironment() (env *metalv1alpha1.Environment, err error) {
env = &metalv1alpha1.Environment{}
if err := c.Get(context.Background(), types.NamespacedName{Namespace: "", Name: metalv1alpha1.EnvironmentDefault}, env); err != nil {
return nil, err
}
appendTalosArguments(env)
return env, nil
}
func newEnvironmentFromServer(server *metalv1alpha1.Server) (env *metalv1alpha1.Environment, err error) {
env = &metalv1alpha1.Environment{}
if err := c.Get(context.Background(), types.NamespacedName{Namespace: "", Name: server.Spec.EnvironmentRef.Name}, env); err != nil {
return nil, err
}
appendTalosArguments(env)
return env, nil
}
func newEnvironmentFromServerClass(serverBinding *infrav1.ServerBinding) (env *metalv1alpha1.Environment, err error) {
serverClassResource := &metalv1alpha1.ServerClass{}
if err := c.Get(context.Background(), types.NamespacedName{Namespace: serverBinding.Spec.ServerClassRef.Namespace, Name: serverBinding.Spec.ServerClassRef.Name}, serverClassResource); err != nil {
return nil, err
}
if serverClassResource.Spec.EnvironmentRef == nil {
return env, nil
}
env = &metalv1alpha1.Environment{}
if err := c.Get(context.Background(), types.NamespacedName{Namespace: "", Name: serverClassResource.Spec.EnvironmentRef.Name}, env); err != nil {
return nil, err
}
appendTalosArguments(env)
return env, nil
}
func appendTalosArguments(env *metalv1alpha1.Environment) {
args := env.Spec.Kernel.Args
talosConfigPrefix := talosconstants.KernelParamConfig + "="
sideroLinkPrefix := talosconstants.KernelParamSideroLink + "="
logDeliveryPrefix := talosconstants.KernelParamLoggingKernel + "="
eventsSinkPrefix := talosconstants.KernelParamEventsSink + "="
for _, prefix := range []string{
talosConfigPrefix,
sideroLinkPrefix,
logDeliveryPrefix,
eventsSinkPrefix,
} {
for _, arg := range args {
if strings.HasPrefix(arg, prefix) {
// Environment already has variable, skip it
return
}
}
switch prefix {
case talosConfigPrefix:
// patch environment with the link to the metadata server
env.Spec.Kernel.Args = append(env.Spec.Kernel.Args,
fmt.Sprintf("%s=http://%s:%d/configdata?uuid=", talosconstants.KernelParamConfig, apiEndpoint, apiPort),
)
case sideroLinkPrefix:
// patch environment with the SideroLink API
env.Spec.Kernel.Args = append(env.Spec.Kernel.Args,
fmt.Sprintf("%s=%s:%d", talosconstants.KernelParamSideroLink, apiEndpoint, apiPort),
)
case logDeliveryPrefix:
// patch environment with the log receiver endpoint
env.Spec.Kernel.Args = append(env.Spec.Kernel.Args,
fmt.Sprintf("%s=tcp://[%s]:%d", talosconstants.KernelParamLoggingKernel, siderolink.Cfg.ServerAddress.IP(), siderolink.LogReceiverPort),
)
case eventsSinkPrefix:
// patch environment with the events sink endpoint
env.Spec.Kernel.Args = append(env.Spec.Kernel.Args,
fmt.Sprintf("%s=[%s]:%d", talosconstants.KernelParamEventsSink, siderolink.Cfg.ServerAddress.IP(), siderolink.EventsSinkPort),
)
}
}
}
func markAsPXEBooted(server *metalv1alpha1.Server) error {
patchHelper, err := patch.NewHelper(server, c)
if err != nil {
return err
}
conditions.MarkTrue(server, metalv1alpha1.ConditionPXEBooted)
return patchHelper.Patch(context.Background(), server, patch.WithOwnedConditions{
Conditions: []clusterv1.ConditionType{metalv1alpha1.ConditionPXEBooted},
})
}