kube

package module
v0.0.0-...-0d8c4eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 19, 2022 License: Apache-2.0 Imports: 34 Imported by: 0

README

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

Documentation

Overview

Package kube implements a Kubernetes client.

Index

Constants

View Source
const (
	// DefaultTimeout default request timeout.
	DefaultTimeout = 10 * time.Second

	// UsePersistentConfig caches client config to avoid reloads.
	UsePersistentConfig = true
)
View Source
const (
	// EnvVarKubeConfig The KUBECONFIG environment variable holds a list of kubeconfig files.
	// For Linux and Mac, the list is colon-delimited. For Windows, the list is semicolon-delimited.
	EnvVarKubeConfig          = "KUBECONFIG"
	DefaultKubeHomeDir        = ".kube"
	DefaultKubeConfigFileName = "config"
)

Variables

View Source
var ErrorMissingNamespace = errors.New("missing namespace")

Functions

func GetObjects

func GetObjects(files []string) ([]runtime.Object, error)

GetObjects returns the list of objects parsed from the given files.

func LoadDefaultKubeConfig

func LoadDefaultKubeConfig() (*clientcmdapi.Config, error)

LoadDefaultKubeConfig starts by running the clientcmdapi.MigrationRules and then takes the loading rules and returns a clientcmdapi.Config object.

func RetrievesDefaultKubeConfig

func RetrievesDefaultKubeConfig() string

RetrievesDefaultKubeConfig returns a available kubeconfig file.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client represents kubernetes Client.

func New

func New(cfg *Config) *Client

New returns a new Client.

func NewDefault

func NewDefault() *Client

NewDefault returns a new Client with default kubeconfig.

func NewInCluster

func NewInCluster() *Client

NewInCluster returns a new Client with InClusterConfig.

func (*Client) ApplyConfigMap

func (c *Client) ApplyConfigMap(ctx context.Context, namespace, name string, data map[string]string) (*corev1.ConfigMap, error)

ApplyConfigMap updates a ConfigMap, and creates a new ConfigMap if not exist.

func (*Client) ApplySecret

func (c *Client) ApplySecret(ctx context.Context, namespace, name string, data map[string]string) (*corev1.Secret, error)

ApplySecret updates a Secret, and creates a new Secret if not exist.

func (*Client) ApplySecretBytes

func (c *Client) ApplySecretBytes(ctx context.Context, namespace, name string, data map[string][]byte) (*corev1.Secret, error)

ApplySecretBytes is like ApplySecret, but with data map[string][]byte.

func (*Client) CreateClusterRole

func (c *Client) CreateClusterRole(ctx context.Context, role *rbacv1.ClusterRole) (*rbacv1.ClusterRole, error)

CreateClusterRole creates a new ClusterRole.

func (*Client) CreateConfigMap

func (c *Client) CreateConfigMap(ctx context.Context, cm *corev1.ConfigMap) (*corev1.ConfigMap, error)

CreateConfigMap creates a new ConfigMap.

func (*Client) CreateCronJob

func (c *Client) CreateCronJob(ctx context.Context, cronjob *v1beta1.CronJob) (*v1beta1.CronJob, error)

CreateCronJob creates a new CronJob.

func (*Client) CreateDaemonSet

func (c *Client) CreateDaemonSet(ctx context.Context, dsData *v1.DaemonSet) (*v1.DaemonSet, error)

CreateDaemonSet creates a new DaemonSet.

func (*Client) CreateDeployment

func (c *Client) CreateDeployment(ctx context.Context, deploy *v1.Deployment) (*v1.Deployment, error)

CreateDeployment creates a new Deployment.

func (*Client) CreateIngress

func (c *Client) CreateIngress(ctx context.Context, ing *networkv1.Ingress) (*networkv1.Ingress, error)

CreateIngress creates a new Ingress.

func (*Client) CreateIngressClass

func (c *Client) CreateIngressClass(ctx context.Context, ingc *networkv1.IngressClass) (*networkv1.IngressClass, error)

