Skip to content
Merged
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ docker run -d \
- STATS_FREQUENCY - default to **30**
- EVENT_ENDPOINT - default to **http://app:8080/events**
- HEALTH_CHECK_ENDPOINT - default to **http://app:8080/version**
- DISK_PATH - mountpoint reported as node disk usage, default to **/** (**\\** on windows)
- DEBUG_EVENT - default to **false**
- DEBUG_STATS - default to **false**

The docker client is configured from the standard docker environment variables:

- DOCKER_HOST - docker endpoint, default to the mounted **/var/run/docker.sock**. Set to e.g. **tcp://proxy:2375** to go through a socket proxy instead of mounting the socket
- DOCKER_API_VERSION - docker api version used by the client. Must be supported by your engine, docker 29 dropped everything below **1.44**
- DOCKER_CERT_PATH, DOCKER_TLS_VERIFY - tls settings for a remote endpoint

## Important!

In case you are deploying agent inside Swarmpit [stack](https://github.com/swarmpit/swarmpit/blob/master/docker-compose.yml)
Expand Down
2 changes: 2 additions & 0 deletions setup/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type args struct {
StatsFrequency int `json:"stats_frequency"`
EventEndpoint string `json:"event_endpoint"`
HealthCheckEndpoint string `json:"healthcheck_endpoint"`
DiskPath string `json:"disk_path"`
Debug debug `json:"debug"`
}

Expand All @@ -22,6 +23,7 @@ func GetArgs() *args {
StatsFrequency: getIntValue(30, "STATS_FREQUENCY"),
EventEndpoint: getStringValue("http://app:8080/events", "EVENT_ENDPOINT"),
HealthCheckEndpoint: getStringValue("http://app:8080/version", "HEALTH_CHECK_ENDPOINT"),
DiskPath: getStringValue("", "DISK_PATH"),
Debug: debug{
Event: getBooleanValue(false, "DEBUG_EVENT"),
Stats: getBooleanValue(false, "DEBUG_STATS")},
Expand Down
13 changes: 12 additions & 1 deletion swarmpit/task/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,20 @@ func getPath() string {
return "/"
}

// diskPath is the mountpoint reported as the node's disk usage. defaults to the
// platform root, override with DISK_PATH when data lives on another partition.
func diskPath() string {
if arg.DiskPath != "" {
return arg.DiskPath
}
return getPath()
}

func DiskUsage() (ds DiskStatus) {
diskStat, err := disk.Usage(getPath())
path := diskPath()
diskStat, err := disk.Usage(path)
if err != nil {
log.Printf("ERROR: Disk usage fetching failed for %s: %s\n", path, err)
return
}

Expand Down
46 changes: 46 additions & 0 deletions swarmpit/task/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,49 @@ func TestContainersUsageSurvivesABadContainer(t *testing.T) {
t.Fatalf("expected container aaa, got %+v", got[0])
}
}

// DISK_PATH lets a node report the partition its data actually lives on
// instead of always reporting root. See swarmpit/agent#5.
func TestDiskPathDefaultsToPlatformRoot(t *testing.T) {
original := arg.DiskPath
defer func() { arg.DiskPath = original }()

arg.DiskPath = ""
if got := diskPath(); got != getPath() {
t.Fatalf("expected the platform root %q, got %q", getPath(), got)
}
}

func TestDiskPathHonoursOverride(t *testing.T) {
original := arg.DiskPath
defer func() { arg.DiskPath = original }()

arg.DiskPath = "/mnt/data"
if got := diskPath(); got != "/mnt/data" {
t.Fatalf("expected /mnt/data, got %q", got)
}
}

func TestDiskUsageReportsTheConfiguredPath(t *testing.T) {
original := arg.DiskPath
defer func() { arg.DiskPath = original }()

arg.DiskPath = "/"
root := DiskUsage()
if root.Total == 0 {
t.Fatal("expected a non-zero total for /")
}
if root.Used+root.Free > root.Total*2 {
t.Fatalf("implausible usage figures: %+v", root)
}
}

func TestDiskUsageOnMissingPathIsZeroNotPanic(t *testing.T) {
original := arg.DiskPath
defer func() { arg.DiskPath = original }()

arg.DiskPath = "/definitely/not/a/mountpoint"
if got := DiskUsage(); got != (DiskStatus{}) {
t.Fatalf("expected a zero status for a bad path, got %+v", got)
}
}
Loading