Skip to content

Commit

Permalink
reduce workloadMap data;only send data of the machine
Browse files Browse the repository at this point in the history
Signed-off-by: anthonyhui <anthonyhui@126.com>
  • Loading branch information
anthonyhui committed Aug 8, 2023
1 parent dca173a commit a5f5cf0
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion collector/pkg/metadata/kubernetes/pod_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func deletePodInfo(podInfo *deletedPodInfo) {
if podInfo.name != "" {
deletePodInfo, ok := globalPodInfo.delete(podInfo.namespace, podInfo.name)
if ok {
globalWorkload.delete(deletePodInfo.Namespace, deletePodInfo.WorkloadName)
workloadMap.delete(deletePodInfo.Namespace, deletePodInfo.WorkloadName)
}
}
if len(podInfo.containerIds) != 0 {
Expand Down
32 changes: 18 additions & 14 deletions collector/pkg/metadata/kubernetes/pod_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kubernetes

import (
"fmt"
"os"
_ "path/filepath"
"regexp"
"strings"
Expand Down Expand Up @@ -30,7 +31,7 @@ type podMap struct {
mutex sync.RWMutex
}

type workloadMap struct {
type WorkloadMap struct {
Info map[string]map[string]*WorkloadInfo
mutex sync.RWMutex
}
Expand All @@ -42,12 +43,12 @@ type WorkloadInfo struct {
}

var globalPodInfo = newPodMap()
var globalWorkload = newWorkloadMap()
var workloadMap = newWorkloadMap()

func GetWorkloadDataGroup() []*model.DataGroup {
globalWorkload.mutex.RLock()
workloadMap.mutex.RLock()
dataGroups := make([]*model.DataGroup, 0)
for _, workloadInfoMap := range globalWorkload.Info {
for _, workloadInfoMap := range workloadMap.Info {
for _, workloadInfo := range workloadInfoMap {
dataGroups = append(dataGroups, &model.DataGroup{
Name: constnames.K8sWorkloadMetricGroupName,
Expand All @@ -62,7 +63,7 @@ func GetWorkloadDataGroup() []*model.DataGroup {
})
}
}
globalWorkload.mutex.RUnlock()
workloadMap.mutex.RUnlock()
return dataGroups
}

Expand All @@ -73,8 +74,8 @@ func newPodMap() *podMap {
}
}

func newWorkloadMap() *workloadMap {
return &workloadMap{
func newWorkloadMap() *WorkloadMap {
return &WorkloadMap{
Info: make(map[string]map[string]*WorkloadInfo),
mutex: sync.RWMutex{},
}
Expand Down Expand Up @@ -103,7 +104,7 @@ func (m *podMap) delete(namespace string, name string) (*K8sPodInfo, bool) {
return podInfo, ok
}

func (m *workloadMap) add(info *WorkloadInfo) {
func (m *WorkloadMap) add(info *WorkloadInfo) {
m.mutex.Lock()
workloadInfoMap, ok := m.Info[info.Namespace]
if !ok {
Expand All @@ -115,7 +116,7 @@ func (m *workloadMap) add(info *WorkloadInfo) {

}

func (m *workloadMap) delete(namespace string, name string) {
func (m *WorkloadMap) delete(namespace string, name string) {
m.mutex.Lock()
workloadInfoMap, ok := m.Info[namespace]
if ok {
Expand Down Expand Up @@ -278,11 +279,14 @@ func onAdd(obj interface{}) {
}
}
globalPodInfo.add(cachePodInfo)
globalWorkload.add(&WorkloadInfo{
Namespace: cachePodInfo.Namespace,
WorkloadName: cachePodInfo.WorkloadName,
WorkloadKind: cachePodInfo.WorkloadKind,
})
//workloadMap only restore the workload in this machine
if pod.Spec.NodeName == os.Getenv("NODE_NAME") {
workloadMap.add(&WorkloadInfo{
Namespace: cachePodInfo.Namespace,
WorkloadName: cachePodInfo.WorkloadName,
WorkloadKind: cachePodInfo.WorkloadKind,
})
}
}

func getControllerKindName(pod *corev1.Pod) (workloadKind string, workloadName string) {
Expand Down

0 comments on commit a5f5cf0

Please sign in to comment.