This repository has been archived by the owner on May 17, 2022. It is now read-only.
/
configuration.go
95 lines (85 loc) · 2.17 KB
/
configuration.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
package cloudburst
type Configuration struct {
PrometheusURL string `json:"prometheus_url"`
ScrapeTargets []struct {
Name string `json:"name"`
Path string `json:"path"`
Description string `json:"description"`
Query string `json:"query"`
ProviderSpec struct {
Weights map[string]int `json:"weights"`
} `json:"provider"`
InstanceSpec struct {
ContainerSpec struct {
Name string `json:"name"`
Image string `json:"image"`
} `json:"container"`
} `json:"spec"`
StaticSpec struct {
Endpoints []string `json:"endpoints"`
} `json:"static"`
} `json:"targets"`
}
func ParseConfiguration(config Configuration) ([]*ScrapeTarget, error) {
var scrapeTargets []*ScrapeTarget
err := validateConfig(config)
if err != nil {
return scrapeTargets, err
}
for _, item := range config.ScrapeTargets {
scrapeTargets = append(scrapeTargets, &ScrapeTarget{
Name: item.Name,
Description: item.Description,
Path: item.Path,
Query: item.Query,
ProviderSpec: ProviderSpec{
Weights: calculateWeights(item.ProviderSpec.Weights),
},
InstanceSpec: InstanceSpec{
Container: ContainerSpec{
Name: item.InstanceSpec.ContainerSpec.Name,
Image: item.InstanceSpec.ContainerSpec.Image,
},
},
StaticSpec: StaticSpec{
Endpoints: item.StaticSpec.Endpoints,
},
})
}
return scrapeTargets, nil
}
func calculateWeights(weights map[string]int) map[string]float32 {
var res = make(map[string]float32)
var sumWeights int
for _, v := range weights {
sumWeights += v
}
for k, v := range weights {
res[k] = float32(v) / float32(sumWeights)
}
return res
}
func validateConfig(config Configuration) error {
return nil
}
/*
func validateScrapeTarget(target) error {
validateProviderSpec()
return nil
}
func validateProviderSpec(config Configuration) error {
hasIllegalProviderWeights(config.)
}
func hasIllegalProviderWeights(weights map[string]int) error {
var sumWeights int
for _,v := range weights {
if v < 0 {
return errors.New("negative values not allowed")
}
sumWeights += v
}
if sumWeights <= 0 {
return errors.New("provider weight sum must be greater then 0")
}
}
*/