diff --git a/cri/ocicni/cni_manager.go b/cri/ocicni/cni_manager.go new file mode 100644 index 0000000000..fabae81d69 --- /dev/null +++ b/cri/ocicni/cni_manager.go @@ -0,0 +1,103 @@ +package ocicni + +import ( + "fmt" + "os" + + "github.com/alibaba/pouch/cri/config" + + "github.com/cri-o/ocicni/pkg/ocicni" + "github.com/sirupsen/logrus" +) + +// CniManager is an implementation of interface CniMgr. +type CniManager struct { + // plugin is used to setup and teardown network when run/stop pod sandbox. + plugin ocicni.CNIPlugin +} + +// NewCniManager initializes a brand new cni manager. +// If initialize failed, return NoopCniManager, we should not make pouchd creashed +// because of the failure of cni manager. +func NewCniManager(cfg *config.Config) CniMgr { + networkPluginBinDir := cfg.NetworkPluginBinDir + networkPluginConfDir := cfg.NetworkPluginConfDir + + // Create CNI configuration directory if it doesn't exist to avoid breaking. + _, err := os.Stat(networkPluginConfDir) + if err != nil && os.IsNotExist(err) { + err = os.MkdirAll(networkPluginConfDir, 0666) + if err != nil { + logrus.Errorf("failed to create configuration directory for CNI: %v", err) + return &NoopCniManager{} + } + } + + plugin, err := ocicni.InitCNI(networkPluginConfDir, networkPluginBinDir) + if err != nil { + logrus.Errorf("failed to initialize cni manager: %v", err) + return &NoopCniManager{} + } + + return &CniManager{ + plugin: plugin, + } +} + +// Name returns the plugin's name. This will be used when searching +// for a plugin by name, e.g. +func (c *CniManager) Name() string { + return c.plugin.Name() +} + +// 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. +func (c *CniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error { + _, err := c.plugin.SetUpPod(*podNetwork) + + defer func() { + if err != nil { + // Teardown network if an error returned. + err := c.plugin.TearDownPod(*podNetwork) + if err != nil { + logrus.Errorf("failed to destroy network for sandbox %q: %v", podNetwork.ID, err) + } + } + }() + + if err != nil { + return fmt.Errorf("failed to setup network for sandbox %q: %v", podNetwork.ID, err) + } + + return nil +} + +// TearDownPodNetwork is the method called before a pod's sandbox container will be deleted. +func (c *CniManager) TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error { + err := c.plugin.TearDownPod(*podNetwork) + if err != nil { + return fmt.Errorf("failed to destroy network for sandbox %q: %v", podNetwork.ID, err) + } + return nil +} + +// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox. +func (c *CniManager) GetPodNetworkStatus(netnsPath string) (string, error) { + // TODO: we need more validation tests. + podNetwork := ocicni.PodNetwork{ + NetNS: netnsPath, + } + + ip, err := c.plugin.GetPodNetworkStatus(podNetwork) + if err != nil { + return "", fmt.Errorf("failed to get pod network status: %v", err) + } + + return ip, nil +} + +// Status returns error if the network plugin is in error state. +func (c *CniManager) Status() error { + return c.plugin.Status() +} diff --git a/cri/ocicni/interface.go b/cri/ocicni/interface.go new file mode 100644 index 0000000000..ce90ee3bca --- /dev/null +++ b/cri/ocicni/interface.go @@ -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 +} diff --git a/cri/ocicni/noop_cni_manager.go b/cri/ocicni/noop_cni_manager.go new file mode 100644 index 0000000000..fe63f1769c --- /dev/null +++ b/cri/ocicni/noop_cni_manager.go @@ -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 +} diff --git a/cri/v1alpha1/cri.go b/cri/v1alpha1/cri.go index ab5e7f040d..3545b8d124 100644 --- a/cri/v1alpha1/cri.go +++ b/cri/v1alpha1/cri.go @@ -14,6 +14,7 @@ import ( apitypes "github.com/alibaba/pouch/apis/types" anno "github.com/alibaba/pouch/cri/annotations" runtime "github.com/alibaba/pouch/cri/apis/v1alpha1" + 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" @@ -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 @@ -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, diff --git a/cri/v1alpha1/cri_network.go b/cri/v1alpha1/cri_network.go index 910c42d2b6..bb73db8f07 100644 --- a/cri/v1alpha1/cri_network.go +++ b/cri/v1alpha1/cri_network.go @@ -1,130 +1,13 @@ package v1alpha1 import ( - "fmt" - "os" "strings" runtime "github.com/alibaba/pouch/cri/apis/v1alpha1" - "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. - plugin ocicni.CNIPlugin -} - -// NewCniManager initializes a brand new cni manager. -// If initialize failed, return NoopCniManager, we should not make pouchd creashed -// because of the failure of cni manager. -func NewCniManager(cfg *config.Config) CniMgr { - networkPluginBinDir := cfg.NetworkPluginBinDir - networkPluginConfDir := cfg.NetworkPluginConfDir - - // Create CNI configuration directory if it doesn't exist to avoid breaking. - _, err := os.Stat(networkPluginConfDir) - if err != nil && os.IsNotExist(err) { - err = os.MkdirAll(networkPluginConfDir, 0666) - if err != nil { - logrus.Errorf("failed to create configuration directory for CNI: %v", err) - return &NoopCniManager{} - } - } - - plugin, err := ocicni.InitCNI(networkPluginConfDir, networkPluginBinDir) - if err != nil { - logrus.Errorf("failed to initialize cni manager: %v", err) - return &NoopCniManager{} - } - - return &CniManager{ - plugin: plugin, - } -} - -// Name returns the plugin's name. This will be used when searching -// for a plugin by name, e.g. -func (c *CniManager) Name() string { - return c.plugin.Name() -} - -// 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. -func (c *CniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error { - _, err := c.plugin.SetUpPod(*podNetwork) - - defer func() { - if err != nil { - // 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) - } - } - }() - - if err != nil { - return fmt.Errorf("failed to setup network for sandbox %q: %v", podNetwork.ID, err) - } - - return nil -} - -// TearDownPodNetwork is the method called before a pod's sandbox container will be deleted. -func (c *CniManager) TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error { - err := c.plugin.TearDownPod(*podNetwork) - if err != nil { - return fmt.Errorf("failed to destroy network for sandbox %q: %v", podNetwork.ID, err) - } - return nil -} - -// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox. -func (c *CniManager) GetPodNetworkStatus(netnsPath string) (string, error) { - // TODO: we need more validation tests. - podNetwork := ocicni.PodNetwork{ - NetNS: netnsPath, - } - - ip, err := c.plugin.GetPodNetworkStatus(podNetwork) - if err != nil { - return "", fmt.Errorf("failed to get pod network status: %v", err) - } - - return ip, nil -} - -// Status returns error if the network plugin is in error state. -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 @@ -141,32 +24,3 @@ func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapp } 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 -} diff --git a/cri/v1alpha2/cri.go b/cri/v1alpha2/cri.go index 7a2c8c4012..c4a43b1573 100644 --- a/cri/v1alpha2/cri.go +++ b/cri/v1alpha2/cri.go @@ -14,6 +14,7 @@ import ( apitypes "github.com/alibaba/pouch/apis/types" anno "github.com/alibaba/pouch/cri/annotations" runtime "github.com/alibaba/pouch/cri/apis/v1alpha2" + 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" @@ -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 @@ -124,7 +125,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, diff --git a/cri/v1alpha2/cri_network.go b/cri/v1alpha2/cri_network.go index c75b40f281..828c213ab3 100644 --- a/cri/v1alpha2/cri_network.go +++ b/cri/v1alpha2/cri_network.go @@ -1,130 +1,13 @@ package v1alpha2 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. - plugin ocicni.CNIPlugin -} - -// NewCniManager initializes a brand new cni manager. -// If initialize failed, return NoopCniManager, we should not make pouchd creashed -// because of the failure of cni manager. -func NewCniManager(cfg *config.Config) CniMgr { - networkPluginBinDir := cfg.NetworkPluginBinDir - networkPluginConfDir := cfg.NetworkPluginConfDir - - // Create CNI configuration directory if it doesn't exist to avoid breaking. - _, err := os.Stat(networkPluginConfDir) - if err != nil && os.IsNotExist(err) { - err = os.MkdirAll(networkPluginConfDir, 0666) - if err != nil { - logrus.Errorf("failed to create configuration directory for CNI: %v", err) - return &NoopCniManager{} - } - } - - plugin, err := ocicni.InitCNI(networkPluginConfDir, networkPluginBinDir) - if err != nil { - logrus.Errorf("failed to initialize cni manager: %v", err) - return &NoopCniManager{} - } - - return &CniManager{ - plugin: plugin, - } -} - -// Name returns the plugin's name. This will be used when searching -// for a plugin by name, e.g. -func (c *CniManager) Name() string { - return c.plugin.Name() -} - -// 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. -func (c *CniManager) SetUpPodNetwork(podNetwork *ocicni.PodNetwork) error { - _, err := c.plugin.SetUpPod(*podNetwork) - - defer func() { - if err != nil { - // 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) - } - } - }() - - if err != nil { - return fmt.Errorf("failed to setup network for sandbox %q: %v", podNetwork.ID, err) - } - - return nil -} - -// TearDownPodNetwork is the method called before a pod's sandbox container will be deleted. -func (c *CniManager) TearDownPodNetwork(podNetwork *ocicni.PodNetwork) error { - err := c.plugin.TearDownPod(*podNetwork) - if err != nil { - return fmt.Errorf("failed to destroy network for sandbox %q: %v", podNetwork.ID, err) - } - return nil -} - -// GetPodNetworkStatus is the method called to obtain the ipv4 or ipv6 addresses of the pod sandbox. -func (c *CniManager) GetPodNetworkStatus(netnsPath string) (string, error) { - // TODO: we need more validation tests. - podNetwork := ocicni.PodNetwork{ - NetNS: netnsPath, - } - - ip, err := c.plugin.GetPodNetworkStatus(podNetwork) - if err != nil { - return "", fmt.Errorf("failed to get pod network status: %v", err) - } - - return ip, nil -} - -// Status returns error if the network plugin is in error state. -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 @@ -141,32 +24,3 @@ func toCNIPortMappings(criPortMappings []*runtime.PortMapping) []ocicni.PortMapp } 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 -}