/
cortex.go
175 lines (147 loc) · 4.56 KB
/
cortex.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
// Package cortex provides methods to query data from our central
// Prometheus data store.
package cortex
import (
"context"
"fmt"
"log"
"time"
"github.com/giantswarm/microerror"
"github.com/prometheus/client_golang/api"
v1 "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/prometheus/common/config"
"github.com/prometheus/common/model"
)
const (
query = `aggregation:giantswarm:cluster_info{pipeline="testing",cluster_type="workload_cluster"}`
// Amount of time to look back. The longer the time frame, the slower
// and more expensive the query.
timeRange = 7 * 24 * time.Hour
// If a cluster has been last seen more than this much time ago,
// it is considered deleted. Be careful to make this at least as
// large as the stepInterval.
lastSeenDurationThreshold = time.Hour
// Step interval to query. Should be large enough to avoid returning a huge
// result with every query.
stepInterval = time.Hour
)
type Config struct {
// URL of the prometheus API server.
URL string
// UserName for authentication with the API server.
UserName string
// Password (API token) for authentication with the API server.
Password string
}
type Service struct {
config Config
client api.Client
}
func New(conf Config) (*Service, error) {
apiConfig := api.Config{
Address: conf.URL,
RoundTripper: config.NewBasicAuthRoundTripper(
conf.UserName,
config.Secret(conf.Password),
"",
api.DefaultRoundTripper),
}
client, err := api.NewClient(apiConfig)
if err != nil {
return nil, microerror.Mask(err)
}
s := &Service{
config: conf,
client: client,
}
return s, nil
}
// Cluster represents a workload cluster.
type Cluster struct {
Installation string
ID string
Release string
Provider string
FirstTimestamp time.Time
}
// QueryClusters queries cortex for a list of workload clusters
// that existed in time series at a given point in time.
// Returns a slice of Clusters.
func (s Service) QueryClusters() ([]Cluster, error) {
clusters := []Cluster{}
v1api := v1.NewAPI(s.client)
now := time.Now().UTC()
queryRange := v1.Range{
Start: now.Add(-timeRange),
End: now,
Step: stepInterval,
}
value, warnings, err := v1api.QueryRange(context.Background(), query, queryRange)
if err != nil {
return clusters, microerror.Mask(err)
}
if len(warnings) > 0 {
fmt.Printf("Warnings while querying: %#v\n", warnings)
}
if value == nil {
return clusters, microerror.Maskf(executionFailedError, "query %s returned nil value", query)
}
matrix, ok := value.(model.Matrix)
if ok {
if matrix.Len() == 0 {
log.Printf("Returned result is empty.\n")
return clusters, microerror.Maskf(executionFailedError, "query %s returned an empty result", query)
} else {
log.Printf("Returned result is matrix with %d series.\n", matrix.Len())
}
for i := 0; i < matrix.Len(); i++ {
installation := ""
clusterID := ""
release := ""
provider := ""
if val, ok := matrix[i].Metric["installation"]; ok {
installation = string(val)
} else {
return clusters, microerror.Maskf(executionFailedError, "could not find required label 'installation' in sample")
}
if val, ok := matrix[i].Metric["cluster_id"]; ok {
clusterID = string(val)
} else {
return clusters, microerror.Maskf(executionFailedError, "could not find required label 'cluster_id' in sample")
}
if val, ok := matrix[i].Metric["provider"]; ok {
provider = string(val)
} else {
return clusters, microerror.Maskf(executionFailedError, "could not find required label 'provider' in sample")
}
// Vintage only: try to find release version.
if val, ok := matrix[i].Metric["release_version"]; ok {
release = string(val)
} else {
// AWS is the only vintage provider out there.
if provider == "aws" {
log.Printf("could not find required label 'release_version' in sample for cluster %s\n", clusterID)
}
}
first := int64(matrix[i].Values[0].Timestamp)
latest := int64(matrix[i].Values[len(matrix[i].Values)-1].Timestamp)
// Determine whether cluster still exists
lastSeenDuration := now.Sub(time.Unix(latest/1000, 0))
if lastSeenDuration > lastSeenDurationThreshold {
// Skip as not existing any more.
continue
}
c := Cluster{
Installation: installation,
ID: clusterID,
Release: release,
Provider: provider,
FirstTimestamp: time.Unix(first/1000, 0),
}
clusters = append(clusters, c)
}
} else {
return clusters, microerror.Maskf(executionFailedError, "query %s did not return a matrix", query)
}
return clusters, nil
}