Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Implement hostPort support with CNI #375

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 37 additions & 12 deletions pkg/network/cni/cni.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

gocni "github.com/containerd/go-cni"
log "github.com/sirupsen/logrus"
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/constants"
"github.com/weaveworks/ignite/pkg/network"
"github.com/weaveworks/ignite/pkg/runtime"
Expand All @@ -31,18 +32,28 @@ const (

// igniteCNIConf is a base CNI configuration that will enable VMs to access the internet connection (docker-bridge style)
var igniteCNIConf = `{
"cniVersion": "0.4.0",
"name": "ignite-containerd-default",
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"isDefaultGateway": true,
"promiscMode": true,
"ipMasq": true,
"ipam": {
"cniVersion": "0.4.0",
"name": "ignite-containerd-bridge",
"plugins": [
{
"type": "bridge",
"bridge": "cni0",
"isGateway": true,
"isDefaultGateway": true,
"promiscMode": true,
"ipMasq": true,
"ipam": {
"type": "host-local",
"subnet": "172.18.0.0/16"
}
}
},
{
"type": "portmap",
"capabilities": {
"portMappings": true
}
}
]
}
`

Expand Down Expand Up @@ -85,7 +96,7 @@ func (plugin *cniNetworkPlugin) PrepareContainerSpec(container *runtime.Containe
return nil
}

func (plugin *cniNetworkPlugin) SetupContainerNetwork(containerid string) (*network.Result, error) {
func (plugin *cniNetworkPlugin) SetupContainerNetwork(containerid string, portMappings ...meta.PortMapping) (*network.Result, error) {
if err := plugin.initialize(); err != nil {
return nil, err
}
Expand All @@ -95,8 +106,22 @@ func (plugin *cniNetworkPlugin) SetupContainerNetwork(containerid string) (*netw
return nil, fmt.Errorf("CNI failed to retrieve network namespace path: %v", err)
}

pms := []gocni.PortMapping{}
for _, pm := range portMappings {
hostIP := ""
if pm.BindAddress != nil {
hostIP = pm.BindAddress.String()
}
pms = append(pms, gocni.PortMapping{
HostPort: int32(pm.HostPort),
ContainerPort: int32(pm.VMPort),
Protocol: pm.Protocol.String(),
HostIP: hostIP,
})
}

netnsPath := fmt.Sprintf(netNSPathFmt, c.PID)
result, err := plugin.cni.Setup(context.Background(), containerid, netnsPath)
result, err := plugin.cni.Setup(context.Background(), containerid, netnsPath, gocni.WithCapabilityPortMap(pms))
if err != nil {
log.Errorf("failed to setup network for namespace %q: %v", containerid, err)
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"

meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/network"
"github.com/weaveworks/ignite/pkg/runtime"
)
Expand All @@ -25,7 +26,7 @@ func (*dockerNetworkPlugin) PrepareContainerSpec(_ *runtime.ContainerConfig) err
return nil
}

func (plugin *dockerNetworkPlugin) SetupContainerNetwork(containerID string) (*network.Result, error) {
func (plugin *dockerNetworkPlugin) SetupContainerNetwork(containerID string, _ ...meta.PortMapping) (*network.Result, error) {
// This is used to fetch the IP address the runtime gives to the VM container
result, err := plugin.runtime.InspectContainer(containerID)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/network/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"

meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
"github.com/weaveworks/ignite/pkg/runtime"
)

Expand All @@ -17,7 +18,7 @@ type Plugin interface {

// SetupContainerNetwork sets up the networking for a container
// This is ran _after_ the container has been started
SetupContainerNetwork(containerID string) (*Result, error)
SetupContainerNetwork(containerID string, portmappings ...meta.PortMapping) (*Result, error)

// RemoveContainerNetwork is the method called before a container using the network plugin can be deleted
RemoveContainerNetwork(containerID string) error
Expand Down
2 changes: 1 addition & 1 deletion pkg/operations/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ func StopVM(vm *api.VM, kill, silent bool) error {
}

func removeNetworking(containerID string) error {
log.Debugf("Removing the container with ID %q from the %q network", containerID, providers.NetworkPlugin.Name())
log.Infof("Removing the container with ID %q from the %q network", containerID, providers.NetworkPlugin.Name())
return providers.NetworkPlugin.RemoveContainerNetwork(containerID)
}
2 changes: 1 addition & 1 deletion pkg/operations/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func StartVM(vm *api.VM, debug bool) error {
}

// Set up the networking
result, err := providers.NetworkPlugin.SetupContainerNetwork(containerID)
result, err := providers.NetworkPlugin.SetupContainerNetwork(containerID, vm.Spec.Network.Ports...)
if err != nil {
return err
}
Expand Down