k8s

package
v0.0.0-...-ec72bf4 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2024 License: Apache-2.0 Imports: 57 Imported by: 0

Documentation

Overview

Package k8s abstracts all Kubernetes specific behaviour

Package k8s abstracts all Kubernetes specific behaviour

Package k8s contains all k8s related logic. +groupName=pkg

Package k8s abstracts all Kubernetes specific behaviour

Index

Constants

View Source
const (
	CCEPrefix = "cce.baidubce.com/"

	// LabelNodeInstanceType type of k8s node
	LabelNodeInstanceType = "node.kubernetes.io/instance-type"

	// LabelPodUseFixedIP Only the pod marked with this annotation when creating the pod will enable the fixed IP
	LabelPodUseFixedIP = "cce.baidubce.com/fixedip"
	LabelNodeName      = "cce.baidubce.com/node"
	LabelInstanceID    = "cce.baidubce.com/instanceid"
	LabelContainerID   = "cce.baidubce.com/containerid"

	LabelClusterRole            = "cluster-role"
	LabelClusterRoleValueMaster = "master"
	LabelClusterRoleValueNode   = "node"

	// AnnotationFixedIPTTLSeconds If the fixed IP is stuck for a long time when the pod fails, IP recycling will be triggered
	// default value is 6040800 (7)
	AnnotationFixedIPTTLSeconds = "fixedip.cce.baidubce.com/ttl"

	// AnnotationNodeAnnotationSynced node have synced with cloud
	AnnotationNodeAnnotationSynced = "node.cce.baidubce.com/annotation-synced"

	// AnnotationNodeLabelSynced node have synced with cloud
	AnnotationCCEInstanceLabel = "kubernetes.io/cce.instance.labels"

	// AnnotationNodeLabelSynced node speicified subnets id use to create eni and allocate ip
	AnnotationNodeEniSubnetIDs = "network.cce.baidubce.com/node-eni-subnet-ids"

	AnnotationNodeMaxENINum           = "network.cce.baidubce.com/node-max-eni-num"
	AnnotationNodeMaxPerENIIPsNum     = "network.cce.baidubce.com/node-eni-max-ips-num"
	AnnotationNodeMaxRdmaEniNum       = "network.cce.baidubce.com/node-max-rdma-eni-num"
	AnnotationNodeMaxPerRdmaEniIpsNum = "network.cce.baidubce.com/node-rdma-eni-max-ips-num"

	// AnnotationUseEnterpriseSecurityGroupIDs use enterprise security group
	AnnotationUseEnterpriseSecurityGroupIDs = "network.cce.baidubce.com/use-esg-ids"
	AnnotationUseSecurityGroupIDs           = "network.cce.baidubce.com/sg-ids"

	// FinalizerOfCCEEndpointRemoteIP finalizer to remove ip from remote iaas
	FinalizerOfCCEEndpointRemoteIP = "RemoteIPFinalizer"

	// FinalizerOfNetResourceSetRoute finalizer to remove ip from remote iaas
	FinalizerOfNetResourceSetRoute = "RemoteRouteFinalizer"

	// LabelENIUseMode is the label used to store the ENI use mode of the node.
	// if the label is set before the NetResourceSet is created, we wiil use the label
	// value as the ENI use mode of the node.
	LabelENIUseMode = "cce.baidubce.com/eni-use-mode"
	LabelENIType    = "cce.baidubce.com/eni-type"

	// VPCIDLabel is the label used to store the VPC ID of the node.
	VPCIDLabel = "cce.baidubce.com/vpc-id"

	// AnnotationIPResourceCapacitySynced is the annotation used to store the ip resource capacity synced status of the node.
	AnnotationIPResourceCapacitySynced = "cce.baidubce.com/ip-resource-capacity-synced"

	// LabelAvailableZone is the label used to store the available zone of the node.
	LabelAvailableZone = "cce.baidubce.com/available-zone"
	LabelRegion        = "topology.kubernetes.io/region"
	LabelZone          = "topology.kubernetes.io/zone"

	// LabelOwnerByReference this label is used to mark the owner of the resource.
	// for example, if a psts is created by a cpsts, the label will be set to
	LabelOwnerByReference = CCEPrefix + "owner-by-reference"
)
View Source
const (
	// maximum number of operations a single json patch may contain.
	// See https://github.com/kubernetes/kubernetes/pull/74000
	MaxJSONPatchOperations = 10000
)

