kubelet

package
v1.30.5 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2024 License: Apache-2.0 Imports: 153 Imported by: 287

Documentation

Overview

Package kubelet is the package that contains the libraries that drive the Kubelet binary. The kubelet is responsible for node level pod management. It runs on each worker in the cluster.

Index

Constants

View Source
const (

	// DefaultContainerLogsDir is the location of container logs.
	DefaultContainerLogsDir = "/var/log/containers"

	// MaxContainerBackOff is the max backoff period, exported for the e2e test
	MaxContainerBackOff = 300 * time.Second

	// ContainerGCPeriod is the period for performing container garbage collection.
	ContainerGCPeriod = time.Minute
	// ImageGCPeriod is the period for performing image garbage collection.
	ImageGCPeriod = 5 * time.Minute
)
View Source
const (
	// KubeIPTablesHintChain is the chain whose existence in either iptables-legacy
	// or iptables-nft indicates which version of iptables the system is using
	KubeIPTablesHintChain utiliptables.Chain = "KUBE-IPTABLES-HINT"

	// KubeFirewallChain is kubernetes firewall rules
	KubeFirewallChain utiliptables.Chain = "KUBE-FIREWALL"
)
View Source
const (
	PodInitializing   = "PodInitializing"
	ContainerCreating = "ContainerCreating"
)

Container state reason list

View Source
const (
	// NetworkNotReadyErrorMsg is used to describe the error that network is not ready
	NetworkNotReadyErrorMsg = "network is not ready"
)

Variables

View Source
var (
	// ContainerLogsDir can be overwritten for testing usage
	ContainerLogsDir = DefaultContainerLogsDir
)
View Source
var (
	// ErrNetworkUnknown indicates the network state is unknown
	ErrNetworkUnknown = errors.New("network state unknown")
)

Functions

func NewInitializedVolumePluginMgr added in v1.3.0

func NewInitializedVolumePluginMgr(
	kubelet *Kubelet,
	secretManager secret.Manager,
	configMapManager configmap.Manager,
	tokenManager *token.Manager,
	clusterTrustBundleManager clustertrustbundle.Manager,
	plugins []volume.VolumePlugin,
	prober volume.DynamicPluginProber) (*volume.VolumePluginMgr, error)

NewInitializedVolumePluginMgr returns a new instance of volume.VolumePluginMgr initialized with kubelets implementation of the volume.VolumeHost interface.

kubelet - used by VolumeHost methods to expose kubelet specific parameters plugins - used to initialize volumePluginMgr

func PreInitRuntimeService added in v1.18.0

func PreInitRuntimeService(kubeCfg *kubeletconfiginternal.KubeletConfiguration, kubeDeps *Dependencies) error

PreInitRuntimeService will init runtime service before RunKubelet.

Types

type Bootstrap added in v1.8.0

type Bootstrap interface {
	GetConfiguration() kubeletconfiginternal.KubeletConfiguration
	BirthCry()
	StartGarbageCollection()
	ListenAndServe(kubeCfg *kubeletconfiginternal.KubeletConfiguration, tlsOptions *server.TLSOptions, auth server.AuthInterface, tp trace.TracerProvider)
	ListenAndServeReadOnly(address net.IP, port uint)
	ListenAndServePodResources()
	Run(<-chan kubetypes.PodUpdate)
	RunOnce(<-chan kubetypes.PodUpdate) ([]RunPodResult, error)
}

Bootstrap is a bootstrapping interface for kubelet, targets the initialization protocol

type Dependencies added in v1.8.0

type Dependencies struct {
	Options []Option

	// Injected Dependencies
	Auth                      server.AuthInterface
	CAdvisorInterface         cadvisor.Interface
	Cloud                     cloudprovider.Interface
	ContainerManager          cm.ContainerManager
	EventClient               v1core.EventsGetter
	HeartbeatClient           clientset.Interface
	OnHeartbeatFailure        func()
	KubeClient                clientset.Interface
	Mounter                   mount.Interface
	HostUtil                  hostutil.HostUtils
	OOMAdjuster               *oom.OOMAdjuster
	OSInterface               kubecontainer.OSInterface
	PodConfig                 *config.PodConfig
	ProbeManager              prober.Manager
	Recorder                  record.EventRecorder
	Subpather                 subpath.Interface
	TracerProvider            trace.TracerProvider
	VolumePlugins             []volume.VolumePlugin
	DynamicPluginProber       volume.DynamicPluginProber
	TLSOptions                *server.TLSOptions
	RemoteRuntimeService      internalapi.RuntimeService
	RemoteImageService        internalapi.ImageManagerService
	PodStartupLatencyTracker  util.PodStartupLatencyTracker
	NodeStartupLatencyTracker util.NodeStartupLatencyTracker
	// contains filtered or unexported fields
}

Dependencies is a bin for things we might consider "injected dependencies" -- objects constructed at runtime that are necessary for running the Kubelet. This is a temporary solution for grouping these objects while we figure out a more comprehensive dependency injection story for the Kubelet.

type KillPodOptions added in v1.3.0

type KillPodOptions struct {
	// CompletedCh is closed when the kill request completes (syncTerminatingPod has completed
	// without error) or if the pod does not exist, or if the pod has already terminated. This
	// could take an arbitrary amount of time to be closed, but is never left open once
	// CouldHaveRunningContainers() returns false.
	CompletedCh chan<- struct{}
	// Evict is true if this is a pod triggered eviction - once a pod is evicted some resources are
	// more aggressively reaped than during normal pod operation (stopped containers).
	Evict bool
	// PodStatusFunc is invoked (if set) and overrides the status of the pod at the time the pod is killed.
	// The provided status is populated from the latest state.
	PodStatusFunc PodStatusFunc
	// PodTerminationGracePeriodSecondsOverride is optional override to use if a pod is being killed as part of kill operation.
	PodTerminationGracePeriodSecondsOverride *int64
}

