Skip to content

Commit c11c70d

Browse files
committed
feat: 🎸 add Dockerfile, rework flags to support ENV variables
1 parent 8c9c066 commit c11c70d

File tree

9 files changed

+71
-46
lines changed

9 files changed

+71
-46
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.dockerignore
2+
.gitignore
3+
Dockerfile
4+
README.md
5+
heimdall

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@
2020
# Go workspace file
2121
go.work
2222

23-
.idea
23+
.idea
24+
25+
heimdall

Dockerfile

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
FROM golang:latest as BUILD
2+
LABEL authors="Robin Heidenis"
3+
4+
WORKDIR /app
5+
6+
# Download dependencies
7+
COPY go.mod go.sum ./
8+
RUN go mod download
9+
10+
# Copy source code
11+
COPY . .
12+
13+
# Build the application
14+
RUN CGO_ENABLED=0 GOOS=linux go build -o /heimdall ./cmd/heimdall
15+
16+
FROM alpine:latest
17+
18+
WORKDIR /
19+
20+
COPY --from=BUILD /heimdall /heimdall
21+
22+
# Expose port 8080 for healthchecks
23+
EXPOSE 8080
24+
25+
HEALTHCHECK --interval=5s --timeout=5s --retries=3 CMD wget localhost:8080/health -q -O - > /dev/null 2>&1
26+
27+
VOLUME /var/run/docker.sock
28+
29+
# Run the application
30+
CMD ["/heimdall"]

readme.md renamed to README.md

File renamed without changes.

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,25 @@ go 1.21
55
require (
66
github.com/davidbanham/human_duration v1.4.0
77
github.com/docker/docker v24.0.7+incompatible
8+
github.com/gtuk/discordwebhook v1.2.0
9+
github.com/jedib0t/go-pretty/v6 v6.4.9
10+
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4
811
)
912

