Skip to content

Commit ba5b1c5

Browse files
Updated to fix bug on greedy match when looking up Docker containers
1 parent 1366690 commit ba5b1c5

File tree

10 files changed

+31
-33
lines changed

10 files changed

+31
-33
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## version 0.0.16
44

5+
### Bugfixes
6+
* By default when looking up the id of a Docker container Docker does a greedy match, this caused issues where we would grab an incorrect id on destroy. Changed to use a regex.
7+
8+
## version 0.0.16
9+
510
### Bugfixes
611
* Fixes a bug where local folders were being created for types other than `bind`
712
* Fix a bug where Volumes for Nomad Clusters were not changed to absolute paths

functional_tests/test_fixtures/nomad/app_config/example2.nomad

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ job "example_2" {
1818
healthy_deadline = "5m"
1919
}
2020

21-
group "cache" {
21+
group "consul" {
2222
count = 1
2323

2424
restart {
@@ -33,16 +33,16 @@ job "example_2" {
3333
size = 200
3434
}
3535

36-
task "redis" {
36+
task "consul" {
3737
# The "driver" parameter specifies the task driver that should be used to
3838
# run the task.
3939
driver = "docker"
4040

4141
config {
42-
image = "redis:3.2"
42+
image = "consul:1.7.1"
4343

4444
port_map {
45-
db = 6380
45+
http = 8500
4646
}
4747
}
4848

@@ -52,20 +52,7 @@ job "example_2" {
5252

5353
network {
5454
mbits = 10
55-
port "db" {}
56-
}
57-
}
58-
59-
service {
60-
name = "redis-cache"
61-
tags = ["global", "cache"]
62-
port = "db"
63-
64-
check {
65-
name = "alive"
66-
type = "tcp"
67-
interval = "10s"
68-
timeout = "2s"
55+
port "http" {}
6956
}
7057
}
7158
}

pkg/clients/docker_tasks.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ func (d *DockerTasks) FindContainerIDs(containerName string, typeName config.Res
233233
fullName := utils.FQDN(containerName, string(typeName))
234234

235235
args := filters.NewArgs()
236-
args.Add("name", fullName)
236+
// By default Docker will wildcard searches, use regex to return the absolute
237+
args.Add("name", fmt.Sprintf("^/%s$", fullName))
237238

238239
opts := types.ContainerListOptions{Filters: args, All: true}
239240

pkg/clients/docker_tasks_find_ids_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestFindContainerIDsReturnsID(t *testing.T) {
3131

3232
// ensure that the FQDN was passed as an argument
3333
args := getCalls(&md.Mock, "ContainerList")[0].Arguments[1].(types.ContainerListOptions)
34-
assert.Equal(t, "test.cloud.shipyard.run", args.Filters.Get("name")[0])
34+
assert.Equal(t, "^/test.cloud.shipyard.run$", args.Filters.Get("name")[0])
3535

3636
// ensure that the id has been returned
3737
assert.Len(t, ids, 2)

pkg/config/parser.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ func parseYardMarkdown(file string, c *Config) error {
124124

125125
fr, body, err := m.Parse(f)
126126
if err != nil && err != front.ErrIsEmpty {
127-
return err
127+
fmt.Println("Error parsing README.md", err)
128+
return nil
128129
}
129130

130131
bp := &Blueprint{}

pkg/providers/cluster_k3s.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func (c *K8sCluster) createK3s() error {
6666
c.log.Info("Creating Cluster", "ref", c.config.Name)
6767

6868
// check the cluster does not already exist
69-
ids, err := c.client.FindContainerIDs(c.config.Name, c.config.Type)
69+
ids, err := c.client.FindContainerIDs(fmt.Sprintf("server.%s", c.config.Name), c.config.Type)
7070
if err != nil {
7171
return err
7272
}
@@ -295,7 +295,7 @@ func (c *K8sCluster) ImportLocalDockerImages(name string, id string, images []co
295295
func (c *K8sCluster) destroyK3s() error {
296296
c.log.Info("Destroy Cluster", "ref", c.config.Name)
297297

298-
ids, err := c.client.FindContainerIDs(c.config.Name, c.config.Type)
298+
ids, err := c.client.FindContainerIDs(fmt.Sprintf("server.%s", c.config.Name), c.config.Type)
299299
if err != nil {
300300
return err
301301
}

pkg/providers/cluster_k3s_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func TestClusterK3ErrorsWhenUnableToLookupIDs(t *testing.T) {
8080

8181
func TestClusterK3ErrorsWhenClusterExists(t *testing.T) {
8282
md := &mocks.MockContainerTasks{}
83-
md.On("FindContainerIDs", mock.Anything, mock.Anything).Return([]string{"abc"}, nil)
83+
md.On("FindContainerIDs", "server."+clusterConfig.Name, mock.Anything).Return([]string{"abc"}, nil)
8484

8585
mk := &mocks.MockKubernetes{}
8686
p := NewK8sCluster(clusterConfig, md, mk, nil, hclog.NewNullLogger())
@@ -340,7 +340,7 @@ func TestClusterK3sDestroyGetsIDr(t *testing.T) {
340340

341341
err := p.Destroy()
342342
assert.NoError(t, err)
343-
md.AssertCalled(t, "FindContainerIDs", clusterConfig.Name, clusterConfig.Type)
343+
md.AssertCalled(t, "FindContainerIDs", "server."+clusterConfig.Name, clusterConfig.Type)
344344
}
345345

346346
func TestClusterK3sDestroyWithFindIDErrorReturnsError(t *testing.T) {

pkg/providers/cluster_nomad.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (c *NomadCluster) createNomad() error {
4545
c.log.Info("Creating Cluster", "ref", c.config.Name)
4646

4747
// check the cluster does not already exist
48-
ids, err := c.client.FindContainerIDs(c.config.Name, c.config.Type)
48+
ids, err := c.client.FindContainerIDs(fmt.Sprintf("server.%s", c.config.Name), c.config.Type)
4949
if len(ids) > 0 {
5050
return ErrorClusterExists
5151
}
@@ -171,19 +171,22 @@ func (c *NomadCluster) ImportLocalDockerImages(name string, id string, images []
171171
}
172172

173173
func (c *NomadCluster) destroyNomad() error {
174-
c.log.Info("Destroy Cluster", "ref", c.config.Name)
174+
c.log.Info("Destroy Nomad Cluster", "ref", c.config.Name)
175175

176-
ids, err := c.client.FindContainerIDs(c.config.Name, c.config.Type)
176+
// FindContainerIDs works on absolute addresses, we need to append the server
177+
178+
ids, err := c.client.FindContainerIDs(fmt.Sprintf("server.%s", c.config.Name), c.config.Type)
177179
if err != nil {
178180
return err
179181
}
180182

181183
for _, i := range ids {
182184
// remove from the networks
183185
for _, n := range c.config.Networks {
186+
c.log.Debug("Detaching container from network", "ref", c.config.Name, "id", i, "network", n.Name)
184187
err := c.client.DetachNetwork(n.Name, i)
185188
if err != nil {
186-
return err
189+
c.log.Error("Unable to detach network", "ref", c.config.Name, "network", n.Name, "error", err)
187190
}
188191
}
189192

pkg/providers/cluster_nomad_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func TestClusterNomadErrorsWhenUnableToLookupIDs(t *testing.T) {
7070

7171
func TestClusterNomadErrorsWhenClusterExists(t *testing.T) {
7272
md := &mocks.MockContainerTasks{}
73-
md.On("FindContainerIDs", mock.Anything, mock.Anything).Return([]string{"abc"}, nil)
73+
md.On("FindContainerIDs", "server."+clusterNomadConfig.Name, mock.Anything).Return([]string{"abc"}, nil)
7474

7575
p := NewNomadCluster(clusterNomadConfig, md, nil, hclog.NewNullLogger())
7676

@@ -260,7 +260,7 @@ func TestClusterNomadDestroyGetsIDr(t *testing.T) {
260260

261261
err := p.Destroy()
262262
assert.NoError(t, err)
263-
md.AssertCalled(t, "FindContainerIDs", clusterNomadConfig.Name, clusterNomadConfig.Type)
263+
md.AssertCalled(t, "FindContainerIDs", "server."+clusterNomadConfig.Name, clusterNomadConfig.Type)
264264
}
265265

266266
func TestClusterNomadDestroyWithFindIDErrorReturnsError(t *testing.T) {

pkg/providers/ingress.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (i *Ingress) Create() error {
180180

181181
// Destroy the ingress
182182
func (i *Ingress) Destroy() error {
183-
i.log.Info("Destroy Ingress", "ref", i.config.Name)
183+
i.log.Info("Destroy Ingress", "ref", i.config.Name, "type", i.config.Type)
184184

185185
ids, err := i.client.FindContainerIDs(i.config.Name, i.config.Type)
186186
if err != nil {
@@ -189,9 +189,10 @@ func (i *Ingress) Destroy() error {
189189

190190
for _, id := range ids {
191191
for _, n := range i.config.Networks {
192+
i.log.Debug("Detaching container from network", "ref", i.config.Name, "id", id, "network", n.Name)
192193
err := i.client.DetachNetwork(n.Name, id)
193194
if err != nil {
194-
i.log.Error("Unable to detach network", "ref", i.config.Name, "network", n.Name)
195+
i.log.Error("Unable to detach network", "ref", i.config.Name, "network", n.Name, "error", err)
195196
}
196197
}
197198

0 commit comments

Comments
 (0)