Как я могу войти (в golang, используя client-go) модуль k8s с несколькими контейнерами через веб-оболочку? - PullRequest
0 голосов
/ 26 апреля 2019

Описание:

Нет проблем при входе в контейнер / модуль с помощью client-go / remotecommand.NewSPDYExecutor, когда в модуле есть только один контейнер.

Но когда в контейнере много контейнеров, я не могу войти в систему, указав идентификатор контейнера.

Может кто-нибудь помочь или дать мне решение? Заранее спасибо.


Nots:

Я использую client-go для подключения к k8s и использую remotecommand.NewSPDYExecutor для входа в контейнер.


Код:

Вот код, который я использую для входа в систему / подключения к модулю / контейнеру

func (s *ShellService) BuildConnection(shellVo vo.ShellContainerVo) error {

    clu := cluster.GetCluster(shellVo.Cluster)

    if clu == nil {
        logs.Error("cannot find cluster: ", shellVo.Cluster)
        return errors.New("cannot find cluster: " + shellVo.Cluster)
    }

    if shellVo.Command == "" {
        shellVo.Command = "/bin/sh"
    }

    config := clu.Config
    groupversion := schema.GroupVersion{
        Group:   "",
        Version: "v1",
    }
    config.GroupVersion = &groupversion
    config.APIPath = "/api"
    config.ContentType = runtime.ContentTypeJSON
    config.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
    restclient, err := rest.RESTClientFor(config)
    if err != nil {
        return err
    }
    fn := func() error {
        req := restclient.Post().
            Resource("pods").
            Name(shellVo.Container).
            Namespace(shellVo.Namespace).
            SubResource("exec").
            Param("container", shellVo.Container).
            Param("stdin", "true").
            Param("stdout", "true").
            Param("stderr", "true").
            Param("command", shellVo.Command).Param("tty", "true")
        c := make(chan *remotecommand.TerminalSize)
        t := &terminalsize{shellVo.Conn, c}
        req.VersionedParams(
            &v1.PodExecOptions{
                Container: shellVo.Container,
                Command:   []string{},
                Stdin:     true,
                Stdout:    true,
                Stderr:    true,
                TTY:       true,
            },
            scheme.ParameterCodec,
        )
        executor, err := remotecommand.NewSPDYExecutor(
            config, http.MethodPost, req.URL(),
        )
        if err != nil {
            return err
        }

        return executor.Stream(remotecommand.StreamOptions{
            //SupportedProtocols: remotecommandconsts.SupportedStreamingProtocols,
            Stdin:             t,
            Stdout:            shellVo.Conn,
            Stderr:            shellVo.Conn,
            Tty:               true,
            TerminalSizeQueue: t,
        })
        //return nil
    }

    inFd, isTerminal := term.GetFdInfo(shellVo.Conn)
    beego.Info(isTerminal)
    state, err := term.SaveState(inFd)
    return interrupt.Chain(nil, func() {
        term.RestoreTerminal(inFd, state)
    }).Run(fn)

    //kubeShell := &pod.Shell{}
    return nil

}

Мой модуль работает с несколькими колясками. поэтому в контейнере много контейнеров. Я надеюсь, что смогу войти в указанный контейнер, указав идентификатор контейнера.

...