Skip to content

Commit

Permalink
incorporated @karimra's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
steiler committed Jun 16, 2021
1 parent d55245c commit 25e3681
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
6 changes: 1 addition & 5 deletions clab/clab.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,9 @@ func (c *CLab) DeleteNodes(ctx context.Context, workers uint, containers []types
log.Debugf("Worker %d terminating...", i)
return
}
name := cont.ID
if len(cont.Names) > 0 {
name = strings.TrimLeft(cont.Names[0], "/")
}
err := c.Runtime.DeleteContainer(ctx, cont)
if err != nil {
log.Errorf("could not remove container '%s': %v", name, err)
log.Errorf("could not remove container '%s': %v", cont.ID, err)
}
case <-ctx.Done():
return
Expand Down
3 changes: 1 addition & 2 deletions clab/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,8 +661,7 @@ func (c *CLab) VerifyContainersUniqueness(ctx context.Context) error {
nctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()

labels := []*types.GenericFilter{}
containers, err := c.Runtime.ListContainers(nctx, labels)
containers, err := c.Runtime.ListContainers(nctx, nil)
if err != nil {
return fmt.Errorf("could not list containers: %v", err)
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ var execCmd = &cobra.Command{
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
labels := []*types.GenericFilter{{FilterType: "label", Match: name, Field: "containerlab", Operator: "="}}
containers, err := c.Runtime.ListContainers(ctx, labels)
filters := []*types.GenericFilter{{FilterType: "label", Match: name, Field: "containerlab", Operator: "="}}
filters = append(filters, types.FilterFromLabelStrings(labels)...)
containers, err := c.Runtime.ListContainers(ctx, filters)
if err != nil {
log.Fatalf("could not list containers: %v", err)
}
Expand Down
23 changes: 5 additions & 18 deletions runtime/containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (c *ContainerdRuntime) WithConfig(cfg *runtime.RuntimeConfig) {
func (c *ContainerdRuntime) WithMgmtNet(n *types.MgmtNet) {
if n.Bridge == "" {
netname := "clab"
if n.Network == "" {
if n.Network != "" {
netname = n.Network
}
n.Bridge = "br-" + netname
Expand Down Expand Up @@ -157,9 +157,6 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod
mounts[idx] = m
}

//mounts = append(mounts, specs.Mount{Type: "cgroup", Source: "cgroup", Destination: "/sys/fs/cgroup", Options: []string{"ro", "nosuid", "noexec", "nodev"}})

_ = cmd
opts := []oci.SpecOpts{
oci.WithImageConfig(img),
oci.WithEnv(utils.ConvertEnvs(node.Env)),
Expand Down Expand Up @@ -246,9 +243,6 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod
return err
}

//s, _ := newContainer.Spec(ctx)
//fmt.Printf("%+v", s.Process)

log.Debugf("Container '%s' created", node.LongName)
log.Debugf("Start container: %s", node.LongName)

Expand Down Expand Up @@ -356,7 +350,7 @@ func cniInit(cId, ifName string, mgmtNet *types.MgmtNet) (*libcni.CNIConfig, *li
]
}
`, mgmtNet.Bridge, mgmtNet.IPv4Subnet, mgmtNet.IPv6Subnet, mgmtNet.MTU)
//log.Debug(cniConfig)

cncl, err := libcni.ConfListFromBytes([]byte(cniConfig))
if err != nil {
return nil, nil, nil, err
Expand Down Expand Up @@ -401,9 +395,6 @@ func (c *ContainerdRuntime) StartContainer(ctx context.Context, containername st
}
task, err := container.NewTask(ctx, cio.LogFile("/tmp/clab/"+containername+".log"))
if err != nil {
log.Fatal(err)
log.Fatalf("Failed to start container %s", containername)

return err
}
err = task.Start(ctx)
Expand Down Expand Up @@ -455,7 +446,7 @@ func (c *ContainerdRuntime) StopContainer(ctx context.Context, containername str
return err
}

err = waitContainerStop(ctx, exitCh, containername)
err = waitContainerStop(ctx, exitCh)
if err != nil {
return err
}
Expand All @@ -469,7 +460,7 @@ func (c *ContainerdRuntime) StopContainer(ctx context.Context, containername str
return nil
}

func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus, id string) error {
func waitContainerStop(ctx context.Context, exitCh <-chan containerd.ExitStatus) error {
select {
case <-ctx.Done():
return ctx.Err()
Expand All @@ -484,11 +475,7 @@ func (c *ContainerdRuntime) getContainerTask(ctx context.Context, containername
if err != nil {
return nil, err
}
task, err := cont.Task(ctx, nil)
if err != nil {
return nil, err
}
return task, nil
return cont.Task(ctx, nil)
}

func (c *ContainerdRuntime) ListContainers(ctx context.Context, filter []*types.GenericFilter) ([]types.GenericContainer, error) {
Expand Down
23 changes: 23 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"os"
"path/filepath"
"strings"
"text/template"

"github.com/cloudflare/cfssl/log"
Expand Down Expand Up @@ -144,3 +145,25 @@ type GenericFilter struct {
// match value
Match string
}

func FilterFromLabelStrings(labels []string) []*GenericFilter {
gfl := []*GenericFilter{}
var gf *GenericFilter
for _, s := range labels {
gf = &GenericFilter{
FilterType: "label",
}
if strings.Contains(s, "=") {
gf.Operator = "="
subs := strings.Split(s, "=")
gf.Field = strings.TrimSpace(subs[0])
gf.Match = strings.TrimSpace(subs[1])
} else {
gf.Match = "exists"
gf.Field = strings.TrimSpace(s)
}

gfl = append(gfl, gf)
}
return gfl
}

0 comments on commit 25e3681

Please sign in to comment.