Variables

View Source
var (
	// LabelPodUseFixedIP use fixed ip
	ValueStringTrue = "true"

	// annotation for PodSubnetTopologySpread
	AnnotationPodSubnetTopologySpread = CCEPrefix + "PodSubnetTopologySpread"

	// cce defined k8s resource name
	ResourceIPForNode      = corev1.ResourceName(CCEPrefix + "ip")
	ResourceENIForNode     = corev1.ResourceName(CCEPrefix + "eni")
	ResourceRdmaIpForNode  = corev1.ResourceName(CCEPrefix + "rdmaip")
	ResourceRdmaEniForNode = corev1.ResourceName(CCEPrefix + "rdmaeni")

	// CrossVPCEni resource name
	ResourceCrossVPCEni = corev1.ResourceName("cross-vpc-eni.cce.io/eni")

	// topodlogy key for psts
	TopologyKeyOfPod = "topology.kubernetes.io/zone"
	// AnnotationDisablePSTSPodAffinity This annotation is included on the pod, which means that the pod does not expect to use the scheduling function extended by pSTS
	AnnotationDisablePSTSPodAffinity = CCEPrefix + "DisablePodSubnetTopologySpreadScheduler"

	// AnnotationExternalENI means ENI was created by external system
	AnnotationExternalENI = "cce.baidubce.com/external-eni"

	// AnnotationExternalENI means ENI primary IP was created by cce
	AnnotationENIIPv6PrimaryIP = "cce.baidubce.com/ipv6-primary-ip"

	// cce defined net resource set annotations in k8s
	AnnotationRDMAInfoMacAddress  = CCEPrefix + "rdma-mac-address"
	AnnotationRDMAInfoVifFeatures = CCEPrefix + "rdma-vif-features"
)
View Source
var (
	PodAnnotationCrossVPCEniUserID                          = "cross-vpc-eni.cce.io/userID"
	PodAnnotationCrossVPCEniSubnetID                        = "cross-vpc-eni.cce.io/subnetID"
	PodAnnotationCrossVPCEniSecurityGroupIDs                = "cross-vpc-eni.cce.io/securityGroupIDs"
	PodAnnotationCrossVPCEniPrivateIPAddress                = "cross-vpc-eni.cce.io/privateIPAddress"
	PodAnnotationCrossVPCEniVPCCIDR                         = "cross-vpc-eni.cce.io/vpcCidr"
	PodAnnotationCrossVPCEniDefaultRouteInterfaceDelegation = "cross-vpc-eni.cce.io/defaultRouteInterfaceDelegation"
	PodAnnotationCrossVPCEniDefaultRouteExcludedCidrs       = "cross-vpc-eni.cce.io/defaultRouteExcludedCidrs"

	NodeAnnotationMaxCrossVPCEni = "cross-vpc-eni.cce.io/maxEniNumber"
	NodeLabelMaxCrossVPCEni      = "cross-vpc-eni.cce.io/max-eni-number"

	PodLabelOwnerNamespace = "cce.io/ownerNamespace"
	PodLabelOwnerName      = "cce.io/ownerName"
	PodLabelOwnerNode      = "cce.io/ownerNode"
	PodLabelOwnerInstance  = "cce.io/ownerInstance"
)

crossvpc labels

Functions

func AnnotationsEqual

func AnnotationsEqual(relevantAnnotations []string, anno1, anno2 map[string]string) bool

AnnotationsEqual returns whether the annotation with any key in relevantAnnotations is equal in anno1 and anno2.

func Configure

func Configure(apiServerURL, kubeconfigPath string, qps float32, burst int)

