This repository has been archived by the owner on Jan 19, 2023. It is now read-only.
generated from vshn/go-bootstrap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (223 loc) · 6.67 KB
/
main.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
package main
import (
"context"
"database/sql"
"errors"
"fmt"
"net/http"
"os"
"strconv"
"time"
"github.com/vshn/cloudscale-metrics-collector/pkg/categoriesmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/datetimesmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/discountsmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/factsmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/kubernetes"
"github.com/vshn/cloudscale-metrics-collector/pkg/productsmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/queriesmodel"
"github.com/vshn/cloudscale-metrics-collector/pkg/tenantsmodel"
"github.com/appuio/appuio-cloud-reporting/pkg/db"
"github.com/cloudscale-ch/cloudscale-go-sdk/v2"
"github.com/jmoiron/sqlx"
)
var (
// these variables are populated by Goreleaser when releasing
version = "unknown"
commit = "-dirty-"
date = time.Now().Format("2006-01-02")
appName = "cloudscale-metrics-collector"
// constants
daysEnvVariable = "DAYS"
tokenEnvVariable = "CLOUDSCALE_API_TOKEN"
dbUrlEnvVariable = "ACR_DB_URL"
kubernetesURLEnvVariable = "KUBERNETES_SERVER_URL"
kubernetesTokenEnvVariable = "KUBERNETES_SERVER_TOKEN"
// source format: 'query:zone:tenant:namespace' or 'query:zone:tenant:namespace:class'
// We do not have real (prometheus) queries here, just random hardcoded strings.
sourceQueryStorage = "object-storage-storage"
sourceQueryTrafficOut = "object-storage-traffic-out"
sourceQueryRequests = "object-storage-requests"
// SourceZone represents the zone of the bucket, not of the cluster where the request for the bucket originated.
// All the zones we use here must be known to the appuio-odoo-adapter as well.
sourceZones = []string{"cloudscale"}
)
type config struct {
apiToken string
databaseURL string
days int
kubeconfig string
kubernetesServerURL string
kubernetesServerToken string
}
func loadConfig() (*config, error) {
cloudscaleApiToken := os.Getenv(tokenEnvVariable)
if cloudscaleApiToken == "" {
return nil, fmt.Errorf("missing env var %q", tokenEnvVariable)
}
dbUrl := os.Getenv(dbUrlEnvVariable)
if dbUrl == "" {
return nil, fmt.Errorf("missing env var %q", dbUrlEnvVariable)
}
daysStr := os.Getenv(daysEnvVariable)
if daysStr == "" {
daysStr = "1"
}
days, err := strconv.Atoi(daysStr)
if err != nil {
return nil, fmt.Errorf("env var %q not an integer: %w", daysEnvVariable, err)
}
kubeconfig := os.Getenv("KUBECONFIG")
url := os.Getenv(kubernetesURLEnvVariable)
if url == "" && kubeconfig == "" {
return nil, fmt.Errorf("missing env var %q", kubernetesURLEnvVariable)
}
token := os.Getenv(kubernetesTokenEnvVariable)
if token == "" && kubeconfig == "" {
return nil, fmt.Errorf("missing env var %q", kubernetesTokenEnvVariable)
}
return &config{
apiToken: cloudscaleApiToken,
databaseURL: dbUrl,
days: days,
// will load KUBECONFIG if defined, otherwise will use server url and token to connect.
kubeconfig: kubeconfig,
kubernetesServerURL: url,
kubernetesServerToken: token,
}, nil
}
func initDb(ctx context.Context, tx *sqlx.Tx) error {
for _, product := range ensureProducts {
_, err := productsmodel.Ensure(ctx, tx, product)
if err != nil {
return err
}
}
for _, discount := range ensureDiscounts {
_, err := discountsmodel.Ensure(ctx, tx, discount)
if err != nil {
return err
}
}
for _, query := range ensureQueries {
_, err := queriesmodel.Ensure(ctx, tx, query)
if err != nil {
return err
}
}
return nil
}
func main() {
ctx := context.Background()
fmt.Fprintf(os.Stderr, "%s: version %s (%s) compiled on %s\n", appName, version, commit, date)
err := sync(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
os.Exit(0)
}
func sync(ctx context.Context) error {
cfg, err := loadConfig()
if err != nil {
return fmt.Errorf("config load: %w", err)
}
cloudscaleClient := cloudscale.NewClient(http.DefaultClient)
cloudscaleClient.AuthToken = cfg.apiToken
k8sclient, err := kubernetes.NewClient(cfg.kubeconfig, cfg.kubernetesServerURL, cfg.kubernetesServerToken)
if err != nil {
return fmt.Errorf("kubernetes client: %w", err)
}
// The cloudscale API works in Europe/Zurich, so we have to use the same, regardless of where this code runs
location, err := time.LoadLocation("Europe/Zurich")
if err != nil {
return err
}
// Fetch statistics of yesterday (as per Europe/Zurich). The metrics will cover the entire day.
now := time.Now().In(location)
date := time.Date(now.Year(), now.Month(), now.Day()-cfg.days, 0, 0, 0, 0, now.Location())
if err != nil {
return err
}
rdb, err := db.Openx(cfg.databaseURL)
if err != nil {
return err
}
defer rdb.Close()
// initialize DB
tx, err := rdb.BeginTxx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
defer func(tx *sqlx.Tx) {
err := tx.Rollback()
if err != nil && !errors.Is(err, sql.ErrTxDone) {
fmt.Fprintf(os.Stderr, "rollback failed: %v", err)
}
}(tx)
err = initDb(ctx, tx)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
accumulated, err := accumulateBucketMetrics(ctx, date, cloudscaleClient, k8sclient)
if err != nil {
return err
}
for source, value := range accumulated {
if value == 0 {
continue
}
fmt.Printf("syncing %s\n", source)
// start new transaction for actual work
tx, err = rdb.BeginTxx(ctx, &sql.TxOptions{})
if err != nil {
return err
}
tenant, err := tenantsmodel.Ensure(ctx, tx, &db.Tenant{Source: source.Tenant})
if err != nil {
return err
}
category, err := categoriesmodel.Ensure(ctx, tx, &db.Category{Source: source.Zone + ":" + source.Namespace})
if err != nil {
return err
}
dateTime := datetimesmodel.New(source.Start)
dateTime, err = datetimesmodel.Ensure(ctx, tx, dateTime)
if err != nil {
return err
}
product, err := productsmodel.GetBestMatch(ctx, tx, source.String(), source.Start)
if err != nil {
return err
}
discount, err := discountsmodel.GetBestMatch(ctx, tx, source.String(), source.Start)
if err != nil {
return err
}
query, err := queriesmodel.GetByName(ctx, tx, source.Query+":"+source.Zone)
if err != nil {
return err
}
var quantity float64
if query.Unit == "GB" || query.Unit == "GBDay" {
quantity = float64(value) / 1000 / 1000 / 1000
} else if query.Unit == "KReq" {
quantity = float64(value) / 1000
} else {
return errors.New("Unknown query unit " + query.Unit)
}
storageFact := factsmodel.New(dateTime, query, tenant, category, product, discount, quantity)
_, err = factsmodel.Ensure(ctx, tx, storageFact)
if err != nil {
return err
}
err = tx.Commit()
if err != nil {
return err
}
}
return nil
}