Skip to content
This repository has been archived by the owner on Jul 25, 2019. It is now read-only.

plugin reorg: merge subdirs, split master/node #305

Merged
merged 8 commits into from Jun 23, 2016
Merged
104 changes: 67 additions & 37 deletions plugins/osdn/common.go
Expand Up @@ -10,33 +10,22 @@ import (
log "github.com/golang/glog"

"github.com/openshift/openshift-sdn/pkg/netutils"
"github.com/openshift/openshift-sdn/plugins/osdn/api"

osclient "github.com/openshift/origin/pkg/client"
osapi "github.com/openshift/origin/pkg/sdn/api"

kapi "k8s.io/kubernetes/pkg/api"
kubetypes "k8s.io/kubernetes/pkg/kubelet/container"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
kerrors "k8s.io/kubernetes/pkg/util/errors"
kexec "k8s.io/kubernetes/pkg/util/exec"
kubeutilnet "k8s.io/kubernetes/pkg/util/net"
)

type PluginHooks interface {
PluginStartMaster(clusterNetwork *net.IPNet, hostSubnetLength uint) error
PluginStartNode(mtu uint) error

SetupSDN(localSubnetCIDR, clusterNetworkCIDR, serviceNetworkCIDR string, mtu uint) (bool, error)

AddHostSubnetRules(subnet *osapi.HostSubnet) error
DeleteHostSubnetRules(subnet *osapi.HostSubnet) error

AddServiceRules(service *kapi.Service, netID uint) error
DeleteServiceRules(service *kapi.Service) error

UpdatePod(namespace string, name string, id kubetypes.DockerID) error
}

