go: randomly terminate pods in kubernetes cluster

7/12/2017

I would like to randomly shut down pods in a kubernetes cluster with go. I already wrote code, which enables to login to the server and run code.

Now I would need to read all the available pods in the cluster, choose some randomly and terminate them. (I am new to go)

Could you please help me doing this?

This is how I am running commands on the cluster/server cli.ExecuteCmd("kubectl get pods")

// Use one connection per command.
// Catch in the client when required.
func (cli *SSHClient)ExecuteCmd(command string){
  conn, err := ssh.Dial("tcp", cli.Hostname+":22", cli.Config)
  if err!=nil {
    logrus.Infof("%s@%s", cli.Config.User, cli.Hostname)
    logrus.Info("Hint: Add you key to the ssh agent: 'ssh-add ~/.ssh/id_rsa'")
    logrus.Fatal(err)
  }
  session, _ := conn.NewSession()
  defer session.Close()
  var stdoutBuf bytes.Buffer
  session.Stdout = &stdoutBuf
  err = session.Run(command)
  if err != nil {
    logrus.Fatalf("Run failed:%v", err)
  }
  logrus.Infof(">%s", stdoutBuf.Bytes())
}
-- user6942447
cluster-computing
go
kubernetes
ssh
terminal

1 Answer

7/12/2017

Use k8s.io/client-go (Github Link) client package to list kubernetes pods, and then delete them randomly.

Use client.CoreV1().Pods() methods to list and delete pods.

-- sadlil
Source: StackOverflow