From 8118636321ae09d99fbd5328b9c8245f255807b3 Mon Sep 17 00:00:00 2001 From: HirazawaUi <695097494plus@gmail.com> Date: Thu, 4 Sep 2025 22:36:41 +0800 Subject: [PATCH] Close container runtime connections after use --- cmd/kubeadm/app/cmd/config.go | 1 + cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go | 2 ++ cmd/kubeadm/app/preflight/checks.go | 2 ++ cmd/kubeadm/app/util/runtime/runtime.go | 15 +++++++++++++++ 4 files changed, 20 insertions(+) diff --git a/cmd/kubeadm/app/cmd/config.go b/cmd/kubeadm/app/cmd/config.go index 3907b0651f0..be8d486b531 100644 --- a/cmd/kubeadm/app/cmd/config.go +++ b/cmd/kubeadm/app/cmd/config.go @@ -387,6 +387,7 @@ func newCmdConfigImagesPull() *cobra.Command { if err := containerRuntime.Connect(); err != nil { return err } + defer containerRuntime.Close() return PullControlPlaneImages(containerRuntime, &internalcfg.ClusterConfiguration) }, Args: cobra.NoArgs, diff --git a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go index 3a8a769edc4..8a163a1686e 100644 --- a/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go +++ b/cmd/kubeadm/app/cmd/phases/reset/cleanupnode.go @@ -138,6 +138,8 @@ func removeContainers(criSocketPath string) error { if err := containerRuntime.Connect(); err != nil { return err } + defer containerRuntime.Close() + containers, err := containerRuntime.ListKubeContainers() if err != nil { return err diff --git a/cmd/kubeadm/app/preflight/checks.go b/cmd/kubeadm/app/preflight/checks.go index 36978405d19..b8030f7a636 100644 --- a/cmd/kubeadm/app/preflight/checks.go +++ b/cmd/kubeadm/app/preflight/checks.go @@ -84,6 +84,7 @@ func (ContainerRuntimeCheck) Name() string { // Check validates the container runtime func (crc ContainerRuntimeCheck) Check() (warnings, errorList []error) { klog.V(1).Infoln("validating the container runtime") + defer crc.runtime.Close() if err := crc.runtime.IsRunning(); err != nil { errorList = append(errorList, err) } @@ -1087,6 +1088,7 @@ func RunPullImagesCheck(execer utilsexec.Interface, cfg *kubeadmapi.InitConfigur if err := containerRuntime.Connect(); err != nil { return handleError(os.Stderr, err.Error()) } + defer containerRuntime.Close() serialPull := true if cfg.NodeRegistration.ImagePullSerial != nil { diff --git a/cmd/kubeadm/app/util/runtime/runtime.go b/cmd/kubeadm/app/util/runtime/runtime.go index 851af6f844a..826330cda94 100644 --- a/cmd/kubeadm/app/util/runtime/runtime.go +++ b/cmd/kubeadm/app/util/runtime/runtime.go @@ -42,6 +42,7 @@ var defaultKnownCRISockets = []string{ // ContainerRuntime is an interface for working with container runtimes type ContainerRuntime interface { Connect() error + Close() SetImpl(impl) IsRunning() error ListKubeContainers() ([]string, error) @@ -93,6 +94,20 @@ func (runtime *CRIRuntime) Connect() error { return nil } +// Close closes the connections to the runtime and image services. +func (runtime *CRIRuntime) Close() { + if runtime.runtimeService != nil { + if err := runtime.runtimeService.Close(); err != nil { + klog.Warningf("failed to close runtime service: %v", err) + } + } + if runtime.imageService != nil { + if err := runtime.imageService.Close(); err != nil { + klog.Warningf("failed to close image service: %v", err) + } + } +} + // IsRunning checks if runtime is running. func (runtime *CRIRuntime) IsRunning() error { ctx, cancel := defaultContext()