type OsdnController struct {
pluginHooks PluginHooks
pluginName string
multitenant bool

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need multitenant anymore?
Now we got the pluginName, can use IsOpenShiftMultitenantNetworkPlugin(oc.pluginName) where ever required.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Later in the branch, both pluginName and multitenant go away in OsdnMaster, and pluginName goes away in OsdnNode. (Keeping oc.multitenant rather than keeping oc.pluginName and using IsOpenShiftMultiTenantNetworkPlugin() involved less code change.)

Registry *Registry
localIP string
localSubnet *osapi.HostSubnet
Expand All @@ -50,15 +39,27 @@ type OsdnController struct {
iptablesSyncPeriod time.Duration
}

// Called by plug factory functions to initialize the generic plugin instance
func (oc *OsdnController) BaseInit(registry *Registry, pluginHooks PluginHooks, pluginName string, hostname string, selfIP string, iptablesSyncPeriod time.Duration) error {
// Called by higher layers to create the plugin SDN master instance
func NewMasterPlugin(pluginName string, osClient *osclient.Client, kClient *kclient.Client) (api.OsdnPlugin, error) {
return createPlugin(osClient, kClient, pluginName, "", "", 0)
}

// Called by higher layers to create the plugin SDN node instance
func NewNodePlugin(pluginName string, osClient *osclient.Client, kClient *kclient.Client, hostname string, selfIP string, iptablesSyncPeriod time.Duration) (api.OsdnPlugin, error) {
return createPlugin(osClient, kClient, pluginName, hostname, selfIP, iptablesSyncPeriod)
}

func createPlugin(osClient *osclient.Client, kClient *kclient.Client, pluginName string, hostname string, selfIP string, iptablesSyncPeriod time.Duration) (api.OsdnPlugin, error) {
if !IsOpenShiftNetworkPlugin(pluginName) {
return nil, nil
}

log.Infof("Starting with configured hostname %q (IP %q), iptables sync period %q", hostname, selfIP, iptablesSyncPeriod.String())

if hostname == "" {
output, err := kexec.New().Command("uname", "-n").CombinedOutput()
if err != nil {
return err
return nil, err
}
hostname = strings.TrimSpace(string(output))
}
Expand All @@ -70,24 +71,25 @@ func (oc *OsdnController) BaseInit(registry *Registry, pluginHooks PluginHooks,
log.V(5).Infof("Failed to determine node address from hostname %s; using default interface (%v)", hostname, err)
defaultIP, err := kubeutilnet.ChooseHostInterface()
if err != nil {
return err
return nil, err
}
selfIP = defaultIP.String()
}
}
log.Infof("Initializing %s plugin for %s (%s)", pluginName, hostname, selfIP)

oc.pluginHooks = pluginHooks
oc.pluginName = pluginName
oc.Registry = registry
oc.localIP = selfIP
oc.HostName = hostname
oc.vnidMap = make(map[string]uint)
oc.podNetworkReady = make(chan struct{})
oc.adminNamespaces = make([]string, 0)
oc.iptablesSyncPeriod = iptablesSyncPeriod

return nil
log.Infof("Initializing %s plugin for %s (%s)", pluginName, hostname, selfIP)
plugin := &OsdnController{
pluginName: pluginName,
multitenant: IsOpenShiftMultitenantNetworkPlugin(pluginName),
Registry: NewRegistry(osClient, kClient),
localIP: selfIP,
HostName: hostname,
vnidMap: make(map[string]uint),
podNetworkReady: make(chan struct{}),
adminNamespaces: make([]string, 0),
iptablesSyncPeriod: iptablesSyncPeriod,
}
return plugin, nil
}

func (oc *OsdnController) validateNetworkConfig(ni *NetworkInfo) error {
Expand Down Expand Up @@ -180,9 +182,16 @@ func (oc *OsdnController) StartMaster(clusterNetworkCIDR string, clusterBitsPerS
}
}

if err := oc.pluginHooks.PluginStartMaster(ni.ClusterNetwork, clusterBitsPerSubnet); err != nil {
return fmt.Errorf("Failed to start plugin: %v", err)
if err := oc.SubnetStartMaster(ni.ClusterNetwork, clusterBitsPerSubnet); err != nil {
return err
}

if oc.multitenant {
if err := oc.VnidStartMaster(); err != nil {
return err
}
}

return nil
}

Expand All @@ -198,8 +207,29 @@ func (oc *OsdnController) StartNode(mtu uint) error {
return fmt.Errorf("Failed to set up iptables: %v", err)
}

if err := oc.pluginHooks.PluginStartNode(mtu); err != nil {
return fmt.Errorf("Failed to start plugin: %v", err)
networkChanged, err := oc.SubnetStartNode(mtu)
if err != nil {
return err
}

if oc.multitenant {
if err := oc.VnidStartNode(); err != nil {
return err
}
}

if networkChanged {
pods, err := oc.GetLocalPods(kapi.NamespaceAll)
if err != nil {
return err
}
for _, p := range pods {
containerID := GetPodContainerID(&p)
err = oc.UpdatePod(p.Namespace, p.Name, kubeletTypes.DockerID(containerID))
if err != nil {
log.Warningf("Could not update pod %q (%s): %s", p.Name, containerID, err)
}
}
}

oc.markPodNetworkReady()
Expand Down
10 changes: 5 additions & 5 deletions plugins/osdn/controller.go
Expand Up @@ -130,7 +130,7 @@ func deleteLocalSubnetRoute(device, localSubnetCIDR string) {
glog.Errorf("Timed out looking for %s route for dev %s; if it appears later it will not be deleted.", localSubnetCIDR, device)
}

func (plugin *ovsPlugin) SetupSDN(localSubnetCIDR, clusterNetworkCIDR, servicesNetworkCIDR string, mtu uint) (bool, error) {
func (plugin *OsdnController) SetupSDN(localSubnetCIDR, clusterNetworkCIDR, servicesNetworkCIDR string, mtu uint) (bool, error) {
_, ipnet, err := net.ParseCIDR(localSubnetCIDR)
localSubnetMaskLength, _ := ipnet.Mask.Size()
localSubnetGateway := netutils.GenerateDefaultGateway(ipnet).String()
Expand Down Expand Up @@ -327,7 +327,7 @@ func (plugin *ovsPlugin) SetupSDN(localSubnetCIDR, clusterNetworkCIDR, servicesN
return true, nil
}

func (plugin *ovsPlugin) AddHostSubnetRules(subnet *osapi.HostSubnet) error {
func (plugin *OsdnController) AddHostSubnetRules(subnet *osapi.HostSubnet) error {
glog.Infof("AddHostSubnetRules for %s", HostSubnetToString(subnet))
otx := ovs.NewTransaction(BR)

Expand All @@ -342,7 +342,7 @@ func (plugin *ovsPlugin) AddHostSubnetRules(subnet *osapi.HostSubnet) error {
return nil
}

func (plugin *ovsPlugin) DeleteHostSubnetRules(subnet *osapi.HostSubnet) error {
func (plugin *OsdnController) DeleteHostSubnetRules(subnet *osapi.HostSubnet) error {
glog.Infof("DeleteHostSubnetRules for %s", HostSubnetToString(subnet))

otx := ovs.NewTransaction(BR)
Expand All @@ -355,7 +355,7 @@ func (plugin *ovsPlugin) DeleteHostSubnetRules(subnet *osapi.HostSubnet) error {
return nil
}

func (plugin *ovsPlugin) AddServiceRules(service *kapi.Service, netID uint) error {
func (plugin *OsdnController) AddServiceRules(service *kapi.Service, netID uint) error {
if !plugin.multitenant {
return nil
}
Expand All @@ -373,7 +373,7 @@ func (plugin *ovsPlugin) AddServiceRules(service *kapi.Service, netID uint) erro
return nil
}

func (plugin *ovsPlugin) DeleteServiceRules(service *kapi.Service) error {
func (plugin *OsdnController) DeleteServiceRules(service *kapi.Service) error {
if !plugin.multitenant {
return nil
}
Expand Down
102 changes: 11 additions & 91 deletions plugins/osdn/plugin.go
Expand Up @@ -2,21 +2,16 @@ package osdn

import (
"fmt"
"net"
"os/exec"
"strconv"
"strings"
"time"

"github.com/golang/glog"

"github.com/openshift/openshift-sdn/plugins/osdn/api"

osclient "github.com/openshift/origin/pkg/client"

kapi "k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/api/resource"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
knetwork "k8s.io/kubernetes/pkg/kubelet/network"
utilsets "k8s.io/kubernetes/pkg/util/sets"
Expand All @@ -31,12 +26,6 @@ const (
AssignMacVlanAnnotation string = "pod.network.openshift.io/assign-macvlan"
)

type ovsPlugin struct {
OsdnController

multitenant bool
}

func IsOpenShiftNetworkPlugin(pluginName string) bool {
switch strings.ToLower(pluginName) {
case SingleTenantPluginName, MultiTenantPluginName:
Expand All @@ -52,75 +41,6 @@ func IsOpenShiftMultitenantNetworkPlugin(pluginName string) bool {
return false
}

// Called by higher layers to create the plugin SDN master instance
func NewMasterPlugin(pluginName string, osClient *osclient.Client, kClient *kclient.Client) (api.OsdnPlugin, error) {
return createPlugin(osClient, kClient, pluginName, "", "", 0)
}

// Called by higher layers to create the plugin SDN node instance
func NewNodePlugin(pluginName string, osClient *osclient.Client, kClient *kclient.Client, hostname string, selfIP string, iptablesSyncPeriod time.Duration) (api.OsdnPlugin, error) {
return createPlugin(osClient, kClient, pluginName, hostname, selfIP, iptablesSyncPeriod)
}

func createPlugin(osClient *osclient.Client, kClient *kclient.Client, pluginName string, hostname string, selfIP string, iptablesSyncPeriod time.Duration) (api.OsdnPlugin, error) {
if !IsOpenShiftNetworkPlugin(pluginName) {
return nil, nil
}

registry := NewRegistry(osClient, kClient)
plugin := &ovsPlugin{multitenant: IsOpenShiftMultitenantNetworkPlugin(pluginName)}

err := plugin.BaseInit(registry, plugin, pluginName, hostname, selfIP, iptablesSyncPeriod)
if err != nil {
return nil, err
}

return plugin, err
}

func (plugin *ovsPlugin) PluginStartMaster(clusterNetwork *net.IPNet, hostSubnetLength uint) error {
if err := plugin.SubnetStartMaster(clusterNetwork, hostSubnetLength); err != nil {
return err
}

if plugin.multitenant {
if err := plugin.VnidStartMaster(); err != nil {
return err
}
}

return nil
}

func (plugin *ovsPlugin) PluginStartNode(mtu uint) error {
networkChanged, err := plugin.SubnetStartNode(mtu)
if err != nil {
return err
}

if plugin.multitenant {
if err := plugin.VnidStartNode(); err != nil {
return err
}
}

if networkChanged {
pods, err := plugin.GetLocalPods(kapi.NamespaceAll)
if err != nil {
return err
}
for _, p := range pods {
containerID := GetPodContainerID(&p)
err = plugin.UpdatePod(p.Namespace, p.Name, kubeletTypes.DockerID(containerID))
if err != nil {
glog.Warningf("Could not update pod %q (%s): %s", p.Name, containerID, err)
}
}
}

return nil
}

//-----------------------------------------------

const (
Expand All @@ -130,27 +50,27 @@ const (
updateCmd = "update"
)

func (plugin *ovsPlugin) getExecutable() string {
func (plugin *OsdnController) getExecutable() string {
return "openshift-sdn-ovs"
}

func (plugin *ovsPlugin) Init(host knetwork.Host) error {
func (plugin *OsdnController) Init(host knetwork.Host) error {
return nil
}

func (plugin *ovsPlugin) Name() string {
func (plugin *OsdnController) Name() string {
if plugin.multitenant {
return MultiTenantPluginName
} else {
return SingleTenantPluginName
}
}

func (plugin *ovsPlugin) Capabilities() utilsets.Int {
func (plugin *OsdnController) Capabilities() utilsets.Int {
return utilsets.NewInt(knetwork.NET_PLUGIN_CAPABILITY_SHAPING)
}

func (plugin *ovsPlugin) getVNID(namespace string) (string, error) {
func (plugin *OsdnController) getVNID(namespace string) (string, error) {
if plugin.multitenant {
vnid, err := plugin.WaitAndGetVNID(namespace)
if err != nil {
Expand Down Expand Up @@ -234,7 +154,7 @@ func getScriptError(output []byte) string {
return string(output)
}

func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes.ContainerID) error {
func (plugin *OsdnController) SetUpPod(namespace string, name string, id kubeletTypes.ContainerID) error {
err := plugin.WaitForPodNetworkReady()
if err != nil {
return err
Expand Down Expand Up @@ -279,7 +199,7 @@ func (plugin *ovsPlugin) SetUpPod(namespace string, name string, id kubeletTypes
}
}

func (plugin *ovsPlugin) TearDownPod(namespace string, name string, id kubeletTypes.ContainerID) error {
func (plugin *OsdnController) TearDownPod(namespace string, name string, id kubeletTypes.ContainerID) error {
// The script's teardown functionality doesn't need the VNID
out, err := exec.Command(plugin.getExecutable(), tearDownCmd, id.ID, "-1", "-1", "-1").CombinedOutput()
glog.V(5).Infof("TearDownPod network plugin output: %s, %v", string(out), err)
Expand All @@ -291,15 +211,15 @@ func (plugin *ovsPlugin) TearDownPod(namespace string, name string, id kubeletTy
}
}

func (plugin *ovsPlugin) Status() error {
func (plugin *OsdnController) Status() error {
return nil
}

func (plugin *ovsPlugin) GetPodNetworkStatus(namespace string, name string, podInfraContainerID kubeletTypes.ContainerID) (*knetwork.PodNetworkStatus, error) {
func (plugin *OsdnController) GetPodNetworkStatus(namespace string, name string, podInfraContainerID kubeletTypes.ContainerID) (*knetwork.PodNetworkStatus, error) {
return nil, nil
}

func (plugin *ovsPlugin) UpdatePod(namespace string, name string, id kubeletTypes.DockerID) error {
func (plugin *OsdnController) UpdatePod(namespace string, name string, id kubeletTypes.DockerID) error {
vnidstr, err := plugin.getVNID(namespace)
if err != nil {
return err
Expand All @@ -315,5 +235,5 @@ func (plugin *ovsPlugin) UpdatePod(namespace string, name string, id kubeletType
}
}

func (plugin *ovsPlugin) Event(name string, details map[string]interface{}) {
func (plugin *OsdnController) Event(name string, details map[string]interface{}) {
}