Categorygithub.com/shipengqi/kube
modulepackage
0.31.1
Repository: https://github.com/shipengqi/kube.git
Documentation: pkg.go.dev

# README

kube

A simple Kubernetes client, based on client-go.

E2E codecov Go Report Card Release License

Getting Started

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)
	}
}

Documentation

You can find the docs at go docs.

Test

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> .

🔋 JetBrains OS licenses

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.

JetBrains Logo (Main) logo.

# Functions

GetObjects returns the list of objects parsed from the given files.
LoadDefaultKubeConfig starts by running the clientcmdapi.MigrationRules and then takes the loading rules and returns a clientcmdapi.Config object.
New returns a new Client for the given Config.
NewConfig returns a new kube client configuration.
NewDefault returns a new Client with default kubeconfig.
NewInCluster returns a new Client with InClusterConfig.
RetrievesDefaultKubeConfig returns a available kubeconfig file.

# Constants

No description provided by the author
No description provided by the author
DefaultTimeout default request timeout.
EnvVarKubeConfig The KUBECONFIG environment variable holds a list of kubeconfig files.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
UsePersistentConfig caches client config to avoid reloads.

# Variables

No description provided by the author

# Structs

Client represents kubernetes Client.
Config represents a kubernetes configuration.

# Type aliases

No description provided by the author
No description provided by the author