From 3a928b593cd146597acb879e7b67e1aa4f0a6b57 Mon Sep 17 00:00:00 2001 From: sevennt Date: Wed, 9 Jan 2019 19:39:56 +0800 Subject: [PATCH] fix: fix fetching services' and pods' data bug --- src/api/handler/service.go | 3 +-- src/api/service/kube.go | 31 ++++++++++++------------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/src/api/handler/service.go b/src/api/handler/service.go index 46c1b7223..73b2a7b56 100644 --- a/src/api/handler/service.go +++ b/src/api/handler/service.go @@ -15,11 +15,10 @@ package handler import ( + "github.com/gin-gonic/gin" "github.com/xiaomi/naftis/src/api/bootstrap" "github.com/xiaomi/naftis/src/api/service" "github.com/xiaomi/naftis/src/api/util" - - "github.com/gin-gonic/gin" ) // Services returns all available services. diff --git a/src/api/service/kube.go b/src/api/service/kube.go index 751d01729..3a2c1e70f 100644 --- a/src/api/service/kube.go +++ b/src/api/service/kube.go @@ -352,39 +352,32 @@ func (k *kubeInfo) sync() { // get services' and pods' data from Kubernetes var serviceCh = make(chan service, 200) - var podCh = make(chan v1.Pod, 100) - k.wg.Add(len(svcs.Items)) for _, i := range svcs.Items { go func(i v1.Service) { s := service{} s.Service = i s.Pods = k.podsFromK8S(i.Spec.Selector) - for _, p := range s.Pods { - podCh <- p - } serviceCh <- s - k.wg.Done() }(i) } - go func() { - k.wg.Wait() - close(serviceCh) - close(podCh) - }() services := make([]service, 0, len(svcs.Items)) - for s := range serviceCh { - services = append(services, s) - } - tmpPods := make(map[string]v1.Pod) pods := make(pods, 0) - for p := range podCh { - if _, ok := tmpPods[string(p.UID)]; !ok { - pods = append(pods, p) + go func() { + for s := range serviceCh { + services = append(services, s) + for _, p := range s.Pods { + if _, ok := tmpPods[string(p.UID)]; !ok { + pods = append(pods, p) + } + } + k.wg.Done() } - } + }() + k.wg.Wait() + close(serviceCh) k.mtx.Lock() k.services = services