talm

Форк
0
/
imported_events.go 
152 строки · 4.7 Кб
1
// Code generated by go run tools/import_commands.go --talos-version v1.7.1 events
2
// DO NOT EDIT.
3

4
// This Source Code Form is subject to the terms of the Mozilla Public
5
// License, v. 2.0. If a copy of the MPL was not distributed with this
6
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7

8
package commands
9

10
import (
11
	"context"
12
	"errors"
13
	"fmt"
14
	"os"
15
	"strings"
16
	"text/tabwriter"
17
	"time"
18

19
	"github.com/siderolabs/gen/xslices"
20
	"github.com/spf13/cobra"
21

22
	"github.com/siderolabs/talos/cmd/talosctl/pkg/talos/helpers"
23
	"github.com/siderolabs/talos/pkg/machinery/api/machine"
24
	"github.com/siderolabs/talos/pkg/machinery/client"
25
)
26

27
var eventsCmdFlags struct {
28
	tailEvents   int32
29
	tailDuration time.Duration
30
	tailID       string
31
	actorID      string
32
	configFiles  []string
33
}
34

35
// eventsCmd represents the events command.
36
var eventsCmd = &cobra.Command{
37
	Use:   "events",
38
	Short: "Stream runtime events",
39
	Long:  ``,
40
	RunE: func(cmd *cobra.Command, args []string) error {
41
		return WithClient(func(ctx context.Context, c *client.Client) error {
42
			w := tabwriter.NewWriter(os.Stdout, 0, 0, 3, ' ', 0)
43
			fmt.Fprintln(w, "NODE\tID\tEVENT\tACTOR\tSOURCE\tMESSAGE")
44

45
			opts := []client.EventsOptionFunc{}
46

47
			if eventsCmdFlags.tailEvents != 0 {
48
				opts = append(opts, client.WithTailEvents(eventsCmdFlags.tailEvents))
49
			}
50

51
			if eventsCmdFlags.tailDuration != 0 {
52
				opts = append(opts, client.WithTailDuration(eventsCmdFlags.tailDuration))
53
			}
54

55
			if eventsCmdFlags.tailID != "" {
56
				opts = append(opts, client.WithTailID(eventsCmdFlags.tailID))
57
			}
58

59
			if eventsCmdFlags.actorID != "" {
60
				opts = append(opts, client.WithActorID(eventsCmdFlags.actorID))
61
			}
62

63
			events, err := c.Events(ctx, opts...)
64
			if err != nil {
65
				return err
66
			}
67

68
			return helpers.ReadGRPCStream(events, func(ev *machine.Event, node string, multipleNodes bool) error {
69
				format := "%s\t%s\t%s\n%s\t%s\t%s\n"
70

71
				event, err := client.UnmarshalEvent(ev)
72
				if err != nil {
73
					if errors.Is(err, client.ErrEventNotSupported) {
74
						return nil
75
					}
76

77
					return err
78
				}
79

80
				var args []interface{}
81

82
				switch msg := event.Payload.(type) {
83
				case *machine.SequenceEvent:
84
					args = []interface{}{msg.GetSequence()}
85
					if msg.Error != nil {
86
						args = append(args, "error:"+" "+msg.GetError().GetMessage())
87
					} else {
88
						args = append(args, msg.GetAction().String())
89
					}
90
				case *machine.PhaseEvent:
91
					args = []interface{}{msg.GetPhase(), msg.GetAction().String()}
92
				case *machine.TaskEvent:
93
					args = []interface{}{msg.GetTask(), msg.GetAction().String()}
94
				case *machine.ServiceStateEvent:
95
					args = []interface{}{msg.GetService(), fmt.Sprintf("%s: %s", msg.GetAction(), msg.GetMessage())}
96
				case *machine.ConfigLoadErrorEvent:
97
					args = []interface{}{"error", msg.GetError()}
98
				case *machine.ConfigValidationErrorEvent:
99
					args = []interface{}{"error", msg.GetError()}
100
				case *machine.AddressEvent:
101
					args = []interface{}{msg.GetHostname(), fmt.Sprintf("ADDRESSES: %s", strings.Join(msg.GetAddresses(), ","))}
102
				case *machine.MachineStatusEvent:
103
					args = []interface{}{
104
						msg.GetStage().String(),
105
						fmt.Sprintf("ready: %v, unmet conditions: %v",
106
							msg.GetStatus().Ready,
107
							xslices.Map(msg.GetStatus().GetUnmetConditions(),
108
								func(c *machine.MachineStatusEvent_MachineStatus_UnmetCondition) string {
109
									return c.Name
110
								},
111
							),
112
						),
113
					}
114
				}
115

116
				args = append([]interface{}{event.Node, event.ID, event.TypeURL, event.ActorID}, args...)
117
				fmt.Fprintf(w, format, args...)
118

119
				return w.Flush()
120
			})
121
		})
122
	},
123
}
124

125
func init() {
126
	eventsCmd.Flags().StringSliceVarP(&eventsCmdFlags.configFiles,
127
		"file", "f", nil, "specify config files or patches in a YAML file (can specify multiple)",
128
	)
129
	eventsCmd.PreRunE = func(cmd *cobra.Command, args []string) error {
130
		nodesFromArgs := len(
131
			GlobalArgs.
132
				Nodes) >
133
			0
134
		endpointsFromArgs :=
135

136
			len(GlobalArgs.Endpoints) > 0
137
		for _, configFile := range eventsCmdFlags.
138
			configFiles {
139
			if err := processModelineAndUpdateGlobals(configFile, nodesFromArgs, endpointsFromArgs, false); err != nil {
140
				return err
141
			}
142
		}
143
		return nil
144

145
	}
146

147
	addCommand(eventsCmd)
148
	eventsCmd.Flags().Int32Var(&eventsCmdFlags.tailEvents, "tail", 0, "show specified number of past events (use -1 to show full history, default is to show no history)")
149
	eventsCmd.Flags().DurationVar(&eventsCmdFlags.tailDuration, "duration", 0, "show events for the past duration interval (one second resolution, default is to show no history)")
150
	eventsCmd.Flags().StringVar(&eventsCmdFlags.tailID, "since", "", "show events after the specified event ID (default is to show no history)")
151
	eventsCmd.Flags().StringVar(&eventsCmdFlags.actorID, "actor-id", "", "filter events by the specified actor ID (default is no filter)")
152
}
153

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.