@@ -7,21 +7,6 @@ import (
77 "k8s.io/client-go/util/homedir"
88)
99
10- type Config struct {
11- KubeConfig string
12- KubeContext string
13- Namespace string
14- PodName string
15- Container string
16- Command []string
17- SSHUser string
18- SSHRequirePassword bool
19- SSHSocketPath string
20- SSHHost string
21- SSHPort string
22- LinuxNs []string
23- }
24-
2510const (
2611 argKubeconfig = "kubeconfig"
2712 argContainer = "container"
@@ -30,9 +15,13 @@ const (
3015 argUser = "user"
3116 argPassword = "password"
3217 argSSHAuthSock = "ssh-auth-sock"
18+ argSSHOpts = "ssh-opt"
3319 argHost = "host"
3420 argPort = "port"
3521 argNs = "ns"
22+ argInteractive = "interactive"
23+ argTTY = "tty"
24+ argUseNodeName = "use-node-name"
3625)
3726
3827var (
@@ -42,88 +31,119 @@ var (
4231 Usage : "kubernetes client config path" ,
4332 EnvVars : []string {"KUBECONFIG" },
4433 Value : fmt .Sprintf ("%s/.kube/config" , homedir .HomeDir ()),
45- Required : false ,
4634 DefaultText : "$HOME/.kube/config" ,
4735 },
4836 & cli.StringFlag {
49- Name : argContainer ,
50- Aliases : []string {"c" },
51- Usage : "use namespace of specified container. By default first running container will taken" ,
52- Value : "" ,
53- Required : false ,
37+ Name : argContainer ,
38+ Aliases : []string {"c" },
39+ Usage : "use namespace of specified container. By default first running container will taken" ,
40+ Value : "" ,
5441 },
5542 & cli.StringFlag {
56- Name : argContext ,
57- Usage : "override current context from kubeconfig" ,
58- Value : "" ,
59- Required : false ,
43+ Name : argContext ,
44+ Usage : "override current context from kubeconfig" ,
45+ Value : "" ,
6046 },
6147 & cli.StringFlag {
62- Name : argNamespace ,
63- Aliases : []string {"n" },
64- Usage : "override namespace of current context from kubeconfig" ,
65- Value : "" ,
66- Required : false ,
48+ Name : argNamespace ,
49+ Aliases : []string {"n" },
50+ Usage : "override namespace of current context from kubeconfig" ,
51+ Value : "" ,
6752 },
6853 & cli.StringFlag {
69- Name : argUser ,
70- Aliases : []string {"u" },
71- Usage : "set username for ssh connection to node" ,
72- EnvVars : []string {"USER" },
73- Required : false ,
54+ Name : argUser ,
55+ Aliases : []string {"u" },
56+ Usage : "set username for ssh connection to node" ,
7457 },
7558 & cli.BoolFlag {
7659 Name : argPassword ,
7760 Aliases : []string {"s" },
7861 Usage : "force ask for node password prompt" ,
79- Value : false ,
8062 },
8163 & cli.StringFlag {
8264 Name : argSSHAuthSock ,
8365 Usage : "sets ssh-agent socket" ,
8466 EnvVars : []string {"SSH_AUTH_SOCK" },
8567 DefaultText : "current shell auth sock" ,
86- Required : false ,
8768 },
8869 & cli.StringFlag {
89- Name : argHost ,
90- Usage : "override node ip" ,
91- Required : false ,
70+ Name : argHost ,
71+ Usage : "override node ip" ,
9272 },
9373 & cli.StringFlag {
94- Name : argPort ,
95- Aliases : []string {"p" },
96- Usage : "sets ssh port" ,
97- Value : "22" ,
98- Required : false ,
74+ Name : argPort ,
75+ Aliases : []string {"p" },
76+ Usage : "sets ssh port" ,
9977 },
10078 & cli.StringSliceFlag {
101- Name : argNs ,
102- Usage : "define container's pid linux namespaces to enter. Sends transparently to nsenter cmd" ,
103- Value : cli .NewStringSlice ("n" ),
104- Required : false ,
79+ Name : argNs ,
80+ Usage : "define container's pid linux namespaces to enter. Sends transparently to nsenter cmd" ,
81+ Value : cli .NewStringSlice ("n" ),
82+ },
83+ & cli.BoolFlag {
84+ Name : argInteractive ,
85+ Aliases : []string {"i" },
86+ Usage : "keep ssh session stdin" ,
87+ Value : false ,
88+ },
89+ & cli.BoolFlag {
90+ Name : argTTY ,
91+ Aliases : []string {"t" },
92+ Usage : "allocate pseudo-TTY for ssh session" ,
93+ Value : false ,
94+ },
95+ & cli.StringSliceFlag {
96+ Name : argSSHOpts ,
97+ Aliases : []string {"o" },
98+ Usage : "same as -o for ssh client" ,
99+ },
100+ & cli.BoolFlag {
101+ Name : argUseNodeName ,
102+ Aliases : []string {"j" },
103+ Usage : "use kubernetes node name to connect with ssh. Useful with ssh configs" ,
104+ EnvVars : []string {"KUBECTL_NSENTER_USE_NODE_NAME" },
105+ Value : true ,
105106 },
106107 }
107108 stringFlags = []string {argKubeconfig , argContainer , argContext , argNamespace , argUser , argSSHAuthSock , argHost , argPort }
108109)
109110
110- func NewConfig (clictx * cli.Context ) (Config , error ) {
111+ type Config struct {
112+ KubeConfig string
113+ KubeContext string
114+ Namespace string
115+ PodName string
116+ Container string
117+ Command []string
118+ SSHUser string
119+ SSHRequirePassword bool
120+ SSHSocketPath string
121+ SSHHost string
122+ SSHPort string
123+ SSHOpts []string
124+ Interactive bool
125+ TTY bool
126+ UseNodeName bool
127+ LinuxNs []string
128+ }
129+
130+ func NewConfig (clictx * cli.Context ) (* Config , error ) {
111131 podName := clictx .Args ().First ()
112132 if podName == "" {
113- return Config {} , errorWithCliHelp (clictx , "you must specify pod name!" )
133+ return nil , errorWithCliHelp (clictx , "you must specify pod name!" )
114134 }
115135
116136 command := clictx .Args ().Tail ()
117137 if len (command ) == 0 {
118- return Config {} , errorWithCliHelp (clictx , "you must provide a command!" )
138+ return nil , errorWithCliHelp (clictx , "you must provide a command!" )
119139 }
120140
121141 err := validateStringFlagsNonEmpty (clictx , stringFlags )
122142 if err != nil {
123- return Config {} , err
143+ return nil , err
124144 }
125145
126- return Config {
146+ return & Config {
127147 KubeConfig : clictx .String (argKubeconfig ),
128148 KubeContext : clictx .String (argContext ),
129149 Namespace : clictx .String (argNamespace ),
@@ -135,6 +155,10 @@ func NewConfig(clictx *cli.Context) (Config, error) {
135155 SSHRequirePassword : clictx .Bool (argPassword ),
136156 SSHHost : clictx .String (argHost ),
137157 SSHPort : clictx .String (argPort ),
158+ SSHOpts : clictx .StringSlice (argSSHOpts ),
159+ Interactive : clictx .Bool (argInteractive ),
160+ TTY : clictx .Bool (argTTY ),
161+ UseNodeName : clictx .Bool (argUseNodeName ),
138162 LinuxNs : clictx .StringSlice (argNs ),
139163 }, nil
140164}
@@ -150,13 +174,13 @@ func validateStringFlagsNonEmpty(clictx *cli.Context, flags []string) error {
150174 return nil
151175}
152176
153- func errorWithCliHelp (clictx * cli.Context , msg string ) error {
177+ func errorWithCliHelp (clictx * cli.Context , a any ) error {
154178 err := cli .ShowAppHelp (clictx )
155179 if err != nil {
156180 return err
157181 }
158182 // nolint:stylecheck
159- return fmt .Errorf ("%s\n " , msg )
183+ return fmt .Errorf ("%s\n " , a )
160184}
161185
162186func errorWithCliHelpf (clictx * cli.Context , format string , a ... any ) error {
0 commit comments