-
Notifications
You must be signed in to change notification settings - Fork 8
/
gce.go
332 lines (291 loc) · 9.79 KB
/
gce.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gcppromd
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/prometheus/common/model"
"golang.org/x/oauth2/google"
"google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/compute/v1"
pmodel "github.com/prometheus/common/model"
pstrutil "github.com/prometheus/prometheus/util/strutil"
)
const (
// Label names generated
promLabel = model.MetaLabelPrefix + "gce_"
promLabelProject = promLabel + "project"
promLabelZone = promLabel + "zone"
promLabelRegion = promLabel + "region"
promLabelNetwork = promLabel + "network"
promLabelSubnetwork = promLabel + "subnetwork"
promLabelPublicIP = promLabel + "public_ip"
promLabelPrivateIP = promLabel + "private_ip"
promLabelInstanceName = promLabel + "instance_name"
promLabelInstanceStatus = promLabel + "instance_status"
promLabelTags = promLabel + "tags"
promLabelMetadata = promLabel + "metadata_"
promLabelName = promLabel + "name"
promLabelLabel = promLabel + "label_"
promLabelDelegateForNames = promLabel + "delegate_for_instances"
// separator used to join the GCE tag in one prom label or to declare
// multiple ports in the same gce metadata
promSeparator = ","
// filter to use to identify the instances scrapable by prometheus
promPresenceFilter = "(labels.prometheus eq .*)"
// Label prefixes scraped from GCE instance labels
gcePrefix = "prometheus_"
gcePrefixPorts = gcePrefix + "ports_"
gcePrefixDelegateAddr = gcePrefix + "delegate_address_"
gcePrefixDelegatePorts = gcePrefix + "delegate_ports_"
)
// GCEReqInstanceDiscovery work unit for a pool of GCEDiscovery workers
type GCEReqInstanceDiscovery struct {
Project string
// Filter passed to the GCE API when looking up instances, see https://cloud.google.com/compute/docs/reference/rest/v1/acceleratorTypes/aggregatedList#body.QUERY_PARAMETERS.filter
Filter string
PrometheusConfigs chan []*PromConfig
Errors chan error
}
// GCEDiscovery represents a Google Compute Engine discovery configuration for one Google project.
type GCEDiscovery struct {
service *compute.Service
}
type delagatedHost struct {
address string
ports []int
delegateFor []string
}
// NewGCEDiscoveryPool creates a pool <size> go routine to process the discovery requests in parallel.
func NewGCEDiscoveryPool(ctx context.Context, size int) (chan *GCEReqInstanceDiscovery, error) {
gced, err := NewGCEDiscovery()
if err != nil {
return nil, err
}
reqs := make(chan *GCEReqInstanceDiscovery)
for n := 0; n < size; n++ {
go func() {
for {
select {
case <-ctx.Done():
return
case req, ok := <-reqs:
if !ok {
return
}
confs, err := gced.Instances(ctx, req.Project, req.Filter)
if err != nil {
req.Errors <- err
} else {
req.PrometheusConfigs <- confs
}
}
}
}()
}
return reqs, nil
}
// NewGCEDiscovery returns an object of GCEDiscovery type.
func NewGCEDiscovery() (*GCEDiscovery, error) {
ctx := context.Background()
cl, err := google.DefaultClient(ctx, compute.CloudPlatformScope)
if err != nil {
return nil, err
}
service, err := compute.New(cl)
if err != nil {
return nil, err
}
d := GCEDiscovery{service: service}
return &d, nil
}
type GCPProjectDiscovery struct {
service *cloudresourcemanager.Service
}
func NewGCPProjectDiscovery() (*GCPProjectDiscovery, error) {
ctx := context.Background()
cl, err := google.DefaultClient(ctx, cloudresourcemanager.CloudPlatformReadOnlyScope)
if err != nil {
return nil, err
}
service, err := cloudresourcemanager.New(cl)
if err != nil {
return nil, err
}
d := GCPProjectDiscovery{service: service}
return &d, nil
}
// DiscoverProjects discovers accessible projects in GCP
func (d *GCPProjectDiscovery) Projects(ctx context.Context) (projects []string, err error) {
req := d.service.Projects.List()
if err := req.Pages(ctx, func(page *cloudresourcemanager.ListProjectsResponse) error {
for _, project := range page.Projects {
projects = append(projects, project.ProjectId)
}
return nil
}); err != nil {
return nil, err
}
return
}
// Instances returns a list of instances of a directory project.
func (d *GCEDiscovery) Instances(ctx context.Context, project, filter string) ([]*PromConfig, error) {
configs := make([]*PromConfig, 0, 100)
ialReq := d.service.Instances.
AggregatedList(project).
Fields(
"nextPageToken",
"items/*/instances(id,status,zone,name,tags,labels,networkInterfaces,selfLink,metadata)",
)
filter = strings.TrimSpace(promPresenceFilter + " AND" + filter)
if filter[len(filter)-3:] == "AND" {
filter = filter[:len(filter)-3]
}
if filter != "" {
ialReq = ialReq.Filter(filter)
}
delagatedHosts := make(map[string]*delagatedHost)
err := ialReq.Pages(ctx, func(ial *compute.InstanceAggregatedList) error {
for _, zone := range ial.Items {
for _, inst := range zone.Instances {
if len(inst.NetworkInterfaces) <= 0 {
continue
}
priIface := inst.NetworkInterfaces[0]
region := extractRegionFromZone(inst.Zone)
labels := pmodel.LabelSet{
promLabelProject: pmodel.LabelValue(project),
promLabelZone: pmodel.LabelValue(inst.Zone),
promLabelRegion: pmodel.LabelValue(region),
promLabelInstanceName: pmodel.LabelValue(inst.Name),
promLabelInstanceStatus: pmodel.LabelValue(inst.Status),
promLabelNetwork: pmodel.LabelValue(priIface.Network),
promLabelSubnetwork: pmodel.LabelValue(priIface.Subnetwork),
promLabelPrivateIP: pmodel.LabelValue(priIface.NetworkIP),
}
if len(priIface.AccessConfigs) > 0 {
ac := priIface.AccessConfigs[0]
if ac.Type == "ONE_TO_ONE_NAT" {
labels[promLabelPublicIP] = pmodel.LabelValue(ac.NatIP)
}
}
if inst.Tags != nil && len(inst.Tags.Items) > 0 {
// We surround the separated list with the separator as well. This way regular expressions
// in relabeling rules don't have to consider tag positions.
tags := promSeparator + strings.Join(inst.Tags.Items, promSeparator) + promSeparator
labels[promLabelTags] = pmodel.LabelValue(tags)
}
if inst.Labels != nil {
for key, v := range inst.Labels {
name := pstrutil.SanitizeLabelName(key)
labels[promLabelLabel+model.LabelName(name)] = model.LabelValue(v)
}
}
// GCE metadata are key-value pairs for user supplied attributes.
if inst.Metadata != nil {
// keep track of the locally created label set.
lTargetsLabels := make([]pmodel.LabelSet, 0)
// this loop do not populates the __meta_gce_metadata_...
// labels only generate the different targets.
for _, i := range inst.Metadata.Items {
// Protect against occasional nil pointers.
if i.Value == nil {
continue
}
key, v := i.Key, *i.Value
paddedKey := key + "_" // pad the key with _ to match naked prefix
if ports, name, ok := parsePorts(paddedKey, v, gcePrefixPorts); ok {
addrs := make([]string, 0, len(ports))
for _, port := range ports {
addr := fmt.Sprintf("%s:%d", priIface.NetworkIP, port)
addrs = append(addrs, addr)
}
targetLabels := labels.Clone()
targetLabels[model.LabelName(promLabelName)] = model.LabelValue(name)
pc := &PromConfig{addrs, targetLabels}
configs = append(configs, pc)
lTargetsLabels = append(lTargetsLabels, targetLabels)
continue
}
if ports, name, ok := parsePorts(paddedKey, v, gcePrefixDelegatePorts); ok {
if _, ok := delagatedHosts[name]; !ok {
delagatedHosts[name] = &delagatedHost{}
}
delagatedHosts[name].ports = ports
continue
}
if strings.HasPrefix(paddedKey, gcePrefixDelegateAddr) {
name := parseNameFromKey(paddedKey, gcePrefixDelegateAddr)
if _, ok := delagatedHosts[name]; !ok {
delagatedHosts[name] = &delagatedHost{}
}
delagatedHosts[name].address = v
delagatedHosts[name].delegateFor = append(
delagatedHosts[name].delegateFor,
inst.SelfLink,
)
continue
}
}
// populates cloned labels wiht the __meta_gce_metadata
for _, i := range inst.Metadata.Items {
if i.Value == nil {
continue
}
key, v := i.Key, *i.Value
for _, tlabels := range lTargetsLabels {
name := pstrutil.SanitizeLabelName(key)
tlabels[promLabelMetadata+model.LabelName(name)] = model.LabelValue(v)
}
}
}
}
}
return nil
})
// nolabels := pmodel.LabelSet{}
for name, delegated := range delagatedHosts {
tags := promSeparator + strings.Join(delegated.delegateFor, promSeparator) + promSeparator
largetLables := pmodel.LabelSet{
promLabelDelegateForNames: pmodel.LabelValue(tags),
model.LabelName(promLabelName): pmodel.LabelValue(name),
}
for _, port := range delegated.ports {
addr := []string{fmt.Sprintf("%s:%d", delegated.address, port)}
fmt.Println(addr)
pc := &PromConfig{addr, largetLables}
configs = append(configs, pc)
}
}
return configs, err
}
func parsePorts(key, value, prefix string) (ports []int, name string, has bool) {
has = strings.HasPrefix(key, prefix)
if !has {
return
}
name = parseNameFromKey(key, prefix)
pvalues := strings.Split(value, promSeparator)
for _, pv := range pvalues {
port, err := strconv.Atoi(pv)
if err != nil {
// ignore invalid port
continue
}
ports = append(ports, port)
}
return
}
func parseNameFromKey(key, prefix string) (name string) {
plen := len(prefix)
if len(key) > plen {
// non-naked prefix extract name
name = strings.TrimRight(key[plen:], "_")
}
return
}
func extractRegionFromZone(zoneURL string) string {
lastIndex := strings.LastIndex(zoneURL, "/")
zone := zoneURL[lastIndex+1:]
return zone[:len(zone)-2]
}