kuber

package
v2.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 41 Imported by: 0

Documentation

Index

Constants

View Source
const NodeRoleLabelPrefix = "node-role.kubernetes.io/"

Variables

View Source
var (
	Nodes = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("nodes"),
		Kind:                 "Node",
	}
	Namespaces = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("namespaces"),
		Kind:                 "Namespace",
	}
	LimitRanges = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("limitranges"),
		Kind:                 "LimitRange",
	}
	Pods = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("pods"),
		Kind:                 "Pod",
	}
	ReplicationControllers = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("replicationcontrollers"),
		Kind:                 "ReplicationController",
	}

	Deployments = GroupVersionResourceKind{
		GroupVersionResource: appsv1.SchemeGroupVersion.WithResource("deployments"),
		Kind:                 "Deployment",
	}
	StatefulSets = GroupVersionResourceKind{
		GroupVersionResource: appsv1.SchemeGroupVersion.WithResource("statefulsets"),
		Kind:                 "StatefulSet",
	}
	DaemonSets = GroupVersionResourceKind{
		GroupVersionResource: appsv1.SchemeGroupVersion.WithResource("daemonsets"),
		Kind:                 "DaemonSet",
	}
	ReplicaSets = GroupVersionResourceKind{
		GroupVersionResource: appsv1.SchemeGroupVersion.WithResource("replicasets"),
		Kind:                 "ReplicaSet",
	}

	Jobs = GroupVersionResourceKind{
		GroupVersionResource: batchv1.SchemeGroupVersion.WithResource("jobs"),
		Kind:                 "Job",
	}
	CronJobs = GroupVersionResourceKind{
		GroupVersionResource: batchv1beta1.SchemeGroupVersion.WithResource("cronjobs"),
		Kind:                 "CronJob",
	}
	Ingresses = GroupVersionResourceKind{
		GroupVersionResource: networkingv1beta1.SchemeGroupVersion.WithResource("ingresses"),
		Kind:                 "Ingress",
	}
	IngressClasses = GroupVersionResourceKind{
		GroupVersionResource: networkingv1beta1.SchemeGroupVersion.WithResource("ingressclasses"),
		Kind:                 "IngressClass",
	}
	NetworkPolicies = GroupVersionResourceKind{
		GroupVersionResource: networkingv1.SchemeGroupVersion.WithResource("networkpolicies"),
		Kind:                 "NetworkPolicy",
	}
	Services = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("services"),
		Kind:                 "Service",
	}
	PersistentVolumes = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("persistentvolumes"),
		Kind:                 "PersistentVolume",
	}
	PersistentVolumeClaims = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("persistentvolumeclaims"),
		Kind:                 "PersistentVolumeClaim",
	}
	StorageClasses = GroupVersionResourceKind{
		GroupVersionResource: storagev1.SchemeGroupVersion.WithResource("storageclasses"),
		Kind:                 "StorageClass",
	}
	Roles = GroupVersionResourceKind{
		GroupVersionResource: rbacv1.SchemeGroupVersion.WithResource("roles"),
		Kind:                 "Role",
	}
	RoleBindings = GroupVersionResourceKind{
		GroupVersionResource: rbacv1.SchemeGroupVersion.WithResource("rolebindings"),
		Kind:                 "RoleBinding",
	}
	ClusterRoles = GroupVersionResourceKind{
		GroupVersionResource: rbacv1.SchemeGroupVersion.WithResource("clusterroles"),
		Kind:                 "ClusterRole",
	}
	ClusterRoleBindings = GroupVersionResourceKind{
		GroupVersionResource: rbacv1.SchemeGroupVersion.WithResource("clusterrolebindings"),
		Kind:                 "ClusterRoleBinding",
	}
	ServiceAccounts = GroupVersionResourceKind{
		GroupVersionResource: corev1.SchemeGroupVersion.WithResource("serviceaccounts"),
		Kind:                 "ServiceAccount",
	}
)

Functions

func GetContainersByNode

func GetContainersByNode(pods []corev1.Pod) map[string]int

func GetEntityKey

func GetEntityKey(namespace string, kind string, name string) string

func RangePods

func RangePods(
	pods []corev1.Pod,
	fn func(corev1.Pod) bool) error

Types

type Container