KillPodOptions are options when performing a pod update whose update type is kill.

type Kubelet

type Kubelet struct {

	// the list of handlers to call during pod sync loop.
	lifecycle.PodSyncLoopHandlers

	// the list of handlers to call during pod sync.
	lifecycle.PodSyncHandlers

	// StatsProvider provides the node and the container stats.
	StatsProvider *stats.Provider
	// contains filtered or unexported fields
}

Kubelet is the main kubelet implementation.

func NewMainKubelet

func NewMainKubelet(kubeCfg *kubeletconfiginternal.KubeletConfiguration,
	kubeDeps *Dependencies,
	crOptions *config.ContainerRuntimeOptions,
	hostname string,
	hostnameOverridden bool,
	nodeName types.NodeName,
	nodeIPs []net.IP,
	providerID string,
	cloudProvider string,
	certDirectory string,
	rootDirectory string,
	podLogsDirectory string,
	imageCredentialProviderConfigFile string,
	imageCredentialProviderBinDir string,
	registerNode bool,
	registerWithTaints []v1.Taint,
	allowedUnsafeSysctls []string,
	experimentalMounterPath string,
	kernelMemcgNotification bool,
	experimentalNodeAllocatableIgnoreEvictionThreshold bool,
	minimumGCAge metav1.Duration,
	maxPerPodContainerCount int32,
	maxContainerCount int32,
	registerSchedulable bool,
	keepTerminatedPodVolumes bool,
	nodeLabels map[string]string,
	nodeStatusMaxImages int32,
	seccompDefault bool,
) (*Kubelet, error)

NewMainKubelet instantiates a new Kubelet object along with all the required internal modules. No initialization of Kubelet and its modules should happen here.

func (*Kubelet) BirthCry added in v0.5.1

func (kl *Kubelet) BirthCry()

BirthCry sends an event that the kubelet has started up.

func (*Kubelet) CheckpointContainer added in v1.25.0

func (kl *Kubelet) CheckpointContainer(
	ctx context.Context,
	podUID types.UID,
	podFullName,
	containerName string,
	options *runtimeapi.CheckpointContainerRequest,
) error

CheckpointContainer tries to checkpoint a container. The parameters are used to look up the specified container. If the container specified by the given parameters cannot be found an error is returned. If the container is found the container engine will be asked to checkpoint the given container into the kubelet's default checkpoint directory.

func (*Kubelet) GeneratePodHostNameAndDomain added in v1.3.0

func (kl *Kubelet) GeneratePodHostNameAndDomain(pod *v1.Pod) (string, string, error)

GeneratePodHostNameAndDomain creates a hostname and domain name for a pod, given that pod's spec and annotations or returns an error.

func (*Kubelet) GenerateRunContainerOptions added in v0.16.0

func (kl *Kubelet) GenerateRunContainerOptions(ctx context.Context, pod *v1.Pod, container *v1.Container, podIP string, podIPs []string) (*kubecontainer.RunContainerOptions, func(), error)

GenerateRunContainerOptions generates the RunContainerOptions, which can be used by the container runtime to set parameters for launching a container.

func (*Kubelet) GetActivePods added in v1.6.0

func (kl *Kubelet) GetActivePods() []*v1.Pod

GetActivePods returns pods that have been admitted to the kubelet that are not fully terminated. This is mapped to the "desired state" of the kubelet - what pods should be running.

WARNING: Currently this list does not include pods that have been force deleted but may still be terminating, which means resources assigned to those pods during admission may still be in use. See https://github.com/kubernetes/kubernetes/issues/104824

func (*Kubelet) GetAttach added in v1.5.0

func (kl *Kubelet) GetAttach(ctx context.Context, podFullName string, podUID types.UID, containerName string, streamOpts remotecommandserver.Options) (*url.URL, error)

GetAttach gets the URL the attach will be served from, or nil if the Kubelet will serve it.

func (*Kubelet) GetCachedMachineInfo added in v0.14.0

func (kl *Kubelet) GetCachedMachineInfo() (*cadvisorapiv1.MachineInfo, error)

GetCachedMachineInfo assumes that the machine info can't change without a reboot

func (*Kubelet) GetCgroupCPUAndMemoryStats added in v1.20.0

func (kl *Kubelet) GetCgroupCPUAndMemoryStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, error)

GetCgroupCPUAndMemoryStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) GetCgroupStats added in v1.20.0

func (kl *Kubelet) GetCgroupStats(cgroupName string, updateStats bool) (*statsapi.ContainerStats, *statsapi.NetworkStats, error)

GetCgroupStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) GetConfiguration added in v1.4.0

func (kl *Kubelet) GetConfiguration() kubeletconfiginternal.KubeletConfiguration

GetConfiguration returns the KubeletConfiguration used to configure the kubelet.

func (*Kubelet) GetExec added in v1.5.0

func (kl *Kubelet) GetExec(ctx context.Context, podFullName string, podUID types.UID, containerName string, cmd []string, streamOpts remotecommandserver.Options) (*url.URL, error)

GetExec gets the URL the exec will be served from, or nil if the Kubelet will serve it.

func (*Kubelet) GetExtraSupplementalGroupsForPod added in v1.3.5

func (kl *Kubelet) GetExtraSupplementalGroupsForPod(pod *v1.Pod) []int64

GetExtraSupplementalGroupsForPod returns a list of the extra supplemental groups for the Pod. These extra supplemental groups come from annotations on persistent volumes that the pod depends on.

func (*Kubelet) GetHostIPs added in v1.20.0

func (kl *Kubelet) GetHostIPs() ([]net.IP, error)

GetHostIPs returns host IPs or nil in case of error.

func (*Kubelet) GetHostname added in v0.12.0

func (kl *Kubelet) GetHostname() string

GetHostname Returns the hostname as the kubelet sees it.

func (*Kubelet) GetKubeletContainerLogs

func (kl *Kubelet) GetKubeletContainerLogs(ctx context.Context, podFullName, containerName string, logOptions *v1.PodLogOptions, stdout, stderr io.Writer) error

GetKubeletContainerLogs returns logs from the container TODO: this method is returning logs of random container attempts, when it should be returning the most recent attempt or all of them.

func (*Kubelet) GetKubeletMappings added in v1.30.0

func (kl *Kubelet) GetKubeletMappings() (uint32, uint32, error)

GetKubeletMappings gets the additional IDs allocated for the Kubelet.

func (*Kubelet) GetMaxPods added in v1.30.0

func (kl *Kubelet) GetMaxPods() int

func (*Kubelet) GetNode added in v0.14.0

func (kl *Kubelet) GetNode() (*v1.Node, error)

GetNode returns the node info for the configured node name of this Kubelet.

func (*Kubelet) GetNodeConfig added in v1.2.0

func (kl *Kubelet) GetNodeConfig() cm.NodeConfig

GetNodeConfig returns the container manager node config.

func (*Kubelet) GetOrCreateUserNamespaceMappings added in v1.25.0

func (kl *Kubelet) GetOrCreateUserNamespaceMappings(pod *v1.Pod, runtimeHandler string) (*runtimeapi.UserNamespace, error)

GetOrCreateUserNamespaceMappings returns the configuration for the sandbox user namespace

func (*Kubelet) GetPodByCgroupfs added in v1.11.0

func (kl *Kubelet) GetPodByCgroupfs(cgroupfs string) (*v1.Pod, bool)

GetPodByCgroupfs provides the pod that maps to the specified cgroup, as well as whether the pod was found.

func (*Kubelet) GetPodByFullName added in v0.12.0

func (kl *Kubelet) GetPodByFullName(podFullName string) (*v1.Pod, bool)

GetPodByFullName gets the pod with the given 'full' name, which incorporates the namespace as well as whether the pod was found.

func (*Kubelet) GetPodByName added in v0.9.0

func (kl *Kubelet) GetPodByName(namespace, name string) (*v1.Pod, bool)

GetPodByName provides the first pod that matches namespace and name, as well as whether the pod was found.

func (*Kubelet) GetPodCgroupParent added in v1.6.0

func (kl *Kubelet) GetPodCgroupParent(pod *v1.Pod) string

GetPodCgroupParent gets pod cgroup parent from container manager.

func (*Kubelet) GetPodCgroupRoot added in v1.10.0

func (kl *Kubelet) GetPodCgroupRoot() string

GetPodCgroupRoot returns the listeral cgroupfs value for the cgroup containing all pods

func (*Kubelet) GetPodDNS added in v1.9.0

func (kl *Kubelet) GetPodDNS(pod *v1.Pod) (*runtimeapi.DNSConfig, error)

GetPodDNS returns DNS settings for the pod. This function is defined in kubecontainer.RuntimeHelper interface so we have to implement it.

func (*Kubelet) GetPodDir added in v0.7.0

func (kl *Kubelet) GetPodDir(podUID types.UID) string

GetPodDir returns the full path to the per-pod data directory for the specified pod. This directory may not exist if the pod does not exist.

func (*Kubelet) GetPods added in v0.13.1

func (kl *Kubelet) GetPods() []*v1.Pod

GetPods returns all pods bound to the kubelet and their spec, and the mirror pods.

func (*Kubelet) GetPortForward added in v1.5.0

func (kl *Kubelet) GetPortForward(ctx context.Context, podName, podNamespace string, podUID types.UID, portForwardOpts portforward.V4Options) (*url.URL, error)

GetPortForward gets the URL the port-forward will be served from, or nil if the Kubelet will serve it.

func (*Kubelet) GetRequestedContainersInfo added in v1.19.0

func (kl *Kubelet) GetRequestedContainersInfo(containerName string, options cadvisorv2.RequestOptions) (map[string]*cadvisorapiv1.ContainerInfo, error)

GetRequestedContainersInfo returns container info.

func (*Kubelet) GetRunningPods added in v0.21.0

func (kl *Kubelet) GetRunningPods(ctx context.Context) ([]*v1.Pod, error)

GetRunningPods returns all pods running on kubelet from looking at the container runtime cache. This function converts kubecontainer.Pod to v1.Pod, so only the fields that exist in both kubecontainer.Pod and v1.Pod are considered meaningful.

func (*Kubelet) GetVersionInfo added in v1.7.3

func (kl *Kubelet) GetVersionInfo() (*cadvisorapiv1.VersionInfo, error)

GetVersionInfo returns information about the version of cAdvisor in use.

func (*Kubelet) HandlePodAdditions added in v1.1.0

func (kl *Kubelet) HandlePodAdditions(pods []*v1.Pod)

HandlePodAdditions is the callback in SyncHandler for pods being added from a config source.

func (*Kubelet) HandlePodCleanups added in v1.1.0

func (kl *Kubelet) HandlePodCleanups(ctx context.Context) error

HandlePodCleanups performs a series of cleanup work, including terminating pod workers, killing unwanted pods, and removing orphaned volumes/pod directories. No config changes are sent to pod workers while this method is executing which means no new pods can appear. After this method completes the desired state of the kubelet should be reconciled with the actual state in the pod worker and other pod-related components.

This function is executed by the main sync loop, so it must execute quickly and all nested calls should be asynchronous. Any slow reconciliation actions should be performed by other components (like the volume manager). The duration of this call is the minimum latency for static pods to be restarted if they are updated with a fixed UID (most should use a dynamic UID), and no config updates are delivered to the pod workers while this method is running.

func (*Kubelet) HandlePodReconcile added in v1.2.0

func (kl *Kubelet) HandlePodReconcile(pods []*v1.Pod)

HandlePodReconcile is the callback in the SyncHandler interface for pods that should be reconciled. Pods are reconciled when only the status of the pod is updated in the API.

func (*Kubelet) HandlePodRemoves added in v1.4.0

func (kl *Kubelet) HandlePodRemoves(pods []*v1.Pod)

HandlePodRemoves is the callback in the SyncHandler interface for pods being removed from a config source.

func (*Kubelet) HandlePodSyncs added in v1.1.0

func (kl *Kubelet) HandlePodSyncs(pods []*v1.Pod)

HandlePodSyncs is the callback in the syncHandler interface for pods that should be dispatched to pod workers for sync.

func (*Kubelet) HandlePodUpdates added in v1.1.0

func (kl *Kubelet) HandlePodUpdates(pods []*v1.Pod)

HandlePodUpdates is the callback in the SyncHandler interface for pods being updated from a config source.

func (*Kubelet) HandlerSupportsUserNamespaces added in v1.30.0

func (kl *Kubelet) HandlerSupportsUserNamespaces(rtHandler string) (bool, error)

HandlerSupportsUserNamespaces checks whether the specified handler supports user namespaces.

func (*Kubelet) ImageFsStats added in v1.20.0

func (kl *Kubelet) ImageFsStats(ctx context.Context) (*statsapi.FsStats, *statsapi.FsStats, error)

ImageFsStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) LatestLoopEntryTime added in v0.20.0

func (kl *Kubelet) LatestLoopEntryTime() time.Time

LatestLoopEntryTime returns the last time in the sync loop monitor.

func (*Kubelet) ListBlockVolumesForPod added in v1.22.0

func (kl *Kubelet) ListBlockVolumesForPod(podUID types.UID) (map[string]volume.BlockVolume, bool)

ListBlockVolumesForPod returns a map of the mounted volumes for the given pod. The key in the map is the OuterVolumeSpecName (i.e. pod.Spec.Volumes[x].Name)

func (*Kubelet) ListMetricDescriptors added in v1.26.0

func (kl *Kubelet) ListMetricDescriptors(ctx context.Context) ([]*runtimeapi.MetricDescriptor, error)

ListMetricDescriptors gets the descriptors for the metrics that will be returned in ListPodSandboxMetrics.

func (*Kubelet) ListPodCPUAndMemoryStats added in v1.20.0

func (kl *Kubelet) ListPodCPUAndMemoryStats(ctx context.Context) ([]statsapi.PodStats, error)

ListPodCPUAndMemoryStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) ListPodSandboxMetrics added in v1.26.0

func (kl *Kubelet) ListPodSandboxMetrics(ctx context.Context) ([]*runtimeapi.PodSandboxMetrics, error)

ListPodSandboxMetrics retrieves the metrics for all pod sandboxes.

func (*Kubelet) ListPodStats added in v1.20.0

func (kl *Kubelet) ListPodStats(ctx context.Context) ([]statsapi.PodStats, error)

ListPodStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) ListPodStatsAndUpdateCPUNanoCoreUsage added in v1.20.0

func (kl *Kubelet) ListPodStatsAndUpdateCPUNanoCoreUsage(ctx context.Context) ([]statsapi.PodStats, error)

ListPodStatsAndUpdateCPUNanoCoreUsage is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) ListPodsFromDisk added in v1.27.0

func (kl *Kubelet) ListPodsFromDisk() ([]types.UID, error)

ListPodsFromDisk gets a list of pods that have data directories.

func (*Kubelet) ListVolumesForPod added in v1.2.0

func (kl *Kubelet) ListVolumesForPod(podUID types.UID) (map[string]volume.Volume, bool)

ListVolumesForPod returns a map of the mounted volumes for the given pod. The key in the map is the OuterVolumeSpecName (i.e. pod.Spec.Volumes[x].Name)

func (*Kubelet) ListenAndServe added in v0.15.0

func (kl *Kubelet) ListenAndServe(kubeCfg *kubeletconfiginternal.KubeletConfiguration, tlsOptions *server.TLSOptions,
	auth server.AuthInterface, tp trace.TracerProvider)

ListenAndServe runs the kubelet HTTP server.

func (*Kubelet) ListenAndServePodResources added in v1.13.0

func (kl *Kubelet) ListenAndServePodResources()

ListenAndServePodResources runs the kubelet podresources grpc service

func (*Kubelet) ListenAndServeReadOnly added in v0.15.0

func (kl *Kubelet) ListenAndServeReadOnly(address net.IP, port uint)

ListenAndServeReadOnly runs the kubelet HTTP server in read-only mode.

func (*Kubelet) PodCouldHaveRunningContainers added in v1.22.9

