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

Commit

Permalink
Reorganize and split out node API
Browse files Browse the repository at this point in the history
  • Loading branch information
danwinship committed Jun 21, 2016
1 parent cfde8dd commit 3395f29
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 171 deletions.
2 changes: 1 addition & 1 deletion plugins/osdn/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
type OsdnNodePlugin interface {
knetwork.NetworkPlugin

Start(mtu uint) error
Start() error
}

type FilteringEndpointsConfigHandler interface {
Expand Down
127 changes: 0 additions & 127 deletions plugins/osdn/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,141 +3,14 @@ package osdn
import (
"fmt"
"strings"
"time"

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"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
kexec "k8s.io/kubernetes/pkg/util/exec"
kubeutilnet "k8s.io/kubernetes/pkg/util/net"
)

type OsdnNode struct {
multitenant bool
Registry *Registry
localIP string
localSubnet *osapi.HostSubnet
HostName string
podNetworkReady chan struct{}
vnids vnidMap
iptablesSyncPeriod time.Duration
}

// 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.OsdnNodePlugin, error) {
if !IsOpenShiftNetworkPlugin(pluginName) {
return nil, nil
}

log.Infof("Initializing SDN node of type %q with configured hostname %q (IP %q), iptables sync period %q", pluginName, hostname, selfIP, iptablesSyncPeriod.String())
if hostname == "" {
output, err := kexec.New().Command("uname", "-n").CombinedOutput()
if err != nil {
return nil, err
}
hostname = strings.TrimSpace(string(output))
log.Infof("Resolved hostname to %q", hostname)
}
if selfIP == "" {
var err error
selfIP, err = netutils.GetNodeIP(hostname)
if err != nil {
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 nil, err
}
selfIP = defaultIP.String()
log.Infof("Resolved IP address to %q", selfIP)
}
}

plugin := &OsdnNode{
multitenant: IsOpenShiftMultitenantNetworkPlugin(pluginName),
Registry: NewRegistry(osClient, kClient),
localIP: selfIP,
HostName: hostname,
vnids: newVnidMap(),
podNetworkReady: make(chan struct{}),
iptablesSyncPeriod: iptablesSyncPeriod,
}
return plugin, nil
}

func (oc *OsdnNode) Start(mtu uint) error {
// Assume we are working with IPv4
ni, err := oc.Registry.GetNetworkInfo()
if err != nil {
return fmt.Errorf("Failed to get network information: %v", err)
}

nodeIPTables := NewNodeIPTables(ni.ClusterNetwork.String(), oc.iptablesSyncPeriod)
if err := nodeIPTables.Setup(); err != nil {
return fmt.Errorf("Failed to set up iptables: %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()

return nil
}

func (oc *OsdnNode) GetLocalPods(namespace string) ([]kapi.Pod, error) {
return oc.Registry.GetRunningPods(oc.HostName, namespace)
}

func (oc *OsdnNode) markPodNetworkReady() {
close(oc.podNetworkReady)
}

func (oc *OsdnNode) WaitForPodNetworkReady() error {
logInterval := 10 * time.Second
numIntervals := 12 // timeout: 2 mins

for i := 0; i < numIntervals; i++ {
select {
// Wait for StartNode() to finish SDN setup
case <-oc.podNetworkReady:
return nil
case <-time.After(logInterval):
log.Infof("Waiting for SDN pod network to be ready...")
}
}
return fmt.Errorf("SDN pod network is not ready(timeout: 2 mins)")
}

func GetNodeIP(node *kapi.Node) (string, error) {
if len(node.Status.Addresses) > 0 && node.Status.Addresses[0].Address != "" {
return node.Status.Addresses[0].Address, nil
Expand Down
140 changes: 140 additions & 0 deletions plugins/osdn/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package osdn

import (
"fmt"
"strings"
"time"

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"
kclient "k8s.io/kubernetes/pkg/client/unversioned"
kubeletTypes "k8s.io/kubernetes/pkg/kubelet/container"
kexec "k8s.io/kubernetes/pkg/util/exec"
kubeutilnet "k8s.io/kubernetes/pkg/util/net"
)

type OsdnNode struct {
multitenant bool
registry *Registry
localIP string
localSubnet *osapi.HostSubnet
hostName string
podNetworkReady chan struct{}
vnids vnidMap
iptablesSyncPeriod time.Duration
mtu uint
}

// 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, mtu uint) (api.OsdnNodePlugin, error) {
if !IsOpenShiftNetworkPlugin(pluginName) {
return nil, nil
}

log.Infof("Initializing SDN node of type %q with configured hostname %q (IP %q), iptables sync period %q", pluginName, hostname, selfIP, iptablesSyncPeriod.String())
if hostname == "" {
output, err := kexec.New().Command("uname", "-n").CombinedOutput()
if err != nil {
return nil, err
}
hostname = strings.TrimSpace(string(output))
log.Infof("Resolved hostname to %q", hostname)
}
if selfIP == "" {
var err error
selfIP, err = netutils.GetNodeIP(hostname)
if err != nil {
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 nil, err
}
selfIP = defaultIP.String()
log.Infof("Resolved IP address to %q", selfIP)
}
}

plugin := &OsdnNode{
multitenant: IsOpenShiftMultitenantNetworkPlugin(pluginName),
registry: NewRegistry(osClient, kClient),
localIP: selfIP,
hostName: hostname,
vnids: newVnidMap(),
podNetworkReady: make(chan struct{}),
iptablesSyncPeriod: iptablesSyncPeriod,
mtu: mtu,
}
return plugin, nil
}

func (node *OsdnNode) Start() error {
ni, err := node.registry.GetNetworkInfo()
if err != nil {
return fmt.Errorf("Failed to get network information: %v", err)
}

nodeIPTables := NewNodeIPTables(ni.ClusterNetwork.String(), node.iptablesSyncPeriod)
if err := nodeIPTables.Setup(); err != nil {
return fmt.Errorf("Failed to set up iptables: %v", err)
}

networkChanged, err := node.SubnetStartNode(node.mtu)
if err != nil {
return err
}

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

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

node.markPodNetworkReady()

return nil
}

func (node *OsdnNode) GetLocalPods(namespace string) ([]kapi.Pod, error) {
return node.registry.GetRunningPods(node.hostName, namespace)
}

func (node *OsdnNode) markPodNetworkReady() {
close(node.podNetworkReady)
}

func (node *OsdnNode) WaitForPodNetworkReady() error {
logInterval := 10 * time.Second
numIntervals := 12 // timeout: 2 mins

for i := 0; i < numIntervals; i++ {
select {
// Wait for StartNode() to finish SDN setup
case <-node.podNetworkReady:
return nil
case <-time.After(logInterval):
log.Infof("Waiting for SDN pod network to be ready...")
}
}
return fmt.Errorf("SDN pod network is not ready(timeout: 2 mins)")
}
2 changes: 1 addition & 1 deletion plugins/osdn/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ func (plugin *OsdnNode) SetUpPod(namespace string, name string, id kubeletTypes.
return err
}

pod, err := plugin.Registry.GetPod(plugin.HostName, namespace, name)
pod, err := plugin.registry.GetPod(plugin.hostName, namespace, name)
if err != nil {
return err
}
Expand Down

0 comments on commit 3395f29

Please sign in to comment.