type Container struct {
	// cluster where host of container located in
	Cluster string `json:"cluster,omitempty"`
	// image of container
	Image string `json:"image"`
	// limits of container
	Limits *ContainerResources `json:"limits,omitempty"`
	// requests of container
	Requests *ContainerResources `json:"requests,omitempty"`
	// name of container (not guaranteed to be unique in cluster scope)
	Name string `json:"name"`
	// namespace where pod located in
	Namespace string `json:"namespace"`
	// node where container located in
	Node string `json:"node"`
	// pod where container located in
	Pod string `json:"pod"`
}

Container user type.

func GetContainers

func GetContainers(
	pods []corev1.Pod,
	namespace *string,
	pod *string,
	container *string,
) ([]*Container, error)

type ContainerResources

type ContainerResources struct {
	CPU    int `json:"cpu"`
	Memory int `json:"memory"`
}

ContainerResources user type.

type ContainerResourcesRequirements

type ContainerResourcesRequirements struct {
	Name     string
	Requests *RequestLimit
	Limits   *RequestLimit
}

ContainerResources container resources

type GetWatcherFromKindFunc

type GetWatcherFromKindFunc func(kind string) (Watcher, bool)

type GroupVersionResourceKind

type GroupVersionResourceKind struct {
	schema.GroupVersionResource
	Kind string
}

func KindToGvrk

func KindToGvrk(kind string) (*GroupVersionResourceKind, error)

TODO: Refactor to a map[kind]GVRK

func (GroupVersionResourceKind) String

func (gvrk GroupVersionResourceKind) String() string

type Identifiable

type Identifiable interface {
	GetOwnerReferences() []apisv1.OwnerReference
	GetNamespace() string
	GetKind() string
	GetName() string
	GetAPIVersion() string
}

type Kube

type Kube struct {
	Clientset   *kubernetes.Clientset
	ClientV1    *kapps.AppsV1Client
	ClientBatch *batch.BatchV1beta1Client
	// contains filtered or unexported fields
}

Kube kube struct

func InitKubernetes

func InitKubernetes(
	config *krest.Config,
) (*Kube, error)

func (*Kube) GetAgentPermissions added in v2.8.0

func (kube *Kube) GetAgentPermissions() (string, error)

func (*Kube) GetCronJob added in v2.6.1

func (kube *Kube) GetCronJob(namespace, name string) (
	*kbeta1.CronJob, error,
)

GetCronJobs get cron jobs

func (*Kube) GetCronJobs

func (kube *Kube) GetCronJobs() (
	*kbeta1.CronJobList, error,
)

GetCronJobs get cron jobs

func (*Kube) GetDaemonSet added in v2.6.1

func (kube *Kube) GetDaemonSet(namespace, name string) (
	*v1.DaemonSet, error,
)

func (*Kube) GetDaemonSets

func (kube *Kube) GetDaemonSets() (
	*appsV1.DaemonSetList, error,
)

GetDaemonSets get daemon sets

func (*Kube) GetDeployments

func (kube *Kube) GetDeployments() (*appsV1.DeploymentList, error)

GetDeployments get deployments

func (*Kube) GetLimitRanges

func (kube *Kube) GetLimitRanges() (
	*kv1.LimitRangeList, error,
)

GetLimitRanges get limits and ranges for namespaces

func (*Kube) GetNameSpacePods added in v2.6.1

func (kube *Kube) GetNameSpacePods(namespace string) (*kv1.PodList, error)

GetPods get kubernetes pods for namespace

func (*Kube) GetNamespaceReplicaSets added in v2.6.1

func (kube *Kube) GetNamespaceReplicaSets(namespace string) (
	*appsV1.ReplicaSetList, error,
)

GetReplicaSets get replicasets

func (*Kube) GetNodes

func (kube *Kube) GetNodes() (*kv1.NodeList, error)

GetNodes get kubernetes nodes

func (*Kube) GetPods

func (kube *Kube) GetPods() (*kv1.PodList, error)

GetPods get kubernetes pods

func (*Kube) GetReplicaSets

func (kube *Kube) GetReplicaSets() (
	*appsV1.ReplicaSetList, error,
)

GetReplicaSets get replicasets

func (*Kube) GetReplicationControllers

