How Controller-runtime client can get RESTClient to run a command

6/20/2019

we have built an operator via kubebuilder 2.0 successfully. In this operator, we need to run a cmd in a pod before we use k8s.io/client-go/kubernetes.Clientset which it grabs restconfig and run like

   execReq := o.clientset.CoreV1().RESTClient().Post().
        Resource("pods").
        Name(Podname).
        Namespace("default").
        SubResource("exec")

   execReq.VersionedParams(&corev1.PodExecOptions{
        Command:   SqlCommand,
        Stdin:     true,
        Stdout:    true,
        Stderr:    true,
        }, scheme.ParameterCodec)

    exec, err := remotecommand.NewSPDYExecutor(o.restConfig, "POST", execReq.URL())
    if err != nil {
        return fmt.Errorf("error while creating Executor: %v", err)
    }

    err = exec.Stream(remotecommand.StreamOptions{
            Stdin:  os.Stdin,
            Stdout: os.Stdout,
            Stderr: os.Stderr,
            Tty:    false,
        })
    if err != nil {
        return fmt.Errorf("error in Stream: %v", err)
    } else {
        return nil
    }

In the controller-runtime world, I can't find any RESTClient from client of controller-runtime.

I wonder how we can execute a cmd in a pod from an operator ?

thank you (edited)

-- Honord
client-go
kubebuilder
kubernetes

0 Answers