kubernetes

package
v0.0.0-...-51da36d Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2024 License: Apache-2.0 Imports: 27 Imported by: 0

Documentation

Overview

Package kubernetes contains all related files with filling metadata of kubernetes

Index

Constants

View Source
const (
	// AuthTypeNone means no auth is required
	AuthTypeNone AuthType = "none"
	// AuthTypeServiceAccount means to use the built-in service account that
	// K8s automatically provisions for each pod.
	AuthTypeServiceAccount AuthType = "serviceAccount"
	// AuthTypeKubeConfig uses local credentials like those used by kubectl.
	AuthTypeKubeConfig AuthType = "kubeConfig"
	// DefaultKubeConfigPath Default kubeconfig path
	DefaultKubeConfigPath string = "~/.kube/config"
	// DefaultGraceDeletePeriod is 60 seconds
	DefaultGraceDeletePeriod = time.Second * 60
)
View Source
const DeploymentKind = "deployment"
View Source
const ReplicaSetKind = "ReplicaSet"

Variables

View Source
var (
	MetaDataCache = New()

	IsInitSuccess = false
)
View Source
var GlobalNodeInfo = newNodeMap()
View Source
var GlobalPodInfo = newPodMap()
View Source
var GlobalRsInfo = newReplicaSetMap()
View Source
var GlobalServiceInfo = newServiceMap()

Functions

func AddNode

func AddNode(obj interface{})

func AddPod

func AddPod(obj interface{})

func AddReplicaSet

func AddReplicaSet(obj interface{})

func AddService

func AddService(obj interface{})

func CompleteGVK

func CompleteGVK(apiVersion string, kind string) string

CompleteGVK returns the complete string of the workload kind. If apiVersion is not one of the built-in groupVersion(see scheme.go), return {apiVersion}-{kind}; return {kind}, otherwise.

func DeleteNode

func DeleteNode(obj interface{})

func DeletePod

func DeletePod(obj interface{})

func DeleteReplicaSet

func DeleteReplicaSet(obj interface{})

func DeleteService

func DeleteService(obj interface{})

func GetWorkloadDataGroup

func GetWorkloadDataGroup() []*model.DataGroup

func InitK8sHandler

func InitK8sHandler(options ...Option) error

func LockMetadataCache

func LockMetadataCache()

func NodeWatch

func NodeWatch(clientSet *kubernetes.Clientset, handler cache.ResourceEventHandler)

func PodWatch

func PodWatch(clientSet *kubernetes.Clientset, graceDeletePeriod time.Duration, handler cache.ResourceEventHandler)

func RLockMetadataCache

func RLockMetadataCache()

Huge Lock, only used when setup and check the reentrant lock before you call

func RUnlockMetadataCache

func RUnlockMetadataCache()

func RsWatch

func RsWatch(clientSet *kubernetes.Clientset, handler cache.ResourceEventHandler)

func SelectorsMatchLabels

func SelectorsMatchLabels(selectors map[string]string, labels map[string]string) bool

SelectorsMatchLabels return true only if labels match all [keys:values] with selectors

func ServiceWatch

func ServiceWatch(clientSet *kubernetes.Clientset, handler cache.ResourceEventHandler)

func SetupCache

func SetupCache(cache *K8sMetaDataCache, nodeMap *NodeMap, serviceMap *ServiceMap, rsMap *ReplicaSetMap)

func TruncateContainerId

func TruncateContainerId(containerId string) string

TruncateContainerId slices the input containerId into two parts separated by "://", and return the first 12 bytes at most of the second part.

If no second part found, return empty string.

func UnlockMetadataCache

func UnlockMetadataCache()

func UpdateNode

func UpdateNode(objOld interface{}, objNew interface{})

func UpdatePod

func UpdatePod(objOld interface{}, objNew interface{})

func UpdateReplicaSet

func UpdateReplicaSet(objOld interface{}, objNew interface{})

func UpdateService

func UpdateService(objOld interface{}, objNew interface{})

Types

type APIConfig

type APIConfig struct {
	// How to authenticate to the K8s API server.  This can be one of `none`
	// (for no auth), `serviceAccount` (to use the standard service account
	// token provided to the agent pod), or `kubeConfig` to use credentials
	// from user-defined file
	AuthType     AuthType `mapstructure:"auth_type"`
	AuthFilePath string
}

APIConfig contains options relevant to connecting to the K8s API

func (APIConfig) Validate

