-
-
Notifications
You must be signed in to change notification settings - Fork 520
/
cmd_status.go
303 lines (276 loc) · 9.38 KB
/
cmd_status.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
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"time"
"github.com/spf13/cobra"
empty "google.golang.org/protobuf/types/known/emptypb"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/client/scout"
"github.com/telepresenceio/telepresence/v2/pkg/iputil"
)
type statusInfo struct {
json bool
out io.Writer
}
type statusOutput struct {
DaemonStatus daemonStatus `json:"root_daemon"`
UserDaemon connectorStatus `json:"user_daemon"`
}
type daemonStatus struct {
Running bool `json:"running,omitempty"`
Version string `json:"version,omitempty"`
APIVersion int32 `json:"api_version,omitempty"`
DNS *daemonStatusDNS `json:"dns,omitempty"`
AlsoProxySubnets []string `json:"also_proxy_subnets,omitempty"`
NeverProxySubnets []string `json:"never_proxy_subnets,omitempty"`
}
type daemonStatusDNS struct {
LocalIP net.IP `json:"local_ip,omitempty"`
RemoteIP net.IP `json:"remote_ip,omitempty"`
ExcludeSuffixes []string `json:"exclude_suffixes,omitempty"`
IncludeSuffixes []string `json:"include_suffixes,omitempty"`
LookupTimeout time.Duration `json:"lookup_timeout_in_nanos,omitempty"`
}
type connectorStatus struct {
Running bool `json:"running,omitempty"`
Version string `json:"version,omitempty"`
APIVersion int32 `json:"api_version,omitempty"`
Executable string `json:"executable,omitempty"`
InstallID string `json:"install_id,omitempty"`
AmbassadorCloud connectorStatusAmbassadorCloud `json:"ambassador_cloud"`
Status string `json:"status,omitempty"`
Error string `json:"error,omitempty"`
KubernetesServer string `json:"kubernetes_server,omitempty"`
KubernetesContext string `json:"kubernetes_context,omitempty"`
Intercepts []connectStatusIntercept `json:"intercepts,omitempty"`
}
type connectorStatusAmbassadorCloud struct {
Status string `json:"status,omitempty"`
UserID string `json:"user_id,omitempty"`
AccountID string `json:"account_id,omitempty"`
Email string `json:"email,omitempty"`
}
type connectStatusIntercept struct {
Name string `json:"name,omitempty"`
Client string `json:"client,omitempty"`
}
func statusCommand() *cobra.Command {
s := &statusInfo{}
cmd := &cobra.Command{
Use: "status",
Args: cobra.NoArgs,
Short: "Show connectivity status",
RunE: s.status,
}
flags := cmd.Flags()
flags.BoolVarP(&s.json, "json", "j", false, "output as json object")
return cmd
}
// status will retrieve connectivity status from the daemon and print it on stdout.
func (s *statusInfo) status(cmd *cobra.Command, _ []string) error {
s.out = cmd.OutOrStdout()
ctx := cmd.Context()
ds, err := s.daemonStatus(ctx)
if err != nil {
return err
}
cs, err := s.connectorStatus(ctx)
if err != nil {
return err
}
if s.json {
return s.printJSON(ds, cs)
}
s.printText(ds, cs)
return nil
}
func (s *statusInfo) daemonStatus(ctx context.Context) (*daemonStatus, error) {
ds := &daemonStatus{}
err := cliutil.WithStartedNetwork(ctx, func(ctx context.Context, daemonClient daemon.DaemonClient) error {
ds.Running = true
var err error
status, err := daemonClient.Status(ctx, &empty.Empty{})
if err != nil {
return err
}
version, err := daemonClient.Version(ctx, &empty.Empty{})
if err != nil {
return err
}
ds.Running = true
ds.Version = version.Version
ds.APIVersion = version.ApiVersion
if obc := status.OutboundConfig; obc != nil {
ds.DNS = &daemonStatusDNS{}
dns := obc.Dns
if dns.LocalIp != nil {
// Local IP is only set when the overriding resolver is used
ds.DNS.LocalIP = dns.LocalIp
}
ds.DNS.RemoteIP = dns.RemoteIp
ds.DNS.ExcludeSuffixes = dns.ExcludeSuffixes
ds.DNS.IncludeSuffixes = dns.IncludeSuffixes
ds.DNS.LookupTimeout = dns.LookupTimeout.AsDuration()
for _, subnet := range obc.AlsoProxySubnets {
ds.AlsoProxySubnets = append(ds.AlsoProxySubnets, iputil.IPNetFromRPC(subnet).String())
}
for _, subnet := range obc.NeverProxySubnets {
ds.NeverProxySubnets = append(ds.NeverProxySubnets, iputil.IPNetFromRPC(subnet).String())
}
}
return nil
})
if err != nil {
if errors.Is(err, cliutil.ErrNoNetwork) {
ds.Running = false
return ds, nil
}
return ds, err
}
return ds, nil
}
func (s *statusInfo) connectorStatus(ctx context.Context) (*connectorStatus, error) {
cs := &connectorStatus{}
err := cliutil.WithStartedConnector(ctx, false, func(ctx context.Context, connectorClient connector.ConnectorClient) error {
cs.Running = true
version, err := connectorClient.Version(ctx, &empty.Empty{})
if err != nil {
return err
}
cs.Version = version.Version
cs.APIVersion = version.ApiVersion
cs.Executable = version.Executable
reporter := scout.NewReporter(ctx, "cli")
cs.InstallID = reporter.InstallID()
if !cliutil.HasLoggedIn(ctx) {
cs.AmbassadorCloud.Status = "Logged out"
} else {
userInfo, err := cliutil.GetCloudUserInfo(ctx, false, true)
if err != nil {
cs.AmbassadorCloud.Status = "Login expired (or otherwise no-longer-operational)"
} else {
cs.AmbassadorCloud.Status = "Logged in"
cs.AmbassadorCloud.UserID = userInfo.Id
cs.AmbassadorCloud.AccountID = userInfo.AccountId
cs.AmbassadorCloud.Email = userInfo.Email
}
}
status, err := connectorClient.Status(ctx, &empty.Empty{})
if err != nil {
return err
}
switch status.Error {
case connector.ConnectInfo_UNSPECIFIED, connector.ConnectInfo_ALREADY_CONNECTED:
cs.Status = "Connected"
case connector.ConnectInfo_MUST_RESTART:
cs.Status = "Connected, but must restart"
case connector.ConnectInfo_DISCONNECTED:
cs.Status = "Not connected"
return nil
case connector.ConnectInfo_CLUSTER_FAILED:
cs.Status = "Not connected, error talking to cluster"
cs.Error = status.ErrorText
return nil
case connector.ConnectInfo_TRAFFIC_MANAGER_FAILED:
cs.Status = "Not connected, error talking to in-cluster Telepresence traffic-manager"
cs.Error = status.ErrorText
return nil
}
cs.KubernetesServer = status.ClusterServer
cs.KubernetesContext = status.ClusterContext
for _, icept := range status.GetIntercepts().GetIntercepts() {
cs.Intercepts = append(cs.Intercepts, connectStatusIntercept{
Name: icept.Spec.Name,
Client: icept.Spec.Client,
})
}
return nil
})
if err != nil {
if errors.Is(err, cliutil.ErrNoUserDaemon) {
cs.Running = false
return cs, nil
}
return cs, err
}
return cs, nil
}
func (s *statusInfo) printJSON(ds *daemonStatus, cs *connectorStatus) error {
output, err := json.Marshal(statusOutput{
DaemonStatus: *ds,
UserDaemon: *cs,
})
if err != nil {
return err
}
s.println(string(output))
return nil
}
func (s *statusInfo) printText(ds *daemonStatus, cs *connectorStatus) {
s.printDaemonText(ds)
s.printConnectorText(cs)
}
func (s *statusInfo) printDaemonText(ds *daemonStatus) {
if ds.Running {
s.println("Root Daemon: Running")
s.printf(" Version : %s (api %d)\n", ds.Version, ds.APIVersion)
if ds.DNS != nil {
s.printf(" DNS :\n")
if len(ds.DNS.LocalIP) > 0 {
s.printf(" Local IP : %v\n", ds.DNS.LocalIP)
}
s.printf(" Remote IP : %v\n", ds.DNS.RemoteIP)
s.printf(" Exclude suffixes: %v\n", ds.DNS.ExcludeSuffixes)
s.printf(" Include suffixes: %v\n", ds.DNS.IncludeSuffixes)
s.printf(" Timeout : %v\n", ds.DNS.LookupTimeout)
s.printf(" Also Proxy : (%d subnets)\n", len(ds.AlsoProxySubnets))
for _, subnet := range ds.AlsoProxySubnets {
s.printf(" - %s\n", subnet)
}
s.printf(" Never Proxy: (%d subnets)\n", len(ds.NeverProxySubnets))
for _, subnet := range ds.NeverProxySubnets {
s.printf(" - %s\n", subnet)
}
}
} else {
s.println("Root Daemon: Not running")
}
}
func (s *statusInfo) printConnectorText(cs *connectorStatus) {
if cs.Running {
s.println("User Daemon: Running")
s.printf(" Version : %s (api %d)\n", cs.Version, cs.APIVersion)
s.printf(" Executable : %s\n", cs.Executable)
s.printf(" Install ID : %s\n", cs.InstallID)
s.printf(" Ambassador Cloud:\n")
s.printf(" Status : %s\n", cs.AmbassadorCloud.Status)
s.printf(" User ID : %s\n", cs.AmbassadorCloud.UserID)
s.printf(" Account ID: %s\n", cs.AmbassadorCloud.AccountID)
s.printf(" Email : %s\n", cs.AmbassadorCloud.Email)
s.printf(" Status : %s\n", cs.Status)
if cs.Error != "" {
s.printf(" Error : %s\n", cs.Error)
}
s.printf(" Kubernetes server : %s\n", cs.KubernetesServer)
s.printf(" Kubernetes context: %s\n", cs.KubernetesContext)
s.printf(" Intercepts : %d total\n", len(cs.Intercepts))
for _, intercept := range cs.Intercepts {
s.printf(" %s: %s\n", intercept.Name, intercept.Client)
}
} else {
s.println("User Daemon: Not running")
}
}
func (s *statusInfo) printf(format string, a ...any) {
_, _ = fmt.Fprintf(s.out, format, a...)
}
func (s *statusInfo) println(a ...any) {
_, _ = fmt.Fprintln(s.out, a...)
}