Skip to content
Open
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
21 changes: 10 additions & 11 deletions swarmd/cmd/swarmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ import (
"os"
"os/signal"

engineapi "github.com/docker/docker/client"
grpc_prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/moby/swarmkit/swarmd/dockerexec"
"github.com/moby/swarmkit/swarmd/internal/defaults"
"github.com/moby/swarmkit/swarmd/version"
prometheus "github.com/grpc-ecosystem/go-grpc-prometheus"
"github.com/moby/moby/client"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/api/genericresource"
"github.com/moby/swarmkit/v2/cli"
Expand All @@ -24,6 +21,10 @@ import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/moby/swarmkit/swarmd/dockerexec"
"github.com/moby/swarmkit/swarmd/internal/defaults"
"github.com/moby/swarmkit/swarmd/version"
)

var externalCAOpt cli.ExternalCAOpt
Expand Down Expand Up @@ -171,14 +172,12 @@ var (
return err
}

client, err := engineapi.NewClientWithOpts(
engineapi.WithHost(engineAddr),
)
apiClient, err := client.New(client.WithHost(engineAddr))
if err != nil {
return err
}

executor := dockerexec.NewExecutor(client, resources)
executor := dockerexec.NewExecutor(apiClient, resources)

if debugAddr != "" {
go func() {
Expand All @@ -191,7 +190,7 @@ var (

if metricsAddr != "" {
// This allows to measure latency distribution.
grpc_prometheus.EnableHandlingTimeHistogram()
prometheus.EnableHandlingTimeHistogram()

l, err := net.Listen("tcp", metricsAddr)
if err != nil {
Expand Down Expand Up @@ -235,7 +234,7 @@ var (
signal.Notify(c, os.Interrupt)
go func() {
<-c
n.Stop(ctx)
_ = n.Stop(ctx)
}()

go func() {
Expand Down
65 changes: 36 additions & 29 deletions swarmd/dockerexec/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@ import (
"strings"
"time"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
engineapi "github.com/docker/docker/client"
gogotypes "github.com/gogo/protobuf/types"
"github.com/moby/moby/api/types/container"
"github.com/moby/moby/api/types/events"
"github.com/moby/moby/client"
"github.com/pkg/errors"
"golang.org/x/time/rate"

"github.com/moby/swarmkit/v2/agent/exec"
"github.com/moby/swarmkit/v2/api"
"github.com/moby/swarmkit/v2/log"
"github.com/pkg/errors"
"golang.org/x/time/rate"
)

// containerAdapter conducts remote operations for a container. All calls
// are mostly naked calls to the client API, seeded with information from
// containerConfig.
type containerAdapter struct {
client engineapi.APIClient
client client.APIClient
container *containerConfig
secrets exec.SecretGetter
}

func newContainerAdapter(client engineapi.APIClient, nodeDescription *api.NodeDescription, task *api.Task, secrets exec.SecretGetter) (*containerAdapter, error) {
func newContainerAdapter(client client.APIClient, nodeDescription *api.NodeDescription, task *api.Task, secrets exec.SecretGetter) (*containerAdapter, error) {
ctnr, err := newContainerConfig(nodeDescription, task)
if err != nil {
return nil, err
Expand All @@ -42,16 +42,16 @@ func newContainerAdapter(client engineapi.APIClient, nodeDescription *api.NodeDe
}, nil
}

func noopPrivilegeFn() (string, error) { return "", nil }
func noopPrivilegeFn(context.Context) (string, error) { return "", nil }

func (c *containerConfig) imagePullOptions() types.ImagePullOptions {
func (c *containerConfig) imagePullOptions() client.ImagePullOptions {
var registryAuth string

if c.spec().PullOptions != nil {
registryAuth = c.spec().PullOptions.RegistryAuth
}

return types.ImagePullOptions{
return client.ImagePullOptions{
// if the image needs to be pulled, the auth config will be retrieved and updated
RegistryAuth: registryAuth,
PrivilegeFunc: noopPrivilegeFn,
Expand Down Expand Up @@ -130,7 +130,7 @@ func (c *containerAdapter) createNetworks(ctx context.Context) error {

func (c *containerAdapter) removeNetworks(ctx context.Context) error {
for _, nid := range c.container.networks() {
if err := c.client.NetworkRemove(ctx, nid); err != nil {
if _, err := c.client.NetworkRemove(ctx, nid, client.NetworkRemoveOptions{}); err != nil {
if isActiveEndpointError(err) {
continue
}
Expand All @@ -144,24 +144,28 @@ func (c *containerAdapter) removeNetworks(ctx context.Context) error {
}

func (c *containerAdapter) create(ctx context.Context) error {
_, err := c.client.ContainerCreate(ctx,
c.container.config(),
c.container.hostConfig(),
c.container.networkingConfig(),
nil,
c.container.name(),
)
_, err := c.client.ContainerCreate(ctx, client.ContainerCreateOptions{
Config: c.container.config(),
HostConfig: c.container.hostConfig(),
NetworkingConfig: c.container.networkingConfig(),
Name: c.container.name(),
})

return err
}

func (c *containerAdapter) start(ctx context.Context) error {
// TODO(nishanttotla): Consider adding checkpoint handling later
return c.client.ContainerStart(ctx, c.container.name(), types.ContainerStartOptions{})
_, err := c.client.ContainerStart(ctx, c.container.name(), client.ContainerStartOptions{})
return err
}

func (c *containerAdapter) inspect(ctx context.Context) (types.ContainerJSON, error) {
return c.client.ContainerInspect(ctx, c.container.name())
func (c *containerAdapter) inspect(ctx context.Context) (container.InspectResponse, error) {
res, err := c.client.ContainerInspect(ctx, c.container.name(), client.ContainerInspectOptions{})
if err != nil {
return container.InspectResponse{}, err
}
return res.Container, nil
}

// events issues a call to the events API and returns a channel with all
Expand All @@ -180,7 +184,7 @@ func (c *containerAdapter) events(ctx context.Context) (<-chan events.Message, <
log.G(ctx).Debugf("waiting on events")
// TODO(stevvooe): For long running tasks, it is likely that we will have
// to restart this under failure.
eventCh, errCh := c.client.Events(ctx, types.EventsOptions{
res := c.client.Events(ctx, client.EventsListOptions{
Since: "0",
Filters: c.container.eventFilter(),
})
Expand All @@ -190,13 +194,13 @@ func (c *containerAdapter) events(ctx context.Context) (<-chan events.Message, <

for {
select {
case msg := <-eventCh:
case msg := <-res.Messages:
select {
case eventsq <- msg:
case <-ctx.Done():
return
}
case err := <-errCh:
case err := <-res.Err:
log.G(ctx).WithError(err).Error("error from events stream")
return
case <-ctx.Done():
Expand All @@ -220,18 +224,21 @@ func (c *containerAdapter) shutdown(ctx context.Context) error {
stopgraceFromProto, _ := gogotypes.DurationFromProto(spec.StopGracePeriod)
stopgraceSeconds = int(stopgraceFromProto.Seconds())
}
return c.client.ContainerStop(ctx, c.container.name(), container.StopOptions{Timeout: &stopgraceSeconds})
_, err := c.client.ContainerStop(ctx, c.container.name(), client.ContainerStopOptions{Timeout: &stopgraceSeconds})
return err
}

func (c *containerAdapter) terminate(ctx context.Context) error {
return c.client.ContainerKill(ctx, c.container.name(), "")
_, err := c.client.ContainerKill(ctx, c.container.name(), client.ContainerKillOptions{})
return err
}

func (c *containerAdapter) remove(ctx context.Context) error {
return c.client.ContainerRemove(ctx, c.container.name(), types.ContainerRemoveOptions{
_, err := c.client.ContainerRemove(ctx, c.container.name(), client.ContainerRemoveOptions{
RemoveVolumes: true,
Force: true,
})
return err
}

func (c *containerAdapter) createVolumes(ctx context.Context) error {
Expand Down Expand Up @@ -268,7 +275,7 @@ func (c *containerAdapter) logs(ctx context.Context, options api.LogSubscription
return nil, errors.New("logs not supported on services with TTY")
}

apiOptions := types.ContainerLogsOptions{
apiOptions := client.ContainerLogsOptions{
Follow: options.Follow,
Timestamps: true,
Details: false,
Expand Down
Loading