Skip to content

Instantly share code, notes, and snippets.

@sahajamit
Last active July 19, 2020 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sahajamit/aab44939036e0b6ee8de1480157e1bf4 to your computer and use it in GitHub Desktop.
Save sahajamit/aab44939036e0b6ee8de1480157e1bf4 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"flag"
"fmt"
"math/rand"
"os"
"time"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
flag.Parse()
//Passing the K8s Master details
config, err := clientcmd.BuildConfigFromFlags("https://192.168.99.100:8443", "")
//Giving the bearer token for the K8s rest apis authentication
config.BearerToken = "eyJhbGciOiJSUzI1NiIsImtpZCI6Il9rTEhlQUxpLU1XcTR0b0pqUGJadnlvd2ZmZjYxcmJkcEpUbllwdmxxZWcifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRlZmF1bHQtdG9rZW4teG14bGsiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtYWNjb3VudC5uYW1lIjoiZGVmYXVsdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjI1YzA2OTU4LWQxNzktNDg4Yi1hNDM4LWI1ZjgzM2IyN2Y3ZCIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDpkZWZhdWx0OmRlZmF1bHQifQ.djVtQwd7ERfW266oOdBKqM-j2ARUh2NfhxBjjFCxgSCo7zcOk00u_xemwQAoXq2QaYlS3dgWM62CPvJvpWct38rBcAQnPLTtdjHELQxUoil9T0yuYy0GNjS2TS4lZAIfNyhJRIQurSiEu2B-FByfTV4hY6eQGJGDFKe5TWzDwFYQ3Kgz4EZ3ypGXBUF0Utnpk5z_izHIyQL4KgFRKYPBZ70ZqSRXtWuJGMvE5L-McJLNU26QXdtqDVGEoFRhvcQeBcrnq-FpFWki8WUWx3PlyT0A3Jt7jqOdxcald7iGocWtjVjd6KXuS8ryzRvkNR1cwu4dXHFPkB1GdhQ8Uy-vOw"
config.Insecure = true
if err != nil {
panic(err.Error())
}
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
panic(err.Error())
}
namespace := "default"
//getting all the running pods in default namespace
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
podsCount := len(pods.Items)
fmt.Printf("There are %d pods in the cluster. Their Names are:\n", podsCount)
for _, pod := range pods.Items {
fmt.Fprintf(os.Stdout, "%v\n", pod.Name)
}
for {
pods, err := clientset.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{})
if err != nil {
panic(err.Error())
}
//deleting a random pod every 10 seconds
randomPodIndex := rand.Intn(podsCount-1) + 1
fmt.Println("Deleting this randon pod: ", pods.Items[randomPodIndex-1].Name)
err = clientset.CoreV1().Pods(namespace).Delete(context.TODO(), pods.Items[randomPodIndex-1].Name, metav1.DeleteOptions{})
if err != nil {
log.Fatal(err)
}
var sleepTime int = 10
duration := time.Duration(sleepTime) * time.Second
fmt.Printf("Now sleeping for %d seconds\n", sleepTime)
time.Sleep(duration)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment