8000 feat: add containerd event listener for real-time container lifecycle by gsakun · Pull Request #2230 · alibaba/loongcollector · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add containerd event listener for real-time container lifecycle #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gsakun
Copy link
Collaborator
@gsakun gsakun commented May 22, 2025

目前在 cri_adapter.go 中,containerd类型容器状态的更新主要依赖于定时轮询(如 syncContainers)。为了提高状态感知的实时性和性能,实现基于containerd容器生命周期事件的监听与处理。

@yyuuttaaoo yyuuttaaoo requested review from Takuka0311, linrunqi08 and Copilot and removed request for Takuka0311 and linrunqi08 May 30, 2025 01:58
Copy link
Contributor
@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a containerd event listener to enhance the real-time monitoring of container lifecycle events in the CRI adapter, reducing reliance on periodic polling. Key changes include the addition of new functions (getContainerType, containerdEventListener, and criRuntimeRecover) in cri_adapter.go, and the conditional launch of the event listener in container_discover_controller.go.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

8000
File Description
pkg/helper/containercenter/cri_adapter.go Added event listener functions and helper methods to process container lifecycle events using containerd events
pkg/helper/containercenter/container_discover_controller.go Updated sync logic to conditionally launch the new event listener based on nativeClient availability

Comment on lines +404 to +412
if errorCount > 10 && containerCenterInstance != nil {
logger.Info(context.Background(), "containerd listener fails and docker wrapper is valid", "stop containerd listener")
break
}
// if always error, sleep 300 secs
if errorCount > 30 {
time.Sleep(time.Duration(300) * time.Second)
} else {
time.Sleep(time.Duration(10) * time.Second)
Copy link
Preview
Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider replacing the magic numbers (e.g., 10 and 30) used in errorCount thresholds and the subsequent sleep durations with named constants to improve code readability and maintainability.

Suggested change
if errorCount > 10 && containerCenterInstance != nil {
logger.Info(context.Background(), "containerd listener fails and docker wrapper is valid", "stop containerd listener")
break
}
// if always error, sleep 300 secs
if errorCount > 30 {
time.Sleep(time.Duration(300) * time.Second)
} else {
time.Sleep(time.Duration(10) * time.Second)
if errorCount > errorThreshold && containerCenterInstance != nil {
logger.Info(context.Background(), "containerd listener fails and docker wrapper is valid", "stop containerd listener")
break
}
// if always error, sleep for a longer duration
if errorCount > highErrorThreshold {
time.Sleep(longSleepDuration)
} else {
time.Sleep(shortSleepDuration)

Copilot uses AI. Check for mistakes.

} else {
continue
}
default:
Copy link
Preview
Copilot AI May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It might be beneficial to log unhandled event types in the default case to aid future debugging efforts if unexpected events are received.

Suggested change
default:
default:
logger.Warnf(context.Background(), "CONTAINERD_EVENT_UNHANDLED", "Unhandled containerd event type: %T", evt)

Copilot uses AI. Check for mistakes.

@yyuuttaaoo yyuuttaaoo requested a review from linrunqi08 May 30, 2025 02:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0