[REV] optimize exec command impl,remove manualy add namespace arg

This commit is contained in:
bay1ts 2017-11-15 10:38:29 +08:00
parent 2193018790
commit a385b4f23c

View File

@ -22,13 +22,15 @@ import (
"github.com/Sirupsen/logrus" "github.com/Sirupsen/logrus"
"os" "os"
"os/exec" "os/exec"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/goodrain/rainbond/pkg/grctl/clients"
) )
func NewCmdExec() cli.Command { func NewCmdExec() cli.Command {
c:=cli.Command{ c:=cli.Command{
Name: "exec", Name: "exec",
Usage: "进入容器方法。grctl exec NAMESPACE POD_NAME COMMAND ", Usage: "进入容器方法。grctl exec POD_NAME COMMAND ",
Action: func(c *cli.Context) error { Action: func(c *cli.Context) error {
Common(c) Common(c)
return execContainer(c) return execContainer(c)
@ -37,14 +39,14 @@ func NewCmdExec() cli.Command {
return c return c
} }
// grctl exec NAMESPACE POD_ID COMMAND // grctl exec POD_ID COMMAND
func execContainer(c *cli.Context) error { func execContainer(c *cli.Context) error {
//podID := c.Args().Get(1) //podID := c.Args().Get(1)
args := c.Args().Tail() args := c.Args().Tail()
tenantID:=c.Args().First() tenantID:=""
//podID := c.Args().First() podName := c.Args().First()
//args := c.Args().Tail() //args := c.Args().Tail()
//tenantID, err := clients.FindNamespaceByPod(podID) //tenantID, err := clients.FindNamespaceByPod(podID)
@ -59,8 +61,18 @@ func execContainer(c *cli.Context) error {
if len(args) == 0 { if len(args) == 0 {
args = []string{"bash"} args = []string{"bash"}
} }
//logrus.Infof("using namespace %s,podid %s",tenantID) pl,err:=clients.K8SClient.Core().Pods("").List(metav1.ListOptions{})
defaultArgs := []string{kubeCtrl, "exec", "-it", "--namespace=" + tenantID} if err != nil {
logrus.Errorf("error get pods by nil namespace")
return err
}
for _,v:=range pl.Items {
if v.Name == podName {
tenantID=v.Namespace
break
}
}
defaultArgs := []string{kubeCtrl, "exec", "-it", "--namespace=" + tenantID,podName}
args = append(defaultArgs, args...) args = append(defaultArgs, args...)
//logrus.Info(args) //logrus.Info(args)
cmd := exec.Cmd{ cmd := exec.Cmd{