func (c APIConfig) Validate() error

Validate validates the K8s API config

type AuthType

type AuthType string

AuthType describes the type of authentication to use for the K8s API

type BuiltInScheme

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

func NewKnownScheme

func NewKnownScheme() *BuiltInScheme

func (*BuiltInScheme) IsBuiltInGV

func (s *BuiltInScheme) IsBuiltInGV(groupVersion string) bool

type Controller

type Controller struct {
	Name       string
	Kind       string
	APIVersion string
}

type HostPortMap

type HostPortMap struct {
	HostPortInfo map[string]*K8sContainerInfo
	// contains filtered or unexported fields
}

func NewHostPortMap

func NewHostPortMap() *HostPortMap

type IpPortKey

type IpPortKey struct {
	Ip   string
	Port uint32
}

type K8sContainerInfo

type K8sContainerInfo struct {
	ContainerId string
	Name        string
	HostPortMap map[int32]int32
	RefPodInfo  *K8sPodInfo
}

type K8sMetaDataCache

type K8sMetaDataCache struct {
	ContainerIdInfo map[string]*K8sContainerInfo

	IpContainerInfo map[string]map[uint32]*K8sContainerInfo

	IpServiceInfo map[string]map[uint32]*K8sServiceInfo

	HostPortInfo *HostPortMap
	// contains filtered or unexported fields
}

func New

func New() *K8sMetaDataCache

func (*K8sMetaDataCache) AddByContainerId

func (c *K8sMetaDataCache) AddByContainerId(containerId string, resource *K8sContainerInfo)

func (*K8sMetaDataCache) AddContainerByHostIpPort

func (c *K8sMetaDataCache) AddContainerByHostIpPort(hostIp string, hostPort uint32, containerInfo *K8sContainerInfo)

func (*K8sMetaDataCache) AddContainerByIpPort

func (c *K8sMetaDataCache) AddContainerByIpPort(ip string, port uint32, resource *K8sContainerInfo)

func (*K8sMetaDataCache) AddServiceByIpPort

func (c *K8sMetaDataCache) AddServiceByIpPort(ip string, port uint32, resource *K8sServiceInfo)

func (*K8sMetaDataCache) ClearAll

func (c *K8sMetaDataCache) ClearAll()

func (*K8sMetaDataCache) DeleteByContainerId

func (c *K8sMetaDataCache) DeleteByContainerId(containerId string)

func (*K8sMetaDataCache) DeleteContainerByHostIpPort

func (c *K8sMetaDataCache) DeleteContainerByHostIpPort(hostIp string, hostPort uint32)

func (*K8sMetaDataCache) DeleteContainerByIpPort

func (c *K8sMetaDataCache) DeleteContainerByIpPort(ip string, port uint32)

func (*K8sMetaDataCache) DeleteServiceByIpPort

func (c *K8sMetaDataCache) DeleteServiceByIpPort(ip string, port uint32)

func (*K8sMetaDataCache) GetByContainerId

func (c *K8sMetaDataCache) GetByContainerId(containerId string) (*K8sContainerInfo, bool)

func (*K8sMetaDataCache) GetCacheContainerIdInfoSize

func (c *K8sMetaDataCache) GetCacheContainerIdInfoSize() int

func (*K8sMetaDataCache) GetContainerByHostIpPort

func (c *K8sMetaDataCache) GetContainerByHostIpPort(hostIp string, hostPort uint32) (*K8sContainerInfo, bool)

func (*K8sMetaDataCache) GetContainerByIpPort

func (c *K8sMetaDataCache) GetContainerByIpPort(ip string, port uint32) (*K8sContainerInfo, bool)

func (*K8sMetaDataCache) GetNodeNameByIp

func (c *K8sMetaDataCache) GetNodeNameByIp(ip string) (string, bool)

func (*K8sMetaDataCache) GetPodByContainerId

func (c *K8sMetaDataCache) GetPodByContainerId(containerId string) (*K8sPodInfo, bool)

func (*K8sMetaDataCache) GetPodByIp

func (c *K8sMetaDataCache) GetPodByIp(ip string) (*K8sPodInfo, bool)

func (*K8sMetaDataCache) GetPodByIpPort

func (c *K8sMetaDataCache) GetPodByIpPort(ip string, port uint32) (*K8sPodInfo, bool)

func (*K8sMetaDataCache) GetServiceByIpPort

