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

Created on 20 Jun 2019  路  2Comments  路  Source: kubernetes-sigs/kubebuilder

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.
https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client
I wonder how we can execute a cmd in a pod from an operator ?
It would be great if we have restclient in controller-runtime.
It could be PR

thank you
/kind feature

kinbug kinfeature

Most helpful comment

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.
https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client
I wonder how we can execute a cmd in a pod from an operator ?
It would be great if we have restclient in controller-runtime.
It could be PR

thank you
/kind feature

How did you use the RESTClientforGVK. How do you build the arguments to pass to it.
Can you share any example to exec into the pod using this function.

All 2 comments

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.
https://godoc.org/sigs.k8s.io/controller-runtime/pkg/client
I wonder how we can execute a cmd in a pod from an operator ?
It would be great if we have restclient in controller-runtime.
It could be PR

thank you
/kind feature

How did you use the RESTClientforGVK. How do you build the arguments to pass to it.
Can you share any example to exec into the pod using this function.

Was this page helpful?
0 / 5 - 0 ratings