CreateIngressClass creates a new IngressClass.

func (*Client) CreateJob

func (c *Client) CreateJob(ctx context.Context, jobData *batchv1.Job) (*batchv1.Job, error)

CreateJob creates a new Job.

func (*Client) CreateNamespace

func (c *Client) CreateNamespace(ctx context.Context, namespace *corev1.Namespace) (*corev1.Namespace, error)

CreateNamespace creates a Namespace.

func (*Client) CreatePod

func (c *Client) CreatePod(ctx context.Context, pod *corev1.Pod) (*corev1.Pod, error)

CreatePod creates a new Pod.

func (*Client) CreateRole

func (c *Client) CreateRole(ctx context.Context, role *rbacv1.Role) (*rbacv1.Role, error)

CreateRole create a new Role.

func (*Client) CreateSecret

func (c *Client) CreateSecret(ctx context.Context, secret *corev1.Secret) (*corev1.Secret, error)

CreateSecret creates a new Secret.

func (*Client) CreateService

func (c *Client) CreateService(ctx context.Context, svc *corev1.Service) (*corev1.Service, error)

CreateService creates a new Service.

func (*Client) CreateServiceAccount

func (c *Client) CreateServiceAccount(ctx context.Context, sa *corev1.ServiceAccount) (*corev1.ServiceAccount, error)

CreateServiceAccount creates a new ServiceAccount.

func (*Client) CreateStatefulSet

func (c *Client) CreateStatefulSet(ctx context.Context, statefulSet *v1.StatefulSet) (*v1.StatefulSet, error)

CreateStatefulSet creates a new StatefulSet.

func (*Client) Delete

func (c *Client) Delete(files []string) error

Delete is like kubectl delete -f.

func (*Client) DeleteClusterRole

func (c *Client) DeleteClusterRole(ctx context.Context, name string) error

DeleteClusterRole deletes a ClusterRole.

func (*Client) DeleteConfigMap

func (c *Client) DeleteConfigMap(ctx context.Context, namespace, name string) error

DeleteConfigMap deletes a ConfigMap.

func (*Client) DeleteCronJob

func (c *Client) DeleteCronJob(ctx context.Context, namespace, name string) error

DeleteCronJob deletes a CronJob.

func (*Client) DeleteDaemonSet

func (c *Client) DeleteDaemonSet(ctx context.Context, namespace, name string) error

DeleteDaemonSet deletes a DaemonSet.

func (*Client) DeleteDeployment

func (c *Client) DeleteDeployment(ctx context.Context, namespace, name string) error

DeleteDeployment deletes a Deployment.

func (*Client) DeleteIngress

func (c *Client) DeleteIngress(ctx context.Context, namespace, name string) error

DeleteIngress deletes a Ingress.

func (*Client) DeleteIngressClass

func (c *Client) DeleteIngressClass(ctx context.Context, name string) error

DeleteIngressClass deletes a IngressClass.

func (*Client) DeleteJob

func (c *Client) DeleteJob(ctx context.Context, namespace, name string) error

DeleteJob deletes a Job.

func (*Client) DeleteNamespace

func (c *Client) DeleteNamespace(ctx context.Context, name string) error

DeleteNamespace deletes a Node.

func (*Client) DeleteNode

func (c *Client) DeleteNode(ctx context.Context, name string) error

DeleteNode deletes a Node.

func (*Client) DeletePod

func (c *Client) DeletePod(ctx context.Context, namespace, name string) error

DeletePod deletes a Pod.

func (*Client) DeleteRole

func (c *Client) DeleteRole(ctx context.Context, namespace, name string) error

DeleteRole deletes a Role.

func (*Client) DeleteSecret

func (c *Client) DeleteSecret(ctx context.Context, namespace, name string) error

DeleteSecret deletes a Secret.

func (*Client) DeleteService

func (c *Client) DeleteService(ctx context.Context, namespace, name string) error

DeleteService deletes a Service.

func (*Client) DeleteServiceAccount