func (kube *Kube) GetReplicationControllers() (
	*kv1.ReplicationControllerList, error,
)

GetReplicationControllers get replication controllers

func (*Kube) GetResources

func (kube *Kube) GetResources() (
	pods []kv1.Pod,
	limitRanges []kv1.LimitRange,
	resources []Resource,
	rawResources map[string]interface{},
	err error,
)

func (*Kube) GetServerMinorVersion added in v2.8.0

func (kube *Kube) GetServerMinorVersion() (int, error)

func (*Kube) GetServerVersion added in v2.6.1

func (kube *Kube) GetServerVersion() (string, error)

func (*Kube) GetStatefulSet

func (kube *Kube) GetStatefulSet(namespace, name string) (
	*v1.StatefulSet, error,
)

func (*Kube) GetStatefulSets

func (kube *Kube) GetStatefulSets() (
	*appsV1.StatefulSetList, error,
)

GetStatefulSets get stateful sets

func (*Kube) SetResources

func (kube *Kube) SetResources(
	kind string,
	name string,
	namespace string,
	totalResources TotalResources,
) (skipped bool, err error)

SetResources set resources for a service

type Node

type Node struct {
	ID            uuid.UUID    `json:"id,omitempty"`
	Name          string       `json:"name"`
	IP            string       `json:"ip"`
	Roles         string       `json:"roles"`
	KubeletPort   int32        `json:"port"`
	Provider      string       `json:"provider,omitempty"`
	Region        string       `json:"region,omitempty"`
	InstanceType  string       `json:"instance_type,omitempty"`
	InstanceSize  string       `json:"instance_size,omitempty"`
	Capacity      NodeCapacity `json:"capacity"`
	Allocatable   NodeCapacity `json:"allocatable"`
	Containers    int          `json:"containers,omitempty"`
	ContainerList []*Container `json:"container_list,omitempty"`
}

func AddContainerListToNodes

func AddContainerListToNodes(
	nodes []Node,
	pods []corev1.Pod,
	namespace *string,
	pod *string,
	container *string,
) []Node

func GetNodes

func GetNodes(nodes []corev1.Node) []Node

func UpdateNodesContainers

func UpdateNodesContainers(nodes []Node, containers map[string]int) []Node

type NodeCapacity

type NodeCapacity struct {
	CPU              int `json:"cpu"`
	Memory           int `json:"memory"`
	StorageEphemeral int `json:"storage_ephemeral"`
	Pods             int `json:"pods"`
}

func GetNodeCapacity

func GetNodeCapacity(resources corev1.ResourceList) NodeCapacity

type Observer

type Observer struct {
	dynamicinformer.DynamicSharedInformerFactory
	ParentsStore *ParentsStore
	// contains filtered or unexported fields
}

func NewObserver

func NewObserver(
	client dynamic.Interface,
	parentsStore *ParentsStore,
	stopCh chan struct{},
	defaultResync time.Duration,
) *Observer

func (*Observer) FindContainer added in v2.9.0

func (observer *Observer) FindContainer(
	namespaceName string,
	controllerKind string,
	controllerName string,
	containerName string,
) (*corev1.Container, error)

func (*Observer) FindController

func (observer *Observer) FindController(
	namespaceName string,
	controllerKind string,
	controllerName string,
) (*unstructured.Unstructured, error)

func (*Observer) FindPodController added in v2.9.0

func (observer *Observer) FindPodController(namespaceName string, podName string) (string, string, error)

func (*Observer) GetNodes

func (observer *Observer) GetNodes() ([]corev1.Node, error)

func (*Observer) GetPods

func (observer *Observer) GetPods() ([]corev1.Pod, error)

func (*Observer) Start

func (observer *Observer) Start()

func (*Observer) Stop

func (observer *Observer) Stop()

func (*Observer) WaitForCacheSync

func (observer *Observer) WaitForCacheSync() error

func (*Observer) Watch

func (observer *Observer) Watch(gvrk GroupVersionResourceKind) *watcher

func (*Observer) WatchAndWaitForSync

func (observer *Observer) WatchAndWaitForSync(gvrk GroupVersionResourceKind) (*watcher, error)

func (*Observer) WatcherFor

func (observer *Observer) WatcherFor(
	gvrk GroupVersionResourceKind,
) *watcher

