k8scache

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2020 License: MIT Imports: 18 Imported by: 0

README

K8SCACHE

local cache for kubernetes apiserver cache

k8scache is based on client-go informer

Feature

support resource list:

  • namespace
  • pod
  • node
  • service
  • replicaSet
  • deployment
  • daemonSet
  • statefulSet
  • event
  • endpoints

Usage

func TestSimple(t *testing.T) {
	cli, err := NewClient()
	assert.Equal(t, err, nil)

	err = cli.BuildForKubecfg()
	assert.Equal(t, err, nil)

	cc, err := NewClusterCache(cli)
	assert.Equal(t, err, nil)

	pods, err := cc.ListPods()
	assert.Equal(t, err, nil)
	assert.Greater(t, len(pods.Items), 0)

	cc.Start()
	cc.SyncCache()

	nslist, err := cc.GetNamespaces()
	assert.Equal(t, err, nil)
	assert.Greater(t, len(nslist), 0)
	fmt.Printf("namespace list %v \n\n", nslist)

	nodes, err := cc.GetNodes()
	assert.Equal(t, err, nil)
	assert.Greater(t, len(nodes), 0)
	fmt.Printf("nodes list %v \n\n", nodes)

	mpods, err := cc.GetPodsWithNS("default")
	assert.Equal(t, err, nil)
	assert.Greater(t, len(mpods), 0)
	fmt.Printf("default pods list %v \n\n", mpods)

	svcs, err := cc.GetServicesWithNS("default")
	assert.Equal(t, err, nil)
	assert.Greater(t, len(svcs), 0)
	fmt.Printf("default services list %v \n\n", svcs)

	reps, err := cc.GetReplicasWithNS("default")
	assert.Equal(t, err, nil)
	assert.Greater(t, len(reps), 0)
	fmt.Printf("default replicas list %v \n\n", reps)

	dms, err := cc.GetDeploymentsWithNS("default")
	assert.Equal(t, err, nil)
	assert.Greater(t, len(dms), 0)
	fmt.Printf("default deployments list %v \n\n", dms)

	time.AfterFunc(1*time.Second, func() {
		cc.Stop()
	})
    cc.Wait()
}

Documentation

Index

Constants

View Source
const (
	TypeNamespace   = "Namespace"
	TypePod         = "Pod"
	TypeNode        = "Node"
	TypeService     = "Service"
	TypeReplicaSet  = "ReplicaSet"
	TypeDeployment  = "Deployment"
	TypeDaemonSet   = "DaemonSet"
	TypeStatefulSet = "StatefulSet"
	TypeEvent       = "Event"
	TypeEndpoint    = "Endpoint"
)

Variables

View Source
var (
	ErrNotFoundNamespace    = errors.New("not found namespace")
	ErrNotFoundName         = errors.New("not found name")
	ErrSyncResourceTimeout  = errors.New("sync resource timeout")
	ErrCacheNotReady        = errors.New("cache not ready, wait to sync full cache in the beginning")
	ErrBeyondExpireInterval = errors.New("beyond expire interval")
	ErrNotSupportResource   = errors.New("not support resource")
)
View Source
var (
	ErrFullQueue    = errors.New("channel is full")
	ErrCauseTimeout = errors.New("timeout")
)

Functions

This section is empty.

Types

type AnyListener added in v1.0.4

type AnyListener struct {
	Name      string
	Namespace string

	sync.Mutex
	// contains filtered or unexported fields
}

func NewAnyListener added in v1.0.5

func NewAnyListener(ns, name string, uequal func(interface{}) bool) *AnyListener

NewAnyListener if ns and name are null, always return true.

func (*AnyListener) Equal added in v1.0.5

func (p *AnyListener) Equal(obj interface{}) bool

func (*AnyListener) Error added in v1.0.5

func (p *AnyListener) Error() error

func (*AnyListener) IsDone added in v1.0.5

func (p *AnyListener) IsDone() bool

func (*AnyListener) Notify added in v1.0.5

func (p *AnyListener) Notify(ev interface{})

func (*AnyListener) Receive added in v1.0.5

func (p *AnyListener) Receive() chan interface{}

func (*AnyListener) Stop added in v1.0.5

func (p *AnyListener) Stop()

type Client

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

func NewClient

func NewClient() (*Client, error)

