arashicage / kube

A simple Kubernetes client, based on client-go.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

kube

A simple Kubernetes client, based on client-go.

Test codecov Release License

Quick Start

package main

import (
    "context"
    "log"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/cli-runtime/pkg/genericclioptions"

	"github.com/arashicage/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([]string{"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)
}

Documentation

You can find the docs at go docs.

Test

go test -v . -kubeconfig <kubeconfig file>

Test Client.Exec

go test -v -coverprofile=coverage.out -kubeconfig <kubeconfig file> -container <container name> -pod <pod name> -namespace <namespace> .

About

A simple Kubernetes client, based on client-go.

License:Apache License 2.0


Languages

Language:Go 100.0%