-
Notifications
You must be signed in to change notification settings - Fork 458
/
machine_status_publisher.go
84 lines (71 loc) · 2.62 KB
/
machine_status_publisher.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
// 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 runtime
import (
"context"
"fmt"
"github.com/cosi-project/runtime/pkg/controller"
"github.com/cosi-project/runtime/pkg/safe"
"github.com/cosi-project/runtime/pkg/state"
"github.com/siderolabs/gen/optional"
"github.com/siderolabs/gen/xslices"
"go.uber.org/zap"
v1alpha1runtime "github.com/siderolabs/talos/internal/app/machined/pkg/runtime"
"github.com/siderolabs/talos/pkg/machinery/api/machine"
"github.com/siderolabs/talos/pkg/machinery/resources/runtime"
)
// MachineStatusPublisherController watches MachineStatusPublishers, sets/resets kernel params.
type MachineStatusPublisherController struct {
V1Alpha1Events v1alpha1runtime.Publisher
}
// Name implements controller.Controller interface.
func (ctrl *MachineStatusPublisherController) Name() string {
return "runtime.MachineStatusPublisherController"
}
// Inputs implements controller.Controller interface.
func (ctrl *MachineStatusPublisherController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: runtime.NamespaceName,
Type: runtime.MachineStatusType,
ID: optional.Some(runtime.MachineStatusID),
Kind: controller.InputWeak,
},
}
}
// Outputs implements controller.Controller interface.
func (ctrl *MachineStatusPublisherController) Outputs() []controller.Output {
return nil
}
// Run implements controller.Controller interface.
func (ctrl *MachineStatusPublisherController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error {
for {
select {
case <-ctx.Done():
return nil
case <-r.EventCh():
}
machineStatus, err := safe.ReaderGet[*runtime.MachineStatus](ctx, r, runtime.NewMachineStatus().Metadata())
if err != nil {
if state.IsNotFoundError(err) {
continue
}
return fmt.Errorf("error reading machine status: %w", err)
}
ctrl.V1Alpha1Events.Publish(ctx, &machine.MachineStatusEvent{
Stage: machine.MachineStatusEvent_MachineStage(machineStatus.TypedSpec().Stage),
Status: &machine.MachineStatusEvent_MachineStatus{
Ready: machineStatus.TypedSpec().Status.Ready,
UnmetConditions: xslices.Map(machineStatus.TypedSpec().Status.UnmetConditions,
func(unmetCondition runtime.UnmetCondition) *machine.MachineStatusEvent_MachineStatus_UnmetCondition {
return &machine.MachineStatusEvent_MachineStatus_UnmetCondition{
Name: unmetCondition.Name,
Reason: unmetCondition.Reason,
}
}),
},
})
r.ResetRestartBackoff()
}
}