refactor(docker): streamline idlewatcher label handling

- Introduced a map for idlewatcher labels to simplify the loading of configuration values.
- Simplify logic to check for the presence of an idle timeout and handle dependencies.
This commit is contained in:
yusing
2025-12-17 10:27:01 +08:00
parent 86a46d191d
commit ea67095967
2 changed files with 25 additions and 11 deletions

View File

@@ -238,23 +238,25 @@ func setPrivateHostname(c *types.Container, helper containerHelper) {
}
func loadDeleteIdlewatcherLabels(c *types.Container, helper containerHelper) {
cfg := map[string]any{
"idle_timeout": helper.getDeleteLabel(LabelIdleTimeout),
"wake_timeout": helper.getDeleteLabel(LabelWakeTimeout),
"stop_method": helper.getDeleteLabel(LabelStopMethod),
"stop_timeout": helper.getDeleteLabel(LabelStopTimeout),
"stop_signal": helper.getDeleteLabel(LabelStopSignal),
"start_endpoint": helper.getDeleteLabel(LabelStartEndpoint),
"depends_on": Dependencies(c),
"no_loading_page": helper.getDeleteLabel(LabelNoLoadingPage),
hasIdleTimeout := false
cfg := make(map[string]any, len(idlewatcherLabels))
for lbl, key := range idlewatcherLabels {
if value := helper.getDeleteLabel(lbl); value != "" {
cfg[key] = value
}
switch lbl {
case LabelIdleTimeout:
hasIdleTimeout = true
case LabelDependsOn:
cfg[key] = Dependencies(c)
}
}
// ensure it's deleted from labels
helper.getDeleteLabel(LabelDependsOn)
// set only if idlewatcher is enabled
idleTimeout := cfg["idle_timeout"]
if idleTimeout != "" {
if hasIdleTimeout {
idwCfg := new(types.IdlewatcherConfig)
idwCfg.Docker = &types.DockerConfig{
DockerHost: c.DockerHost,

View File

@@ -17,3 +17,15 @@ const (
LabelNoLoadingPage = NSProxy + ".no_loading_page" // No loading page when using idlewatcher
LabelNetwork = NSProxy + ".network"
)
// key: label, value: key in IdlewatcherConfig
var idlewatcherLabels = map[string]string{
LabelIdleTimeout: "idle_timeout",
LabelWakeTimeout: "wake_timeout",
LabelStopMethod: "stop_method",
LabelStopTimeout: "stop_timeout",
LabelStopSignal: "stop_signal",
LabelStartEndpoint: "start_endpoint",
LabelDependsOn: "depends_on",
LabelNoLoadingPage: "no_loading_page",
}