ok8s

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: Apache-2.0 Imports: 21 Imported by: 0

README ΒΆ

πŸ’» A High encapsulation Kubernetes Golang-api

πŸ˜ƒ Easy use make you Happy

GoDoc

πŸŽ‰ Completion Resource

  • Ingress
  • Service
  • Deployment
  • ConfigMap
  • Secret
  • Pod

πŸ‘Resource Option

  • Create
  • Delete
  • List
  • Update
  • Watch
  • IsExits

Pod's Metrics methods

  • GetCpu
  • GetMem

πŸ”§ EXAMPLE

Your can cat example Directory Complete code

package main

import (
	"fmt"
	"github.com/chulinx/ok8s"
)

func main() {
	nsName := "test"
	client := ok8s.NewTestClientSet()

	// Create
	namespaceInstance := ok8s.NewNameSpace(client)
	namespaceInstance.Create(nsName)

	deploymentInstance := ok8s.NewDeployment(client)
	deploymentInstance.Create(nsName, deployStruct)

	svcInstance := ok8s.NewService(client)
	svcInstance.Create(nsName, serviceStruct)

	ingressInstance := ok8s.NewIngress(client)
	ingressInstance.Create(nsName, ingressStruct)

	// Get
	_, deployment := deploymentInstance.Get(nsName, resourceName)
	FormatPrint("Deployment", deployment.Deployment.Name)

	_, service := svcInstance.Get(nsName, resourceName)
	FormatPrint("Service", service.Service.Name)

	_, ingress := ingressInstance.Get(nsName, resourceName)
	FormatPrint("Ingress", ingress.Ingress.Name)

	// Delete
	deploymentInstance.Delete(nsName, resourceName)
	if deploymentInstance.IsExits(nsName, resourceName) {
		fmt.Println("Not delete")
	} else {
		fmt.Println("Delete Success")
	}

	svcInstance.Delete(nsName, resourceName)
	if svcInstance.IsExits(nsName, resourceName) {
		fmt.Println("Not delete")
	} else {
		fmt.Println("Delete Success")
	}

	ingressInstance.Delete(nsName, resourceName)
	if ingressInstance.IsExits(nsName, resourceName) {
		fmt.Println("Not delete")
	} else {
		fmt.Println("Delete Success")
	}

	namespaceInstance.Delete(nsName)
}

func FormatPrint(Type, name string) {
	fmt.Printf("Resource: %s     Name: %s\n", Type, name)
}

Documentation ΒΆ

Overview ΒΆ

Package ok8s help you manger kubernetes resource

A High encapsulation Kubernetes Golang-api base client-go ΒΆ

As quick start

nsName := "test"
client := ok8s.NewTestClientSet()

Create deployment

namespaceInstance := ok8s.NewNameSpace(client)
namespaceInstance.Create(nsName)

Get deployment

_, deployment := deploymentInstance.Get(nsName, resourceName)
FormatPrint("Deployment", deployment.Deployment.Name)

Delete deployment

deploymentInstance.Delete(nsName, resourceName)
if deploymentInstance.IsExits(nsName, resourceName) {
	fmt.Println("Not delete")
} else {
	fmt.Println("Delete Success")
}

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func AllToMi ΒΆ

func AllToMi(value string) string

func BuildConfig ΒΆ added in v0.1.8

func BuildConfig(kubeConfigPath ...string) (*rest.Config, error)

func ClientSet ΒΆ

func ClientSet(file string) *kubernetes.Clientset

func DefaultTimeOut ΒΆ

func DefaultTimeOut() context.Context

func MetricsClientSet ΒΆ

func MetricsClientSet(file string) *versioned.Clientset

MetricsClientSet use get pod cpu mem and other monitor metrics

func MetricsToInt ΒΆ

func MetricsToInt(metric, sep string) int

func NewInformer ΒΆ

func NewInformer(sets *kubernetes.Clientset)

func TimeOut ΒΆ

func TimeOut(time time.Duration) context.Context

Types ΒΆ

type ClientSets ΒΆ

type ClientSets struct {
	ClientSet *kubernetes.Clientset
}

ClientSet kubernetes.Clientset

