-
-
Notifications
You must be signed in to change notification settings - Fork 517
/
cmds_misc.go
151 lines (138 loc) · 4.79 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
151
package cli
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
empty "google.golang.org/protobuf/types/known/emptypb"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
"github.com/datawire/k8sapi/pkg/k8sapi"
"github.com/telepresenceio/telepresence/rpc/v2/connector"
"github.com/telepresenceio/telepresence/rpc/v2/daemon"
"github.com/telepresenceio/telepresence/v2/pkg/client"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/ann"
"github.com/telepresenceio/telepresence/v2/pkg/client/cli/util"
"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 kubeFlags *pflag.FlagSet
var request *connector.ConnectRequest
cmd := &cobra.Command{
Use: "connect [flags] [-- <command to run while connected>]",
Args: cobra.ArbitraryArgs,
Short: "Connect to a cluster",
Annotations: map[string]string{
ann.RootDaemon: ann.Required,
ann.Session: ann.Required,
},
RunE: func(cmd *cobra.Command, args []string) error {
request.KubeFlags = kubeFlagMap(kubeFlags)
cmd.SetContext(util.WithConnectionRequest(cmd.Context(), request))
if err := util.InitCommand(cmd); err != nil {
return err
}
if len(args) == 0 {
return nil
}
ctx := cmd.Context()
if util.GetSession(ctx).Started {
defer func() {
_ = util.Disconnect(ctx, false)
}()
}
return proc.Run(ctx, nil, cmd, args[0], args[1:]...)
},
}
request, kubeFlags = initConnectRequest(cmd)
return cmd
}
func initConnectRequest(cmd *cobra.Command) (*connector.ConnectRequest, *pflag.FlagSet) {
cr := connector.ConnectRequest{}
flags := cmd.Flags()
nwFlags := pflag.NewFlagSet("Telepresence networking flags", 0)
nwFlags.StringSliceVar(&cr.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
kubeFlags := pflag.NewFlagSet("Kubernetes flags", 0)
kubeConfig.AddFlags(kubeFlags)
flags.AddFlagSet(kubeFlags)
return &cr, kubeFlags
}
func quitCommand() *cobra.Command {
quitDaemons := false
quitRootDaemon := false
quitUserDaemon := false
cmd := &cobra.Command{
Use: "quit",
Args: cobra.NoArgs,
Short: "Tell telepresence daemon to quit",
Annotations: map[string]string{ann.UserDaemon: ann.Optional},
RunE: func(cmd *cobra.Command, _ []string) error {
if err := util.InitCommand(cmd); err != nil {
return err
}
if quitUserDaemon {
fmt.Fprintln(os.Stderr, "--user-daemon (-u) is deprecated, please use --stop-daemons (-s)")
quitDaemons = true
}
if quitRootDaemon {
fmt.Fprintln(os.Stderr, "--root-daemon (-r) is deprecated, please use --stop-daemons (-s)")
quitDaemons = true
}
ctx := cmd.Context()
if quitDaemons && util.GetUserDaemon(ctx) == nil {
// User daemon isn't running. If the root daemon is running, we must
// kill it from here.
if conn, err := client.DialSocket(ctx, client.DaemonSocketName); err == nil {
_, _ = daemon.NewDaemonClient(conn).Quit(ctx, &empty.Empty{})
}
}
return util.Disconnect(cmd.Context(), quitDaemons)
},
}
flags := cmd.Flags()
flags.BoolVarP(&quitDaemons, "stop-daemons", "s", false, "stop the traffic-manager and network daemons")
flags.BoolVarP(&quitRootDaemon, "root-daemon", "r", false, "stop daemons")
flags.BoolVarP(&quitUserDaemon, "user-daemon", "u", false, "stop daemons")
// retained for backward compatibility but hidden from now on
flags.Lookup("root-daemon").Hidden = true
flags.Lookup("user-daemon").Hidden = true
return cmd
}