type ParentController

type ParentController struct {
	Kind       string `json:"kind"`
	Name       string `json:"name"`
	APIVersion string `json:"api_version"`
	IsWatched  bool   `json:"is_watched"`

	Parent *ParentController `json:"parent"`
}

func GetParents

func GetParents(
	obj Identifiable,
	parentsStore *ParentsStore,
	getWatcher GetWatcherFromKindFunc,
) (*ParentController, error)

func RootParent

func RootParent(parent *ParentController) *ParentController

type ParentsStore

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

TODO: Extract into a dependency

func NewParentsStore

func NewParentsStore() *ParentsStore

func (*ParentsStore) Delete

func (s *ParentsStore) Delete(namespace string, kind string, name string)

func (*ParentsStore) GetParents

func (s *ParentsStore) GetParents(namespace string, kind string, name string) (*ParentController, bool)

func (*ParentsStore) SetParents

func (s *ParentsStore) SetParents(namespace string, kind string, name string, parent *ParentController)

type RawResources

type RawResources struct {
	PodList        *kv1.PodList
	LimitRangeList *kv1.LimitRangeList

	CronJobList *kbeta1.CronJobList

	DeploymentList  *appsV1.DeploymentList
	StatefulSetList *appsV1.StatefulSetList
	DaemonSetList   *appsV1.DaemonSetList
	ReplicaSetList  *appsV1.ReplicaSetList
}

type RequestLimit

type RequestLimit struct {
	CPU    *int64
	Memory *int64
}

RequestLimit request limit

type Resource

type Resource struct {
	Namespace      string
	Name           string
	Kind           string
	Annotations    map[string]string
	ReplicasStatus proto.ReplicasStatus
	Containers     []kv1.Container
	PodRegexp      *regexp.Regexp
}

type ResourceEventHandler

type ResourceEventHandler interface {
	OnAdd(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
	OnUpdate(now time.Time, gvrk GroupVersionResourceKind, oldObj, newObj unstructured.Unstructured)
	OnDelete(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
}

type ResourceEventHandlerFuncs

type ResourceEventHandlerFuncs struct {
	Observer   *Observer
	AddFunc    func(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
	UpdateFunc func(now time.Time, gvrk GroupVersionResourceKind, oldObj, newObj unstructured.Unstructured)
	DeleteFunc func(now time.Time, gvrk GroupVersionResourceKind, obj unstructured.Unstructured)
}

ResourceEventHandlerFuncs is an adaptor to let you easily specify as many or as few of the notification functions as you want while still implementing ResourceEventHandler.

func (ResourceEventHandlerFuncs) OnAdd

OnAdd calls AddFunc if it's not nil.

func (ResourceEventHandlerFuncs) OnDelete

OnDelete calls DeleteFunc if it's not nil.

func (ResourceEventHandlerFuncs) OnUpdate

OnUpdate calls UpdateFunc if it's not nil.

type TotalResources

type TotalResources struct {
	Containers []ContainerResourcesRequirements
}

TotalResources service resources and replicas

type Watcher

type Watcher interface {
	GetGroupVersionResourceKind() GroupVersionResourceKind

	Lister() cache.GenericLister

	// AddEventHandler adds an event handler to the shared informer using the shared informer's resync
	// period.  Events to a single handler are delivered sequentially, but there is no coordination
	// between different handlers.
	AddEventHandler(handler ResourceEventHandler)
	// AddEventHandlerWithResyncPeriod adds an event handler to the
	// shared informer using the specified resync period.  The resync
	// operation consists of delivering to the handler a create
	// notification for every object in the informer's local cache; it
	// does not add any interactions with the authoritative storage.
	AddEventHandlerWithResyncPeriod(handler ResourceEventHandler, resyncPeriod time.Duration)

	// HasSynced returns true if the shared informer's store has been
	// informed by at least one full LIST of the authoritative state
	// of the informer's object collection.  This is unrelated to "resync".
	HasSynced() bool
	// LastSyncResourceVersion is the resource version observed when last synced with the underlying
	// store. The value returned is not synchronized with access to the underlying store and is not
	// thread-safe.
	LastSyncResourceVersion() string
}

Jump to

Keyboard shortcuts

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