From f4a3ae7d279fe66310942b58b827f97a62853958 Mon Sep 17 00:00:00 2001 From: Adrian Stobbe Date: Tue, 9 Jul 2024 09:27:32 +0200 Subject: [PATCH] ci: fix IDE setup on mac (#3226) --- bootstrapper/cmd/bootstrapper/BUILD.bazel | 1 + bootstrapper/cmd/bootstrapper/run.go | 28 ++++-------------- bootstrapper/internal/etcdio/BUILD.bazel | 16 ++++++++-- bootstrapper/internal/etcdio/etcdio.go | 4 +-- .../internal/etcdio/setioprio_cross.go | 17 +++++++++++ .../internal/etcdio/setioprio_linux.go | 19 ++++++++++++ bootstrapper/internal/reboot/BUILD.bazel | 11 +++++++ bootstrapper/internal/reboot/reboot_cross.go | 14 +++++++++ bootstrapper/internal/reboot/reboot_linux.go | 29 +++++++++++++++++++ 9 files changed, 112 insertions(+), 27 deletions(-) create mode 100644 bootstrapper/internal/etcdio/setioprio_cross.go create mode 100644 bootstrapper/internal/etcdio/setioprio_linux.go create mode 100644 bootstrapper/internal/reboot/BUILD.bazel create mode 100644 bootstrapper/internal/reboot/reboot_cross.go create mode 100644 bootstrapper/internal/reboot/reboot_linux.go diff --git a/bootstrapper/cmd/bootstrapper/BUILD.bazel b/bootstrapper/cmd/bootstrapper/BUILD.bazel index 6a8c61c50..77896efe7 100644 --- a/bootstrapper/cmd/bootstrapper/BUILD.bazel +++ b/bootstrapper/cmd/bootstrapper/BUILD.bazel @@ -21,6 +21,7 @@ go_library( "//bootstrapper/internal/kubernetes/k8sapi", "//bootstrapper/internal/kubernetes/kubewaiter", "//bootstrapper/internal/nodelock", + "//bootstrapper/internal/reboot", "//internal/atls", "//internal/attestation/choose", "//internal/attestation/initialize", diff --git a/bootstrapper/cmd/bootstrapper/run.go b/bootstrapper/cmd/bootstrapper/run.go index 95bd46b06..815d879ce 100644 --- a/bootstrapper/cmd/bootstrapper/run.go +++ b/bootstrapper/cmd/bootstrapper/run.go @@ -10,17 +10,15 @@ import ( "context" "fmt" "log/slog" - "log/syslog" "net" "sync" - "syscall" - "time" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/clean" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/diskencryption" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/initserver" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/joinclient" "github.com/edgelesssys/constellation/v2/bootstrapper/internal/nodelock" + "github.com/edgelesssys/constellation/v2/bootstrapper/internal/reboot" "github.com/edgelesssys/constellation/v2/internal/atls" "github.com/edgelesssys/constellation/v2/internal/attestation/initialize" "github.com/edgelesssys/constellation/v2/internal/attestation/vtpm" @@ -46,13 +44,13 @@ func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handl nodeBootstrapped, err := initialize.IsNodeBootstrapped(openDevice) if err != nil { log.With(slog.Any("error", err)).Error("Failed to check if node was previously bootstrapped") - reboot(fmt.Errorf("checking if node was previously bootstrapped: %w", err)) + reboot.Reboot(fmt.Errorf("checking if node was previously bootstrapped: %w", err)) } if nodeBootstrapped { if err := kube.StartKubelet(); err != nil { log.With(slog.Any("error", err)).Error("Failed to restart kubelet") - reboot(fmt.Errorf("restarting kubelet: %w", err)) + reboot.Reboot(fmt.Errorf("restarting kubelet: %w", err)) } return } @@ -61,7 +59,7 @@ func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handl initServer, err := initserver.New(context.Background(), nodeLock, kube, issuer, disk, fileHandler, metadata, log) if err != nil { log.With(slog.Any("error", err)).Error("Failed to create init server") - reboot(fmt.Errorf("creating init server: %w", err)) + reboot.Reboot(fmt.Errorf("creating init server: %w", err)) } dialer := dialer.New(issuer, nil, &net.Dialer{}) @@ -79,7 +77,7 @@ func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handl if err := joinClient.Start(cleaner); err != nil { log.With(slog.Any("error", err)).Error("Failed to join cluster") markDiskForReset(disk) - reboot(fmt.Errorf("joining cluster: %w", err)) + reboot.Reboot(fmt.Errorf("joining cluster: %w", err)) } }() @@ -89,7 +87,7 @@ func run(issuer atls.Issuer, openDevice vtpm.TPMOpenFunc, fileHandler file.Handl if err := initServer.Serve(bindIP, bindPort, cleaner); err != nil { log.With(slog.Any("error", err)).Error("Failed to serve init server") markDiskForReset(disk) - reboot(fmt.Errorf("serving init server: %w", err)) + reboot.Reboot(fmt.Errorf("serving init server: %w", err)) } }() wg.Wait() @@ -122,20 +120,6 @@ func markDiskForReset(disk *diskencryption.DiskEncryption) { _ = disk.MarkDiskForReset() } -// reboot writes an error message to the system log and reboots the system. -// We call this instead of os.Exit() since failures in the bootstrapper usually require a node reset. -func reboot(e error) { - syslogWriter, err := syslog.New(syslog.LOG_EMERG|syslog.LOG_KERN, "bootstrapper") - if err != nil { - _ = syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART) - } - _ = syslogWriter.Err(e.Error()) - _ = syslogWriter.Emerg("bootstrapper has encountered a non recoverable error. Rebooting...") - time.Sleep(time.Minute) // sleep to allow the message to be written to syslog and seen by the user - - _ = syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART) -} - type clusterInitJoiner interface { joinclient.ClusterJoiner initserver.ClusterInitializer diff --git a/bootstrapper/internal/etcdio/BUILD.bazel b/bootstrapper/internal/etcdio/BUILD.bazel index b7725d106..7f33bd901 100644 --- a/bootstrapper/internal/etcdio/BUILD.bazel +++ b/bootstrapper/internal/etcdio/BUILD.bazel @@ -2,8 +2,20 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library") go_library( name = "etcdio", - srcs = ["etcdio.go"], + srcs = [ + "etcdio.go", + "setioprio_cross.go", + "setioprio_linux.go", + ], importpath = "github.com/edgelesssys/constellation/v2/bootstrapper/internal/etcdio", visibility = ["//bootstrapper:__subpackages__"], - deps = ["@org_golang_x_sys//unix"], + deps = select({ + "@io_bazel_rules_go//go/platform:android": [ + "@org_golang_x_sys//unix", + ], + "@io_bazel_rules_go//go/platform:linux": [ + "@org_golang_x_sys//unix", + ], + "//conditions:default": [], + }), ) diff --git a/bootstrapper/internal/etcdio/etcdio.go b/bootstrapper/internal/etcdio/etcdio.go index e6c967225..f9caf7dbb 100644 --- a/bootstrapper/internal/etcdio/etcdio.go +++ b/bootstrapper/internal/etcdio/etcdio.go @@ -16,8 +16,6 @@ import ( "path" "strconv" "time" - - "golang.org/x/sys/unix" ) var ( @@ -97,7 +95,7 @@ func (c *Client) setIOPriority() error { prioVal := ((targetClass & ioPrioClassMask) << ioPrioClassShift) | (targetPrio & ioPrioPrioMask) // see https://man7.org/linux/man-pages/man2/ioprio_set.2.html - ret, _, errno := unix.Syscall(unix.SYS_IOPRIO_SET, ioPrioWhoProcess, uintptr(pid), uintptr(prioVal)) + ret, _, errno := setioprio(ioPrioWhoProcess, uintptr(pid), uintptr(prioVal)) if ret != 0 { return fmt.Errorf("setting I/O priority for etcd: %w", errno) } diff --git a/bootstrapper/internal/etcdio/setioprio_cross.go b/bootstrapper/internal/etcdio/setioprio_cross.go new file mode 100644 index 000000000..6422f0c60 --- /dev/null +++ b/bootstrapper/internal/etcdio/setioprio_cross.go @@ -0,0 +1,17 @@ +//go:build !linux + +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +package etcdio + +import ( + "syscall" +) + +func setioprio(_, _, _ uintptr) (uintptr, uintptr, syscall.Errno) { + panic("setioprio not implemented on non-Linux platforms") +} diff --git a/bootstrapper/internal/etcdio/setioprio_linux.go b/bootstrapper/internal/etcdio/setioprio_linux.go new file mode 100644 index 000000000..61d82248f --- /dev/null +++ b/bootstrapper/internal/etcdio/setioprio_linux.go @@ -0,0 +1,19 @@ +//go:build linux + +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +package etcdio + +import ( + "syscall" + + "golang.org/x/sys/unix" +) + +func setioprio(ioPrioWhoProcess, pid, prioVal uintptr) (uintptr, uintptr, syscall.Errno) { + return unix.Syscall(unix.SYS_IOPRIO_SET, ioPrioWhoProcess, pid, prioVal) +} diff --git a/bootstrapper/internal/reboot/BUILD.bazel b/bootstrapper/internal/reboot/BUILD.bazel new file mode 100644 index 000000000..ce71293b3 --- /dev/null +++ b/bootstrapper/internal/reboot/BUILD.bazel @@ -0,0 +1,11 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "reboot", + srcs = [ + "reboot_cross.go", + "reboot_linux.go", + ], + importpath = "github.com/edgelesssys/constellation/v2/bootstrapper/internal/reboot", + visibility = ["//bootstrapper:__subpackages__"], +) diff --git a/bootstrapper/internal/reboot/reboot_cross.go b/bootstrapper/internal/reboot/reboot_cross.go new file mode 100644 index 000000000..708749461 --- /dev/null +++ b/bootstrapper/internal/reboot/reboot_cross.go @@ -0,0 +1,14 @@ +//go:build !linux + +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +package reboot + +// Reboot is not implemented on non-Linux platforms. +func Reboot(_ error) { + panic("reboot not implemented on non-Linux platforms") +} diff --git a/bootstrapper/internal/reboot/reboot_linux.go b/bootstrapper/internal/reboot/reboot_linux.go new file mode 100644 index 000000000..c39d1cce9 --- /dev/null +++ b/bootstrapper/internal/reboot/reboot_linux.go @@ -0,0 +1,29 @@ +//go:build linux + +/* +Copyright (c) Edgeless Systems GmbH + +SPDX-License-Identifier: AGPL-3.0-only +*/ + +package reboot + +import ( + "log/syslog" + "syscall" + "time" +) + +// Reboot writes an error message to the system log and reboots the system. +// We call this instead of os.Exit() since failures in the bootstrapper usually require a node reset. +func Reboot(e error) { + syslogWriter, err := syslog.New(syslog.LOG_EMERG|syslog.LOG_KERN, "bootstrapper") + if err != nil { + _ = syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART) + } + _ = syslogWriter.Err(e.Error()) + _ = syslogWriter.Emerg("bootstrapper has encountered a non recoverable error. Rebooting...") + time.Sleep(time.Minute) // sleep to allow the message to be written to syslog and seen by the user + + _ = syscall.Reboot(syscall.LINUX_REBOOT_CMD_RESTART) +}