-
Notifications
You must be signed in to change notification settings - Fork 509
/
cmds_misc.go
150 lines (131 loc) · 4.52 KB
/
cmds_misc.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
package cli
import (
"context"
"fmt"
"runtime"
"github.com/pkg/browser"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/cliutil"
"github.com/telepresenceio/telepresence/v2/pkg/k8sapi"
"github.com/telepresenceio/telepresence/v2/pkg/proc"
)
// ClusterIdCommand is a simple command that makes it easier for users to
// figure out what their cluster ID is. For now this is just used when
// people are making licenses for air-gapped environments
func ClusterIdCommand() *cobra.Command {
kubeConfig := genericclioptions.NewConfigFlags(false)
cmd := &cobra.Command{
Use: "current-cluster-id",
Args: cobra.NoArgs,
Short: "Get cluster ID for your kubernetes cluster",
Long: "Get cluster ID for your kubernetes cluster, mostly used for licenses in air-gapped environments",
RunE: func(cmd *cobra.Command, _ []string) error {
restConfig, err := kubeConfig.ToRESTConfig()
if err != nil {
return err
}
ki, err := kubernetes.NewForConfig(restConfig)
if err != nil {
return err
}
clusterID, err := k8sapi.GetClusterID(k8sapi.WithK8sInterface(cmd.Context(), ki))
if err != nil {
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Cluster ID: %s\n", clusterID)
return nil
},
}
kubeFlags := pflag.NewFlagSet("Kubernetes flags", 0)
kubeConfig.AddFlags(kubeFlags)
cmd.Flags().AddFlagSet(kubeFlags)
return cmd
}
func connectCommand() *cobra.Command {
var dnsIP string
var mappedNamespaces []string
kubeFlags := pflag.NewFlagSet("Kubernetes flags", 0)
cmd := &cobra.Command{
Use: "connect [flags] [-- <command to run while connected>]",
Args: cobra.ArbitraryArgs,
Short: "Connect to a cluster",
RunE: func(cmd *cobra.Command, args []string) error {
request := &connector.ConnectRequest{
KubeFlags: kubeFlagMap(kubeFlags),
MappedNamespaces: mappedNamespaces,
}
if len(args) == 0 {
return withConnector(cmd, true, request, func(_ context.Context, _ *connectorState) error {
return nil
})
}
return withConnector(cmd, false, request, func(ctx context.Context, _ *connectorState) error {
return proc.Run(ctx, nil, args[0], args[1:]...)
})
},
}
flags := cmd.Flags()
nwFlags := pflag.NewFlagSet("Telepresence networking flags", 0)
// TODO: Those flags aren't applicable on a Linux with systemd-resolved configured either but
// that's unknown until it's been tested during the first connect attempt.
if runtime.GOOS != "darwin" && runtime.GOOS != "windows" {
nwFlags.StringVarP(&dnsIP,
"dns", "", "",
"DNS IP address to intercept locally. Defaults to the first nameserver listed in /etc/resolv.conf.",
)
}
nwFlags.StringSliceVar(&mappedNamespaces,
"mapped-namespaces", nil, ``+
`Comma separated list of namespaces considered by DNS resolver and NAT for outbound connections. `+
`Defaults to all namespaces`)
flags.AddFlagSet(nwFlags)
kubeConfig := genericclioptions.NewConfigFlags(false)
kubeConfig.Namespace = nil // "connect", don't take --namespace
kubeConfig.AddFlags(kubeFlags)
flags.AddFlagSet(kubeFlags)
return cmd
}
func dashboardCommand() *cobra.Command {
return &cobra.Command{
Use: "dashboard",
Args: cobra.NoArgs,
Short: "Open the dashboard in a web page",
RunE: func(cmd *cobra.Command, args []string) error {
cloudCfg := client.GetConfig(cmd.Context()).Cloud
// Ensure we're logged in
resultCode, err := cliutil.EnsureLoggedIn(cmd.Context(), "")
if err != nil {
return err
}
if resultCode == connector.LoginResult_OLD_LOGIN_REUSED {
// The LoginFlow takes the user to the dashboard, so we only need to
// explicitly take the user to the dashboard if they were already
// logged in.
if err := browser.OpenURL(fmt.Sprintf("https://%s/cloud/preview", cloudCfg.SystemaHost)); err != nil {
return err
}
}
return nil
}}
}
func quitCommand() *cobra.Command {
quitRootDaemon := false
quitUserDaemon := false
cmd := &cobra.Command{
Use: "quit",
Args: cobra.NoArgs,
Short: "Tell telepresence daemon to quit",
RunE: func(cmd *cobra.Command, _ []string) error {
return cliutil.Disconnect(cmd.Context(), quitUserDaemon, quitRootDaemon)
},
}
flags := cmd.Flags()
flags.BoolVarP(&quitRootDaemon, "root-daemon", "r", false, "stop root daemon")
flags.BoolVarP(&quitUserDaemon, "user-daemon", "u", false, "stop user daemon")
return cmd
}