feat(plugin): check container name is not empty when call kubectl exec

This commit is contained in:
wrype 2023-08-03 14:39:18 +08:00
parent b2808c9ff7
commit 7385a371f6
3 changed files with 21 additions and 6 deletions

View file

@ -17,6 +17,8 @@ limitations under the License.
package exec package exec
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
@ -60,7 +62,7 @@ func exec(flags *genericclioptions.ConfigFlags, podName string, deployment strin
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("exec in %s\n", pod.Name)
args := []string{"exec"} args := []string{"exec"}
if opts.TTY { if opts.TTY {
args = append(args, "-t") args = append(args, "-t")
@ -69,7 +71,10 @@ func exec(flags *genericclioptions.ConfigFlags, podName string, deployment strin
args = append(args, "-i") args = append(args, "-i")
} }
args = append(args, []string{"-n", pod.Namespace, "-c", container, pod.Name, "--"}...) args = append(args, "-n", pod.Namespace, pod.Name)
args = append(args, cmd...) if len(container) > 0 {
args = append(args, "-c", container)
}
args = append(args, append([]string{"--"}, cmd...)...)
return kubectl.Exec(flags, args) return kubectl.Exec(flags, args)
} }

View file

@ -101,7 +101,10 @@ func logs(flags *genericclioptions.ConfigFlags, podName string, deployment strin
return err return err
} }
cmd := []string{"logs", "-n", pod.Namespace, "-c", container, pod.Name} cmd := []string{"logs", "-n", pod.Namespace, pod.Name}
if len(container) > 0 {
cmd = append(cmd, "-c", container)
}
cmd = append(cmd, opts.toStrings()...) cmd = append(cmd, opts.toStrings()...)
return kubectl.Exec(flags, cmd) return kubectl.Exec(flags, cmd)
} }

View file

@ -17,6 +17,8 @@ limitations under the License.
package ssh package ssh
import ( import (
"fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions" "k8s.io/cli-runtime/pkg/genericclioptions"
@ -50,6 +52,11 @@ func ssh(flags *genericclioptions.ConfigFlags, podName string, deployment string
if err != nil { if err != nil {
return err return err
} }
fmt.Printf("exec in %s\n", pod.Name)
return kubectl.Exec(flags, []string{"exec", "-it", "-n", pod.Namespace, "-c", container, pod.Name, "--", "/bin/bash"}) args := []string{"exec", "-it", "-n", pod.Namespace, pod.Name}
if len(container) > 0 {
args = append(args, "-c", container)
}
args = append(args, "--", "/bin/bash")
return kubectl.Exec(flags, args)
} }