Skip to content

Commit

Permalink
Fixes #271
Browse files Browse the repository at this point in the history
Related to Scalingo/go-utils#944
With etienne's expert eye
  • Loading branch information
sc-zenokerr committed Aug 13, 2024
1 parent 095b8f4 commit 8b6b30a
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 48 deletions.
52 changes: 24 additions & 28 deletions cmd/sand-agent/sand-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/moby/moby/pkg/reexec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net/http"
"os"
"path/filepath"

Expand Down Expand Up @@ -92,17 +91,17 @@ func main() {
nctrl := web.NewNetworksController(c, networkRepository, endpointRepository, ipAllocator)
ectrl := web.NewEndpointsController(c, networkRepository, endpointRepository, ipAllocator)

r := handlers.NewRouter(log)
r.Use(handlers.ErrorMiddleware)
r.HandleFunc("/version", vctrl.Show).Methods("GET")
r.HandleFunc("/networks", nctrl.List).Methods("GET")
r.HandleFunc("/networks", nctrl.Create).Methods("POST")
r.HandleFunc("/networks/{id}", nctrl.Show).Methods("GET")
r.HandleFunc("/networks/{id}", nctrl.Destroy).Methods("DELETE")
r.HandleFunc("/networks/{id}", nctrl.Connect).Methods("CONNECT")
r.HandleFunc("/endpoints", ectrl.Create).Methods("POST")
r.HandleFunc("/endpoints", ectrl.List).Methods("GET")
r.HandleFunc("/endpoints/{id}", ectrl.Destroy).Methods("DELETE")
sandRouter := handlers.NewRouter(log)
sandRouter.Use(handlers.ErrorMiddleware)
sandRouter.HandleFunc("/version", vctrl.Show).Methods("GET")
sandRouter.HandleFunc("/networks", nctrl.List).Methods("GET")
sandRouter.HandleFunc("/networks", nctrl.Create).Methods("POST")
sandRouter.HandleFunc("/networks/{id}", nctrl.Show).Methods("GET")
sandRouter.HandleFunc("/networks/{id}", nctrl.Destroy).Methods("DELETE")
sandRouter.HandleFunc("/networks/{id}", nctrl.Connect).Methods("CONNECT")
sandRouter.HandleFunc("/endpoints", ectrl.Create).Methods("POST")
sandRouter.HandleFunc("/endpoints", ectrl.List).Methods("GET")
sandRouter.HandleFunc("/endpoints/{id}", ectrl.Destroy).Methods("DELETE")

log.WithField("port", c.HttpPort).Info("Listening")
serviceEndpoint := fmt.Sprintf(":%d", c.HttpPort)
Expand All @@ -114,16 +113,22 @@ func main() {
}
gracefulService := graceful.NewService(graceful.WithNumServers(numServers))

tlsConfig, err := apptls.NewConfig(c.HttpTLSCA, c.HttpTLSCert, c.HttpTLSKey, true)
if err != nil {
log.WithError(err).Error("fail to create tls configuration")
os.Exit(-1)
}

if c.EnableDockerPlugin {
log.WithField("port", c.DockerPluginHttpPort).Info("Enabling docker plugin")
dockerRepository := docker.NewRepository(c, dataStore)
plugin := docker.NewDockerPlugin(
c, networkRepository, endpointRepository, dockerRepository, ipAllocator,
)
manifest := `{"Implements": ["NetworkDriver", "IpamDriver"]}`
handler := dockersdk.NewHandler(log, manifest)
dockernetwork.ConfigureHandler(handler, plugin.DockerNetworkPlugin)
dockeripam.ConfigureHandler(handler, plugin.DockerIPAMPlugin)
dockerPluginRouter := dockersdk.NewHandler(log, manifest)
dockernetwork.ConfigureHandler(dockerPluginRouter, plugin.DockerNetworkPlugin)
dockeripam.ConfigureHandler(dockerPluginRouter, plugin.DockerIPAMPlugin)

err = docker.WritePluginSpecsOnDisk(ctx, c)
if err != nil {
Expand All @@ -137,9 +142,9 @@ func main() {
ctxDocker := logger.ToCtx(ctx, logDocker)

if c.IsHttpTLSEnabled() {
err = tlsListener(ctxDocker, c, gracefulService, dockerPluginEndpoint, handler)
err = gracefulService.ListenAndServeTLS(ctxDocker, "tcp", dockerPluginEndpoint, dockerPluginRouter, tlsConfig)
} else {
err = gracefulService.ListenAndServe(ctxDocker, "tcp", dockerPluginEndpoint, handler)
err = gracefulService.ListenAndServe(ctxDocker, "tcp", dockerPluginEndpoint, dockerPluginRouter)
}
if err != nil {
log.WithError(err).Error("fail to initialize docker plugin listener")
Expand All @@ -151,9 +156,9 @@ func main() {
ctxHandler := logger.ToCtx(ctx, logHandler)

if c.IsHttpTLSEnabled() {
err = tlsListener(ctxHandler, c, gracefulService, serviceEndpoint, r)
err = gracefulService.ListenAndServeTLS(ctxHandler, "tcp", serviceEndpoint, sandRouter, tlsConfig)
} else {
err = gracefulService.ListenAndServe(ctxHandler, "tcp", serviceEndpoint, r)
err = gracefulService.ListenAndServe(ctxHandler, "tcp", serviceEndpoint, sandRouter)
}
if err != nil {
log.WithError(err).Error("fail to listen and serve")
Expand All @@ -165,15 +170,6 @@ func main() {
log.Info("All APIs stopped, shutting down..")
}

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")
}

return gracefulService.ListenAndServeTLS(ctx, "tcp", serviceEndpoint, handler, config)
}

func ensureNetworks(ctx context.Context, c *config.Config, repo network.Repository, erepo endpoint.Repository) error {
log := logger.Get(ctx)
ctx = logger.ToCtx(ctx, log)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/Scalingo/go-handlers v1.8.2
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.4-0.20240801184811-e018d245a0e8
github.com/Scalingo/go-utils/graceful v1.1.4-0.20240813150622-91051c00f3ee
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
4 changes: 2 additions & 2 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.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/graceful v1.1.4-0.20240813150622-91051c00f3ee h1:r3bLDBpUG0ywE/8jcoIVgFxPr1oaWNuEwkR4ew7sj8s=
github.com/Scalingo/go-utils/graceful v1.1.4-0.20240813150622-91051c00f3ee/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 Down
30 changes: 16 additions & 14 deletions vendor/github.com/Scalingo/go-utils/graceful/service.go

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

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ github.com/Scalingo/go-utils/errors/v2
# github.com/Scalingo/go-utils/etcd v1.1.2
## explicit; go 1.20
github.com/Scalingo/go-utils/etcd
# github.com/Scalingo/go-utils/graceful v1.1.4-0.20240801184811-e018d245a0e8
# github.com/Scalingo/go-utils/graceful v1.1.4-0.20240813150622-91051c00f3ee
## explicit; go 1.22
github.com/Scalingo/go-utils/graceful
# github.com/Scalingo/go-utils/logger v1.2.0
Expand Down

0 comments on commit 8b6b30a

Please sign in to comment.