func (c *Client) DeleteServiceAccount(ctx context.Context, namespace, name string) error

DeleteServiceAccount deletes a ServiceAccount.

func (*Client) DeleteStatefulSet

func (c *Client) DeleteStatefulSet(ctx context.Context, namespace, name string) error

DeleteStatefulSet deletes a StatefulSet.

func (*Client) Dial

func (c *Client) Dial() (kubernetes.Interface, error)

Dial returns a client to the kubernetes apiserver.

func (*Client) DialMetrics

func (c *Client) DialMetrics() (*versioned.Clientset, error)

DialMetrics returns a client to the metrics server.

func (*Client) Exec

func (c *Client) Exec(pod, container, namespace, command string) (string, string, error)

Exec is like kubectl exec.

func (*Client) GetClusterRole

func (c *Client) GetClusterRole(ctx context.Context, name string) (*rbacv1.ClusterRole, error)

GetClusterRole returns a ClusterRole with the given name.

func (*Client) GetClusterRoles

func (c *Client) GetClusterRoles(ctx context.Context, label ...string) (*rbacv1.ClusterRoleList, error)

GetClusterRoles returns a ClusterRoleList.

func (*Client) GetConfigMap

func (c *Client) GetConfigMap(ctx context.Context, namespace, name string) (*corev1.ConfigMap, error)

GetConfigMap returns a ConfigMap with the given name.

func (*Client) GetConfigMaps

func (c *Client) GetConfigMaps(ctx context.Context, namespace string, label ...string) (*corev1.ConfigMapList, error)

GetConfigMaps returns a ConfigMapList.

func (*Client) GetCronJob

func (c *Client) GetCronJob(ctx context.Context, namespace, name string) (*v1beta1.CronJob, error)

GetCronJob returns a GetCronJob with given name.

func (*Client) GetCronJobs

func (c *Client) GetCronJobs(ctx context.Context, namespace string, label ...string) (*v1beta1.CronJobList, error)

GetCronJobs returns a CronJobList.

func (*Client) GetDaemonSet

func (c *Client) GetDaemonSet(ctx context.Context, namespace, name string) (*v1.DaemonSet, error)

GetDaemonSet returns a DaemonSet with given name.

func (*Client) GetDaemonSets

func (c *Client) GetDaemonSets(ctx context.Context, namespace string, label ...string) (*v1.DaemonSetList, error)

GetDaemonSets returns a DaemonSetList.

func (*Client) GetDeployment

func (c *Client) GetDeployment(ctx context.Context, namespace, name string) (*v1.Deployment, error)

GetDeployment returns a Deployment with the given name.

func (*Client) GetDeployments

func (c *Client) GetDeployments(ctx context.Context, namespace string, label ...string) (*v1.DeploymentList, error)

GetDeployments returns a DeploymentList.

func (*Client) GetIngress

func (c *Client) GetIngress(ctx context.Context, namespace, name string) (*networkv1.Ingress, error)

GetIngress returns an Ingress with the given name.

func (*Client) GetIngressClass

func (c *Client) GetIngressClass(ctx context.Context, name string) (*networkv1.IngressClass, error)

GetIngressClass returns an IngressClass with the given name.

func (*Client) GetIngressClasses

func (c *Client) GetIngressClasses(ctx context.Context, label ...string) (*networkv1.IngressClassList, error)

GetIngressClasses returns a IngressClassList.

func (*Client) GetIngresses

func (c *Client) GetIngresses(ctx context.Context, namespace string, label ...string) (*networkv1.IngressList, error)

GetIngresses returns a IngressList.

func (*Client) GetJob

func (c *Client) GetJob(ctx context.Context, namespace, name string) (*batchv1.Job, error)

GetJob returns a Job with given name.

func (*Client) GetJobs

func (c *Client) GetJobs(ctx context.Context, namespace string, label ...string) (*batchv1.JobList, error)

GetJobs returns a JobList.

func (*Client) GetNamespace

func (c *Client) GetNamespace(ctx context.Context, name string) (*corev1.Namespace, error)

