-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
session.go
107 lines (95 loc) · 2.85 KB
/
session.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
package cli
import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client"
)
type sessionInfo struct {
cmd *cobra.Command
}
// withDaemon establishes a daemon session and calls the function with the gRPC client. If
// retain is false, the session will end unless it was already started.
func (si *sessionInfo) withDaemon(retain bool, f func(state *daemonState) error) error {
// OK with dns and fallback empty. Daemon must be up and running
ds, err := si.newDaemonState()
if err == errDaemonIsNotRunning {
err = nil
}
if err != nil {
return err
}
defer ds.disconnect()
return client.WithEnsuredState(ds, retain, func() error { return f(ds) })
}
func kubeFlagMap() map[string]string {
kubeFlagMap := make(map[string]string)
kubeFlags.VisitAll(func(flag *pflag.Flag) {
if flag.Changed {
kubeFlagMap[flag.Name] = flag.Value.String()
}
})
return kubeFlagMap
}
func withStartedDaemon(cmd *cobra.Command, f func(state *daemonState) error) error {
err := assertDaemonStarted()
if err != nil {
return err
}
si := &sessionInfo{cmd: cmd}
return si.withDaemon(false, f)
}
// withConnector establishes a daemon and a connector session and calls the function with the gRPC client. If
// retain is false, the sessions will end unless they were already started.
func (si *sessionInfo) withConnector(retain bool, f func(state *connectorState) error) error {
return si.withDaemon(retain, func(ds *daemonState) error {
cs, err := si.newConnectorState(ds.grpc)
if err == errConnectorIsNotRunning {
err = nil
}
if err != nil {
return err
}
defer cs.disconnect()
return client.WithEnsuredState(cs, retain, func() error { return f(cs) })
})
}
func withStartedConnector(cmd *cobra.Command, f func(state *connectorState) error) error {
err := assertDaemonStarted()
if err != nil {
return err
}
err = assertConnectorStarted()
if err != nil {
return err
}
si := &sessionInfo{cmd: cmd}
return si.withConnector(false, f)
}
func (si *sessionInfo) connect(cmd *cobra.Command, args []string) error {
si.cmd = cmd
if len(args) == 0 {
return si.withConnector(true, func(_ *connectorState) error { return nil })
}
return si.withConnector(false, func(cs *connectorState) error {
return start(args[0], args[1:], true, cmd.InOrStdin(), cmd.OutOrStdout(), cmd.ErrOrStderr())
})
}
func (si *sessionInfo) newConnectorState(daemon daemon.DaemonClient) (*connectorState, error) {
cs := NewConnectorState(si, daemon)
err := assertConnectorStarted()
if err == nil {
err = cs.connect()
}
return cs, err
}
func connectCommand() *cobra.Command {
si := &sessionInfo{}
cmd := &cobra.Command{
Use: "connect [flags] [-- <additional kubectl arguments...>]",
Args: cobra.ArbitraryArgs,
Short: "Connect to a cluster",
RunE: si.connect,
}
return cmd
}