Skip to content

Commit

Permalink
feat: test docker and redis connections at startup
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan committed Dec 2, 2021
1 parent 6ceaf47 commit 1f93599
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 14 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Makefile
LICENSE
README.md
docs
dist/
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.vscode/
./traefik-kop

dist/
/traefik-kop
/dist
16 changes: 8 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,11 @@ PROJECT=ghcr.io/jittering/traefik-kop

SHELL := bash

build-docker:
docker buildx build --platform linux/amd64,linux/arm64 --pull --push -t ${PROJECT}:latest .
build-docker: build-linux
docker build --platform linux/amd64 -t ${PROJECT}:latest .

build-docker-local:
if [[ -n "$$PROC" ]]; then \
docker build --build-arg PROC=$$PROC --pull -t ${PROJECT}:latest .; \
else \
docker build --pull -t ${PROJECT}:latest .; \
fi;
build-linux:
GOOS=linux go build ./bin/traefik-kop

build:
go build ./bin/traefik-kop
Expand All @@ -22,3 +18,7 @@ run:
go run ./bin/traefik-kop

serve: run

clean:
rm -rf dist/
rm -f traefik-kop
4 changes: 4 additions & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func NewStore(hostname string, addr string, pass string, db int) *Store {
return store
}

func (s *Store) Ping() error {
return s.client.Ping().Err()
}

// sk returns the 'set key' for keeping track of our services/routers/middlewares
// e.g., traefik_http_routers@culture.local
func (s Store) sk(b string) string {
Expand Down
22 changes: 19 additions & 3 deletions traefik_kop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"
"net/url"
"os"
"strings"
"time"

"github.com/sirupsen/logrus"
Expand All @@ -16,24 +18,38 @@ import (
"github.com/traefik/traefik/v2/pkg/server"
)

const defaultEndpointPath = "/var/run/docker.sock"
const defaultThrottleDuration = 5 * time.Second

func Start(config Config) {
defaultThrottleDuration := 5 * time.Second

_, err := os.Stat(defaultEndpointPath)
if err != nil {
logrus.Fatal(err)
}

dp := &docker.Provider{
Endpoint: "unix:///var/run/docker.sock",
Endpoint: "unix://" + defaultEndpointPath,
// HTTPClientTimeout: pduration,
Watch: true,
SwarmModeRefreshSeconds: ptypes.Duration(15 * time.Second),
}

store := NewStore(config.Hostname, config.Addr, config.Pass, config.DB)
err = store.Ping()
if err != nil {
if strings.Contains(err.Error(), config.Addr) {
logrus.Fatalf("failed to connect to redis: %s", err)
}
logrus.Fatalf("failed to connect to redis at %s: %s", config.Addr, err)
}

providers := &static.Providers{
Docker: dp,
}
providerAggregator := aggregator.NewProviderAggregator(*providers)

err := providerAggregator.Init()
err = providerAggregator.Init()
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 1f93599

Please sign in to comment.