GetNamespace returns a Namespace with the given name.

func (*Client) GetNamespaces

func (c *Client) GetNamespaces(ctx context.Context, label ...string) (*corev1.NamespaceList, error)

GetNamespaces returns a NamespaceList.

func (*Client) GetNode

func (c *Client) GetNode(ctx context.Context, name string) (*corev1.Node, error)

GetNode returns a Node with the given name.

func (*Client) GetNodes

func (c *Client) GetNodes(ctx context.Context, label ...string) (*corev1.NodeList, error)

GetNodes returns a NodeList.

func (*Client) GetPod

func (c *Client) GetPod(ctx context.Context, namespace, name string) (*corev1.Pod, error)

GetPod returns a Pod with the given name.

func (*Client) GetPods

func (c *Client) GetPods(ctx context.Context, namespace string, label ...string) (*corev1.PodList, error)

GetPods returns a PodList.

func (*Client) GetRole

func (c *Client) GetRole(ctx context.Context, namespace, name string) (*rbacv1.Role, error)

GetRole returns a Role with the given name.

func (*Client) GetRoles

func (c *Client) GetRoles(ctx context.Context, namespace string, label ...string) (*rbacv1.RoleList, error)

GetRoles returns a RoleList.

func (*Client) GetSecret

func (c *Client) GetSecret(ctx context.Context, namespace, name string) (*corev1.Secret, error)

GetSecret returns a Secret with the given name.

func (*Client) GetSecrets

func (c *Client) GetSecrets(ctx context.Context, namespace string, label ...string) (*corev1.SecretList, error)

GetSecrets returns a SecretList.

func (*Client) GetService

func (c *Client) GetService(ctx context.Context, namespace, name string) (*corev1.Service, error)

GetService returns a Service with the given name.

func (*Client) GetServiceAccount

func (c *Client) GetServiceAccount(ctx context.Context, namespace, name string) (*corev1.ServiceAccount, error)

GetServiceAccount returns a ServiceAccount with the given name.

func (*Client) GetServiceAccounts

func (c *Client) GetServiceAccounts(ctx context.Context, namespace string, label ...string) (*corev1.ServiceAccountList, error)

GetServiceAccounts returns a ServiceAccountList.

func (*Client) GetServices

func (c *Client) GetServices(ctx context.Context, namespace string, label ...string) (*corev1.ServiceList, error)

GetServices returns a ServiceList.

func (*Client) GetStatefulSet

func (c *Client) GetStatefulSet(ctx context.Context, namespace, name string) (*v1.StatefulSet, error)

GetStatefulSet returns a StatefulSet with given name.

func (*Client) GetStatefulSets

func (c *Client) GetStatefulSets(ctx context.Context, namespace string, label ...string) (*v1.StatefulSetList, error)

GetStatefulSets returns a StatefulSetList.

func (*Client) PatchConfigMap

func (c *Client) PatchConfigMap(ctx context.Context, namespace, name string, data []byte) (*corev1.ConfigMap, error)

PatchConfigMap patch a ConfigMap.

func (*Client) PatchSecret

func (c *Client) PatchSecret(ctx context.Context, namespace, name string, data []byte) (*corev1.Secret, error)

PatchSecret patch the given Secret.

func (*Client) ResourceClient

func (c *Client) ResourceClient(gv schema.GroupVersion) (rest.Interface, error)

ResourceClient returns a client to the given schema.GroupVersion.

func (*Client) RestConfig

func (c *Client) RestConfig() (*rest.Config, error)

RestConfig returns a complete rest client config.

func (*Client) UpdateConfigMap

func (c *Client) UpdateConfigMap(ctx context.Context, cm *corev1.ConfigMap) (*corev1.ConfigMap, error)

UpdateConfigMap updates a ConfigMap.

func (*Client) UpdateSecret

func (c *Client) UpdateSecret(ctx context.Context, secret *corev1.Secret) (*corev1.Secret, error)

UpdateSecret updates the given Secret.

func (*Client) WithProxy