func (kl *Kubelet) PodCouldHaveRunningContainers(pod *v1.Pod) bool

PodCouldHaveRunningContainers returns true if the pod with the given UID could still have running containers. This returns false if the pod has not yet been started or the pod is unknown.

func (*Kubelet) PodIsFinished added in v1.27.0

func (kl *Kubelet) PodIsFinished(pod *v1.Pod) bool

PodIsFinished returns true if SyncTerminatedPod is finished, ie. all required node-level resources that a pod was consuming have been reclaimed by the kubelet.

func (*Kubelet) PrepareDynamicResources added in v1.27.0

func (kl *Kubelet) PrepareDynamicResources(pod *v1.Pod) error

PrepareDynamicResources calls the container Manager PrepareDynamicResources API This method implements the RuntimeHelper interface

func (*Kubelet) ResyncInterval added in v0.20.0

func (kl *Kubelet) ResyncInterval() time.Duration

ResyncInterval returns the interval used for periodic syncs.

func (*Kubelet) RlimitStats added in v1.20.0

func (kl *Kubelet) RlimitStats() (*statsapi.RlimitStats, error)

RlimitStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) RootFsStats added in v1.20.0

func (kl *Kubelet) RootFsStats() (*statsapi.FsStats, error)

RootFsStats is delegated to StatsProvider, which implements stats.Provider interface

func (*Kubelet) Run

func (kl *Kubelet) Run(updates <-chan kubetypes.PodUpdate)

Run starts the kubelet reacting to config updates

func (*Kubelet) RunInContainer

func (kl *Kubelet) RunInContainer(ctx context.Context, podFullName string, podUID types.UID, containerName string, cmd []string) ([]byte, error)

RunInContainer runs a command in a container, returns the combined stdout, stderr as an array of bytes

func (*Kubelet) RunOnce

func (kl *Kubelet) RunOnce(updates <-chan kubetypes.PodUpdate) ([]RunPodResult, error)

RunOnce polls from one configuration update and run the associated pods.

func (*Kubelet) ServeLogs

func (kl *Kubelet) ServeLogs(w http.ResponseWriter, req *http.Request)

ServeLogs returns logs of current machine.

func (*Kubelet) StartGarbageCollection added in v0.14.0

func (kl *Kubelet) StartGarbageCollection()

StartGarbageCollection starts garbage collection threads.

func (*Kubelet) SyncPod added in v1.27.0

func (kl *Kubelet) SyncPod(ctx context.Context, updateType kubetypes.SyncPodType, pod, mirrorPod *v1.Pod, podStatus *kubecontainer.PodStatus) (isTerminal bool, err error)

SyncPod is the transaction script for the sync of a single pod (setting up) a pod. This method is reentrant and expected to converge a pod towards the desired state of the spec. The reverse (teardown) is handled in SyncTerminatingPod and SyncTerminatedPod. If SyncPod exits without error, then the pod runtime state is in sync with the desired configuration state (pod is running). If SyncPod exits with a transient error, the next invocation of SyncPod is expected to make progress towards reaching the desired state. SyncPod exits with isTerminal when the pod was detected to have reached a terminal lifecycle phase due to container exits (for RestartNever or RestartOnFailure) and the next method invoked will be SyncTerminatingPod. If the pod terminates for any other reason, SyncPod will receive a context cancellation and should exit as soon as possible.

Arguments:

updateType - whether this is a create (first time) or an update, should only be used for metrics since this method must be reentrant

pod - the pod that is being set up

mirrorPod - the mirror pod known to the kubelet for this pod, if any

podStatus - the most recent pod status observed for this pod which can be used to determine the set of actions that should be taken during this loop of SyncPod

The workflow is:

  • If the pod is being created, record pod worker start latency
  • Call generateAPIPodStatus to prepare an v1.PodStatus for the pod
  • If the pod is being seen as running for the first time, record pod start latency
  • Update the status of the pod in the status manager
  • Stop the pod's containers if it should not be running due to soft admission
  • Ensure any background tracking for a runnable pod is started
  • Create a mirror pod if the pod is a static pod, and does not already have a mirror pod
  • Create the data directories for the pod if they do not exist
  • Wait for volumes to attach/mount
  • Fetch the pull secrets for the pod
  • Call the container runtime's SyncPod callback
  • Update the traffic shaping for the pod's ingress and egress limits

If any step of this workflow errors, the error is returned, and is repeated on the next SyncPod call.

This operation writes all events that are dispatched in order to provide the most accurate information possible about an error situation to aid debugging. Callers should not write an event if this operation returns an error.

func (*Kubelet) SyncTerminatedPod added in v1.27.0

func (kl *Kubelet) SyncTerminatedPod(ctx context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus) error

SyncTerminatedPod cleans up a pod that has terminated (has no running containers). The invocations in this call are expected to tear down all pod resources. When this method exits the pod is expected to be ready for cleanup. This method reduces the latency of pod cleanup but is not guaranteed to get called in all scenarios.

Because the kubelet has no local store of information, all actions in this method that modify on-disk state must be reentrant and be garbage collected by HandlePodCleanups or a separate loop. This typically occurs when a pod is force deleted from configuration (local disk or API) and the kubelet restarts in the middle of the action.

func (*Kubelet) SyncTerminatingPod added in v1.27.0

func (kl *Kubelet) SyncTerminatingPod(_ context.Context, pod *v1.Pod, podStatus *kubecontainer.PodStatus, gracePeriod *int64, podStatusFn func(*v1.PodStatus)) error

