Skip to content

Commit

Permalink
Merge pull request #1719 from Starnop/cri-cni
Browse files Browse the repository at this point in the history
refactor: Extract cni manager as the public part
  • Loading branch information
allencloud authored Jul 30, 2018
2 parents 9b8aa47 + 9245c2e commit ba29cd3
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 247 deletions.
73 changes: 2 additions & 71 deletions cri/v1alpha2/cri_network.go → cri/ocicni/cni_manager.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,15 @@
package v1alpha2
package ocicni

import (
"fmt"
"os"
"strings"

runtime "github.com/alibaba/pouch/cri/apis/v1alpha2"
"github.com/alibaba/pouch/cri/config"

"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/sirupsen/logrus"
)

// CniMgr as an interface defines all operations against CNI.
type CniMgr interface {
// Name returns the plugin's name. This will be used when searching
// for a plugin by name, e.g.
Name() string

// SetUpPodNetwork is the method called after the sandbox container of the
// pod has been created but before the other containers of the pod
// are launched.
SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error

// TearDownPodNetwork is the method called before a pod's sandbox container will be deleted.
TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error

// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox.
GetPodNetworkStatus(netnsPath string) (string, error)

// Status returns error if the network plugin is in error state.
Status() error
}

// CniManager is an implementation of interface CniMgr.
type CniManager struct {
// plugin is used to setup and teardown network when run/stop pod sandbox.
Expand Down Expand Up @@ -84,7 +61,7 @@ func (c *CniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error {
// Teardown network if an error returned.
err := c.plugin.TearDownPod(*podNetwork)
if err != nil {
logrus.Errorf("failed to detroy network for sandbox %q: %v", podNetwork.ID, err)
logrus.Errorf("failed to destroy network for sandbox %q: %v", podNetwork.ID, err)
}
}
}()
Expand Down Expand Up @@ -124,49 +101,3 @@ func (c *CniManager) GetPodNetworkStatus(netnsPath string) (string, error) {
func (c *CniManager) Status() error {
return c.plugin.Status()
}

// toCNIPortMappings converts CRI port mappings to CNI.
func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapping {
var portMappings []ocicni.PortMapping
for _, mapping := range criPortMappings {
if mapping.HostPort <= 0 {
continue
}
portMappings = append(portMappings, ocicni.PortMapping{
HostPort: mapping.HostPort,
ContainerPort: mapping.ContainerPort,
Protocol: strings.ToLower(mapping.Protocol.String()),
HostIP: mapping.HostIp,
})
}
return portMappings
}

// NoopCniManager is an implementation of interface CniMgr, but makes no operation.
type NoopCniManager struct {
}

// Name of NoopCniManager return the name of plugin as "none".
func (n *NoopCniManager) Name() string {
return "noop"
}

// SetUpPodNetwork of NoopCniManager makes no operation.
func (n *NoopCniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error {
return nil
}

// TearDownPodNetwork of NoopCniManager makes no operation.
func (n *NoopCniManager) TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error {
return nil
}

// GetPodNetworkStatus of NoopCniManager makes no operation.
func (n *NoopCniManager) GetPodNetworkStatus(netnsPath string) (string, error) {
return "", nil
}

// Status of NoopCniManager makes no operation.
func (n *NoopCniManager) Status() error {
return nil
}
24 changes: 24 additions & 0 deletions cri/ocicni/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ocicni

import "github.com/cri-o/ocicni/pkg/ocicni"

// CniMgr as an interface defines all operations against CNI.
type CniMgr interface {
// Name returns the plugin's name. This will be used when searching
// for a plugin by name, e.g.
Name() string

// SetUpPodNetwork is the method called after the sandbox container of the
// pod has been created but before the other containers of the pod
// are launched.
SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error

// TearDownPodNetwork is the method called before a pod's sandbox container will be deleted.
TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error

// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox.
GetPodNetworkStatus(netnsPath string) (string, error)

// Status returns error if the network plugin is in error state.
Status() error
}
32 changes: 32 additions & 0 deletions cri/ocicni/noop_cni_manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ocicni

import "github.com/cri-o/ocicni/pkg/ocicni"

// NoopCniManager is an implementation of interface CniMgr, but makes no operation.
type NoopCniManager struct {
}

// Name of NoopCniManager return the name of plugin as "none".
func (n *NoopCniManager) Name() string {
return "noop"
}

// SetUpPodNetwork of NoopCniManager makes no operation.
func (n *NoopCniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error {
return nil
}

// TearDownPodNetwork of NoopCniManager makes no operation.
func (n *NoopCniManager) TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error {
return nil
}

// GetPodNetworkStatus of NoopCniManager makes no operation.
func (n *NoopCniManager) GetPodNetworkStatus(netnsPath string) (string, error) {
return "", nil
}

// Status of NoopCniManager makes no operation.
func (n *NoopCniManager) Status() error {
return nil
}
5 changes: 3 additions & 2 deletions cri/v1alpha1/cri.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

apitypes "github.com/alibaba/pouch/apis/types"
anno "github.com/alibaba/pouch/cri/annotations"
cni "github.com/alibaba/pouch/cri/ocicni"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"
"github.com/alibaba/pouch/pkg/errtypes"
Expand Down Expand Up @@ -93,7 +94,7 @@ type CriMgr interface {
type CriManager struct {
ContainerMgr mgr.ContainerMgr
ImageMgr mgr.ImageMgr
CniMgr CniMgr
CniMgr cni.CniMgr

// StreamServer is the stream server of CRI serves container streaming request.
StreamServer Server
Expand Down Expand Up @@ -123,7 +124,7 @@ func NewCriManager(config *config.Config, ctrMgr mgr.ContainerMgr, imgMgr mgr.Im
c := &CriManager{
ContainerMgr: ctrMgr,
ImageMgr: imgMgr,
CniMgr: NewCniManager(&config.CriConfig),
CniMgr: cni.NewCniManager(&config.CriConfig),
StreamServer: streamServer,
SandboxBaseDir: path.Join(config.HomeDir, "sandboxes"),
SandboxImage: config.CriConfig.SandboxImage,
Expand Down
172 changes: 0 additions & 172 deletions cri/v1alpha1/cri_network.go

This file was deleted.

20 changes: 20 additions & 0 deletions cri/v1alpha1/cri_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/containerd/cgroups"
containerdmount "github.com/containerd/containerd/mount"
"github.com/containerd/typeurl"
"github.com/cri-o/ocicni/pkg/ocicni"
"github.com/go-openapi/strfmt"
"golang.org/x/net/context"
"golang.org/x/sys/unix"
Expand Down Expand Up @@ -954,3 +955,22 @@ func parseResourcesFromCRI(runtimeResources *runtime.LinuxContainerResources) ap
CpusetMems: runtimeResources.GetCpusetMems(),
}
}

// CNI Network related tool functions.

// toCNIPortMappings converts CRI port mappings to CNI.
func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapping {
var portMappings []ocicni.PortMapping
for _, mapping := range criPortMappings {
if mapping.HostPort <= 0 {
continue
}
portMappings = append(portMappings, ocicni.PortMapping{
HostPort: mapping.HostPort,
ContainerPort: mapping.ContainerPort,
Protocol: strings.ToLower(mapping.Protocol.String()),
HostIP: mapping.HostIp,
})
}
return portMappings
}
Loading

0 comments on commit ba29cd3

Please sign in to comment.