|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +//nolint:dupl |
| 6 | +package runtime |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "fmt" |
| 11 | + "os" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/cosi-project/runtime/pkg/controller" |
| 15 | + "github.com/cosi-project/runtime/pkg/safe" |
| 16 | + "go.uber.org/zap" |
| 17 | + |
| 18 | + machineruntime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime" |
| 19 | + "github.com/siderolabs/talos/pkg/machinery/resources/runtime" |
| 20 | +) |
| 21 | + |
| 22 | +// BootIDController presents /proc/sys/kernel/random/boot_id as a resource. |
| 23 | +type BootIDController struct { |
| 24 | + V1Alpha1Mode machineruntime.Mode |
| 25 | +} |
| 26 | + |
| 27 | +// Name implements controller.Controller interface. |
| 28 | +func (ctrl *BootIDController) Name() string { |
| 29 | + return "runtime.BootIDController" |
| 30 | +} |
| 31 | + |
| 32 | +// Inputs implements controller.Controller interface. |
| 33 | +func (ctrl *BootIDController) Inputs() []controller.Input { |
| 34 | + return nil |
| 35 | +} |
| 36 | + |
| 37 | +// Outputs implements controller.Controller interface. |
| 38 | +func (ctrl *BootIDController) Outputs() []controller.Output { |
| 39 | + return []controller.Output{ |
| 40 | + { |
| 41 | + Type: runtime.BootIDType, |
| 42 | + Kind: controller.OutputExclusive, |
| 43 | + }, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// Run implements controller.Controller interface. |
| 48 | +func (ctrl *BootIDController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error { |
| 49 | + if ctrl.V1Alpha1Mode.InContainer() { |
| 50 | + // no boot_id in containers |
| 51 | + return nil |
| 52 | + } |
| 53 | + |
| 54 | + select { |
| 55 | + case <-ctx.Done(): |
| 56 | + return nil |
| 57 | + case <-r.EventCh(): |
| 58 | + } |
| 59 | + |
| 60 | + contents, err := os.ReadFile("/proc/sys/kernel/random/boot_id") |
| 61 | + if err != nil { |
| 62 | + return fmt.Errorf("error reading boot_id: %w", err) |
| 63 | + } |
| 64 | + |
| 65 | + if err := safe.WriterModify( |
| 66 | + ctx, r, |
| 67 | + runtime.NewBootID(), |
| 68 | + func(res *runtime.BootID) error { |
| 69 | + res.TypedSpec().BootID = strings.TrimSpace(string(contents)) |
| 70 | + |
| 71 | + return nil |
| 72 | + }, |
| 73 | + ); err != nil { |
| 74 | + return fmt.Errorf("error updating BootID resource: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + return nil |
| 78 | +} |
0 commit comments