Configure sets the parameters of the Kubernetes package

func ConvertToCCEEndpoint

func ConvertToCCEEndpoint(obj interface{}) interface{}

ConvertToCCEEndpoint converts a *cce_v2.NetResourceSet into a *cce_v2.NetResourceSet or a cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a *cce_v2.NetResourceSet in its Obj. If the given obj can't be cast into either *cce_v2.NetResourceSet nor cache.DeletedFinalStateUnknown, the original obj is returned.

func ConvertToK8sService

func ConvertToK8sService(obj interface{}) interface{}

ConvertToK8sService converts a *v1.Service into a *corev1.Service or a cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a *corev1.Service in its Obj. If the given obj can't be cast into either *corev1.Service nor cache.DeletedFinalStateUnknown, the original obj is returned.

func ConvertToK8sV1LoadBalancerIngress

func ConvertToK8sV1LoadBalancerIngress(slimLBIngs []corev1.LoadBalancerIngress) []v1.LoadBalancerIngress

func ConvertToK8sV1ServiceAffinityConfig

func ConvertToK8sV1ServiceAffinityConfig(saCfg *corev1.SessionAffinityConfig) *v1.SessionAffinityConfig

func ConvertToK8sV1ServicePorts

func ConvertToK8sV1ServicePorts(slimPorts []corev1.ServicePort) []v1.ServicePort

func ConvertToNetResourceSet

func ConvertToNetResourceSet(obj interface{}) interface{}

ConvertToNetResourceSet converts a *cce_v2.NetResourceSet into a *cce_v2.NetResourceSet or a cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a *cce_v2.NetResourceSet in its Obj. If the given obj can't be cast into either *cce_v2.NetResourceSet nor cache.DeletedFinalStateUnknown, the original obj is returned.

func ConvertToNode

func ConvertToNode(obj interface{}) interface{}

ConvertToNode converts a *v1.Node into a *types.Node or a cache.DeletedFinalStateUnknown into a cache.DeletedFinalStateUnknown with a *types.Node in its Obj. If the given obj can't be cast into either *v1.Node nor cache.DeletedFinalStateUnknown, the original obj is returned. WARNING calling this function will set *all* fields of the given Node as empty.

func CreateConfig

func CreateConfig() (*rest.Config, error)

CreateConfig creates a client configuration based on the configured API server and Kubeconfig path

func EqualV1Services

func EqualV1Services(k8sSVC1, k8sSVC2 *corev1.Service, nodeAddressing dpTypes.NodeAddressing) bool

func EventBroadcaster

func EventBroadcaster() record.EventBroadcaster

func ExtractFixedIPTTLSeconds

func ExtractFixedIPTTLSeconds(pod *corev1.Pod) int64

ExtractFixedIPTTLSeconds Extract the expiration time of the fixed IP from the annotation of the pod. The unit is s

func FinalizerAddRemoteIP

func FinalizerAddRemoteIP(obj metav1.Object)

func FinalizerRemoveRemoteIP

func FinalizerRemoveRemoteIP(obj metav1.Object) bool

func GetAPIServerURL

func GetAPIServerURL() string

GetAPIServerURL returns the configured API server URL address

func GetBurst

func GetBurst() int

GetBurst gets the burst limit of the K8s configuration.

func GetKubeconfigPath

func GetKubeconfigPath() string

GetKubeconfigPath returns the configured path to the kubeconfig configuration file

func GetQPS

func GetQPS() float32

GetQPS gets the QPS of the K8s configuration.

func HaveFixedIPLabel

func HaveFixedIPLabel(obj metav1.Object) bool

func HavePrimaryENILabel

func HavePrimaryENILabel(obj metav1.Object) bool

HavePrimaryENILabel check if the object has the primary ENI label

func Init

func Init(conf k8sconfig.Configuration) error

Init initializes the Kubernetes package. It is required to call Configure() beforehand.

func InitNewFakeK8sClient

func InitNewFakeK8sClient() error

func InitNewK8sClient

