Skip to content

Commit

Permalink
fix: fix fetching services' and pods' data bug
Browse files Browse the repository at this point in the history
  • Loading branch information
sevennt committed Jan 9, 2019
1 parent f8d5420 commit 3a928b5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/api/handler/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
31 changes: 12 additions & 19 deletions src/api/service/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a928b5

Please sign in to comment.