From d33387a495a81e5011f5890c18145bc8c87251e7 Mon Sep 17 00:00:00 2001 From: steiler Date: Wed, 2 Jun 2021 13:50:34 +0000 Subject: [PATCH] port bindings and mac address config --- runtime/containerd/containerd.go | 44 ++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/runtime/containerd/containerd.go b/runtime/containerd/containerd.go index 6f50562d6..1bbb79118 100644 --- a/runtime/containerd/containerd.go +++ b/runtime/containerd/containerd.go @@ -168,12 +168,39 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod return err } - var rc libcni.RuntimeConf - rc.ContainerID = node.LongName - rc.IfName = "eth0" - rc.NetNS = node.NSPath + cnirc := &libcni.RuntimeConf{ + ContainerID: node.ShortName, + IfName: "eth0", + NetNS: node.NSPath, + CapabilityArgs: make(map[string]interface{}), + //"portMappings": []portMapping{ + // {HostPort: 8080, ContainerPort: 80, Protocol: "tcp"}, + //}, + } + + // set mac if defined in node + if node.MacAddress != "" { + cnirc.CapabilityArgs["mac"] = node.MacAddress + } + + portmappings := []portMapping{} + + for contdatasl, hostdata := range node.PortBindings { + //fmt.Printf("%+v", hostdata) + //fmt.Printf("%+v", contdatasl) + for _, x := range hostdata { + hostport, err := strconv.Atoi(x.HostPort) + if err != nil { + return err + } + portmappings = append(portmappings, portMapping{HostPort: hostport, ContainerPort: contdatasl.Int(), Protocol: contdatasl.Proto()}) + } + } + if len(portmappings) > 0 { + cnirc.CapabilityArgs["portMappings"] = portmappings + } - res, err := cnic.AddNetworkList(ctx, cncl, &rc) + res, err := cnic.AddNetworkList(ctx, cncl, cnirc) if err != nil { return err } @@ -183,6 +210,13 @@ func (c *ContainerdRuntime) CreateContainer(ctx context.Context, node *types.Nod } +type portMapping struct { + HostPort int `json:"hostPort"` + HostIP string `json:"hostIP,omitempty"` + ContainerPort int `json:"containerPort"` + Protocol string `json:"protocol"` +} + func WithSysctls(sysctls map[string]string) oci.SpecOpts { return func(ctx context.Context, client oci.Client, c *containers.Container, s *specs.Spec) error { if s.Linux == nil {