Skip to content

Commit 77c61f9

Browse files
srm6867xagent003
authored andcommitted
close containerd client connection to avoid socket leak
1 parent 939b3c9 commit 77c61f9

File tree

4 files changed

+22
-17
lines changed

4 files changed

+22
-17
lines changed

nodelet/pkg/phases/container_runtime/start_containerd.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (cp *ContainerdRunPhase) Stop(ctx context.Context, cfg config.Config) error
9191
phaseutils.SetHostStatus(cp.hostPhase, constants.FailedState, err.Error())
9292
return err
9393
}
94+
defer containerUtil.CloseClientConnection()
9495

9596
namespaces := []string{constants.K8sNamespace, constants.MobyNamespace}
9697
err = containerUtil.DestroyContainersInNamespacesList(ctx, namespaces)

nodelet/pkg/utils/container_runtime/container_runtime.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,10 @@ type ContainerUtils interface {
1818
CreateContainer(ctx context.Context, containerName string, containerImage string) (containerd.Container, error)
1919
RemoveContainer(ctx context.Context, container containerd.Container, force bool) error
2020
StopContainer(ctx context.Context, container containerd.Container, timeoutStr string) error
21+
CloseClientConnection()
2122
}
2223

2324
type ImageUtils interface {
2425
LoadImagesFromDir(context.Context, string, string) error
2526
LoadImagesFromFile(context.Context, string) error
2627
}
27-
28-
// type InstallRuntime interface {
29-
// EnsureContainerdInstalled(ctx context.Context) error
30-
// EnsureRuncInstalled() error
31-
// EnsureCNIPluginsInstalled() error
32-
// LoadKernelModules(ctx context.Context, modules []string) error
33-
// SetContainerdSysctlParams(ctx context.Context) error
34-
// GenerateContainerdUnit() error
35-
// GenerateContainerdConfig() error
36-
// }

nodelet/pkg/utils/container_runtime/container_utils.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type ContainerUtility struct {
2323
log *zap.SugaredLogger
2424
}
2525

26-
const timeOut = "10s"
26+
const TimeOut = "10s"
2727

2828
func NewContainerUtil() (ContainerUtils, error) {
2929

@@ -49,9 +49,20 @@ func NewContainerdClient() (*containerd.Client, error) {
4949
return containerdclient, nil
5050
}
5151

52+
func (c *ContainerUtility) CloseClientConnection() {
53+
if c.Client != nil {
54+
err := c.Client.Close()
55+
if err != nil {
56+
c.log.Warnf("couldn't close containerd client connection: %v", err)
57+
return
58+
}
59+
c.log.Info("closed containerd client connection")
60+
}
61+
}
62+
5263
func (c *ContainerUtility) EnsureFreshContainerRunning(ctx context.Context, containerName string, containerImage string) error {
5364

54-
err := c.EnsureContainerDestroyed(ctx, containerName, timeOut)
65+
err := c.EnsureContainerDestroyed(ctx, containerName, TimeOut)
5566
if err != nil {
5667
return err
5768
}
@@ -168,7 +179,7 @@ func (c *ContainerUtility) DestroyContainersInNamespace(ctx context.Context, nam
168179

169180
ctx = namespaces.WithNamespace(ctx, namespace)
170181

171-
err = c.EnsureContainersDestroyed(ctx, containers, timeOut)
182+
err = c.EnsureContainersDestroyed(ctx, containers, TimeOut)
172183
if err != nil {
173184
return errors.Wrapf(err, "could not destroy containers in namespace: %s", namespace)
174185
}

nodelet/pkg/utils/container_runtime/image_utils.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ func NewImageUtil() ImageUtils {
2626

2727
// LoadImagesFromDir loads images from all tar files in the given directory to container runtime with given namespace
2828
func (i *ImageUtility) LoadImagesFromDir(ctx context.Context, imageDir string, namespace string) error {
29-
items, _ := ioutil.ReadDir(imageDir)
29+
items, err := ioutil.ReadDir(imageDir)
30+
if err != nil {
31+
return errors.Wrapf(err, "could not read dir: %s", imageDir)
32+
}
3033
for _, item := range items {
31-
if item.IsDir() {
32-
continue
33-
} else {
34+
if !item.IsDir() {
3435
imageFile := fmt.Sprintf("%s/%s", imageDir, item.Name())
3536
ctx = namespaces.WithNamespace(ctx, namespace)
3637
err := i.LoadImagesFromFile(ctx, imageFile)
@@ -63,6 +64,7 @@ func (i *ImageUtility) LoadImagesFromFile(ctx context.Context, fileName string)
6364
if err != nil {
6465
return errors.Wrap(err, "failed to create container runtime client")
6566
}
67+
defer client.Close()
6668

6769
imgs, err := client.Import(ctx, decompressor, containerd.WithDigestRef(archive.DigestTranslator(constants.DefaultSnapShotter)), containerd.WithSkipDigestRef(func(name string) bool { return name != "" }), containerd.WithImportPlatform(platform))
6870
if err != nil {

0 commit comments

Comments
 (0)