func (c *Client) WithProxy(fn func(request *http.Request) (*url.URL, error)) *Client

WithProxy set proxy. Proxy is the proxy func to be used for all requests made by this client. If Proxy is nil, http.ProxyFromEnvironment is used. If Proxy returns a nil *URL, no proxy is used.

type Config

type Config struct {
	// contains filtered or unexported fields
}

Config represents a kubernetes configuration. references: - https://kubernetes.io/docs/concepts/configuration/organize-cluster-access-kubeconfig/ - https://kubernetes.io/docs/tasks/access-application-cluster/configure-access-multiple-clusters/

func NewConfig

func NewConfig(f *genericclioptions.ConfigFlags) *Config

NewConfig returns a new kube client configuration.

func (*Config) ClusterNameFromContext

func (c *Config) ClusterNameFromContext(context string) (string, error)

ClusterNameFromContext returns the cluster associated with the given context.

func (*Config) ClusterNames

func (c *Config) ClusterNames() ([]string, error)

ClusterNames returns all kubeconfig defined clusters.

func (*Config) ConfigAccess

func (c *Config) ConfigAccess() (clientcmd.ConfigAccess, error)

ConfigAccess returns the current kubeconfig api server access configuration.

func (*Config) Contexts

func (c *Config) Contexts() (map[string]*clientcmdapi.Context, error)

Contexts returns all available contexts.

func (*Config) CurrentClusterName

func (c *Config) CurrentClusterName() (string, error)

CurrentClusterName returns the active cluster name.

func (*Config) CurrentContextName

func (c *Config) CurrentContextName() (string, error)

CurrentContextName returns the currently active config context.

func (*Config) CurrentNamespace

func (c *Config) CurrentNamespace() (string, error)

CurrentNamespace retrieves the active namespace.

func (*Config) CurrentUserName

func (c *Config) CurrentUserName() (string, error)

CurrentUserName retrieves the active user name.

func (*Config) Flags

func (c *Config) Flags() *genericclioptions.ConfigFlags

Flags returns configuration flags.

func (*Config) GetCluster

func (c *Config) GetCluster(name string) (*clientcmdapi.Cluster, error)

GetCluster returns a cluster with the given name.

func (*Config) GetContext

func (c *Config) GetContext(n string) (*clientcmdapi.Context, error)

GetContext returns a context with the given name.

func (*Config) GetCredential

func (c *Config) GetCredential(name string) (*clientcmdapi.AuthInfo, error)

GetCredential returns a credential with the given name.

func (*Config) RESTConfig

func (c *Config) RESTConfig() (*rest.Config, error)

RESTConfig returns a complete rest client config.

func (*Config) RawConfig

func (c *Config) RawConfig() (clientcmdapi.Config, error)

func (*Config) RemoveCluster

func (c *Config) RemoveCluster(name string) error

RemoveCluster remove the given cluster from the configuration.

func (*Config) RemoveContext

func (c *Config) RemoveContext(n string) error

RemoveContext remove the given context from the configuration.

func (*Config) RemoveCredential

func (c *Config) RemoveCredential(name string) error

RemoveCredential remove the given credential from the configuration.

func (*Config) SetCluster

func (c *Config) SetCluster(name string, cluster *clientcmdapi.Cluster) error

SetCluster set k/v of the given cluster or add a new cluster if not exist.

func (*Config) SetContext

func (c *Config) SetContext(name string, ctx *clientcmdapi.Context) error

SetContext set k/v of the given context or add a new context if not exist.

func (*Config) SetCredential

func (c *Config) SetCredential(name string, auth *clientcmdapi.AuthInfo) error

SetCredential set k/v of the given credential or add a new credential if not exist.

func (*Config) Timeout

func (c *Config) Timeout() time.Duration

Timeout returns the request timeout if set or the default if not set.

func (*Config) UseContext

func (c *Config) UseContext(name string) error

UseContext changes the current context.

type Operation

type Operation int
const (
	OperationApply Operation = iota
	OperationDelete
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL