Skip to content

Commit

Permalink
Fixes #271
Browse files Browse the repository at this point in the history
  • Loading branch information
sc-zenokerr committed Aug 1, 2024
1 parent 9ff878a commit 5c11ef7
Show file tree
Hide file tree
Showing 10 changed files with 196 additions and 86 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM golang:1.22

RUN go install github.com/cespare/reflex@latest
RUN go install github.com/cespare/reflex@latest && \
go install github.com/go-delve/delve/cmd/dlv@latest
ADD . /go/src/github.com/Scalingo/sand
WORKDIR /go/src/github.com/Scalingo/sand
EXPOSE 9999
Expand Down
49 changes: 21 additions & 28 deletions cmd/sand-agent/sand-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package main
import (
"context"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"

"github.com/moby/moby/pkg/reexec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net/http"
"os"
"path/filepath"

"github.com/Scalingo/go-etcd-lock/v5/lock"
"github.com/Scalingo/go-handlers"
Expand Down Expand Up @@ -109,7 +107,12 @@ func main() {
log.WithField("port", c.HttpPort).Info("Listening")
serviceEndpoint := fmt.Sprintf(":%d", c.HttpPort)

wg := &sync.WaitGroup{}
// We can only have one graceful service per process
numServers := 1
if c.EnableDockerPlugin {
numServers++
}
gracefulService := graceful.NewService(graceful.WithNumServers(numServers))

if c.EnableDockerPlugin {
log.WithField("port", c.DockerPluginHttpPort).Info("Enabling docker plugin")
Expand All @@ -133,52 +136,42 @@ func main() {
logDocker := log.WithField("service", "docker-plugin")
ctxDocker := logger.ToCtx(ctx, logDocker)

wg.Add(1)
go func() {
var err error

defer wg.Done()
if c.IsHttpTLSEnabled() {
err = tlsListener(ctxDocker, c, dockerPluginEndpoint, handler)
} else {
gracefulService := graceful.NewService()
err = gracefulService.ListenAndServe(ctxDocker, "tcp", dockerPluginEndpoint, handler)
}
if err != nil {
log.WithError(err).Error("fail to intialize docker plugin listener")
os.Exit(-1)
}
log.Info("docker plugin stopped")
}()
if c.IsHttpTLSEnabled() {
err = tlsListener(ctxDocker, c, gracefulService, dockerPluginEndpoint, handler)
} else {
err = gracefulService.ListenAndServe(ctxDocker, "tcp", dockerPluginEndpoint, handler)
}
if err != nil {
log.WithError(err).Error("fail to intialize docker plugin listener")
os.Exit(-1)
}
// log.Info("docker plugin stopped")
}

logHandler := log.WithField("service", "sand-api")
ctxHandler := logger.ToCtx(ctx, logHandler)

if c.IsHttpTLSEnabled() {
err = tlsListener(ctxHandler, c, serviceEndpoint, r)
err = tlsListener(ctxHandler, c, gracefulService, serviceEndpoint, r)
} else {
gracefulService := graceful.NewService()
err = gracefulService.ListenAndServe(ctxHandler, "tcp", serviceEndpoint, r)
}
if err != nil {
log.WithError(err).Error("fail to listen and serve")
os.Exit(-1)
}
log.Info("HTTP API stopped")
wg.Wait()
log.Info("Stop watching etcd changes")
endpointsWatcher.Close()
log.Info("All APIs stopped, shutting down..")
}

func tlsListener(ctx context.Context, c *config.Config, serviceEndpoint string, handler http.Handler) error {
func tlsListener(ctx context.Context, c *config.Config, gracefulService *graceful.Service, serviceEndpoint string, handler http.Handler) error {
config, err := apptls.NewConfig(c.HttpTLSCA, c.HttpTLSCert, c.HttpTLSKey, true)
if err != nil {
return errors.Wrapf(err, "fail to create tls configuration")
}

gracefulService := graceful.NewService()
return gracefulService.ListenAndServeTLS(ctx, "tcp", serviceEndpoint, handler, config)
}

Expand Down
10 changes: 9 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ services:
ETCD_HOSTS: "https://172.17.0.1:22379,https://172.17.0.1:22381,https://172.17.0.1:22383"
ENABLE_DOCKER_PLUGIN: "true"
PUBLIC_HOSTNAME: "dev.172.17.0.1.xip.st-sc.fr"
command: reflex -r '\.go$$' --inverse-regex='cmd/sand-agent-cli' -s -- sh -c 'go install -buildvcs=false -race github.com/Scalingo/sand/cmd/sand-agent && /go/bin/sand-agent'
ports:
- ${DEBUG_PORT:-}:2345
# command: reflex -r '\.go$$' --inverse-regex='cmd/sand-agent-cli' -s -- sh -c 'go install -buildvcs=false -race github.com/Scalingo/sand/cmd/sand-agent && /go/bin/sand-agent'
command: >
reflex -r '\.go$$' --inverse-regex='_test\.go$$' --inverse-regex='_mock\.go$$' -s -- sh -c
'go build -buildvcs=false -race github.com/Scalingo/sand/cmd/sand-agent &&
( [ -z "${DEBUG_PORT}" ] &&
./sand-agent ||
dlv --listen=:${DEBUG_PORT:-2345} --headless=true --api-version=2 --accept-multiclient exec ./sand-agent )'
test:
build: .
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/Scalingo/go-handlers v1.8.1
github.com/Scalingo/go-plugins-helpers v1.3.0
github.com/Scalingo/go-utils/etcd v1.1.2
github.com/Scalingo/go-utils/graceful v1.1.2
github.com/Scalingo/go-utils/graceful v1.1.4-0.20240801184811-e018d245a0e8
github.com/Scalingo/go-utils/logger v1.2.0
github.com/bits-and-blooms/bitset v1.13.0
github.com/gofrs/uuid/v5 v5.2.0
Expand Down
14 changes: 2 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ github.com/Scalingo/go-utils/errors/v2 v2.4.0 h1:vKG0Js3kzWG7+03LEvH7j8fw+picEcR
github.com/Scalingo/go-utils/errors/v2 v2.4.0/go.mod h1:WU6Kzi19AlZyUfoxFkdvEeYkIa0W0f172hKPqkOeIpU=
github.com/Scalingo/go-utils/etcd v1.1.2 h1:93Xe+v9hrwaabv2+a5SxztpqcsfTHHt2Va7bLaGCRM0=
github.com/Scalingo/go-utils/etcd v1.1.2/go.mod h1:qJDD/f8GOUcLyCigG80T6no4A3Ixu4Tw+7o8LHUkWO0=
github.com/Scalingo/go-utils/graceful v1.1.2 h1:eATcDb3lIJca89nAPgcGyTZgd/rL5hCtoqvFl9Gf2h8=
github.com/Scalingo/go-utils/graceful v1.1.2/go.mod h1:R6iABp2n7RIkTaf73GQ80j0n8w8yOep+dwkI1IvRx2M=
github.com/Scalingo/go-utils/graceful v1.1.4-0.20240801184811-e018d245a0e8 h1:nixVrq+LTFJlSwnI4woF2QuhIAO35gaWlTcOeHCRgao=
github.com/Scalingo/go-utils/graceful v1.1.4-0.20240801184811-e018d245a0e8/go.mod h1:ANPe9nIdemnVGvOzfHYROak4ZZCoolP3kfwidDiBq0k=
github.com/Scalingo/go-utils/logger v1.2.0 h1:E3jtaoRxpIsFcZu/jsvWew8ttUAwKUYQufdPqGYp7EU=
github.com/Scalingo/go-utils/logger v1.2.0/go.mod h1:JArjD1gHdB/vwnlcVG7rYxuIY0tk8/VG4MtirnRwn8k=
github.com/Scalingo/go-utils/security v1.0.0 h1:BW4FeZKmqW9nmDb/hdm49w4bhznQ8j7cVNxMxknGz3A=
Expand All @@ -42,16 +42,6 @@ github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-plugins-helpers v0.0.0-20200102110956-c9a8a2d92ccc h1:/A+mPcpajLsWiX9gSnzdVKM/IzZoYiNqXHe83z50k2c=
github.com/docker/go-plugins-helpers v0.0.0-20200102110956-c9a8a2d92ccc/go.mod h1:LFyLie6XcDbyKGeVK6bHe+9aJTYCxWLBg5IrJZOaXKA=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0=
github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c/go.mod h1:Yg+htXGokKKdzcwhuNDwVvN+uBxDGXJ7G/VN1d8fa64=
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9 h1:wWke/RUCl7VRjQhwPlR/v0glZXNYzBHdNUzf/Am2Nmg=
github.com/facebookgo/freeport v0.0.0-20150612182905-d4adf43b75b9/go.mod h1:uPmAp6Sws4L7+Q/OokbWDAK1ibXYhB3PXFP1kol5hPg=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434 h1:mOp33BLbcbJ8fvTAmZacbBiOASfxN+MLcLxymZCIrGE=
github.com/facebookgo/grace v0.0.0-20180706040059-75cf19382434/go.mod h1:KigFdumBXUPSwzLDbeuzyt0elrL7+CP7TKuhrhT4bcU=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 h1:JWuenKqqX8nojtoVVWjGfOF9635RETekkoH6Cc9SX0A=
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052/go.mod h1:UbMTZqLaRiH3MsBH8va0n7s1pQYcu3uTb8G4tygF4Zg=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4 h1:7HZCaLC5+BZpmbhCOZJ293Lz68O7PYrF2EzeiFMwCLk=
github.com/facebookgo/subset v0.0.0-20200203212716-c811ad88dec4/go.mod h1:5tD+neXqOorC30/tWg0LCSkrqj/AR6gu8yY8/fpw1q0=
github.com/frankban/quicktest v1.2.2 h1:xfmOhhoH5fGPgbEAlhLpJH9p0z/0Qizio9osmvn9IUY=
github.com/frankban/quicktest v1.2.2/go.mod h1:Qh/WofXFeiAFII1aEBu529AtJo6Zg2VHscnEsbBnJ20=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
Expand Down
8 changes: 8 additions & 0 deletions vendor/github.com/Scalingo/go-utils/graceful/CHANGELOG.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion vendor/github.com/Scalingo/go-utils/graceful/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5c11ef7

Please sign in to comment.