SyncTerminatingPod is expected to terminate all running containers in a pod. Once this method returns without error, the pod is considered to be terminated and it will be safe to clean up any pod state that is tied to the lifetime of running containers. The next method invoked will be SyncTerminatedPod. This method is expected to return with the grace period provided and the provided context may be cancelled if the duration is exceeded. The method may also be interrupted with a context cancellation if the grace period is shortened by the user or the kubelet (such as during eviction). This method is not guaranteed to be called if a pod is force deleted from the configuration and the kubelet is restarted - SyncTerminatingRuntimePod handles those orphaned pods.

func (*Kubelet) SyncTerminatingRuntimePod added in v1.27.0

func (kl *Kubelet) SyncTerminatingRuntimePod(_ context.Context, runningPod *kubecontainer.Pod) error

SyncTerminatingRuntimePod is expected to terminate running containers in a pod that we have no configuration for. Once this method returns without error, any remaining local state can be safely cleaned up by background processes in each subsystem. Unlike syncTerminatingPod, we lack knowledge of the full pod spec and so cannot perform lifecycle related operations, only ensure that the remnant of the running pod is terminated and allow garbage collection to proceed. We do not update the status of the pod because with the source of configuration removed, we have no place to send that status.

func (*Kubelet) UnprepareDynamicResources added in v1.27.0

func (kl *Kubelet) UnprepareDynamicResources(pod *v1.Pod) error

UnprepareDynamicResources calls the container Manager UnprepareDynamicResources API This method implements the RuntimeHelper interface

type OnCompleteFunc added in v1.3.0

type OnCompleteFunc func(err error)

OnCompleteFunc is a function that is invoked when an operation completes. If err is non-nil, the operation did not complete successfully.

type Option added in v1.2.0

type Option func(*Kubelet)

Option is a functional option type for Kubelet

type PodStatusFunc added in v1.3.0

type PodStatusFunc func(podStatus *v1.PodStatus)

PodStatusFunc is a function that is invoked to override the pod status when a pod is killed.

type PodWorkerState added in v1.22.3

type PodWorkerState int

PodWorkType classifies the status of pod as seen by the pod worker - setup (sync), teardown of containers (terminating), or cleanup (terminated).

const (
	// SyncPod is when the pod is expected to be started and running.
	SyncPod PodWorkerState = iota
	// TerminatingPod is when the pod is no longer being set up, but some
	// containers may be running and are being torn down.
	TerminatingPod
	// TerminatedPod indicates the pod is stopped, can have no more running
	// containers, and any foreground cleanup can be executed.
	TerminatedPod
)

func (PodWorkerState) String added in v1.27.0

func (state PodWorkerState) String() string

type PodWorkerSync added in v1.27.0

type PodWorkerSync struct {
	// State of the pod.
	State PodWorkerState
	// Orphan is true if the pod is no longer in the desired set passed to SyncKnownPods.
	Orphan bool
	// HasConfig is true if we have a historical pod spec for this pod.
	HasConfig bool
	// Static is true if we have config and the pod came from a static source.
	Static bool
}

PodWorkerSync is the summarization of a single pod worker for sync. Values besides state are used to provide metric counts for operators.

type PodWorkers added in v0.17.0

