-
Notifications
You must be signed in to change notification settings - Fork 18
/
ssh.go
90 lines (69 loc) · 2.97 KB
/
ssh.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
/*
SPDX-FileCopyrightText: 2021 SAP SE or an SAP affiliate company and Gardener contributors
SPDX-License-Identifier: Apache-2.0
*/
package ssh
import (
"strings"
"github.com/spf13/cobra"
"k8s.io/klog/v2"
"github.com/gardener/gardenctl-v2/internal/util"
"github.com/gardener/gardenctl-v2/pkg/cmd/base"
"github.com/gardener/gardenctl-v2/pkg/flags"
)
// NewCmdSSH returns a new ssh command.
func NewCmdSSH(f util.Factory, o *SSHOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "ssh [NODE_NAME]",
Short: "Establish an SSH connection to a node of a Shoot cluster",
Long: `Establish an SSH connection to a node of a Shoot cluster by specifying its name.
A bastion is created to access the node and is automatically cleaned up afterwards.
If a node name is not provided, gardenctl will display the hostnames/IPs of the Shoot worker nodes and the corresponding SSH command.
To connect to a desired node, copy the printed SSH command, replace the target hostname accordingly, and execute the command.`,
Example: `# Establish an SSH connection to a specific Shoot cluster node
gardenctl ssh my-shoot-node-1
# Establish an SSH connection with custom CIDRs to allow access to the bastion host
gardenctl ssh my-shoot-node-1 --cidr 10.1.2.3/32
# Establish an SSH connection to any Shoot cluster node
# Copy the printed SSH command, replace the 'IP_OR_HOSTNAME' placeholder for the target hostname/IP, and execute the command to connect to the desired node
gardenctl ssh
# Create the bastion and output the connection information in JSON format
gardenctl ssh --no-keepalive --keep-bastion --interactive=false --output json
# Reuse a previously created bastion
gardenctl ssh --keep-bastion --bastion-name cli-xxxxxxxx --public-key-file /path/to/ssh/key.pub --private-key-file /path/to/ssh/key
`,
Args: cobra.MaximumNArgs(1),
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
ctx := f.Context()
logger := klog.FromContext(ctx)
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
manager, err := f.Manager()
if err != nil {
logger.Error(err, "could not get manager from factory")
return nil, cobra.ShellCompDirectiveNoFileComp
}
nodeNames, err := getNodeNamesFromMachinesOrNodes(ctx, manager)
if err != nil {
logger.Error(err, "could not get node names from shoot")
return nil, cobra.ShellCompDirectiveNoFileComp
}
var completions []string
for _, nodeName := range nodeNames {
if strings.HasPrefix(nodeName, toComplete) {
completions = append(completions, nodeName)
}
}
return completions, cobra.ShellCompDirectiveNoFileComp
},
RunE: base.WrapRunE(o, f),
}
o.AddFlags(cmd.Flags())
o.RegisterCompletionsForOutputFlag(cmd)
o.AccessConfig.AddFlags(cmd.Flags())
RegisterCompletionFuncsForAccessConfigFlags(cmd, f)
f.TargetFlags().AddFlags(cmd.Flags())
flags.RegisterCompletionFuncsForTargetFlags(cmd, f, o.IOStreams, cmd.Flags())
return cmd
}