diff --git a/pkg/device/builder/builder.go b/pkg/device/builder/builder.go index 22da3da84..f4c38299a 100644 --- a/pkg/device/builder/builder.go +++ b/pkg/device/builder/builder.go @@ -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 != "" { diff --git a/pkg/types/types.go b/pkg/types/types.go index 71322b1ab..ed100de37 100644 --- a/pkg/types/types.go +++ b/pkg/types/types.go @@ -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 } diff --git a/pkg/watch/node/node.go b/pkg/watch/node/node.go index 07752311e..c045b3a4c 100644 --- a/pkg/watch/node/node.go +++ b/pkg/watch/node/node.go @@ -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) { @@ -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 diff --git a/pkg/watch/node/node_test.go b/pkg/watch/node/node_test.go new file mode 100644 index 000000000..feb9cbde1 --- /dev/null +++ b/pkg/watch/node/node_test.go @@ -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) + } +} diff --git a/pkg/watch/pod/pod.go b/pkg/watch/pod/pod.go index 857fbbd08..9da8e11a9 100644 --- a/pkg/watch/pod/pod.go +++ b/pkg/watch/pod/pod.go @@ -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), @@ -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 {