func InitNewK8sClient() (*rest.Config, rest.Interface, error)

func IsEnabled

func IsEnabled() bool

IsEnabled checks if CCE is being used in tandem with Kubernetes.

func IsLocalNetResourceSet

func IsLocalNetResourceSet(n *ccev2.NetResourceSet) bool

IsLocalNetResourceSet returns true if the given NetResourceSet object refers to the NetResourceSet object representing the local node.

func K8sErrorHandler

func K8sErrorHandler(e error)

K8sErrorHandler handles the error messages in a non verbose way by omitting repeated instances of the same error message for a timeout defined with k8sErrLogTimeout.

func MatchNetworkResourceType

func MatchNetworkResourceType(annotations map[string]string, networkType string) bool

func ObjToNetResourceSet

func ObjToNetResourceSet(obj interface{}) *cce_v2.NetResourceSet

ObjToNetResourceSet attempts to cast object to a NetResourceSet object and returns the NetResourceSet objext if the cast succeeds. Otherwise, nil is returned.

func ObjToV1Beta1EndpointSlice

func ObjToV1Beta1EndpointSlice(obj interface{}) *slim_discover_v1beta1.EndpointSlice

func ObjToV1EndpointSlice

func ObjToV1EndpointSlice(obj interface{}) *slim_discover_v1.EndpointSlice

func ObjToV1Endpoints

func ObjToV1Endpoints(obj interface{}) *corev1.Endpoints

func ObjToV1Ingress

func ObjToV1Ingress(obj interface{}) *slim_networkingv1.Ingress

func ObjToV1Namespace

func ObjToV1Namespace(obj interface{}) *corev1.Namespace

func ObjToV1NetworkPolicy

func ObjToV1NetworkPolicy(obj interface{}) *slim_networkingv1.NetworkPolicy

func ObjToV1Node

func ObjToV1Node(obj interface{}) *v1.Node

func ObjToV1PartialObjectMetadata

func ObjToV1PartialObjectMetadata(obj interface{}) *slim_metav1.PartialObjectMetadata

func ObjToV1Secret

func ObjToV1Secret(obj interface{}) *corev1.Secret

func ObjToV1Services

func ObjToV1Services(obj interface{}) *corev1.Service

func ObjTov1Pod

func ObjTov1Pod(obj interface{}) *corev1.Pod

func ParseNode

func ParseNode(k8sNode *corev1.Node) *nodeTypes.Node

ParseNode parses a kubernetes node to a cce node

func ParseNodeAddressType

func ParseNodeAddressType(k8sAddress corev1.NodeAddressType) (addressing.AddressType, error)

ParseNodeAddressType converts a Kubernetes NodeAddressType to a CCE NodeAddressType. If the Kubernetes NodeAddressType does not have a corresponding CCE AddressType, returns an error.

func WaitForNodeInformation

func WaitForNodeInformation(ctx context.Context, nodeGetter nodeGetter) error

WaitForNodeInformation retrieves the node information via the NetResourceSet or Kubernetes Node resource. This function will block until the information is received. nodeGetter is a function used to retrieved the node from either the kube-apiserver or a local cache, depending on the caller.

Types

type JSONPatch

type JSONPatch struct {
	OP    string      `json:"op,omitempty"`
	Path  string      `json:"path,omitempty"`
	Value interface{} `json:"value"`
}

JSONPatch structure based on the RFC 6902

type K8sAPIExtensionsClient

type K8sAPIExtensionsClient struct {
	apiextclientset.Interface
}

K8sAPIExtensionsClient is a wrapper around clientset.Interface.

func APIExtClient

func APIExtClient() *K8sAPIExtensionsClient

APIExtClient returns the default API Extension client.

func WatcherAPIExtClient

func WatcherAPIExtClient() *K8sAPIExtensionsClient

WatcherAPIExtClient returns the client dedicated to API Extensions watchers.

type K8sCCEClient

type K8sCCEClient struct {
	clientset.Interface
	Informers cceInformer.SharedInformerFactory
}

