Skip to content

Commit

Permalink
deconflict networks before creating (#172)
Browse files Browse the repository at this point in the history
#171 introduced a bug where duplicate networks were being sent to the
daemon request when additional networks were specified. this adds
deduplication before sending the request and adds a test
  • Loading branch information
joshrwolf authored Aug 20, 2024
1 parent 4166df9 commit 46abc5c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
24 changes: 18 additions & 6 deletions internal/harness/k3s/k3s.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (h *k3s) Create(ctx context.Context) error {
}

if err := h.startSandbox(ctx, cli, kresp); err != nil {
return fmt.Errorf("starting sandbox: %w", err)
return fmt.Errorf("creating sandbox: %w", err)
}

return nil
Expand Down Expand Up @@ -263,12 +263,24 @@ func (h *k3s) startSandbox(ctx context.Context, cli *docker.Client, resp *docker
return fmt.Errorf("getting kubeconfig: %w", err)
}

// Attach the sandbox to all networks the k3s service is also part of
networks := make(map[string]struct{})
for _, nw := range h.Sandbox.Networks {
networks[nw.ID] = struct{}{}
}

// Attach the sandbox to any networks k3s is also a part of, excluding any
// invalid networks or networks already attached (the daemon cannot deconflict
// these)
for nn, nw := range resp.NetworkSettings.Networks {
h.Sandbox.Networks = append(h.Sandbox.Networks, docker.NetworkAttachment{
Name: nn,
ID: nw.NetworkID,
})
if nn == "" {
continue
}
if _, ok := networks[nn]; !ok {
h.Sandbox.Networks = append(h.Sandbox.Networks, docker.NetworkAttachment{
Name: nn,
ID: nw.NetworkID,
})
}
}

h.Sandbox.Name = resp.Name + "-sandbox"
Expand Down
27 changes: 27 additions & 0 deletions internal/provider/harness_k3s_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,33 @@ resource "imagetest_feature" "test" {
cmd = "kubectl get po -A"
},
]
}
`,
},
},
"with additional networks": {
// Create testing
{
ExpectNonEmptyPlan: true,
Config: `
data "imagetest_inventory" "this" {}
resource "imagetest_harness_k3s" "test" {
name = "test"
inventory = data.imagetest_inventory.this
networks = { "bridge" = { name = "bridge" } }
}
resource "imagetest_feature" "test" {
name = "Simple k3s based test"
description = "Test that we can attach existing networks"
harness = imagetest_harness_k3s.test
steps = [
{
name = "Access cluster"
cmd = "kubectl get po -A"
},
]
}
`,
},
Expand Down

0 comments on commit 46abc5c

Please sign in to comment.