Skip to content

Commit c97e96d

Browse files
henrybarretogustavosbarreto
authored andcommitted
feat(agent): introduce connector label for selective management
This commit introduces the ability to dynamically configure which containers are added to ShellHub as devices when the agent is started in connector mode. By allowing users to specify a label for the containers, the ShellHub agent can now selectively manage containers based on user-defined criteria. This enhancement improves the flexibility and usability of the ShellHub agent, enabling more precise control over which containers are integrated into the system, ultimately streamlining device management.
1 parent bf5b6ad commit c97e96d

File tree

3 files changed

+31
-18
lines changed

3 files changed

+31
-18
lines changed

agent/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ func main() {
227227
logger.Info("Starting ShellHub Agent Connector")
228228

229229
connector.ConnectorVersion = AgentVersion
230-
connector, err := connector.NewDockerConnector(cfg.ServerAddress, cfg.TenantID, cfg.PrivateKeys)
230+
connector, err := connector.NewDockerConnector(cfg)
231231
if err != nil {
232232
logger.Fatal("Failed to create ShellHub Agent Connector")
233233
}

install.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ podman_install() {
2525
MODE="connector"
2626
DEFAULT_CONTAINER_NAME="shellhub-connector"
2727
ARGS="$ARGS -e SHELLHUB_PRIVATE_KEYS=${PRIVATE_KEYS:-/host/etc/shellhub/connector/keys}"
28+
ARGS="$ARGS -e SHELLHUB_CONNECTOR_LABEL=${SHELLHUB_CONNECTOR_LABEL}"
2829

2930
echo "🚀 Starting ShellHub container in Docker Connector mode..."
3031
shift 1
@@ -96,6 +97,7 @@ docker_install() {
9697
MODE="connector"
9798
DEFAULT_CONTAINER_NAME="shellhub-connector"
9899
ARGS="$ARGS -e SHELLHUB_PRIVATE_KEYS=${PRIVATE_KEYS:-/host/etc/shellhub/connector/keys}"
100+
ARGS="$ARGS -e SHELLHUB_CONNECTOR_LABEL=${SHELLHUB_CONNECTOR_LABEL}"
99101

100102
echo "🚀 Starting ShellHub container in Docker Connector mode..."
101103
shift 1

pkg/agent/connector/docker.go

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/Masterminds/semver"
1010
"github.com/docker/docker/api/types/container"
1111
"github.com/docker/docker/api/types/events"
12+
"github.com/docker/docker/api/types/filters"
1213
dockerclient "github.com/docker/docker/client"
1314
"github.com/shellhub-io/shellhub/pkg/agent"
1415
"github.com/shellhub-io/shellhub/pkg/api/client"
@@ -30,6 +31,8 @@ type DockerConnector struct {
3031
cli *dockerclient.Client
3132
// privateKeys is the path to the directory that contains the private keys for the containers.
3233
privateKeys string
34+
// Label is the label used to identify the containers managed by the ShellHub agent.
35+
Label string
3336
// cancels is a map that contains the cancel functions for each container.
3437
// This is used to stop the agent for a container, marking as done its context and closing the agent.
3538
cancels map[string]context.CancelFunc
@@ -55,6 +58,9 @@ type Config struct {
5558
// has a direct impact of the bandwidth used by the device when in idle
5659
// state. Default is 30 seconds.
5760
KeepAliveInterval int `env:"KEEPALIVE_INTERVAL,overwrite,default=30"`
61+
62+
// Label is the label used to identify the containers managed by the ShellHub agent.
63+
Label string `env:"CONNECTOR_LABEL,default="`
5864
}
5965

6066
func LoadConfigFromEnv() (*Config, map[string]interface{}, error) {
@@ -73,39 +79,44 @@ func LoadConfigFromEnv() (*Config, map[string]interface{}, error) {
7379
return cfg, nil, nil
7480
}
7581

76-
func NewDockerConnectorWithClient(cli *dockerclient.Client, server string, tenant string, privateKey string) (Connector, error) {
77-
return &DockerConnector{
78-
server: server,
79-
tenant: tenant,
80-
cli: cli,
81-
privateKeys: privateKey,
82-
cancels: make(map[string]context.CancelFunc),
83-
}, nil
84-
}
85-
8682
// NewDockerConnector creates a new [Connector] that uses Docker as the container runtime.
87-
func NewDockerConnector(server string, tenant string, privateKey string) (Connector, error) {
83+
func NewDockerConnector(config *Config) (Connector, error) {
8884
cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv, dockerclient.WithAPIVersionNegotiation())
8985
if err != nil {
9086
return nil, err
9187
}
9288

9389
return &DockerConnector{
94-
server: server,
95-
tenant: tenant,
9690
cli: cli,
97-
privateKeys: privateKey,
91+
server: config.ServerAddress,
92+
tenant: config.TenantID,
93+
privateKeys: config.PrivateKeys,
94+
Label: config.Label,
9895
cancels: make(map[string]context.CancelFunc),
9996
}, nil
10097
}
10198

10299
// events returns the docker events.
103100
func (d *DockerConnector) events(ctx context.Context) (<-chan events.Message, <-chan error) {
104-
return d.cli.Events(ctx, events.ListOptions{})
101+
filters := filters.NewArgs()
102+
if d.Label != "" {
103+
filters.Add("label", d.Label)
104+
}
105+
106+
return d.cli.Events(ctx, events.ListOptions{
107+
Filters: filters,
108+
})
105109
}
106110

107111
func (d *DockerConnector) List(ctx context.Context) ([]Container, error) {
108-
containers, err := d.cli.ContainerList(ctx, container.ListOptions{})
112+
filters := filters.NewArgs()
113+
if d.Label != "" {
114+
filters.Add("label", d.Label)
115+
}
116+
117+
containers, err := d.cli.ContainerList(ctx, container.ListOptions{
118+
Filters: filters,
119+
})
109120
if err != nil {
110121
return nil, err
111122
}
@@ -187,7 +198,7 @@ func (d *DockerConnector) Listen(ctx context.Context) error {
187198
case err := <-errs:
188199
return err
189200
case container := <-events:
190-
// NOTICE: "start" and "die" Docker's events are call every time a new container start or stop,
201+
// NOTE: "start" and "die" Docker's events are call every time a new container start or stop,
191202
// independently how the command was run. For example, if a container was started with `docker run -d`, the
192203
// "start" event will be called, but if the same container was started with `docker start <container-id>`,
193204
// the "start" event will be called too. The same happens with the "die" event.

0 commit comments

Comments
 (0)