-
Notifications
You must be signed in to change notification settings - Fork 122
/
client_docker.go
61 lines (49 loc) · 1.64 KB
/
client_docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package sender
import (
"context"
"github.com/newrelic/infrastructure-agent/pkg/ipc"
"github.com/docker/docker/client"
"github.com/newrelic/infrastructure-agent/internal/os/api/signals"
"github.com/newrelic/infrastructure-agent/pkg/helpers"
"github.com/pkg/errors"
)
type dockerClient struct {
client *client.Client
apiVersion string
containerID string
}
// RequiredContainerIDErr is the error to return when container ID is not provided.
var RequiredContainerIDErr = errors.New("container ID of the agent is required")
// NewDockerClient creates a containerised agent control client for Docker containers.
// Didn't use helpers.DockerClient because it'll broad the interface breaking SRP.
func NewDockerClient(apiVersion string, containerID string) (c Client, err error) {
if containerID == "" {
err = RequiredContainerIDErr
return
}
if !helpers.IsDockerRunning() {
err = helpers.ErrNoDockerd
return
}
cl, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
err = errors.Wrap(err, "failed to initialize docker client")
return
}
c = &dockerClient{
client: cl,
apiVersion: apiVersion,
containerID: containerID,
}
return
}
// Notify will notify a running agent process inside a docker container.
func (c *dockerClient) Notify(ctx context.Context, _ ipc.Message) (err error) {
return c.client.ContainerKill(ctx, c.containerID, signals.NotificationStr)
}
// Return the identification for the notified agent.
func (c *dockerClient) GetID() string {
return c.containerID
}