func (*Client) BuildForCustomKubecfg

func (cs *Client) BuildForCustomKubecfg(kubecfg string) error

func (*Client) BuildForInCluster

func (cs *Client) BuildForInCluster(kubecfg string) error

func (*Client) BuildForKubecfg

func (cs *Client) BuildForKubecfg() error

func (*Client) BuildForParams

func (cs *Client) BuildForParams(addr, token, cert, key, ca string) error

func (*Client) GetClient

func (cs *Client) GetClient() *kubernetes.Clientset

func (*Client) SetClient added in v1.0.3

func (cs *Client) SetClient(cli *kubernetes.Clientset)

type ClusterCache

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

func NewClusterCache

func NewClusterCache(client *Client, opts ...OptionFunc) (*ClusterCache, error)

func (*ClusterCache) GetAllDaemons added in v1.0.1

func (c *ClusterCache) GetAllDaemons(ns string) ([]*v1.DaemonSet, error)

func (*ClusterCache) GetAllDaemonsWithLabels added in v1.0.4

func (c *ClusterCache) GetAllDaemonsWithLabels(ns string, filter labels.Selector) ([]*v1.DaemonSet, error)

func (*ClusterCache) GetAllDeployments added in v1.0.1

func (c *ClusterCache) GetAllDeployments(ns string) ([]*v1.Deployment, error)

func (*ClusterCache) GetAllEndpoints added in v1.0.1

func (c *ClusterCache) GetAllEndpoints(ns string) ([]*corev1.Endpoints, error)

func (*ClusterCache) GetAllEndpointsWithLabels added in v1.0.4

func (c *ClusterCache) GetAllEndpointsWithLabels(ns string, filter labels.Selector) ([]*corev1.Endpoints, error)

func (*ClusterCache) GetAllEvents added in v1.0.1

func (c *ClusterCache) GetAllEvents(ns string) ([]*corev1.Event, error)

func (*ClusterCache) GetAllEventsWithLabels added in v1.0.4

func (c *ClusterCache) GetAllEventsWithLabels(ns string, filter labels.Selector) ([]*corev1.Event, error)

func (*ClusterCache) GetAllPods added in v1.0.1

func (c *ClusterCache) GetAllPods(ns string) ([]*corev1.Pod, error)

func (*ClusterCache) GetAllReplicas added in v1.0.1

func (c *ClusterCache) GetAllReplicas(ns string) ([]*v1.ReplicaSet, error)

func (*ClusterCache) GetAllServices added in v1.0.1

func (c *ClusterCache) GetAllServices(ns string) ([]*corev1.Service, error)

func (*ClusterCache) GetAllStatefuls added in v1.0.1

func (c *ClusterCache) GetAllStatefuls(ns string) ([]*v1.StatefulSet, error)

func (*ClusterCache) GetAllStatefulsWithLabels added in v1.0.4

func (c *ClusterCache) GetAllStatefulsWithLabels(ns string, filter labels.Selector) ([]*v1.StatefulSet, error)

func (*ClusterCache) GetDaemon

func (c *ClusterCache) GetDaemon(ns string, name string) (*v1.DaemonSet, error)

func (*ClusterCache) GetDeployment

func (c *ClusterCache) GetDeployment(ns string, name string) (*v1.Deployment, error)

func (*ClusterCache) GetDeploymentsWithLabels added in v1.0.6

func (c *ClusterCache) GetDeploymentsWithLabels(ns string, filter labels.Selector) ([]*v1.Deployment, error)

func (*ClusterCache) GetEndpoints added in v1.0.1

func (c *ClusterCache) GetEndpoints(ns string, name string) (*corev1.Endpoints, error)

func (*ClusterCache) GetEvents added in v1.0.1

func (c *ClusterCache) GetEvents(ns, name string) (*corev1.Event, error)

func (*ClusterCache) GetNamespace added in v1.0.6

func (c *ClusterCache) GetNamespace(ns string) (*corev1.Namespace, error)

func (*ClusterCache) GetNamespaces

func (c *ClusterCache) GetNamespaces() ([]*corev1.Namespace, error)

func (*ClusterCache) GetNamespacesWithLabels added in v1.0.6

func (c *ClusterCache) GetNamespacesWithLabels(filter labels.Selector) ([]*corev1.Namespace, error)