K8sCCEClient is a wrapper around clientset.Interface.

func CCEClient

func CCEClient() *K8sCCEClient

CCEClient returns the default CCE Kubernetes client.

type K8sClient

type K8sClient struct {
	// kubernetes.Interface is the object through which interactions with
	// Kubernetes are performed.
	kubernetes.Interface
	Informers informers.SharedInformerFactory
}

K8sClient is a wrapper around kubernetes.Interface.

func Client

func Client() *K8sClient

Client returns the default Kubernetes client.

func WatcherClient

func WatcherClient() *K8sClient

WatcherClient returns the client dedicated to K8s watchers.

func (K8sClient) AnnotateNode

func (k8sCli K8sClient) AnnotateNode(nodeName string, encryptKey uint8, v4CIDR, v6CIDR *cidr.CIDR, v4HealthIP, v6HealthIP, v4IngressIP, v6IngressIP, v4CCEHostIP, v6CCEHostIP net.IP) error

AnnotateNode writes v4 and v6 CIDRs and health IPs in the given k8s node name. In case of failure while updating the node, this function while spawn a go routine to retry the node update indefinitely.

func (K8sClient) GetK8sNode

func (k8sCli K8sClient) GetK8sNode(ctx context.Context, nodeName string) (*core_v1.Node, error)

GetK8sNode returns the node with the given nodeName.

func (K8sClient) GetSecrets

func (k8sCli K8sClient) GetSecrets(ctx context.Context, ns, name string) (map[string][]byte, error)

GetSecrets returns the secrets found in the given namespace and name.

Directories

Path Synopsis
apis
cce.baidubce.com/v1
v1 版本的api包
v1 版本的api包
cce.baidubce.com/v2
* Copyright (c) 2023 Baidu, Inc.
* Copyright (c) 2023 Baidu, Inc.
client
clientset/versioned
This package has the automatically generated clientset.
This package has the automatically generated clientset.
clientset/versioned/fake
This package has the automatically generated fake clientset.
This package has the automatically generated fake clientset.
clientset/versioned/scheme
This package contains the scheme of the automatically generated clientset.
This package contains the scheme of the automatically generated clientset.
clientset/versioned/typed/cce.baidubce.com/v1
This package has the automatically generated typed clients.
This package has the automatically generated typed clients.
clientset/versioned/typed/cce.baidubce.com/v1/fake
Package fake has the automatically generated clients.
Package fake has the automatically generated clients.
clientset/versioned/typed/cce.baidubce.com/v2
This package has the automatically generated typed clients.
This package has the automatically generated typed clients.
clientset/versioned/typed/cce.baidubce.com/v2/fake
Package fake has the automatically generated clients.
Package fake has the automatically generated clients.
clientset/versioned/typed/cce.baidubce.com/v2alpha1
This package has the automatically generated typed clients.
This package has the automatically generated typed clients.
clientset/versioned/typed/cce.baidubce.com/v2alpha1/fake
Package fake has the automatically generated clients.
Package fake has the automatically generated clients.
Package synced provides tools for tracking if k8s resources have been initially sychronized with the k8s apiserver.
Package synced provides tools for tracking if k8s resources have been initially sychronized with the k8s apiserver.
Package types contains slimmer versions of k8s types.
Package types contains slimmer versions of k8s types.
* Copyright (c) 2023 Baidu, Inc.
* Copyright (c) 2023 Baidu, Inc.
Package version keeps track of the Kubernetes version the client is connected to
Package version keeps track of the Kubernetes version the client is connected to
cm
resources
This package contains exported resource identifiers and metric resource labels related to K8s watchers.
This package contains exported resource identifiers and metric resource labels related to K8s watchers.
subscriber
Package subscriber implements a mechanism to represent K8s watcher subscribers and allows K8s events to objects / resources to notify their respective subscribers.
Package subscriber implements a mechanism to represent K8s watcher subscribers and allows K8s events to objects / resources to notify their respective subscribers.

Jump to

Keyboard shortcuts

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