A simple Kubernetes client, based on client-go.
package main
import (
"context"
"log"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"github.com/shipengqi/kube"
)
func main() {
kubeconfig := "testdata/config"
flags := genericclioptions.NewConfigFlags(false)
flags.KubeConfig = &kubeconfig
cfg := kube.NewConfig(flags)
cli := kube.New(cfg)
k8s, err := cli.Dial()
if err != nil {
log.Fatal(err)
}
// get a configmap named "configmapname"
cm, err := k8s.CoreV1().ConfigMaps("default").Get(context.TODO(), "configmapname", metav1.GetOptions{})
log.Println(cm.Data)
// or
cm, err = cli.GetConfigMap(context.TODO(), "default", "configmapname")
log.Println(cm.Data)
// apply file, is like "kubectl apply -f testdata/content-apply.yaml"
err = cli.Apply("testdata/content-apply.yaml")
if err != nil {
log.Fatal(err)
}
// delete file, is like "kubectl delete -f testdata/content-apply.yaml"
err = cli.Delete("testdata/content-apply.yaml")
if err != nil {
log.Fatal(err)
}
// Exec in a pod, is like "kubectl exec <pod name> -n <namespace> -c <container name> -- <command>"
stdout, stderr, err := cli.Exec("podname", "containername", "namespace", "command")
if err != nil {
log.Println(stderr)
log.Fatal(err)
}
log.Println(stdout)
// Uploads local file to a remote pod
err = cli.Upload(context.TODO(), "podname", "containername", "namespace", "testdata/upload.txt", "/testdata")
if err != nil {
log.Fatal(err)
}
// Downloads file from a remote pod to local file system
err = cli.Download(context.TODO(), "podname", "containername", "namespace", "testdata/upload.txt", "testdata")
if err != nil {
log.Fatal(err)
}
}
You can find the docs at go docs.
go test -v . -kubeconfig <kubeconfig file>
Tests Client.Upload, Client.Download And Client.Exec with specified pod, container, namespace:
go test -v -kubeconfig <kubeconfig file> -container <container name> -pod <pod name> -namespace <namespace> .
kube
had been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.