Skip to content

Commit 10371e5

Browse files
author
Motalleb Fallahnezhad
committed
fix: docker label matcher issues
1 parent a6450df commit 10371e5

File tree

4 files changed

+59
-39
lines changed

4 files changed

+59
-39
lines changed

config.local.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,23 @@ jobs:
66
concurrency: 3
77
debounce: 5s
88
tasks:
9-
- command: echo {{ .Vars.name }}
9+
- command: echo {{ .Vars.name }}; cat /etc/os-release
10+
env:
11+
SHELL: /bin/sh
1012
retry-delay: 1s
1113
retries: 0
1214
retry-jitter: 1s
1315
retry-mode: fibonacci
1416
vars:
1517
name: '{{ now | date "2006-01-02_15-04-05" }}'
16-
# connections:
17-
# - image: "library/alpine"
18+
connections:
19+
- label: "connection.selector"
1820

1921
events:
2022
# - cron: "0 * * * * *"
2123
# - interval: 5s
2224
- on-init: true
2325
- web-event: test
24-
# - docker:
25-
# image: nginx
26+
- docker:
27+
image: nginx
28+
name: "(webserver|app|application)"

core/cmd_connection/docker_attach.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@ func init() {
2222
}
2323

2424
type DockerAttachConnection struct {
25-
conn *config.TaskConnection
26-
log *zap.Logger
27-
cli *client.Client
28-
execCFG *container.ExecOptions
29-
containerID string
30-
ctx context.Context
25+
conn *config.TaskConnection
26+
log *zap.Logger
27+
cli *client.Client
28+
execCFG *container.ExecOptions
29+
ctx context.Context
3130
}
3231

3332
// NewDockerAttachConnection creates a new DockerAttachConnection instance.
@@ -66,33 +65,6 @@ func (d *DockerAttachConnection) Prepare(ctx context.Context, task *config.Task)
6665
d.log.Debug("No explicit docker connection specified, using default: `unix:///var/run/docker.sock`")
6766
d.conn.DockerConnection = "unix:///var/run/docker.sock"
6867
}
69-
cid := d.conn.ContainerName
70-
if cid == "" {
71-
label := d.conn.ContainerLabel
72-
if label == "" {
73-
return errors.New("neither container name nor label provided")
74-
}
75-
76-
args := filters.NewArgs()
77-
args.Add("label", label)
78-
79-
containers, err := d.cli.ContainerList(ctx, container.ListOptions{
80-
Filters: args,
81-
})
82-
if err != nil {
83-
return err
84-
}
85-
86-
if len(containers) == 0 {
87-
return fmt.Errorf("no container found with label %q", label)
88-
}
89-
90-
if len(containers) != 1 {
91-
return fmt.Errorf("more than one container found with label %q", label)
92-
}
93-
cid = containers[0].ID
94-
}
95-
d.containerID = cid
9668
shell, shellArgs, environments := cmdCtx.BuildExecuteParams(task.Command)
9769
cmd := append(
9870
[]string{shell},
@@ -135,8 +107,35 @@ func (d *DockerAttachConnection) Connect() error {
135107
// - A byte slice containing the command output.
136108
// - An error if the execution fails, otherwise nil.
137109
func (d *DockerAttachConnection) Execute() ([]byte, error) {
110+
cid := d.conn.ContainerName
111+
if cid == "" {
112+
label := d.conn.ContainerLabel
113+
if label == "" {
114+
return nil, errors.New("neither container name nor label provided")
115+
}
116+
117+
args := filters.NewArgs()
118+
args.Add("label", label)
119+
120+
containers, err := d.cli.ContainerList(d.ctx, container.ListOptions{
121+
Filters: args,
122+
})
123+
if err != nil {
124+
return nil, err
125+
}
126+
127+
if len(containers) == 0 {
128+
return nil, fmt.Errorf("no container found with label %q", label)
129+
}
130+
131+
if len(containers) != 1 {
132+
return nil, fmt.Errorf("more than one container found with label %q", label)
133+
}
134+
cid = containers[0].ID
135+
}
136+
138137
// Create the exec instance
139-
exec, err := d.cli.ContainerExecCreate(d.ctx, d.containerID, *d.execCFG)
138+
exec, err := d.cli.ContainerExecCreate(d.ctx, cid, *d.execCFG)
140139
if err != nil {
141140
return nil, err
142141
}

docker-compose.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
cron:
3+
build:
4+
context: .
5+
target: slim
6+
volumes:
7+
- "./config.local.yaml:/config.yaml"
8+
- "/var/run/docker.sock:/var/run/docker.sock"
9+
tester:
10+
image: alpine:latest
11+
command: ["sh", "-c", "while true; do echo hello world; sleep 5; done"]
12+
labels:
13+
- connection.selector=true

schema.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@
436436
"title": "container name/id matcher",
437437
"description": "Should match only one container, if matches two or more container the command will fail"
438438
},
439+
"label": {
440+
"type": "string",
441+
"title": "container label matcher",
442+
"description": "Finds container that has this label, should match only one container, if matches two or more container the command will fail"
443+
},
439444
"image": {
440445
"type": "string",
441446
"title": "Image name/id"

0 commit comments

Comments
 (0)