Skip to content

Commit

Permalink
Reduce item not found
Browse files Browse the repository at this point in the history
Signed-off-by: Longhui li <longhui.li@woqutech.com>
  • Loading branch information
llhhbc committed Jan 29, 2023
1 parent bfe2fb2 commit 5710183
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
2 changes: 1 addition & 1 deletion collector/pkg/metadata/kubernetes/k8scache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
60 changes: 60 additions & 0 deletions collector/pkg/metadata/kubernetes/node_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kubernetes

import (
"fmt"
"net"
"os"
"sync"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
3 changes: 2 additions & 1 deletion deploy/scripts/run_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5710183

Please sign in to comment.