func (*ClusterCache) GetNode added in v1.0.6

func (c *ClusterCache) GetNode(name string) (*corev1.Node, error)

func (*ClusterCache) GetNodes

func (c *ClusterCache) GetNodes() ([]*corev1.Node, error)

func (*ClusterCache) GetNodesWithLabels added in v1.0.6

func (c *ClusterCache) GetNodesWithLabels(filter labels.Selector) ([]*corev1.Node, error)

func (*ClusterCache) GetPod

func (c *ClusterCache) GetPod(ns string, name string) (*corev1.Pod, error)

func (*ClusterCache) GetPodsWithLabels added in v1.0.6

func (c *ClusterCache) GetPodsWithLabels(ns string, filter labels.Selector) ([]*corev1.Pod, error)

func (*ClusterCache) GetReplica

func (c *ClusterCache) GetReplica(ns string, name string) (*v1.ReplicaSet, error)

func (*ClusterCache) GetReplicasWithLabels added in v1.0.6

func (c *ClusterCache) GetReplicasWithLabels(ns string, filter labels.Selector) ([]*v1.ReplicaSet, error)

func (*ClusterCache) GetService

func (c *ClusterCache) GetService(ns string, name string) (*corev1.Service, error)

func (*ClusterCache) GetServicesWithLabels added in v1.0.6

func (c *ClusterCache) GetServicesWithLabels(ns string, filter labels.Selector) ([]*corev1.Service, error)

func (*ClusterCache) GetStateful

func (c *ClusterCache) GetStateful(ns string, name string) (*v1.StatefulSet, error)

func (*ClusterCache) GetUpdateTime

func (c *ClusterCache) GetUpdateTime(ns string) (time.Time, error)

func (*ClusterCache) ListDaemons

func (c *ClusterCache) ListDaemons(ns string) (*v1.DaemonSetList, error)

func (*ClusterCache) ListDeployments

func (c *ClusterCache) ListDeployments(ns string) (*v1.DeploymentList, error)

func (*ClusterCache) ListEvents

func (c *ClusterCache) ListEvents(ns string) (*corev1.EventList, error)

func (*ClusterCache) ListPods

func (c *ClusterCache) ListPods(ns string) (*corev1.PodList, error)

func (*ClusterCache) ListReplicaSets

func (c *ClusterCache) ListReplicaSets(ns string) (*v1.ReplicaSetList, error)

func (*ClusterCache) ListServices

func (c *ClusterCache) ListServices(ns string) (*corev1.ServiceList, error)

func (*ClusterCache) ListStatefuls

func (c *ClusterCache) ListStatefuls(ns string) (*v1.StatefulSetList, error)

func (*ClusterCache) RegisterListener added in v1.0.4

func (c *ClusterCache) RegisterListener(genre string, lis Listener)

func (*ClusterCache) Reset

func (c *ClusterCache) Reset()

func (*ClusterCache) Start

func (c *ClusterCache) Start()

func (*ClusterCache) Stop

func (c *ClusterCache) Stop()

func (*ClusterCache) SyncCache

func (c *ClusterCache) SyncCache() error

func (*ClusterCache) SyncCacheWithTimeout

func (c *ClusterCache) SyncCacheWithTimeout(timeout time.Duration) error

func (*ClusterCache) SyncDaemonCache

func (c *ClusterCache) SyncDaemonCache(timeout time.Duration) error

func (*ClusterCache) SyncDeploymentsCache

func (c *ClusterCache) SyncDeploymentsCache(timeout time.Duration) error

func (*ClusterCache) SyncEndpointsCache added in v1.0.1

func (c *ClusterCache) SyncEndpointsCache(timeout time.Duration) error

func (*ClusterCache) SyncEventsCache

func (c *ClusterCache) SyncEventsCache(timeout time.Duration) error

func (*ClusterCache) SyncNamespacesCache

func (c *ClusterCache) SyncNamespacesCache(timeout time.Duration) error

func (*ClusterCache) SyncNodesCache

func (c *ClusterCache) SyncNodesCache(timeout time.Duration) error

func (*ClusterCache) SyncPodsCache

func (c *ClusterCache) SyncPodsCache(timeout time.Duration) error

func (*ClusterCache) SyncReplicasCache