type PodWorkers interface {
	// UpdatePod notifies the pod worker of a change to a pod, which will then
	// be processed in FIFO order by a goroutine per pod UID. The state of the
	// pod will be passed to the syncPod method until either the pod is marked
	// as deleted, it reaches a terminal phase (Succeeded/Failed), or the pod
	// is evicted by the kubelet. Once that occurs the syncTerminatingPod method
	// will be called until it exits successfully, and after that all further
	// UpdatePod() calls will be ignored for that pod until it has been forgotten
	// due to significant time passing. A pod that is terminated will never be
	// restarted.
	UpdatePod(options UpdatePodOptions)
	// SyncKnownPods removes workers for pods that are not in the desiredPods set
	// and have been terminated for a significant period of time. Once this method
	// has been called once, the workers are assumed to be fully initialized and
	// subsequent calls to ShouldPodContentBeRemoved on unknown pods will return
	// true. It returns a map describing the state of each known pod worker. It
	// is the responsibility of the caller to re-add any desired pods that are not
	// returned as knownPods.
	SyncKnownPods(desiredPods []*v1.Pod) (knownPods map[types.UID]PodWorkerSync)

	// IsPodKnownTerminated returns true once SyncTerminatingPod completes
	// successfully - the provided pod UID it is known by the pod
	// worker to be terminated. If the pod has been force deleted and the pod worker
	// has completed termination this method will return false, so this method should
	// only be used to filter out pods from the desired set such as in admission.
	//
	// Intended for use by the kubelet config loops, but not subsystems, which should
	// use ShouldPod*().
	IsPodKnownTerminated(uid types.UID) bool
	// CouldHaveRunningContainers returns true before the pod workers have synced,
	// once the pod workers see the pod (syncPod could be called), and returns false
	// after the pod has been terminated (running containers guaranteed stopped).
	//
	// Intended for use by the kubelet config loops, but not subsystems, which should
	// use ShouldPod*().
	CouldHaveRunningContainers(uid types.UID) bool

	// ShouldPodBeFinished returns true once SyncTerminatedPod completes
	// successfully - the provided pod UID it is known to the pod worker to
	// be terminated and have resources reclaimed. It returns false before the
	// pod workers have synced (syncPod could be called). Once the pod workers
	// have synced it returns false if the pod has a sync status until
	// SyncTerminatedPod completes successfully. If the pod workers have synced,
	// but the pod does not have a status it returns true.
	//
	// Intended for use by subsystem sync loops to avoid performing background setup
	// after termination has been requested for a pod. Callers must ensure that the
	// syncPod method is non-blocking when their data is absent.
	ShouldPodBeFinished(uid types.UID) bool
	// IsPodTerminationRequested returns true when pod termination has been requested
	// until the termination completes and the pod is removed from config. This should
	// not be used in cleanup loops because it will return false if the pod has already
	// been cleaned up - use ShouldPodContainersBeTerminating instead. Also, this method
	// may return true while containers are still being initialized by the pod worker.
	//
	// Intended for use by the kubelet sync* methods, but not subsystems, which should
	// use ShouldPod*().
	IsPodTerminationRequested(uid types.UID) bool

	// ShouldPodContainersBeTerminating returns false before pod workers have synced,
	// or once a pod has started terminating. This check is similar to
	// ShouldPodRuntimeBeRemoved but is also true after pod termination is requested.
	//
	// Intended for use by subsystem sync loops to avoid performing background setup
	// after termination has been requested for a pod. Callers must ensure that the
	// syncPod method is non-blocking when their data is absent.
	ShouldPodContainersBeTerminating(uid types.UID) bool
	// ShouldPodRuntimeBeRemoved returns true if runtime managers within the Kubelet
	// should aggressively cleanup pod resources that are not containers or on disk
	// content, like attached volumes. This is true when a pod is not yet observed
	// by a worker after the first sync (meaning it can't be running yet) or after
	// all running containers are stopped.
	// TODO: Once pod logs are separated from running containers, this method should
	// be used to gate whether containers are kept.
	//
	// Intended for use by subsystem sync loops to know when to start tearing down
	// resources that are used by running containers. Callers should ensure that
	// runtime content they own is not required for post-termination - for instance
	// containers are required in docker to preserve pod logs until after the pod
	// is deleted.
	ShouldPodRuntimeBeRemoved(uid types.UID) bool
	// ShouldPodContentBeRemoved returns true if resource managers within the Kubelet
	// should aggressively cleanup all content related to the pod. This is true
	// during pod eviction (when we wish to remove that content to free resources)
	// as well as after the request to delete a pod has resulted in containers being
	// stopped (which is a more graceful action). Note that a deleting pod can still
	// be evicted.
	//
	// Intended for use by subsystem sync loops to know when to start tearing down
	// resources that are used by non-deleted pods. Content is generally preserved
	// until deletion+removal_from_etcd or eviction, although garbage collection
	// can free content when this method returns false.
	ShouldPodContentBeRemoved(uid types.UID) bool
	// IsPodForMirrorPodTerminatingByFullName returns true if a static pod with the
	// provided pod name is currently terminating and has yet to complete. It is
	// intended to be used only during orphan mirror pod cleanup to prevent us from
	// deleting a terminating static pod from the apiserver before the pod is shut
	// down.
	IsPodForMirrorPodTerminatingByFullName(podFullname string) bool
}

PodWorkers is an abstract interface for testability.

type ReasonCache added in v1.2.0

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

ReasonCache stores the failure reason of the latest container start in a string, keyed by <pod_UID>_<container_name>. The goal is to propagate this reason to the container status. This endeavor is "best-effort" for two reasons:

  1. The cache is not persisted.
  2. We use an LRU cache to avoid extra garbage collection work. This means that some entries may be recycled before a pod has been deleted.

TODO(random-liu): Use more reliable cache which could collect garbage of failed pod. TODO(random-liu): Move reason cache to somewhere better.

func NewReasonCache added in v1.2.0

func NewReasonCache() *ReasonCache

NewReasonCache creates an instance of 'ReasonCache'.

func (*ReasonCache) Get added in v1.2.0

func (c *ReasonCache) Get(uid types.UID, name string) (*ReasonItem, bool)

Get gets error reason from the cache. The return values are error reason, error message and whether an error reason is found in the cache. If no error reason is found, empty string will be returned for error reason and error message.

func (*ReasonCache) Remove added in v1.2.0

func (c *ReasonCache) Remove(uid types.UID, name string)

Remove removes error reason from the cache

func (*ReasonCache) Update added in v1.2.0

func (c *ReasonCache) Update(uid types.UID, result kubecontainer.PodSyncResult)

Update updates the reason cache with the SyncPodResult. Only SyncResult with StartContainer action will change the cache.

type ReasonItem added in v1.17.0

type ReasonItem struct {
	Err     error
	Message string
}

ReasonItem is the cached item in ReasonCache

type RunPodResult

type RunPodResult struct {
	Pod *v1.Pod
	Err error
}

RunPodResult defines the running results of a Pod.

type SyncHandler

type SyncHandler interface {
	HandlePodAdditions(pods []*v1.Pod)
	HandlePodUpdates(pods []*v1.Pod)
	HandlePodRemoves(pods []*v1.Pod)
	HandlePodReconcile(pods []*v1.Pod)
	HandlePodSyncs(pods []*v1.Pod)
	HandlePodCleanups(ctx context.Context) error
}

SyncHandler is an interface implemented by Kubelet, for testability

type UpdatePodOptions added in v1.3.0

