Skip to content
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

feat: add flag to disable to the memory swappiness #2072

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add flag to disable to the memory swappiness
soyji committed Jan 23, 2025
commit 51a66ff65e635f75a29c02f5264a36625a54f1af
12 changes: 7 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -117,17 +117,19 @@ func PreRun(cmd *cobra.Command, _ []string) {
reviveStopped, _ := f.GetBool("revive-stopped")
removeVolumes, _ := f.GetBool("remove-volumes")
warnOnHeadPullFailed, _ := f.GetString("warn-on-head-failure")
disableMemorySwappiness, _ := f.GetBool("disable-memory-swappiness")

if monitorOnly && noPull {
log.Warn("Using `WATCHTOWER_NO_PULL` and `WATCHTOWER_MONITOR_ONLY` simultaneously might lead to no action being taken at all. If this is intentional, you may safely ignore this message.")
}

client = container.NewClient(container.ClientOptions{
IncludeStopped: includeStopped,
ReviveStopped: reviveStopped,
RemoveVolumes: removeVolumes,
IncludeRestarting: includeRestarting,
WarnOnHeadFailed: container.WarningStrategy(warnOnHeadPullFailed),
IncludeStopped: includeStopped,
ReviveStopped: reviveStopped,
RemoveVolumes: removeVolumes,
IncludeRestarting: includeRestarting,
DisableMemorySwappiness: disableMemorySwappiness,
WarnOnHeadFailed: container.WarningStrategy(warnOnHeadPullFailed),
})

notifier = notifications.NewNotifier(cmd)
12 changes: 12 additions & 0 deletions docs/arguments.md
Original file line number Diff line number Diff line change
@@ -465,3 +465,15 @@ Environment Variable: WATCHTOWER_PORCELAIN
Possible values: v1
Default: -
```

## Compatibility with podman (Disable memory swappiness)

Disable memory swappiness. By default, podman sets the memory-swappiness value to 0 when no memory-swappiness is defined
When this flag is specified, watchtower will set the memory-swappiness to nil, fixing a compatibility issue with podman running with crun and cgroupv2

```text
Argument: --disable-memory-swappiness
Environment Variable: WATCHTOWER_DISABLE_MEMORY_SWAPPINESS
Type: Boolean
Default: false
```
7 changes: 7 additions & 0 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
@@ -211,6 +211,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
"",
envBool("WATCHTOWER_LABEL_TAKE_PRECEDENCE"),
"Label applied to containers take precedence over arguments")

flags.BoolP(
"disable-memory-swappiness",
"",
envBool("WATCHTOWER_DISABLE_MEMORY_SWAPPINESS"),
"Label used for setting memory swappiness as nil when recreating the container, used for compatibility with podman")
}

// RegisterNotificationFlags that are used by watchtower to send notifications
@@ -430,6 +436,7 @@ func SetDefaults() {
viper.SetDefault("WATCHTOWER_NOTIFICATION_SLACK_IDENTIFIER", "watchtower")
viper.SetDefault("WATCHTOWER_LOG_LEVEL", "info")
viper.SetDefault("WATCHTOWER_LOG_FORMAT", "auto")
viper.SetDefault("WATCHTOWER_DISABLE_MEMORY_SWAPPINESS", false)
}

// EnvConfig translates the command-line options into environment variables
16 changes: 11 additions & 5 deletions pkg/container/client.go
Original file line number Diff line number Diff line change
@@ -57,11 +57,12 @@ func NewClient(opts ClientOptions) Client {

// ClientOptions contains the options for how the docker client wrapper should behave
type ClientOptions struct {
RemoveVolumes bool
IncludeStopped bool
ReviveStopped bool
IncludeRestarting bool
WarnOnHeadFailed WarningStrategy
RemoveVolumes bool
IncludeStopped bool
ReviveStopped bool
IncludeRestarting bool
DisableMemorySwappiness bool
WarnOnHeadFailed WarningStrategy
}

// WarningStrategy is a value determining when to show warnings
@@ -251,6 +252,11 @@ func (client dockerClient) StartContainer(c t.Container) (t.ContainerID, error)
hostConfig := c.GetCreateHostConfig()
networkConfig := client.GetNetworkConfig(c)

// this is a flag set for podman compatibility
if client.DisableMemorySwappiness {
hostConfig.MemorySwappiness = nil
}

// simpleNetworkConfig is a networkConfig with only 1 network.
// see: https://github.com/docker/docker/issues/29265
simpleNetworkConfig := func() *network.NetworkingConfig {