func (c *ClusterCache) SyncReplicasCache(timeout time.Duration) error

func (*ClusterCache) SyncServicesCache

func (c *ClusterCache) SyncServicesCache(timeout time.Duration) error

func (*ClusterCache) SyncStatefulCache

func (c *ClusterCache) SyncStatefulCache(timeout time.Duration) error

func (*ClusterCache) ToLabels added in v1.0.6

func (c *ClusterCache) ToLabels(mm map[string]string) labels.Selector

func (*ClusterCache) UnRegisterListener added in v1.0.4

func (c *ClusterCache) UnRegisterListener(genre string, lis Listener)

func (*ClusterCache) Wait

func (c *ClusterCache) Wait()

func (*ClusterCache) WatchDaemons

func (c *ClusterCache) WatchDaemons(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchDeployments

func (c *ClusterCache) WatchDeployments(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchEvents

func (c *ClusterCache) WatchEvents(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchPods

func (c *ClusterCache) WatchPods(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchReplicaSets

func (c *ClusterCache) WatchReplicaSets(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchServices

func (c *ClusterCache) WatchServices(ns string) (<-chan watch.Event, error)

func (*ClusterCache) WatchStatefuls

func (c *ClusterCache) WatchStatefuls(ns string) (<-chan watch.Event, error)

type ClusterCachePool

type ClusterCachePool struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewClusterCachePool added in v1.0.3

func NewClusterCachePool() *ClusterCachePool

func (*ClusterCachePool) Get added in v1.0.3

func (cp *ClusterCachePool) Get(id string) (*ClusterCache, bool)

func (*ClusterCachePool) Set added in v1.0.3

func (cp *ClusterCachePool) Set(id string, cc *ClusterCache)

type DataSet

type DataSet struct {
	Pods         map[string]*corev1.Pod // key: pod.name value: pod body
	Services     map[string]*corev1.Service
	Deployments  map[string]*v1.Deployment
	Replicas     map[string]*v1.ReplicaSet
	Daemons      map[string]*v1.DaemonSet
	StatefulSets map[string]*v1.StatefulSet
	Endpoints    map[string]*corev1.Endpoints
	Events       map[string]map[string][]*corev1.Event // key: Kind, key inside: name
	Namespace    string                                // current ns
	UpdateAT     time.Time

	sync.RWMutex // todo
}

func (*DataSet) CopyDaemons

func (d *DataSet) CopyDaemons() map[string]*v1.DaemonSet

func (*DataSet) CopyDeployments

func (d *DataSet) CopyDeployments() map[string]*v1.Deployment

func (*DataSet) CopyEndpoints added in v1.0.1

func (d *DataSet) CopyEndpoints() map[string]*corev1.Endpoints

func (*DataSet) CopyEvents added in v1.0.1

func (d *DataSet) CopyEvents() map[string]map[string][]*corev1.Event

func (*DataSet) CopyPods

func (d *DataSet) CopyPods() map[string]*corev1.Pod

func (*DataSet) CopyReplicas

func (d *DataSet) CopyReplicas() map[string]*v1.ReplicaSet

func (*DataSet) CopyServices

func (d *DataSet) CopyServices() map[string]*corev1.Service

func (*DataSet) CopyStatefuls

func (d *DataSet) CopyStatefuls() map[string]*v1.StatefulSet

type Listener added in v1.0.4

type Listener interface {
	Receive() chan interface{}
	Notify(interface{})
	IsDone() bool
	Equal(interface{}) bool
	Stop()
}

type ListenerPool added in v1.0.4

type ListenerPool struct {
	sync.Mutex
	// contains filtered or unexported fields
}

type OptionFunc

type OptionFunc func(*ClusterCache) error

func WithContext

func WithContext(ctx context.Context) OptionFunc

func WithFollowNamespaces

func WithFollowNamespaces(nlist []string) OptionFunc

func WithFollowResource

func WithFollowResource(rlist []string) OptionFunc

func WithNamespace

func WithNamespace(ns string) OptionFunc

func WithNotFollowNamespaces

func WithNotFollowNamespaces(nlist []string) OptionFunc

func WithNotFollowResource

func WithNotFollowResource(rlist []string) OptionFunc

func WithStopper

func WithStopper(stopper chan struct{}) OptionFunc

Jump to

Keyboard shortcuts

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