github.com/telepresenceio/telepresence/v2@v2.20.0-pro.6.0.20240517030216-236ea954e789/pkg/client/cli/cmd/config.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 6 "github.com/spf13/cobra" 7 empty "google.golang.org/protobuf/types/known/emptypb" 8 9 "github.com/telepresenceio/telepresence/rpc/v2/connector" 10 "github.com/telepresenceio/telepresence/v2/pkg/client" 11 "github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann" 12 "github.com/telepresenceio/telepresence/v2/pkg/client/cli/connect" 13 "github.com/telepresenceio/telepresence/v2/pkg/client/cli/daemon" 14 "github.com/telepresenceio/telepresence/v2/pkg/client/cli/output" 15 ) 16 17 func configCmd() *cobra.Command { 18 cmd := &cobra.Command{ 19 Use: "config", 20 } 21 cmd.AddCommand(configView()) 22 return cmd 23 } 24 25 const clientOnlyFlag = "client-only" 26 27 func configView() *cobra.Command { 28 cmd := &cobra.Command{ 29 Use: "view", 30 Args: cobra.NoArgs, 31 PersistentPreRunE: output.DefaultYAML, 32 Short: "View current Telepresence configuration", 33 RunE: runConfigView, 34 Annotations: map[string]string{ 35 ann.Session: ann.Optional, 36 }, 37 } 38 cmd.Flags().BoolP(clientOnlyFlag, "c", false, "Only view config from client file.") 39 return cmd 40 } 41 42 // GetCommandKubeConfig will return the fully resolved client.Kubeconfig for the given command. 43 func GetCommandKubeConfig(cmd *cobra.Command) (*client.Kubeconfig, error) { 44 ctx := cmd.Context() 45 uc := daemon.GetUserClient(ctx) 46 var kc *client.Kubeconfig 47 var err error 48 if uc != nil && !cmd.Flag("context").Changed { 49 // Get the context that we're currently connected to. 50 var ci *connector.ConnectInfo 51 ci, err = uc.Status(ctx, &empty.Empty{}) 52 if err == nil { 53 kc, err = client.NewKubeconfig(ctx, map[string]string{"context": ci.ClusterContext}, "") 54 } 55 } else { 56 if daemon.GetRequest(ctx) == nil { 57 if ctx, err = daemon.WithDefaultRequest(ctx, cmd); err != nil { 58 return nil, err 59 } 60 } 61 rq := daemon.GetRequest(ctx) 62 kc, err = client.NewKubeconfig(ctx, rq.KubeFlags, rq.ManagerNamespace) 63 } 64 return kc, err 65 } 66 67 func runConfigView(cmd *cobra.Command, _ []string) error { 68 var cfg client.SessionConfig 69 clientOnly, _ := cmd.Flags().GetBool(clientOnlyFlag) 70 if !clientOnly { 71 cmd.Annotations = map[string]string{ 72 ann.Session: ann.Required, 73 } 74 if err := connect.InitCommand(cmd); err != nil { 75 // Unable to establish a session, so try to convey the local config instead. It 76 // may be helpful in diagnosing the problem. 77 cmd.Annotations = map[string]string{} 78 clientOnly = true 79 } 80 } 81 82 if clientOnly { 83 if err := connect.InitCommand(cmd); err != nil { 84 return err 85 } 86 87 kc, err := GetCommandKubeConfig(cmd) 88 if err != nil { 89 return err 90 } 91 ctx := cmd.Context() 92 cfg.Config = client.GetConfig(ctx) 93 cfg.ClientFile = client.GetConfigFile(ctx) 94 cfg.Routing.AlsoProxy = kc.AlsoProxy 95 cfg.Routing.NeverProxy = kc.NeverProxy 96 cfg.Routing.AllowConflicting = kc.AllowConflictingSubnets 97 if dns := kc.DNS; dns != nil { 98 cfg.DNS.ExcludeSuffixes = dns.ExcludeSuffixes 99 cfg.DNS.IncludeSuffixes = dns.IncludeSuffixes 100 cfg.DNS.LookupTimeout = dns.LookupTimeout.Duration 101 cfg.DNS.LocalIP = dns.LocalIP.IP() 102 cfg.DNS.RemoteIP = dns.RemoteIP.IP() 103 } 104 if mgr := kc.Manager; mgr != nil { 105 cfg.ManagerNamespace = mgr.Namespace 106 } 107 output.Object(cmd.Context(), &cfg, true) 108 return nil 109 } 110 111 cmd.Annotations = map[string]string{ 112 ann.Session: ann.Required, 113 } 114 if err := connect.InitCommand(cmd); err != nil { 115 return err 116 } 117 ctx := cmd.Context() 118 cc, err := daemon.GetUserClient(ctx).GetConfig(ctx, &empty.Empty{}) 119 if err != nil { 120 return err 121 } 122 err = json.Unmarshal(cc.Json, &cfg) 123 if err != nil { 124 return err 125 } 126 output.Object(ctx, &cfg, true) 127 return nil 128 }