-
Notifications
You must be signed in to change notification settings - Fork 506
/
cmd_list.go
216 lines (199 loc) · 5.85 KB
/
cmd_list.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
package cli
import (
"context"
"fmt"
"io"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/datawire/dlib/dlog"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/output"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/util"
)
type listInfo struct {
onlyIntercepts bool
onlyAgents bool
onlyInterceptable bool
debug bool
namespace string
watch bool
}
func listCommand() *cobra.Command {
s := &listInfo{}
cmd := &cobra.Command{
Use: "list",
Args: cobra.NoArgs,
Short: "List current intercepts",
RunE: s.list,
Annotations: map[string]string{
ann.Session: ann.Required,
},
}
flags := cmd.Flags()
flags.BoolVarP(&s.onlyIntercepts, "intercepts", "i", false, "intercepts only")
flags.BoolVarP(&s.onlyAgents, "agents", "a", false, "with installed agents only")
flags.BoolVarP(&s.onlyInterceptable, "only-interceptable", "o", true, "interceptable workloads only")
flags.BoolVar(&s.debug, "debug", false, "include debugging information")
flags.StringVarP(&s.namespace, "namespace", "n", "", "If present, the namespace scope for this CLI request")
flags.BoolVarP(&s.watch, "watch", "w", false, "watch a namespace. --agents and --intercepts are disabled if this flag is set")
wf := flags.Lookup("watch")
wf.Hidden = true
wf.Deprecated = `Use "--output json-stream" instead of "--watch"`
_ = cmd.RegisterFlagCompletionFunc("namespace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
shellCompDir := cobra.ShellCompDirectiveNoFileComp
if err := util.InitCommand(cmd); err != nil {
shellCompDir |= cobra.ShellCompDirectiveError
return nil, shellCompDir
}
ctx := cmd.Context()
userD := util.GetUserDaemon(ctx)
resp, err := userD.GetNamespaces(ctx, &connector.GetNamespacesRequest{
ForClientAccess: false,
Prefix: toComplete,
})
if err != nil {
dlog.Debugf(cmd.Context(), "error getting namespaces: %v", err)
shellCompDir |= cobra.ShellCompDirectiveError
return nil, shellCompDir
}
return resp.Namespaces, shellCompDir
})
return cmd
}
// list requests a list current intercepts from the daemon.
func (s *listInfo) list(cmd *cobra.Command, _ []string) error {
if err := util.InitCommand(cmd); err != nil {
return err
}
stdout := cmd.OutOrStdout()
ctx := cmd.Context()
userD := util.GetUserDaemon(ctx)
var filter connector.ListRequest_Filter
switch {
case s.onlyIntercepts:
filter = connector.ListRequest_INTERCEPTS
case s.onlyAgents:
filter = connector.ListRequest_INSTALLED_AGENTS
case s.onlyInterceptable:
filter = connector.ListRequest_INTERCEPTABLE
default:
filter = connector.ListRequest_EVERYTHING
}
cfg := client.GetConfig(ctx)
maxRecSize := int64(1024 * 1024 * 20) // Default to 20 Mb here. List can be quit long.
if !cfg.Grpc.MaxReceiveSize.IsZero() {
if mz, ok := cfg.Grpc.MaxReceiveSize.AsInt64(); ok {
if mz > maxRecSize {
maxRecSize = mz
}
}
}
formattedOutput := output.WantsFormatted(cmd)
if !output.WantsStream(cmd) {
r, err := userD.List(ctx, &connector.ListRequest{Filter: filter, Namespace: s.namespace}, grpc.MaxCallRecvMsgSize(int(maxRecSize)))
if err != nil {
return err
}
s.printList(ctx, r.Workloads, stdout, formattedOutput)
return nil
}
stream, err := userD.WatchWorkloads(ctx, &connector.WatchWorkloadsRequest{Namespaces: []string{s.namespace}}, grpc.MaxCallRecvMsgSize(int(maxRecSize)))
if err != nil {
return err
}
ch := make(chan *connector.WorkloadInfoSnapshot)
go func() {
for {
r, err := stream.Recv()
if err != nil {
break
}
ch <- r
}
}()
looper:
for {
select {
case r := <-ch:
s.printList(ctx, r.Workloads, stdout, formattedOutput)
case <-ctx.Done():
break looper
}
}
return nil
}
func (s *listInfo) printList(ctx context.Context, workloads []*connector.WorkloadInfo, stdout io.Writer, formattedOut bool) {
if len(workloads) == 0 {
if formattedOut {
output.Object(ctx, []struct{}{}, false)
} else {
fmt.Fprintln(stdout, "No Workloads (Deployments, StatefulSets, or ReplicaSets)")
}
return
}
state := func(workload *connector.WorkloadInfo) string {
if iis := workload.InterceptInfos; len(iis) > 0 {
return util.DescribeIntercepts(iis, nil, s.debug)
}
ai := workload.AgentInfo
if ai != nil {
return "ready to intercept (traffic-agent already installed)"
}
if workload.NotInterceptableReason != "" {
return "not interceptable (traffic-agent not installed): " + workload.NotInterceptableReason
} else {
return "ready to intercept (traffic-agent not yet installed)"
}
}
if formattedOut {
output.Object(ctx, workloads, false)
} else {
includeNs := false
ns := s.namespace
for _, dep := range workloads {
depNs := dep.Namespace
if depNs == "" {
// Local-only, so use namespace of first intercept
depNs = dep.InterceptInfos[0].Spec.Namespace
}
if ns != "" && depNs != ns {
includeNs = true
break
}
ns = depNs
}
nameLen := 0
for _, dep := range workloads {
n := dep.Name
if n == "" {
// Local-only, so use name of first intercept
n = dep.InterceptInfos[0].Spec.Name
}
nl := len(n)
if includeNs {
nl += len(dep.Namespace) + 1
}
if nl > nameLen {
nameLen = nl
}
}
for _, workload := range workloads {
if workload.Name == "" {
// Local-only, so use name of first intercept
n := workload.InterceptInfos[0].Spec.Name
if includeNs {
n += "." + workload.Namespace
}
fmt.Fprintf(stdout, "%-*s: local-only intercept\n", nameLen, n)
} else {
n := workload.Name
if includeNs {
n += "." + workload.Namespace
}
fmt.Fprintf(stdout, "%-*s: %s\n", nameLen, n, state(workload))
}
}
}
}