func (c *K8sMetaDataCache) GetServiceByIpPort(ip string, port uint32) (*K8sServiceInfo, bool)

func (*K8sMetaDataCache) String

func (c *K8sMetaDataCache) String() string

type K8sPodInfo

type K8sPodInfo struct {
	UID          string
	Ip           string
	PodName      string
	Ports        []int32
	HostPorts    []int32
	ContainerIds []string
	Labels       map[string]string
	// TODO: There may be multiple kinds of workload or services for the same pod
	WorkloadKind string
	WorkloadName string
	Namespace    string
	NodeName     string
	NodeAddress  string

	ServiceInfo *K8sServiceInfo
	// contains filtered or unexported fields
}

type K8sServiceInfo

type K8sServiceInfo struct {
	Ip          string
	ServiceName string
	Namespace   string

	Selector map[string]string
	// TODO: How to delete the workload info when it is deleted?
	WorkloadKind string
	WorkloadName string
	// contains filtered or unexported fields
}

type MetaDataProviderConfig

type MetaDataProviderConfig struct {
	Enable bool `mapstructure:"enable"`
	// print every K8s Metadata received from mp, used for debug
	EnableTrace bool `mapstructure:"enable_trace"`
	// Endpoint is where metadata-provider deloyed and provide service
	// e.g "http://localhost:9504"
	Endpoint string `mapstructure:"endpoint"`
}

type NodeInfo

type NodeInfo struct {
	Ip     string
	Name   string
	Labels map[string]string
}

type NodeMap

type NodeMap struct {
	Info map[string]*NodeInfo
	// contains filtered or unexported fields
}

type Option

type Option func(cfg *config)

func WithAuthType

func WithAuthType(authType AuthType) Option

WithAuthType sets way of authenticating kubernetes api-server Supported AuthTypeNone, AuthTypeServiceAccount, AuthTypeKubeConfig

func WithFetchReplicaSet

func WithFetchReplicaSet(fetch bool) Option

WithFetchReplicaSet sets whether to fetch ReplicaSet information.

func WithGraceDeletePeriod

func WithGraceDeletePeriod(interval int) Option

WithGraceDeletePeriod sets the graceful period of deleting Pod's metadata after receiving "delete" event from client-go.

func WithKubeConfigDir

func WithKubeConfigDir(dir string) Option

WithKubeConfigDir sets the directory where the file "kubeconfig" is stored

func WithMetaDataProviderConfig

func WithMetaDataProviderConfig(mpCfg *MetaDataProviderConfig, listAndWatch func(SetPreprocessingMetaDataCache) error) Option

func WithNodeEventHander

func WithNodeEventHander(handler cache.ResourceEventHandler) Option

func WithPodEventHander

func WithPodEventHander(handler cache.ResourceEventHandler) Option

func WithReplicaSetEventHander

func WithReplicaSetEventHander(handler cache.ResourceEventHandler) Option

func WithServiceEventHander

func WithServiceEventHander(handler cache.ResourceEventHandler) Option

type ReplicaSetMap

type ReplicaSetMap struct {
	// Key is ${namespace}/${name}
	Info map[string]Controller
	// contains filtered or unexported fields
}

func (*ReplicaSetMap) GetOwnerReference

func (rs *ReplicaSetMap) GetOwnerReference(key string) (Controller, bool)

func (*ReplicaSetMap) GetSize

func (rs *ReplicaSetMap) GetSize() int

type ServiceMap

type ServiceMap struct {
	// Service name could be duplicated in different namespace, so here
	// service name must not be the key of map. Therefore, a map with the
	// following structure is constructed.
	//
	// namespace1:
	//   servicename1: ServiceInfo{}
	//   servicename2: ServiceInfo{}
	// namespace2:
	//   servicename1: ServiceInfo{}
	ServiceMap map[string]map[string]*K8sServiceInfo
	// contains filtered or unexported fields
}

func (*ServiceMap) GetServiceMatchLabels

func (s *ServiceMap) GetServiceMatchLabels(namespace string, labels map[string]string) []*K8sServiceInfo

GetServiceMatchLabels gets K8sServiceInfos which match labels in such namespace. Return empty slice if not found. Note there may be multiple matches.

type SetPreprocessingMetaDataCache

type SetPreprocessingMetaDataCache func(cache *K8sMetaDataCache, nodeMap *NodeMap, serviceMap *ServiceMap, rsMap *ReplicaSetMap)

Jump to

Keyboard shortcuts

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