Skip to content

Commit

Permalink
nodelink-controller: Support multiple internal IPs.
Browse files Browse the repository at this point in the history
In a baremetal environment, we may have servers with multiple network
interfaces.  In that case, the Machine may have multiple internal IPs
listed for the different network interfaces.

This patch makes the nodelink-controller keep track of all Machine
internal IPs so that it can successfully match a Node's internal IP
against whichever IP is being used by the kubelet.

The patch also includes unit test coverage for
add/update/deleteMachine() methods.
  • Loading branch information
russellb committed May 29, 2019
1 parent 5cba6ea commit 3d8bbb2
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 7 deletions.
98 changes: 94 additions & 4 deletions cmd/nodelink-controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"reflect"
"testing"

mapiv1alpha1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
mapiv1 "github.com/openshift/cluster-api/pkg/apis/machine/v1beta1"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -32,9 +32,9 @@ func node(taints *[]corev1.Taint) *corev1.Node {
}
}

func machine(taints *[]corev1.Taint) *mapiv1alpha1.Machine {
return &mapiv1alpha1.Machine{
Spec: mapiv1alpha1.MachineSpec{
func machine(taints *[]corev1.Taint) *mapiv1.Machine {
return &mapiv1.Machine{
Spec: mapiv1.MachineSpec{
Taints: *taints,
},
}
Expand Down Expand Up @@ -83,3 +83,93 @@ func TestAddTaintsToNode(t *testing.T) {
}
}
}

func fakeController() *Controller {
c := Controller{}
c.machineAddress = make(map[string]*mapiv1.Machine)
return &c
}

func TestAddUpdateDeleteMachine(t *testing.T) {
testCases := []struct {
description string
machine mapiv1.Machine
numAddresses int
}{
{
description: "Machine with no addresses",
machine: mapiv1.Machine{},
numAddresses: 0,
},
{
description: "Machine with one address",
machine: mapiv1.Machine{
Status: mapiv1.MachineStatus{
Addresses: []corev1.NodeAddress{
corev1.NodeAddress{
Address: "192.168.1.1",
Type: "InternalIP",
},
},
},
},
numAddresses: 1,
},
{
description: "Machine with two addresses",
machine: mapiv1.Machine{
Status: mapiv1.MachineStatus{
Addresses: []corev1.NodeAddress{
corev1.NodeAddress{
Address: "192.168.1.1",
Type: "InternalIP",
},
corev1.NodeAddress{
Address: "172.0.20.2",
Type: "InternalIP",
},
},
},
},
numAddresses: 2,
},
{
description: "Use InternalIP only",
machine: mapiv1.Machine{
Status: mapiv1.MachineStatus{
Addresses: []corev1.NodeAddress{
corev1.NodeAddress{
Address: "192.168.1.1",
Type: "InternalIP",
},
corev1.NodeAddress{
Address: "10.0.20.2",
Type: "ExternalIP",
},
corev1.NodeAddress{
Address: "host.example.com",
Type: "Hostname",
},
},
},
},
numAddresses: 1,
},
}

for _, test := range testCases {
c := fakeController()
c.addMachine(&test.machine)
if len(c.machineAddress) != test.numAddresses {
t.Errorf("Test case: %s, after addMachine(), Expected %d addresses, got %d", test.description, test.numAddresses, len(c.machineAddress))
}
c.updateMachine(mapiv1.Machine{}, &test.machine)
if len(c.machineAddress) != test.numAddresses {
t.Errorf("Test case: %s, after updateMachine(), Expected %d addresses, got %d", test.description, test.numAddresses, len(c.machineAddress))
}
c.deleteMachine(&test.machine)
if len(c.machineAddress) > 0 {
t.Errorf("Test case: %s, after deleteMachine(), Expected 0 addresses, got %d", test.description, len(c.machineAddress))
}
}
}
3 changes: 0 additions & 3 deletions cmd/nodelink-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ func (c *Controller) addMachine(obj interface{}) {
if a.Type == corev1.NodeInternalIP {
glog.V(3).Infof("Adding machine %q into machineAddress list for %q", machine.Name, a.Address)
c.machineAddress[a.Address] = machine
break
}
}
}
Expand All @@ -195,7 +194,6 @@ func (c *Controller) updateMachine(old, cur interface{}) {
if a.Type == corev1.NodeInternalIP {
c.machineAddress[a.Address] = machine
glog.V(3).Infof("Updating machine addresses list. Machine: %q, address: %q", machine.Name, a.Address)
break
}
}
}
Expand All @@ -210,7 +208,6 @@ func (c *Controller) deleteMachine(obj interface{}) {
// Use the internal IP to look for matches:
if a.Type == corev1.NodeInternalIP {
delete(c.machineAddress, a.Address)
break
}
}
glog.V(3).Infof("Delete obsolete machines from machine addresses list")
Expand Down

0 comments on commit 3d8bbb2

Please sign in to comment.