mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-06-20 13:22:14 +08:00
webterm init
This commit is contained in:
46
pkg/webconsole/command/command.go
Normal file
46
pkg/webconsole/command/command.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
|
||||
"yunion.io/x/log"
|
||||
)
|
||||
|
||||
const (
|
||||
PROTOCOL_TTY string = "tty"
|
||||
//PROTOCOL_VNC string = "vnc"
|
||||
)
|
||||
|
||||
type ICommand interface {
|
||||
GetProtocol() string
|
||||
GetCommand() *exec.Cmd
|
||||
Cleanup() error
|
||||
}
|
||||
|
||||
type BaseCommand struct {
|
||||
name string
|
||||
args []string
|
||||
}
|
||||
|
||||
func NewBaseCommand(name string, args ...string) *BaseCommand {
|
||||
return &BaseCommand{
|
||||
name: name,
|
||||
args: args,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *BaseCommand) AppendArgs(args ...string) *BaseCommand {
|
||||
for _, arg := range args {
|
||||
c.args = append(c.args, arg)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (c BaseCommand) GetCommand() *exec.Cmd {
|
||||
return exec.Command(c.name, c.args...)
|
||||
}
|
||||
|
||||
func (c BaseCommand) Cleanup() error {
|
||||
log.Infof("BaseCommand Cleanup do nothing")
|
||||
return nil
|
||||
}
|
||||
127
pkg/webconsole/command/kube_command.go
Normal file
127
pkg/webconsole/command/kube_command.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
|
||||
"yunion.io/x/log"
|
||||
)
|
||||
|
||||
type Kubectl struct {
|
||||
*BaseCommand
|
||||
kubeconfig string
|
||||
}
|
||||
|
||||
func NewKubectlCommand(kubeconfig, namespace string) *Kubectl {
|
||||
name := "kubectl"
|
||||
if len(namespace) == 0 {
|
||||
namespace = "default"
|
||||
}
|
||||
cmd := NewBaseCommand(name, "--namespace", namespace)
|
||||
return &Kubectl{
|
||||
BaseCommand: cmd,
|
||||
kubeconfig: kubeconfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Kubectl) GetCommand() *exec.Cmd {
|
||||
cmd := c.BaseCommand.GetCommand()
|
||||
cmd.Env = append(cmd.Env, fmt.Sprintf("KUBECONFIG=%s", c.kubeconfig))
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c Kubectl) GetProtocol() string {
|
||||
return PROTOCOL_TTY
|
||||
}
|
||||
|
||||
func (c *Kubectl) Cleanup() error {
|
||||
log.Debugf("Remove temp kubeconfig file: %s", c.kubeconfig)
|
||||
return os.Remove(c.kubeconfig)
|
||||
}
|
||||
|
||||
type KubectlExec struct {
|
||||
*Kubectl
|
||||
}
|
||||
|
||||
func (c *Kubectl) Exec() *KubectlExec {
|
||||
// Execute a command in a container
|
||||
cmd := &KubectlExec{
|
||||
Kubectl: c,
|
||||
}
|
||||
cmd.AppendArgs("exec")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *KubectlExec) Stdin() *KubectlExec {
|
||||
// -i: Pass stdin to the container
|
||||
c.AppendArgs("-i")
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *KubectlExec) TTY() *KubectlExec {
|
||||
// -t: Stdin is a TTY
|
||||
c.AppendArgs("-t")
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *KubectlExec) Container(name string) *KubectlExec {
|
||||
if len(name) == 0 {
|
||||
return c
|
||||
}
|
||||
// -c: Container name. If ommitted, the first container in the pod will be chosen
|
||||
c.AppendArgs("-c", name)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *KubectlExec) Pod(name string) *KubectlExec {
|
||||
// Pod name
|
||||
c.AppendArgs(name)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *KubectlExec) Command(cmd string, args ...string) *KubectlExec {
|
||||
c.AppendArgs("--", cmd)
|
||||
c.AppendArgs(args...)
|
||||
return c
|
||||
}
|
||||
|
||||
func NewPodBashCommand(kubeconfig, namespace, pod, container string) ICommand {
|
||||
return NewKubectlCommand(kubeconfig, namespace).Exec().
|
||||
Stdin().
|
||||
TTY().
|
||||
Pod(pod).
|
||||
Container(container).
|
||||
Command("bash", "-i", "-l")
|
||||
}
|
||||
|
||||
type KubectlLog struct {
|
||||
*Kubectl
|
||||
}
|
||||
|
||||
func (c *Kubectl) Logs() *KubectlLog {
|
||||
// Print the logs for a container in a pod
|
||||
cmd := &KubectlLog{
|
||||
Kubectl: c,
|
||||
}
|
||||
cmd.AppendArgs("logs")
|
||||
return cmd
|
||||
}
|
||||
|
||||
func (c *KubectlLog) Follow() *KubectlLog {
|
||||
// -f: Specify if the logs should be streamed
|
||||
c.AppendArgs("-f")
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *KubectlLog) Pod(name string) *KubectlLog {
|
||||
// Pod name
|
||||
c.AppendArgs(name)
|
||||
return c
|
||||
}
|
||||
|
||||
func NewPodLogCommand(kubeconfig, namespace, pod, container string) ICommand {
|
||||
return NewKubectlCommand(kubeconfig, namespace).Logs().
|
||||
Follow().
|
||||
Pod(pod)
|
||||
}
|
||||
51
pkg/webconsole/command/kube_command_test.go
Normal file
51
pkg/webconsole/command/kube_command_test.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package command
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestKubectlExec_Command(t *testing.T) {
|
||||
type fields struct {
|
||||
Kubectl *Kubectl
|
||||
}
|
||||
type args struct {
|
||||
cmd string
|
||||
args []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "bash command",
|
||||
fields: fields{
|
||||
Kubectl: NewKubectlCommand("system"),
|
||||
},
|
||||
args: args{
|
||||
cmd: "bash",
|
||||
args: []string{"-il"},
|
||||
},
|
||||
want: "kubectl --namespace system exec -i -t Pod1 -c Container1 -- bash -il",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
c := tt.fields.Kubectl.Exec().
|
||||
Stdin().TTY().
|
||||
Pod("Pod1").
|
||||
Container("Container1")
|
||||
cmd := c.Command(tt.args.cmd, tt.args.args...)
|
||||
name := cmd.name
|
||||
args := cmd.args
|
||||
got := []string{name}
|
||||
got = append(got, args...)
|
||||
if !reflect.DeepEqual(strings.Join(got, " "), tt.want) {
|
||||
t.Errorf("KubectlExec.Command() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user