Skip to content

Instantly share code, notes, and snippets.

@pracps
Created October 9, 2021 15:12
Show Gist options
  • Save pracps/1816e57af25e732e428ac1f78fdb7630 to your computer and use it in GitHub Desktop.
Save pracps/1816e57af25e732e428ac1f78fdb7630 to your computer and use it in GitHub Desktop.
kubernetes remotecommand go-client
// A working version of remotecommand
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/remotecommand"
)
var (
// Clientset ...
Clientset *kubernetes.Clientset
// ClientConfigMap ...
ClientConfigMap map[string]ClientConfig
// ConfigPath ...
ConfigPath *string
)
type configsList []string
var myConfigList configsList
func (i *configsList) String() string {
return "Path to kube config"
}
func (i *configsList) Set(value string) error {
*i = append(*i, value)
return nil
}
type ClientConfig struct {
Clientset *kubernetes.Clientset
RestConfig *rest.Config
ConfigPath string
}
func main() {
fmt.Println("Start ok")
flag.Var(&myConfigList, "configPath", "Please provide the Config Map as -configPath=<name>:<configPath>")
flag.Parse()
ClientConfigMap = make(map[string]ClientConfig)
if len(myConfigList) == 0 {
//if No configPath is provided then Below is the default Kube config Path
defaultConfigPath := "~/.kube/config"
cfg, err := clientcmd.BuildConfigFromFlags("", defaultConfigPath)
if err != nil {
panic(err)
}
Clientset, err = kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}
ClientConfigMap["default"] = ClientConfig{Clientset: Clientset, ConfigPath: defaultConfigPath}
} else {
for _, singleConfigPath := range myConfigList {
configPathSplit := strings.Split(singleConfigPath, ":")
cfg, err := clientcmd.BuildConfigFromFlags("", configPathSplit[1])
if err != nil {
panic(err)
}
Clientset, err = kubernetes.NewForConfig(cfg)
if err != nil {
panic(err)
}
ClientConfigMap[configPathSplit[0]] = ClientConfig{Clientset: Clientset, ConfigPath: configPathSplit[1], RestConfig: cfg}
}
}
log.SetFlags(log.LstdFlags | log.Lshortfile)
kubeConfigPath := ClientConfigMap["dev"].ConfigPath
restClient := ClientConfigMap["dev"].RestConfig
clientset := ClientConfigMap["dev"].Clientset
fmt.Println(kubeConfigPath)
req := clientset.CoreV1().RESTClient().Post().
Resource("pods").
Name("service-prod-78c4bcb475-8pnxj").
Namespace("serviceapp").
SubResource("exec")
option := &v1.PodExecOptions{
Command: []string{"bash", "-c", "pmap 45 > lock"},
Container: "corecon",
Stdin: false,
Stdout: true,
Stderr: true,
TTY: false,
}
req.VersionedParams(
option,
scheme.ParameterCodec,
)
log.Println(req.URL().Scheme, req.URL().Host, req.URL().Path)
cmd, err := remotecommand.NewSPDYExecutor(restClient, "POST", req.URL())
if err != nil {
log.Println("-- Failed in execution -- ")
log.Println(err)
return
}
err = cmd.Stream(remotecommand.StreamOptions{
// Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
Tty: false,
})
if err != nil {
log.Println(err)
log.Println(err.Error())
// log.Println(stdout)
// log.Println(stderr)
}
log.Println("----------Done----------")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment