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
6066func 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.
103100func (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
107111func (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