func NewClientSet ΒΆ

func NewClientSet(clientset *kubernetes.Clientset) *ClientSets

func NewTestClientSet ΒΆ

func NewTestClientSet() ClientSets

func (*ClientSets) Prefix ΒΆ

func (c *ClientSets) Prefix(namespace string) interface{}

type ConfigMapType ΒΆ

type ConfigMapType struct {
	ClientSets
	KResource
}

func (*ConfigMapType) Create ΒΆ

func (c *ConfigMapType) Create(namespace string, resource interface{}) (bool, error)

The resource struct is apicorev1.ConfigMapType

func (*ConfigMapType) Delete ΒΆ

func (c *ConfigMapType) Delete(namespace, name string) (bool, error)

func (*ConfigMapType) Get ΒΆ

func (c *ConfigMapType) Get(namespace, name string) (bool, KResource)

Get return apicorev1.ConfigMapType

func (*ConfigMapType) IsExits ΒΆ

func (c *ConfigMapType) IsExits(namespace, name string) bool

func (*ConfigMapType) List ΒΆ

func (c *ConfigMapType) List(namespace string) (KResource, error)

List return multiple apicorev1.ConfigMapType

func (*ConfigMapType) Update ΒΆ

func (c *ConfigMapType) Update(namespace string, resource interface{}) bool

The resource struct is apicorev1.ConfigMapType

func (*ConfigMapType) Watch ΒΆ

