/
manager.go
234 lines (223 loc) Β· 5.02 KB
/
manager.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
package cloud_pricing
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
"github.com/coroot/coroot/model"
"github.com/coroot/coroot/timeseries"
"github.com/coroot/coroot/utils"
"k8s.io/klog"
)
const (
dumpURL = "https://coroot.github.io/cloud-pricing/data/cloud-pricing.json.gz"
dumpFileName = "cloud-pricing.json.gz"
dumpTimeout = time.Second * 30
updateInterval = time.Hour * 24
)
type Manager struct {
dataDir string
lock sync.Mutex
model *Model
}
func NewManager(dataDir string) (*Manager, error) {
if err := utils.CreateDirectoryIfNotExists(dataDir); err != nil {
return nil, err
}
var err error
m := &Manager{dataDir: dataDir}
m.model, err = loadFromFile(path.Join(dataDir, dumpFileName))
if err != nil {
if os.IsNotExist(err) {
err = m.updateModel()
}
if err != nil {
klog.Warningln("failed to load cloud pricing:", err)
}
}
go func() {
if err := m.updateModel(); err != nil {
klog.Warningln("failed to update cloud pricing:", err)
}
for range time.Tick(updateInterval) {
if err := m.updateModel(); err != nil {
klog.Warningln("failed to update cloud pricing:", err)
}
}
}()
return m, nil
}
func (mgr *Manager) GetNodePrice(node *model.Node) *model.NodePrice {
mgr.lock.Lock()
defer mgr.lock.Unlock()
if mgr.model == nil {
return nil
}
var pricing *CloudPricing
var price float32
switch strings.ToLower(node.CloudProvider.Value()) {
case "aws":
pricing = mgr.model.AWS
case "gcp":
pricing = mgr.model.GCP
case "azure":
pricing = mgr.model.Azure
default:
return nil
}
region := Region(strings.ToLower(node.Region.Value()))
switch {
case len(node.Instances) == 1 && node.Instances[0].Rds != nil: //RDS
rds := node.Instances[0].Rds
reg, ok := pricing.ManagedDB[region]
if !ok {
return nil
}
e, ok := reg[Engine(rds.Engine.Value())]
if !ok {
return nil
}
i, ok := e[InstanceType(node.InstanceType.Value())]
if !ok {
return nil
}
switch rds.MultiAz.Value() {
case "", "false":
price = i.SingleAz.OnDemand
case "true":
price = i.MultiAz.OnDemand
}
case len(node.Instances) == 1 && node.Instances[0].Elasticache != nil: //Elasticache
ec := node.Instances[0].Elasticache
reg, ok := pricing.ManagedCache[region]
if !ok {
return nil
}
e, ok := reg[Engine(ec.Engine.Value())]
if !ok {
return nil
}
i, ok := e[InstanceType(node.InstanceType.Value())]
if !ok {
return nil
}
price = i.OnDemand
default: //compute
reg, ok := pricing.Compute[region]
if !ok {
return nil
}
i, ok := reg[InstanceType(node.InstanceType.Value())]
if !ok {
return nil
}
switch strings.ToLower(node.InstanceLifeCycle.Value()) {
case "spot", "preemptible":
price = i.Spot
default:
price = i.OnDemand
}
}
if !(price > 0) {
return nil
}
price /= float32(timeseries.Hour)
cpuCores := node.CpuCapacity.Last()
memBytes := node.MemoryTotalBytes.Last()
np := &model.NodePrice{Total: price}
if timeseries.IsNaN(cpuCores) || timeseries.IsNaN(memBytes) {
return np
}
const gb = 1e9
perUnit := price / (cpuCores + memBytes/gb) // assume that 1Gb of memory costs the same as 1 vCPU
np.PerCPUCore = perUnit
np.PerMemoryByte = perUnit / gb
return np
}
func (mgr *Manager) updateModel() error {
req, err := http.NewRequest("GET", dumpURL, nil)
if err != nil {
return err
}
if t := mgr.getCurrentModelTimestamp(); !t.IsZero() {
req.Header.Set("If-Modified-Since", t.Format(http.TimeFormat))
}
ctx, cancel := context.WithTimeout(context.Background(), dumpTimeout)
defer cancel()
req = req.WithContext(ctx)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
if resp.StatusCode == http.StatusNotModified {
return nil
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf(resp.Status)
}
defer resp.Body.Close()
tmp, err := os.CreateTemp(mgr.dataDir, dumpFileName)
if err != nil {
return err
}
defer os.Remove(tmp.Name())
defer tmp.Close()
if _, err = io.Copy(tmp, resp.Body); err != nil {
return err
}
if err = tmp.Close(); err != nil {
return err
}
if t, err := time.Parse(http.TimeFormat, resp.Header.Get("last-modified")); err == nil {
_ = os.Chtimes(tmp.Name(), t, t)
}
m, err := loadFromFile(tmp.Name())
if err != nil {
return err
}
mgr.lock.Lock()
defer mgr.lock.Unlock()
if err = os.Rename(tmp.Name(), path.Join(mgr.dataDir, dumpFileName)); err != nil {
return err
}
mgr.model = m
return nil
}
func (mgr *Manager) getCurrentModelTimestamp() time.Time {
mgr.lock.Lock()
defer mgr.lock.Unlock()
if mgr.model == nil {
return time.Time{}
}
return mgr.model.timestamp
}
func loadFromFile(p string) (*Model, error) {
st, err := os.Stat(p)
if err != nil {
return nil, err
}
data, err := os.ReadFile(p)
if err != nil {
return nil, err
}
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
defer r.Close()
m := &Model{
timestamp: st.ModTime().UTC(),
}
if err = json.NewDecoder(r).Decode(m); err != nil {
return nil, err
}
return m, nil
}