-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmd_config.go
100 lines (91 loc) · 2.78 KB
/
cmd_config.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
package cli
import (
"encoding/json"
"path/filepath"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
empty "google.golang.org/protobuf/types/known/emptypb"
"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"
"github.com/telepresenceio/telepresence/v2/pkg/filelocation"
)
func configCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
}
cmd.AddCommand(configViewCommand())
return cmd
}
func configViewCommand() *cobra.Command {
var kubeFlags *pflag.FlagSet
var request *connector.ConnectRequest
cmd := &cobra.Command{
Use: "view",
Args: cobra.NoArgs,
PersistentPreRunE: output.DefaultYAML,
Short: "View current Telepresence configuration",
RunE: func(cmd *cobra.Command, args []string) error {
request.KubeFlags = kubeFlagMap(kubeFlags)
util.AddKubeconfigEnv(request)
cmd.SetContext(util.WithConnectionRequest(cmd.Context(), request))
return configView(cmd, args)
},
}
cmd.Flags().BoolP("client-only", "c", false, "Only view config from client file.")
request, kubeFlags = initConnectRequest(cmd)
return cmd
}
func configView(cmd *cobra.Command, _ []string) error {
var cfg client.SessionConfig
clientOnly, _ := cmd.Flags().GetBool("client-only")
if clientOnly {
// Unable to establish a session, so try to convey the local config instead. It
// may be helpful in diagnosing the problem.
ctx := cmd.Context()
cfgDir, err := filelocation.AppUserConfigDir(ctx)
if err != nil {
return err
}
cfg.Config = client.GetConfig(cmd.Context())
cfg.ClientFile = filepath.Join(cfgDir, client.ConfigFile)
rq := util.GetConnectRequest(ctx)
kc, err := client.NewKubeconfig(ctx, rq.KubeFlags)
if err != nil {
return err
}
cfg.Routing.AlsoProxy = kc.AlsoProxy
cfg.Routing.NeverProxy = kc.NeverProxy
if dns := kc.DNS; dns != nil {
cfg.DNS.ExcludeSuffixes = dns.ExcludeSuffixes
cfg.DNS.IncludeSuffixes = dns.IncludeSuffixes
cfg.DNS.LookupTimeout = dns.LookupTimeout.Duration
cfg.DNS.LocalIP = dns.LocalIP.IP()
cfg.DNS.RemoteIP = dns.RemoteIP.IP()
}
if mgr := kc.Manager; mgr != nil {
cfg.ManagerNamespace = mgr.Namespace
}
output.Object(ctx, &cfg, true)
return nil
}
cmd.Annotations = map[string]string{
ann.Session: ann.Required,
}
if err := util.InitCommand(cmd); err != nil {
return err
}
ctx := cmd.Context()
cc, err := util.GetUserDaemon(ctx).GetConfig(ctx, &empty.Empty{})
if err != nil {
return err
}
err = json.Unmarshal(cc.Json, &cfg)
if err != nil {
return err
}
output.Object(ctx, &cfg, true)
return nil
}