Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pkg/device/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ func (b *Builder) System(name, value string) types.DeviceOption {
return setProperty("system."+name, value)
}

// Custom implements types.DeviceBuilder.
func (b *Builder) Custom(name, value string) types.DeviceOption {
return setProperty(name, value)
}

func setProperty(name, value string) types.DeviceOption {
return func(device *lm.RestDevice) {
if value != "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,6 @@ type DeviceBuilder interface {
Auto(string, string) DeviceOption
// System adds a system property to the device.
System(string, string) DeviceOption
// System adds a custom property to the device.
Custom(string, string) DeviceOption
}
14 changes: 11 additions & 3 deletions pkg/watch/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ func (w *Watcher) args(node *v1.Node, category string) []types.DeviceOption {

// getInternalAddress finds the node's internal address.
func getInternalAddress(addresses []v1.NodeAddress) *v1.NodeAddress {
var hostname *v1.NodeAddress
for _, address := range addresses {
if address.Type == v1.NodeInternalIP {
return &address
}
if address.Type == v1.NodeHostName {
hostname = &address
}
}

return nil
//if there is no internal IP for this node, the host name will be used
return hostname
}

func (w *Watcher) createRoleDeviceGroup(labels map[string]string) {
Expand Down Expand Up @@ -201,7 +205,11 @@ func GetNodesMap(k8sClient *kubernetes.Clientset) (map[string]string, error) {
return nil, err
}
for _, nodeInfo := range nodeList.Items {
nodesMap[nodeInfo.Name] = getInternalAddress(nodeInfo.Status.Addresses).Address
address := getInternalAddress(nodeInfo.Status.Addresses)
if address == nil {
continue
}
nodesMap[nodeInfo.Name] = address.Address
}

return nodesMap, nil
Expand Down
34 changes: 34 additions & 0 deletions pkg/watch/node/node_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package node

import (
"testing"

"k8s.io/api/core/v1"
)

func TestGetInternalAddress(t *testing.T) {
var addresses []v1.NodeAddress
address := getInternalAddress(addresses)
if address != nil {
t.Errorf("invalid address: %v", address)
}
addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeHostName,
Address: "test",
})

address = getInternalAddress(addresses)
if address == nil || address.Address != "test" {
t.Errorf("invalid address: %v", address)
}

addresses = append(addresses, v1.NodeAddress{
Type: v1.NodeInternalIP,
Address: "127.0.0.1",
})

address = getInternalAddress(addresses)
if address == nil || address.Address != "127.0.0.1" {
t.Errorf("invalid address: %v", address)
}
}
6 changes: 5 additions & 1 deletion pkg/watch/pod/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (w *Watcher) move(pod *v1.Pod) {

func (w *Watcher) args(pod *v1.Pod, category string) []types.DeviceOption {
categories := utilities.BuildSystemCategoriesFromLabels(category, pod.Labels)
return []types.DeviceOption{
options := []types.DeviceOption{
w.Name(getPodDNSName(pod)),
w.ResourceLabels(pod.Labels),
w.DisplayName(pod.Name),
Expand All @@ -145,6 +145,10 @@ func (w *Watcher) args(pod *v1.Pod, category string) []types.DeviceOption {
w.Auto("uid", string(pod.UID)),
w.System("ips", pod.Status.PodIP),
}
if pod.Spec.HostNetwork {
options = append(options, w.Custom("kubernetes.pod.hostNetwork", "true"))
}
return options
}

func getPodDNSName(pod *v1.Pod) string {
Expand Down