func (d *ConfigMapType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

type DeploymentType ΒΆ

type DeploymentType struct {
	ClientSets
	KResource
}

func (*DeploymentType) Create ΒΆ

func (d *DeploymentType) Create(namespace string, resource interface{}) (bool, error)

func (*DeploymentType) Delete ΒΆ

func (d *DeploymentType) Delete(namespace, name string) (bool, error)

func (*DeploymentType) Get ΒΆ

func (d *DeploymentType) Get(namespace, name string) (bool, KResource)

func (*DeploymentType) IsExits ΒΆ

func (d *DeploymentType) IsExits(namespace, name string) bool

func (*DeploymentType) Labels ΒΆ

func (d *DeploymentType) Labels(deployment, namespace string) map[string]map[string]string

func (*DeploymentType) List ΒΆ

func (d *DeploymentType) List(namespace string) (KResource, error)

func (*DeploymentType) Prefix ΒΆ

func (d *DeploymentType) Prefix(namespace string) interface{}

func (*DeploymentType) Update ΒΆ

func (d *DeploymentType) Update(namespace string, resource interface{}) bool

func (*DeploymentType) Watch ΒΆ

func (d *DeploymentType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

type IngressType ΒΆ

type IngressType struct {
	ClientSets
	KResource
}

func (*IngressType) Create ΒΆ

func (i *IngressType) Create(namespace string, resource interface{}) (bool, error)

func (*IngressType) Delete ΒΆ

func (i *IngressType) Delete(namespace, name string) (bool, error)

func (*IngressType) Get ΒΆ

func (i *IngressType) Get(namespace, name string) (bool, KResource)

func (*IngressType) IsExits ΒΆ

func (i *IngressType) IsExits(namespace, name string) bool

func (*IngressType) List ΒΆ

func (i *IngressType) List(namespace string) (KResource, error)

func (*IngressType) Prefix ΒΆ

func (i *IngressType) Prefix(namespace string) interface{}

func (*IngressType) Update ΒΆ

func (i *IngressType) Update(namespace string, resource interface{}) bool

func (*IngressType) Watch ΒΆ

func (i *IngressType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

type K8sApi ΒΆ

type K8sApi interface {
	// The resource api interface
	Prefix(namespace string) interface{}
	// Create resource from namespace and resource struct
	/* For DeploymentType k8s.io/api/apps/v1 v1.deployment
	 * For Configmap k8s.io/api/core/v1  v1.configmap
	 */
	Create(namespace string, resource interface{}) (bool, error)
	// Get a resource from namespace and resource name
	Get(namespace, name string) (bool, KResource)
	// IsExits judge resource exits
	IsExits(namespace, name string) bool
	// Delete a resource from namespace and resource name
	Delete(namespace, name string) (bool, error)
	// List multiple resource from one namespace
	List(namespace string) (KResource, error)
	// Update a resource
	Update(namespace string, resource interface{}) bool
	//Labels(DeploymentType, namespace string) map[string]map[string]string
	// Watch a resource
	/* Example:
	*  eventFuncs := new(cache.ResourceEventHandlerFuncs)
	*  eventFuncs.DeleteFunc= func(obj interface{}) {
	*  		DeploymentType := obj.(*appsv1.DeploymentType)
	*  		log.Printf("delete DeploymentType %s",DeploymentType.Name)
	*  		fmt.Println(Deployment.Labels,DeploymentType.Spec.Template.Labels)
	*  }
	*  eventFuncs.AddFunc= func(obj interface{}) {
	*  		DeploymentType := obj.(*appsv1.DeploymentType)
	*  		log.Printf("add DeploymentType %s",DeploymentType.Name)
	*  		fmt.Println(DeploymentType.Labels,DeploymentType.Spec.Template.Labels)
	*  }
	*  Watch("default",eventFuncs)
	 */
	Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)
}

K8sApi define resource operate method

func NewConfigMap ΒΆ

func NewConfigMap(cs ClientSets) K8sApi

func NewDeployment ΒΆ

func NewDeployment(cs ClientSets) K8sApi

func NewIngress ΒΆ

func NewIngress(cs ClientSets) K8sApi

func NewK8 ΒΆ

func NewK8(inter K8sApi) K8sApi

func NewPod ΒΆ

func NewPod(cs ClientSets) K8sApi

NewPod is Pod's K8sApi interface

func NewSecret ΒΆ

func NewSecret(cs ClientSets) K8sApi

func NewService ΒΆ

func NewService(cs ClientSets) K8sApi

type KResource ΒΆ

type KResource struct {
	Deployment     appsv1.Deployment
	DeploymentList appsv1.DeploymentList
	Configmap      apicorev1.ConfigMap
	ConfigmapList  apicorev1.ConfigMapList
	Service        apicorev1.Service
	ServiceList    apicorev1.ServiceList
	Secret         apicorev1.Secret
	SecretList     apicorev1.SecretList
	Ingress        v1beta1.Ingress
	IngressList    v1beta1.IngressList
	Pod            apicorev1.Pod
	PodList        apicorev1.PodList
}

Kubernetes Resource struct

Unified all resource

type MetricsClientSets ΒΆ

type MetricsClientSets struct {
	MClientSets *versioned.Clientset
}

type MetricsType ΒΆ added in v0.1.6

type MetricsType struct {
	MetricsClientSets
	PodType
}

func NewMetrics ΒΆ added in v0.1.6

func NewMetrics(kubeConfig string) *MetricsType

func (*MetricsType) GetContainerCpu ΒΆ added in v0.1.6

func (m *MetricsType) GetContainerCpu(namespace, name string) (map[string]int64, error)

GetContainerCpu return a pod all containers cpu(map[string]int)

func (*MetricsType) GetLimitResource ΒΆ added in v0.1.6

func (m *MetricsType) GetLimitResource(namespace, name string) map[string]int64

func (*MetricsType) GetPodMetrics ΒΆ added in v0.1.7

func (m *MetricsType) GetPodMetrics(namespace, name string) map[string]int64

GetPodMetrics return a map. This contain cpu(m)、mem(Mi) and storage

func (*MetricsType) GetRequestResource ΒΆ added in v0.1.6

func (m *MetricsType) GetRequestResource(namespace, name string) map[string]int64

default unit cpu is,m mem is Mi

func (*MetricsType) GetSinglePodCpuUsage ΒΆ added in v0.1.7

func (m *MetricsType) GetSinglePodCpuUsage(namespace, name string) (cpu int64)

GetSinglePodCpuUsage return pod cpu(int)

func (*MetricsType) GetSinglePodMemUsage ΒΆ added in v0.1.7

func (m *MetricsType) GetSinglePodMemUsage(namespace, name string) int64

GetSinglePodMemUsage return mem(int)

type NameSpace ΒΆ

type NameSpace struct {
	ClientSets
}

func NewNameSpace ΒΆ

func NewNameSpace(cs ClientSets) *NameSpace

func (*NameSpace) Create ΒΆ

func (ns *NameSpace) Create(namespace string) (bool, error)

func (*NameSpace) Delete ΒΆ

func (ns *NameSpace) Delete(name string) (bool, error)

func (*NameSpace) IsExits ΒΆ

func (ns *NameSpace) IsExits(name string) bool

func (*NameSpace) Label ΒΆ

func (ns *NameSpace) Label(name string, labels map[string]string) (bool, error)

func (*NameSpace) List ΒΆ

func (ns *NameSpace) List() (v1.NamespaceList, error)

func (*NameSpace) Prefix ΒΆ

func (ns *NameSpace) Prefix() v12.NamespaceInterface

func (*NameSpace) ShowLabels ΒΆ

func (ns *NameSpace) ShowLabels(name string) map[string]string

type PodType ΒΆ

type PodType struct {
	ClientSets
	KResource
}

func NewPodFunc ΒΆ added in v0.1.6

func NewPodFunc(kubeConfig string) *PodType

func (*PodType) Create ΒΆ

func (p *PodType) Create(namespace string, resource interface{}) (bool, error)

func (*PodType) Delete ΒΆ

func (p *PodType) Delete(namespace, name string) (bool, error)

func (*PodType) Get ΒΆ

func (p *PodType) Get(namespace, name string) (bool, KResource)

func (*PodType) GetByLabel ΒΆ

func (p *PodType) GetByLabel(namespace string, label string) (*corev1.PodList, error)

func (*PodType) IsExits ΒΆ

func (p *PodType) IsExits(namespace, name string) bool

func (*PodType) List ΒΆ

func (p *PodType) List(namespace string) (KResource, error)

func (*PodType) Prefix ΒΆ

func (p *PodType) Prefix(namespace string) interface{}

func (*PodType) Update ΒΆ

func (p *PodType) Update(namespace string, resource interface{}) bool

func (*PodType) Watch ΒΆ

func (p *PodType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

type SecretType ΒΆ

type SecretType struct {
	ClientSets
	KResource
}

func (*SecretType) Create ΒΆ

func (s *SecretType) Create(namespace string, resource interface{}) (bool, error)

func (*SecretType) Delete ΒΆ

func (s *SecretType) Delete(namespace, name string) (bool, error)

func (*SecretType) Get ΒΆ

func (s *SecretType) Get(namespace, name string) (bool, KResource)

func (*SecretType) IsExits ΒΆ

func (s *SecretType) IsExits(namespace, name string) bool

func (*SecretType) List ΒΆ

func (s *SecretType) List(namespace string) (KResource, error)

func (*SecretType) Prefix ΒΆ

func (s *SecretType) Prefix(namespace string) interface{}

func (*SecretType) Update ΒΆ

func (s *SecretType) Update(namespace string, resource interface{}) bool

func (*SecretType) Watch ΒΆ

func (s *SecretType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

type ServiceType ΒΆ

type ServiceType struct {
	ClientSets
	KResource
}

func (*ServiceType) Create ΒΆ

func (s *ServiceType) Create(namespace string, resource interface{}) (bool, error)

func (*ServiceType) Delete ΒΆ

func (s *ServiceType) Delete(namespace, name string) (bool, error)

func (*ServiceType) Get ΒΆ

func (s *ServiceType) Get(namespace, name string) (bool, KResource)

func (*ServiceType) IsExits ΒΆ

func (s *ServiceType) IsExits(namespace, name string) bool

func (*ServiceType) List ΒΆ

func (s *ServiceType) List(namespace string) (KResource, error)

func (*ServiceType) Prefix ΒΆ

func (s *ServiceType) Prefix(namespace string) interface{}

func (*ServiceType) Update ΒΆ

func (s *ServiceType) Update(namespace string, resource interface{}) bool

func (*ServiceType) Watch ΒΆ

func (s *ServiceType) Watch(namespace string, eventFuncs cache.ResourceEventHandlerFuncs)

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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