Skip to content
Draft
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
19 changes: 12 additions & 7 deletions cmd/sand-agent/sand-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

"github.com/Scalingo/go-etcd-lock/v5/lock"
"github.com/Scalingo/go-handlers"
dockeripam "github.com/Scalingo/go-plugins-helpers/ipam"
dockernetwork "github.com/Scalingo/go-plugins-helpers/network"
dockersdk "github.com/Scalingo/go-plugins-helpers/sdk"
dockerpluginsipam "github.com/Scalingo/go-plugins-helpers/v2/ipam"
dockerpluginsnetwork "github.com/Scalingo/go-plugins-helpers/v2/network"
dockerpluginssdk "github.com/Scalingo/go-plugins-helpers/v2/sdk"
"github.com/Scalingo/go-utils/graceful"
"github.com/Scalingo/go-utils/logger"
"github.com/Scalingo/go-utils/logger/plugins/rollbarplugin"
Expand Down Expand Up @@ -130,10 +130,15 @@ func main() {
plugin := docker.NewDockerPlugin(
c, networkRepository, endpointRepository, dockerRepository, ipAllocator,
)
manifest := `{"Implements": ["NetworkDriver", "IpamDriver"]}`
dockerPluginRouter := dockersdk.NewHandler(log, manifest)
dockernetwork.ConfigureHandler(dockerPluginRouter, plugin.DockerNetworkPlugin)
dockeripam.ConfigureHandler(dockerPluginRouter, plugin.DockerIPAMPlugin)
manifest := dockerpluginssdk.Manifest{
Implements: []dockerpluginssdk.DriverImplementationName{
dockerpluginsnetwork.ImplementationName,
dockerpluginsipam.ImplementationName,
},
}
dockerPluginRouter := dockerpluginssdk.NewHandler(log, manifest)
dockerpluginsnetwork.ConfigureHandler(dockerPluginRouter, plugin.DockerNetworkPlugin)
dockerpluginsipam.ConfigureHandler(dockerPluginRouter, plugin.DockerIPAMPlugin)

err = docker.WritePluginSpecsOnDisk(ctx, c)
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/Scalingo/sand

go 1.25.0
go 1.25.5

require (
github.com/Scalingo/go-etcd-lock/v5 v5.0.11
github.com/Scalingo/go-handlers v1.11.1
github.com/Scalingo/go-plugins-helpers v1.4.0
github.com/Scalingo/go-plugins-helpers/v2 v2.5.1
github.com/Scalingo/go-utils/etcd v1.2.2
github.com/Scalingo/go-utils/graceful v1.3.3
github.com/Scalingo/go-utils/logger v1.12.2
Expand All @@ -23,7 +23,7 @@ require (
go.etcd.io/etcd/api/v3 v3.6.10
go.etcd.io/etcd/client/v3 v3.6.10
go.uber.org/mock v0.6.0
golang.org/x/sys v0.43.0
golang.org/x/sys v0.44.0
google.golang.org/grpc v1.80.0
gopkg.in/errgo.v1 v1.0.1
)
Expand All @@ -41,7 +41,7 @@ require (
github.com/coreos/go-semver v0.3.1 // indirect
github.com/coreos/go-systemd/v22 v22.7.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/go-connections v0.7.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.3 // indirect
Expand All @@ -51,7 +51,7 @@ require (
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.29.0 // indirect
github.com/moby/sys/reexec v0.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rollbar/rollbar-go v1.4.8 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/urfave/negroni/v3 v3.1.1 // indirect
Expand Down
14 changes: 8 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ github.com/Scalingo/go-etcd-lock/v5 v5.0.11 h1:WgiuwUUGwcmhYk/+crsK9QoTxtUoJObwt
github.com/Scalingo/go-etcd-lock/v5 v5.0.11/go.mod h1:bc6kyqFtV9Fdk18SqrqeGd04m6qlTJleIebIazPD9Kw=
github.com/Scalingo/go-handlers v1.11.1 h1:c1BnZe2EhCebIzfygoOmcl7iZlUdn5OYuG/4xdAddT8=
github.com/Scalingo/go-handlers v1.11.1/go.mod h1:FP+IibVd+yWkSY9B+rzcIi629MMk/7kNfcEjIZUhMq4=
github.com/Scalingo/go-plugins-helpers v1.4.0 h1:TXysHoesEcH5Q4JBCo0prxWLFY0/yp2Q2WFBGa+Curo=
github.com/Scalingo/go-plugins-helpers v1.4.0/go.mod h1:pPivLa4FeG8iB0nff7NwI7gIZ4bMr62u/prYXktoWIo=
github.com/Scalingo/go-plugins-helpers/v2 v2.5.1 h1:UCjyKYXc6iJ0j+kyh1ocV7epgW/VGa0EPakZyGJC/X4=
github.com/Scalingo/go-plugins-helpers/v2 v2.5.1/go.mod h1:PLP/0axAGPxWBP6f8Q1cUNQ8ckHBHmm1iDvsqKcKv9Y=
github.com/Scalingo/go-utils/crypto v1.1.1 h1:+F4JqC/8Lu8b1MDJwdQjT19yzLhShWPk+K93cUv7KVg=
github.com/Scalingo/go-utils/crypto v1.1.1/go.mod h1:29dsjRaXxNI6tUG2ZbNWs4A5pqmwCAztdgfudGQDuhY=
github.com/Scalingo/go-utils/errors/v2 v2.5.1 h1:1tfJW6/ZxTgrRmFTlKQCOtArQquOW0/XdZQzx8wMHoM=
Expand Down Expand Up @@ -40,8 +40,9 @@ github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7
github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo=
github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-connections v0.7.0 h1:6SsRfJddP22WMrCkj19x9WKjEDTB+ahsdiGYf0mN39c=
github.com/docker/go-connections v0.7.0/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q=
github.com/docker/go-plugins-helpers v0.0.0-20240701071450-45e2431495c8 h1:IMfrF5LCzP2Vhw7j4IIH3HxPsCLuZYjDqFAM/C88ulg=
Expand Down Expand Up @@ -91,8 +92,9 @@ github.com/moby/sys/reexec v0.1.0/go.mod h1:EqjBg8F3X7iZe5pU6nRZnYCMUTXoxsjiIfHu
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
github.com/rollbar/rollbar-go v1.4.8 h1:SAKy97CHXSFZjxQUxmuBnQmfzCjX54kvQGEQZHEqwuQ=
Expand Down Expand Up @@ -175,8 +177,8 @@ golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
Expand Down
22 changes: 10 additions & 12 deletions integrations/docker/driver_ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"context"
"net"

"github.com/Scalingo/go-plugins-helpers/ipam"
"github.com/pkg/errors"

"github.com/Scalingo/go-plugins-helpers/v2/ipam"
"github.com/Scalingo/go-utils/logger"
"github.com/Scalingo/sand/api/types"
"github.com/Scalingo/sand/ipallocator"
"github.com/Scalingo/sand/network"
"github.com/pkg/errors"
)

type dockerIPAMPlugin struct {
Expand All @@ -31,12 +32,11 @@ func (p *dockerIPAMPlugin) GetDefaultAddressSpaces(context.Context) (*ipam.Addre
}

func (p *dockerIPAMPlugin) RequestPool(ctx context.Context, req *ipam.RequestPoolRequest) (*ipam.RequestPoolResponse, error) {
log := logger.Get(ctx)

id := req.Options["sand-id"]
if id == "" {
return nil, errors.New("IPAM option sand-id is mandatory")
}
ctx, log := logger.WithFieldToCtx(ctx, "pool_id", id)

network, ok, err := p.networkRepository.Exists(ctx, id)
if err != nil {
Expand All @@ -46,7 +46,7 @@ func (p *dockerIPAMPlugin) RequestPool(ctx context.Context, req *ipam.RequestPoo
return nil, errors.Errorf("SAND network %v does not exist", id)
}

log.Info("pool initialized")
log.Info("Pool initialized")
res := ipam.RequestPoolResponse{
PoolID: id,
Pool: network.IPRange,
Expand All @@ -61,10 +61,9 @@ func (p *dockerIPAMPlugin) ReleasePool(ctx context.Context, req *ipam.ReleasePoo
}

func (p *dockerIPAMPlugin) RequestAddress(ctx context.Context, req *ipam.RequestAddressRequest) (*ipam.RequestAddressResponse, error) {
log := logger.Get(ctx)
log = log.WithField("pool_id", req.PoolID)
ctx, log := logger.WithFieldToCtx(ctx, "pool_id", req.PoolID)

if req.Options["RequestAddressType"] == "com.docker.network.gateway" {
if req.Options.RequestAddressType == ipam.RequestAddressTypeGateway {
id := req.PoolID
network, ok, err := p.networkRepository.Exists(ctx, id)
if err != nil {
Expand All @@ -85,16 +84,15 @@ func (p *dockerIPAMPlugin) RequestAddress(ctx context.Context, req *ipam.Request
if err != nil {
return nil, errors.Wrapf(err, "fail to request address in pool %v - %v", req.PoolID, req.Address)
}
log.Infof("obtained address: %v", ip)
log.Infof("Obtained address: %v", ip)
return &ipam.RequestAddressResponse{
Address: ip,
}, nil
}

func (p *dockerIPAMPlugin) ReleaseAddress(ctx context.Context, req *ipam.ReleaseAddressRequest) error {
log := logger.Get(ctx)
id := req.PoolID
log = log.WithField("pool_id", id)
ctx, log := logger.WithFieldToCtx(ctx, "pool_id", id)

network, ok, err := p.networkRepository.Exists(ctx, id)
if err != nil {
Expand All @@ -118,6 +116,6 @@ func (p *dockerIPAMPlugin) ReleaseAddress(ctx context.Context, req *ipam.Release
if err != nil {
return errors.Wrapf(err, "fail to release address in pool %v - %v", id, req.Address)
}
log.Infof("released address: %v", req.Address)
log.Infof("Released address: %v", req.Address)
return nil
}
37 changes: 15 additions & 22 deletions integrations/docker/driver_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/Scalingo/go-plugins-helpers/network"
"github.com/Scalingo/go-plugins-helpers/v2/network"
"github.com/Scalingo/go-utils/logger"
"github.com/Scalingo/sand/api/params"
"github.com/Scalingo/sand/endpoint"
Expand All @@ -30,17 +30,15 @@ func (p *dockerNetworkPlugin) CreateNetwork(ctx context.Context, req *network.Cr
log := logger.Get(ctx)
log.Info("Create network by docker integration")

opts, ok := req.Options["com.docker.network.generic"].(map[string]interface{})
if !ok {
return errors.Errorf("invalid generic options: %+v, not a map[string]interface{}", req.Options["com.docker.network.generic"])
if req.Options.Generic == nil {
return errors.New("generic options must be defined")
}

id, ok := opts["sand-id"].(string)
if !ok {
return errors.New("sand-id should be a string")
id := req.Options.Generic["sand-id"]
if id == "" {
return errors.New("sand-id must be defined")
}
log = log.WithField("network_id", id)
ctx = logger.ToCtx(ctx, log)
ctx, log = logger.WithFieldToCtx(ctx, "network_id", id)

network, ok, err := p.networkRepository.Exists(ctx, id)
if err != nil {
Expand All @@ -66,17 +64,15 @@ func (p *dockerNetworkPlugin) AllocateNetwork(ctx context.Context, req *network.
}

func (p *dockerNetworkPlugin) DeleteNetwork(ctx context.Context, req *network.DeleteNetworkRequest) error {
log := logger.Get(ctx).WithField("docker_network_id", req.NetworkID)
ctx = logger.ToCtx(ctx, log)
ctx, log := logger.WithFieldToCtx(ctx, "docker_network_id", req.NetworkID)
log.Info("Delete network by docker integration")

dpn, err := p.dockerPluginRepository.GetNetworkByDockerID(ctx, req.NetworkID)
if err != nil {
return errors.Wrapf(err, "fail to get docker id binding")
}

log = log.WithField("network_id", dpn.SandNetworkID)
ctx = logger.ToCtx(ctx, log)
ctx, log = logger.WithFieldToCtx(ctx, "network_id", dpn.SandNetworkID)

network, ok, err := p.networkRepository.Exists(ctx, dpn.SandNetworkID)
if err != nil {
Expand Down Expand Up @@ -105,16 +101,15 @@ func (p *dockerNetworkPlugin) FreeNetwork(ctx context.Context, req *network.Free
}

func (p *dockerNetworkPlugin) CreateEndpoint(ctx context.Context, req *network.CreateEndpointRequest) (*network.CreateEndpointResponse, error) {
log := logger.Get(ctx).WithField("docker_network_id", req.NetworkID)
ctx = logger.ToCtx(ctx, log)
ctx, log := logger.WithFieldToCtx(ctx, "docker_network_id", req.NetworkID)
log.Info("Create endpoint by docker integration")

dpn, err := p.dockerPluginRepository.GetNetworkByDockerID(ctx, req.NetworkID)
if err != nil {
return nil, errors.Wrapf(err, "fail to get docker id binding")
}
ctx, _ = logger.WithFieldToCtx(ctx, "network_id", dpn.SandNetworkID)

log = log.WithField("network_id", dpn.SandNetworkID)
ctx = logger.ToCtx(ctx, log)
n, ok, err := p.networkRepository.Exists(ctx, dpn.SandNetworkID)
if err != nil {
return nil, errors.Wrapf(err, "fail to get network %v", dpn.SandNetworkID)
Expand All @@ -136,8 +131,7 @@ func (p *dockerNetworkPlugin) CreateEndpoint(ctx context.Context, req *network.C
if err != nil {
return nil, errors.Wrapf(err, "fail to create endpoint")
}
log = log.WithField("endpoint_id", e.ID)
ctx = logger.ToCtx(ctx, log)
ctx, log = logger.WithFieldToCtx(ctx, "endpoint_id", e.ID)

err = p.dockerPluginRepository.SaveEndpoint(ctx, DockerPluginEndpoint{
DockerPluginNetwork: dpn,
Expand All @@ -159,19 +153,18 @@ func (p *dockerNetworkPlugin) CreateEndpoint(ctx context.Context, req *network.C
}

func (p *dockerNetworkPlugin) DeleteEndpoint(ctx context.Context, req *network.DeleteEndpointRequest) error {
log := logger.Get(ctx).WithField("docker_endpoint_id", req.EndpointID)
ctx, log := logger.WithFieldToCtx(ctx, "docker_endpoint_id", req.EndpointID)
log.Info("Delete endpoint by docker integration")

dpe, err := p.dockerPluginRepository.GetEndpointByDockerID(ctx, req.EndpointID)
if err != nil {
return errors.Wrapf(err, "fail to get docker id binding")
}

log = log.WithFields(logrus.Fields{
ctx, log = logger.WithFieldsToCtx(ctx, logrus.Fields{
"endpoint_id": dpe.SandEndpointID,
"network_id": dpe.SandNetworkID,
})
ctx = logger.ToCtx(ctx, log)

n, ok, err := p.networkRepository.Exists(ctx, dpe.SandNetworkID)
if err != nil {
Expand Down
Loading