type UpdatePodOptions struct {
	// The type of update (create, update, sync, kill).
	UpdateType kubetypes.SyncPodType
	// StartTime is an optional timestamp for when this update was created. If set,
	// when this update is fully realized by the pod worker it will be recorded in
	// the PodWorkerDuration metric.
	StartTime time.Time
	// Pod to update. Required.
	Pod *v1.Pod
	// MirrorPod is the mirror pod if Pod is a static pod. Optional when UpdateType
	// is kill or terminated.
	MirrorPod *v1.Pod
	// RunningPod is a runtime pod that is no longer present in config. Required
	// if Pod is nil, ignored if Pod is set.
	RunningPod *kubecontainer.Pod
	// KillPodOptions is used to override the default termination behavior of the
	// pod or to update the pod status after an operation is completed. Since a
	// pod can be killed for multiple reasons, PodStatusFunc is invoked in order
	// and later kills have an opportunity to override the status (i.e. a preemption
	// may be later turned into an eviction).
	KillPodOptions *KillPodOptions
}

UpdatePodOptions is an options struct to pass to a UpdatePod operation.

Directories

Path Synopsis
apis
podresources/testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package cadvisor provides an interface for Kubelet interactions with cAdvisor.
Package cadvisor provides an interface for Kubelet interactions with cAdvisor.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package clustertrustbundle abstracts access to ClusterTrustBundles so that projected volumes can use them.
Package clustertrustbundle abstracts access to ClusterTrustBundles so that projected volumes can use them.
cm
Package cm (abbreviation of "container manager") and its subpackages contain all the kubelet code to manage containers.
Package cm (abbreviation of "container manager") and its subpackages contain all the kubelet code to manage containers.
cpumanager/topology
Package topology contains helpers for the CPU manager.
Package topology contains helpers for the CPU manager.
dra
Package config implements the pod configuration readers.
Package config implements the pod configuration readers.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
cri
remote
Package remote contains gRPC implementation of internalapi.RuntimeService and internalapi.ImageManagerService.
Package remote contains gRPC implementation of internalapi.RuntimeService and internalapi.ImageManagerService.
remote/fake
Package fake containers a fake gRPC implementation of internalapi.RuntimeService and internalapi.ImageManagerService.
Package fake containers a fake gRPC implementation of internalapi.RuntimeService and internalapi.ImageManagerService.
Package envvars is the package that build the environment variables that kubernetes provides to the containers run by it.
Package envvars is the package that build the environment variables that kubernetes provides to the containers run by it.
Package eviction is responsible for enforcing eviction thresholds to maintain node stability.
Package eviction is responsible for enforcing eviction thresholds to maintain node stability.
api
Package images is responsible for managing lifecycle of container images.
Package images is responsible for managing lifecycle of container images.
kubeletconfig
Package kuberuntime contains an implementation of kubecontainer.Runtime using the interface in pkg/kubelet/v1.
Package kuberuntime contains an implementation of kubecontainer.Runtime using the interface in pkg/kubelet/v1.
Package lifecycle contains handlers for pod lifecycle events and interfaces to integrate with kubelet admission, synchronization, and eviction of pods.
Package lifecycle contains handlers for pod lifecycle events and interfaces to integrate with kubelet admission, synchronization, and eviction of pods.
network
dns
Package nodeshutdown can watch for node level shutdown events and trigger graceful termination of pods running on the node prior to a system shutdown.
Package nodeshutdown can watch for node level shutdown events and trigger graceful termination of pods running on the node prior to a system shutdown.
systemd
Package systemd provides utility functions for kubelet to perform systemd related operations.
Package systemd provides utility functions for kubelet to perform systemd related operations.
Package pleg contains types and a generic implementation of the pod lifecycle event generator.
Package pleg contains types and a generic implementation of the pod lifecycle event generator.
cache
Package cache implements data structures used by the kubelet plugin manager to keep track of registered plugins.
Package cache implements data structures used by the kubelet plugin manager to keep track of registered plugins.
operationexecutor
Package operationexecutor implements interfaces that enable execution of register and unregister operations with a goroutinemap so that more than one operation is never triggered on the same plugin.
Package operationexecutor implements interfaces that enable execution of register and unregister operations with a goroutinemap so that more than one operation is never triggered on the same plugin.
reconciler
Package reconciler implements interfaces that attempt to reconcile the desired state of the world with the actual state of the world by triggering relevant actions (register/deregister plugins).
Package reconciler implements interfaces that attempt to reconcile the desired state of the world with the actual state of the world by triggering relevant actions (register/deregister plugins).
pod
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package qos contains helper functions for quality of service.
Package qos contains helper functions for quality of service.
Package server contains functions related to serving Kubelet's external interface.
Package server contains functions related to serving Kubelet's external interface.
stats
Package stats handles exporting Kubelet and container stats.
Package stats handles exporting Kubelet and container stats.
stats/testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
testing
Package testing is a generated GoMock package.
Package testing is a generated GoMock package.
Package token implements a manager of serviceaccount tokens for pods running on the node.
Package token implements a manager of serviceaccount tokens for pods running on the node.
Package types contains common types in the Kubelet.
Package types contains common types in the Kubelet.
Package util holds utility functions.
Package util holds utility functions.
store
Package store hosts a Store interface and its implementations.
Package store hosts a Store interface and its implementations.
cache
Package cache implements data structures used by the kubelet volume manager to keep track of attached volumes and the pods that mounted them.
Package cache implements data structures used by the kubelet volume manager to keep track of attached volumes and the pods that mounted them.
populator
Package populator implements interfaces that monitor and keep the states of the caches in sync with the "ground truth".
Package populator implements interfaces that monitor and keep the states of the caches in sync with the "ground truth".
Package winstats provides a client to get node and pod level stats on windows
Package winstats provides a client to get node and pod level stats on windows

Jump to

Keyboard shortcuts

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