1013
require (
1114
github.com/Microsoft/go-winio v0.6.1 // indirect
1215
github.com/distribution/reference v0.5.0 // indirect
1316
github.com/docker/distribution v2.8.3+incompatible // indirect
1417
github.com/docker/go-connections v0.4.0 // indirect
15-
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c // indirect
1618
github.com/docker/go-units v0.5.0 // indirect
1719
github.com/gogo/protobuf v1.3.2 // indirect
18-
github.com/gtuk/discordwebhook v1.2.0 // indirect
19-
github.com/jedib0t/go-pretty/v6 v6.4.9 // indirect
2020
github.com/mattn/go-runewidth v0.0.13 // indirect
2121
github.com/moby/term v0.5.0 // indirect
2222
github.com/morikuni/aec v1.0.0 // indirect
2323
github.com/opencontainers/go-digest v1.0.0 // indirect
2424
github.com/opencontainers/image-spec v1.0.2 // indirect
2525
github.com/pkg/errors v0.9.1 // indirect
2626
github.com/rivo/uniseg v0.2.0 // indirect
27-
github.com/sirupsen/logrus v1.9.0 // indirect
2827
github.com/stretchr/testify v1.8.4 // indirect
2928
golang.org/x/mod v0.8.0 // indirect
3029
golang.org/x/net v0.6.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ github.com/docker/docker v24.0.7+incompatible h1:Wo6l37AuwP3JaMnZa226lzVXGA3F9Ig
1515
github.com/docker/docker v24.0.7+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
1616
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
1717
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
18-
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8=
19-
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c/go.mod h1:Uw6UezgYA44ePAFQYUehOuCzmy5zmg/+nl2ZfMWGkpA=
2018
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
2119
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
2220
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
@@ -39,18 +37,19 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
3937
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
4038
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
4139
github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
40+
github.com/pelletier/go-toml/v2 v2.0.9 h1:uH2qQXheeefCCkuBBSLi7jCiSmj3VRh2+Goq2N7Xxu0=
41+
github.com/pelletier/go-toml/v2 v2.0.9/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc=
42+
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4 h1:aiqS8aBlF9PsAKeMddMSfbwp3smONCn3UO8QfUg0Z7Y=
43+
github.com/peterbourgon/ff/v4 v4.0.0-alpha.4/go.mod h1:H/13DK46DKXy7EaIxPhk2Y0EC8aubKm35nBjBe8AAGc=
4244
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4345
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4446
github.com/pkg/profile v1.6.0/go.mod h1:qBsxPvzyUincmltOk6iyRVxHYg4adc0OFOv72ZdLa18=
4547
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4648
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4749
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
4850
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
49-
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
50-
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
5151
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5252
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
53-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5453
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5554
github.com/stretchr/testify v1.7.4/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
5655
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
@@ -78,7 +77,6 @@ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
7877
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
7978
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
8079
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
81-
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8280
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
8381
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
8482
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -97,6 +95,8 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
9795
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9896
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
9997
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
98+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
99+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
100100
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
101101
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
102102
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

heimdall

339 KB
Binary file not shown.

pkg/heimdall/heimdall.go

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,48 @@ package heimdall
33
import (
44
"context"
55
"encoding/json"
6-
"flag"
76
"fmt"
87
"github.com/docker/docker/client"
8+
"github.com/peterbourgon/ff/v4"
9+
"github.com/peterbourgon/ff/v4/ffhelp"
910
"io"
11+
"os"
1012
"strconv"
1113
"time"
1214
)
1315

1416
var DebugMode bool
15-
var Timeout int
1617
var PeriodicNotifications bool
1718
var PeriodicNotificationInterval int
1819
var AllContainers bool
20+
var Retry int
1921
var Provider IProvider
2022

2123
var IsHealthy = false
2224

2325
func init() {
24-
flag.BoolVar(&DebugMode, "debug", false, "Enable debug mode")
26+
fs := ff.NewFlagSet("heimdall")
2527

26-
flag.BoolVar(&PeriodicNotifications, "periodic-notification", false, "Enable periodic notifications about the state of containers")
27-
flag.IntVar(&PeriodicNotificationInterval, "notification-interval", 60, "Interval in minutes between periodic notifications")
28-
flag.BoolVar(&AllContainers, "all-containers", false, "Enable periodic notifications for all containers, including stopped ones")
28+
fs.BoolVar(&DebugMode, 'd', "debug", "Enable debug mode")
29+
fs.BoolVar(&PeriodicNotifications, 'n', "periodic-notification", "Enable periodic notifications about the state of containers")
30+
fs.IntVar(&PeriodicNotificationInterval, 'i', "notification-interval", 60, "Interval in minutes between periodic notifications")
31+
fs.BoolVar(&AllContainers, 'a', "all-containers", "Enable periodic notifications for all containers, including stopped ones")
32+
fs.IntVar(&Retry, 'r', "retry", 10, "Retry in seconds when the docker event stream ends")
33+
var provider = fs.StringEnum('p', "provider", "Provider to use for notifications", "discord")
34+
var webhookUrl = fs.String('w', "webhook-url", "", "Webhook URL to use for notifications")
2935

30-
flag.IntVar(&Timeout, "retry", 10, "Retry in seconds when the docker event stream ends")
36+
err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("HEIMDALL"))
3137

32-
provider := flag.String("provider", "discord", "Provider to use for notifications")
33-
webhookUrl := flag.String("webhook-url", "", "Webhook URL to use for notifications")
34-
35-
flag.Parse()
38+
if err != nil {
39+
fmt.Printf("%s\n", ffhelp.Flags(fs))
40+
Fatal("Something went wrong while parsing flags: " + err.Error())
41+
}
3642

3743
if PeriodicNotifications {
3844
Info(fmt.Sprintf("Periodic notifications enabled. Interval: %d minutes\n", PeriodicNotificationInterval))
45+
if AllContainers {
46+
Info("All containers option specified, sending notifications about all containers, including stopped ones")
47+
}
3948
} else {
4049
if PeriodicNotificationInterval != 60 {
4150
Warn("Periodic notifications disabled. Ignoring notification interval.")
@@ -96,8 +105,8 @@ func Start() {
96105
case err := <-errorChannel:
97106
if err == io.EOF {
98107
IsHealthy = false
99-
Warn(fmt.Sprintf("No containers running. Sleeping for %s seconds and trying again.\n", strconv.Itoa(Timeout)))
100-
time.Sleep(time.Duration(Timeout * int(time.Second)))
108+
Warn(fmt.Sprintf("No containers running. Sleeping for %s seconds and trying again.\n", strconv.Itoa(Retry)))
109+
time.Sleep(time.Duration(Retry * int(time.Second)))
101110
IsHealthy = true
102111
go EventRoutine(cli, ctx, eventChannel, logChannel, errorChannel)
103112
} else {

pkg/heimdall/periodCheckRoutine.go

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package heimdall
22

33
import (
44
"context"
5-
"github.com/davidbanham/human_duration"
65
"github.com/docker/docker/api/types"
76
"github.com/docker/docker/client"
87
"github.com/jedib0t/go-pretty/v6/table"
@@ -41,30 +40,11 @@ func PeriodicCheckRoutine(ticker time.Ticker, cli *client.Client, ctx context.Co
4140

4241
var health = inspectedContainer.State.Status
4342

44-
if inspectedContainer.State.Health != nil {
43+
if inspectedContainer.State.Health != nil && health != "exited" {
4544
health = inspectedContainer.State.Health.Status
4645
}
4746

48-
startTimeStr := inspectedContainer.State.StartedAt
49-
50-
// Parse the start time string into a time.Time object
51-
startTime, err := time.Parse(time.RFC3339, startTimeStr)
52-
if err != nil {
53-
Fatal(err.Error())
54-
}
55-
56-
// Calculate the uptime
57-
uptime := time.Since(startTime)
58-
59-
c := Container{
60-
ID: listContainer.ID,
61-
Name: strings.Split(inspectedContainer.Name, "/")[1],
62-
Image: listContainer.Image,
63-
Uptime: human_duration.String(uptime, human_duration.Second),
64-
Health: HealthStatus(health),
65-
}
66-
67-
t.AppendRow(table.Row{i, c.Name, c.Health})
47+
t.AppendRow(table.Row{i, strings.Split(inspectedContainer.Name, "/")[1], HealthStatus(health)})
6848
t.AppendSeparator()
6949
}
7050

0 commit comments

Comments
 (0)