forked from webdevops/azure-devops-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
servicediscovery.go
130 lines (104 loc) · 3.06 KB
/
servicediscovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"sync"
"time"
cache "github.com/patrickmn/go-cache"
"go.uber.org/zap"
AzureDevops "github.com/webdevops/azure-devops-exporter/azure-devops-client"
)
const (
azureDevopsServiceDiscoveryCacheKeyProjectList = "projects"
azureDevopsServiceDiscoveryCacheKeyAgentPoolList = "agentpools"
)
type (
azureDevopsServiceDiscovery struct {
cache *cache.Cache
cacheExpiry time.Duration
logger *zap.SugaredLogger
lock struct {
projectList sync.Mutex
agentpoolList sync.Mutex
}
}
)
func NewAzureDevopsServiceDiscovery() *azureDevopsServiceDiscovery {
sd := &azureDevopsServiceDiscovery{}
sd.cacheExpiry = opts.ServiceDiscovery.RefreshDuration
sd.cache = cache.New(sd.cacheExpiry, time.Duration(1*time.Minute))
sd.logger = logger.With(zap.String("component", "servicediscovery"))
sd.logger.Infof("init AzureDevops servicediscovery with %v cache", sd.cacheExpiry.String())
return sd
}
func (sd *azureDevopsServiceDiscovery) Update() {
sd.cache.Flush()
sd.ProjectList()
sd.AgentPoolList()
}
func (sd *azureDevopsServiceDiscovery) ProjectList() (list []AzureDevops.Project) {
sd.lock.projectList.Lock()
defer sd.lock.projectList.Unlock()
if val, ok := sd.cache.Get(azureDevopsServiceDiscoveryCacheKeyProjectList); ok {
// fetched from cache
list = val.([]AzureDevops.Project)
return
}
// cache was invalid, fetch data from api
sd.logger.Infof("updating project list")
result, err := AzureDevopsClient.ListProjects()
if err != nil {
sd.logger.Panic(err)
}
sd.logger.Infof("fetched %v projects", result.Count)
list = result.List
// whitelist
if len(opts.AzureDevops.FilterProjects) > 0 {
rawList := list
list = []AzureDevops.Project{}
for _, project := range rawList {
if arrayStringContains(opts.AzureDevops.FilterProjects, project.Id) {
list = append(list, project)
}
}
}
// blacklist
if len(opts.AzureDevops.BlacklistProjects) > 0 {
// filter ignored azure devops projects
rawList := list
list = []AzureDevops.Project{}
for _, project := range rawList {
if !arrayStringContains(opts.AzureDevops.BlacklistProjects, project.Id) {
list = append(list, project)
}
}
}
// save to cache
sd.cache.SetDefault(azureDevopsServiceDiscoveryCacheKeyProjectList, list)
return
}
func (sd *azureDevopsServiceDiscovery) AgentPoolList() (list []int64) {
sd.lock.agentpoolList.Lock()
defer sd.lock.agentpoolList.Unlock()
if val, ok := sd.cache.Get(azureDevopsServiceDiscoveryCacheKeyAgentPoolList); ok {
// fetched from cache
list = val.([]int64)
return
}
if opts.AzureDevops.AgentPoolIdList != nil {
sd.logger.Infof("using predefined AgentPool list")
list = *opts.AzureDevops.AgentPoolIdList
} else {
sd.logger.Infof("upading AgentPool list")
result, err := AzureDevopsClient.ListAgentPools()
if err != nil {
sd.logger.Panic(err)
return
}
sd.logger.Infof("fetched %v agentpools", result.Count)
for _, agentPool := range result.Value {
list = append(list, agentPool.ID)
}
}
// save to cache
sd.cache.SetDefault(azureDevopsServiceDiscoveryCacheKeyAgentPoolList, list)
return
}