diff --git a/collector/pkg/metadata/kubernetes/k8scache.go b/collector/pkg/metadata/kubernetes/k8scache.go index 552d9bc58..e172d29a3 100644 --- a/collector/pkg/metadata/kubernetes/k8scache.go +++ b/collector/pkg/metadata/kubernetes/k8scache.go @@ -183,7 +183,7 @@ func (c *K8sMetaDataCache) GetPodByIp(ip string) (*K8sPodInfo, bool) { } // find the first pod whose network mode is not hostnetwork for _, info := range portContainerInfo { - if !info.RefPodInfo.isHostNetwork && info.RefPodInfo.WorkloadKind != "daemonset" { + if !info.RefPodInfo.isHostNetwork { return info.RefPodInfo, true } } diff --git a/collector/pkg/metadata/kubernetes/node_watch.go b/collector/pkg/metadata/kubernetes/node_watch.go index 6e08fb626..edaf6ac0e 100644 --- a/collector/pkg/metadata/kubernetes/node_watch.go +++ b/collector/pkg/metadata/kubernetes/node_watch.go @@ -2,6 +2,8 @@ package kubernetes import ( "fmt" + "net" + "os" "sync" corev1 "k8s.io/api/core/v1" @@ -86,6 +88,21 @@ func NodeWatch(clientSet *kubernetes.Clientset) { UpdateFunc: UpdateNode, DeleteFunc: DeleteNode, }) + + // sync self all ip info + selfNode := os.Getenv("MY_NODE_NAME") + self, err := factory.Core().V1().Nodes().Lister().Get(selfNode) + if err != nil { + runtime.HandleError(fmt.Errorf("get self node info failed %s, %v. ", selfNode, err)) + return + } + + err = KeepSelfAllAddressInfo(self) + if err != nil { + runtime.HandleError(fmt.Errorf("keep self node info failed %s, %v. ", selfNode, err)) + return + } + // TODO: use workqueue to avoid blocking <-stopper } @@ -115,3 +132,46 @@ func DeleteNode(obj interface{}) { node := obj.(*corev1.Node) globalNodeInfo.delete(node.Name) } + +func KeepSelfAllAddressInfo(node *corev1.Node) error { + nI := &NodeInfo{ + Ip: "", + Name: node.Name, + Labels: node.Labels, + } + + internalIp := "" + for _, nodeAddress := range node.Status.Addresses { + if nodeAddress.Type == "InternalIP" { + internalIp = nodeAddress.Address + } + } + + ifaces, err := net.Interfaces() + if err != nil { + return fmt.Errorf("localAddresses: %v\n", err.Error()) + } + for _, i := range ifaces { + addrs, err := i.Addrs() + if err != nil { + // skip iface without addr + continue + } + for idx, a := range addrs { + if ipv4Addr := a.(*net.IPNet).IP.To4(); ipv4Addr != nil { + nI.Ip = ipv4Addr.String() + if nI.Ip == internalIp { + nI.Name = node.Name + } else { + if idx > 0 { + nI.Name = fmt.Sprintf("%s_%s_%d", node.Name, i.Name, idx) + } else { + nI.Name = fmt.Sprintf("%s_%s", node.Name, i.Name) + } + } + globalNodeInfo.add(nI) + } + } + } + return nil +} diff --git a/deploy/scripts/run_docker.sh b/deploy/scripts/run_docker.sh index 84c9feff0..d13a5227b 100755 --- a/deploy/scripts/run_docker.sh +++ b/deploy/scripts/run_docker.sh @@ -6,7 +6,8 @@ docker_image_with_tag="kindlingproject/agent-builder:latest" configs=(-v "$HOME/.config:/root/.config" \ -v "$HOME/.ssh:/root/.ssh" \ - -v "$HOME/.gitconfig:/root/.gitconfig") + -v "$HOME/.docker:/root/.docker" \ + -v "$HOME/.gitconfig:/root/.gitconfig") IFS=' ' # Read the environment variable and set it to an array. This allows