-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathwrapped.go
More file actions
104 lines (84 loc) · 3.08 KB
/
Copy pathwrapped.go
File metadata and controls
104 lines (84 loc) · 3.08 KB
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
/*
Copyright 2018 The KubeVirt Authors
Copyright 2024 Flant JSC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Initially copied from https://github.com/kubevirt/kubevirt/blob/main/pkg/virtctl/ssh/wrapped.go
*/
package ssh
import (
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/klog/v2"
)
func addLocalSSHClientFlags(flagset *pflag.FlagSet, opts *SSHOptions) {
flagset.StringArrayVarP(&opts.AdditionalSSHLocalOptions, additionalOpts, additionalOptsShort, opts.AdditionalSSHLocalOptions,
"Additional options to be passed to the ssh client if --local-ssh=true is set")
flagset.BoolVar(&opts.WrapLocalSSH, wrapLocalSSHFlag, opts.WrapLocalSSH,
"Use the SSH/SCP client available on your system by using this command as ProxyCommand; Default is false: use embedded SSH client with limited capabilities")
}
func RunLocalClient(cmd *cobra.Command, namespace, name string, options *SSHOptions, clientArgs []string) error {
args := []string{"-o"}
args = append(args, buildProxyCommandOption(cmd, namespace, name, options.SSHPort))
if len(options.AdditionalSSHLocalOptions) > 0 {
args = append(args, options.AdditionalSSHLocalOptions...)
}
if options.IdentityFilePathProvided {
args = append(args, "-i", options.IdentityFilePath)
}
args = append(args, clientArgs...)
command := exec.Command(options.LocalClientName, args...)
klog.V(3).Info("running: ", command)
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Stdin = os.Stdin
return command.Run()
}
func buildProxyCommandOption(cmd *cobra.Command, namespace, name string, port int) string {
parents := make([]string, 0, 2)
for cmd.HasParent() {
cmd = cmd.Parent()
parents = append(parents, cmd.Name())
}
parents[len(parents)-1] = os.Args[0]
pcmd := strings.Builder{}
for i := 1; i <= len(parents); i++ {
pcmd.WriteString(parents[len(parents)-i])
pcmd.WriteString(" ")
}
proxyCommand := strings.Builder{}
proxyCommand.WriteString("ProxyCommand=")
proxyCommand.WriteString(pcmd.String())
proxyCommand.WriteString("port-forward --stdio=true ")
fmt.Fprintf(&proxyCommand, "%s.%s", name, namespace)
proxyCommand.WriteString(" ")
proxyCommand.WriteString(strconv.Itoa(port))
return proxyCommand.String()
}
func (o *SSH) buildSSHTarget(namespace, name string) (opts []string) {
target := strings.Builder{}
if len(o.options.SSHUsername) > 0 {
target.WriteString(o.options.SSHUsername)
target.WriteRune('@')
}
target.WriteString(name)
target.WriteRune('.')
target.WriteString(namespace)
opts = append(opts, target.String())
if o.command != "" {
opts = append(opts, o.command)
}
return opts
}