func genKubeconfig(contexts ...string) string {
	var sb strings.Builder
	sb.WriteString(`---
apiVersion: v1
kind: Config
clusters:
`)
	for _, ctx := range contexts {
		sb.WriteString(`- cluster:
    server: ` + ctx + `
  name: ` + ctx + `
`)
	}
	sb.WriteString("contexts:\n")
	for _, ctx := range contexts {
		sb.WriteString(`- context:
    cluster: ` + ctx + `
    user: ` + ctx + `
  name: ` + ctx + `
`)
	}
	sb.WriteString("users:\n")
	for _, ctx := range contexts {
		sb.WriteString(`- name: ` + ctx + `
`)
	}
	sb.WriteString("preferences: {}\n")
	if len(contexts) > 0 {
		sb.WriteString("current-context: " + contexts[0] + "\n")
	}
	return sb.String()
}
genKubeconfig used to genearte kube config file
we can provide multiple contexts as well
package conf
import (
	"flag"
	"io/ioutil"
	"log"
	"os"
	"path/filepath"
	"strings"
	"testing"
	"k8s.io/client-go/util/homedir"
)
func TestMain(m *testing.M) {
	setup()
	code := m.Run()
	teardown()
	os.Exit(code)
}
func setup() {
	home := homedir.HomeDir()
	kubeconfigdir := filepath.Join(home, "test", ".kube")
	kubeconfigpath := filepath.Join(kubeconfigdir, "config")
	_, err := os.Stat(kubeconfigpath)
	if os.IsNotExist(err) {
		err = os.MkdirAll(kubeconfigdir, 0o777)
		if err != nil {
			log.Println("failed to create .kube dir", err)
			return
		}
		data := genKubeconfig("test-context")
		err = createFile(kubeconfigpath, data)
		if err != nil {
			log.Println("failed to create config file", err)
			return
		}
	}
	log.Println("file created successfully", kubeconfigpath)
	flag.StringVar(&kubeconfig, "kubeconfig", "", "Paths to a kubeconfig. Only required if out-of-cluster.")
	err = flag.Set("kubeconfig", kubeconfigpath)
	if err != nil {
		log.Println("unexpected error while setting flag value", err)
		return
	}
	flag.Parse()
}
func createFile(path, data string) error {
	return ioutil.WriteFile(path, []byte(data), 0o644)
}