/
updater.go
395 lines (355 loc) · 11.5 KB
/
updater.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmrevisionupdater
import (
"context"
"strconv"
"strings"
"time"
"github.com/juju/charm/v12"
"github.com/juju/charm/v12/resource"
"github.com/juju/clock"
"github.com/juju/collections/set"
"github.com/juju/errors"
"github.com/juju/loggo"
apiservererrors "github.com/juju/juju/apiserver/errors"
"github.com/juju/juju/charmhub"
corebase "github.com/juju/juju/core/base"
charmmetrics "github.com/juju/juju/core/charm/metrics"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/version"
)
var logger = loggo.GetLogger("juju.apiserver.charmrevisionupdater")
// CharmRevisionUpdater defines the methods on the charmrevisionupdater API end point.
type CharmRevisionUpdater interface {
UpdateLatestRevisions() (params.ErrorResult, error)
}
// CharmRevisionUpdaterAPI implements the CharmRevisionUpdater interface and is the concrete
// implementation of the api end point.
type CharmRevisionUpdaterAPI struct {
state State
clock clock.Clock
newCharmhubClient newCharmhubClientFunc
}
type newCharmhubClientFunc func(st State) (CharmhubRefreshClient, error)
var _ CharmRevisionUpdater = (*CharmRevisionUpdaterAPI)(nil)
// NewCharmRevisionUpdaterAPIState creates a new charmrevisionupdater API
// with a State interface directly (mainly for use in tests).
func NewCharmRevisionUpdaterAPIState(
state State,
clock clock.Clock,
newCharmhubClient newCharmhubClientFunc,
) (*CharmRevisionUpdaterAPI, error) {
return &CharmRevisionUpdaterAPI{
state: state,
clock: clock,
newCharmhubClient: newCharmhubClient,
}, nil
}
// UpdateLatestRevisions retrieves the latest revision information from the charm store for all deployed charms
// and records this information in state.
func (api *CharmRevisionUpdaterAPI) UpdateLatestRevisions() (params.ErrorResult, error) {
if err := api.updateLatestRevisions(); err != nil {
return params.ErrorResult{Error: apiservererrors.ServerError(err)}, nil
}
return params.ErrorResult{}, nil
}
func (api *CharmRevisionUpdaterAPI) updateLatestRevisions() error {
// Look up the information for all the deployed charms. This is the
// "expensive" part.
latest, err := api.retrieveLatestCharmInfo()
if err != nil {
return errors.Trace(err)
}
// Process the resulting info for each charm.
resources := api.state.Resources()
for _, info := range latest {
// First, add a charm placeholder to the model for each.
if err := api.state.AddCharmPlaceholder(info.url); err != nil {
return errors.Trace(err)
}
// Then save the resources
err := resources.SetCharmStoreResources(info.appID, info.resources, info.timestamp)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
type latestCharmInfo struct {
url *charm.URL
timestamp time.Time
revision int
resources []resource.Resource
appID string
}
type appInfo struct {
id string
charmURL *charm.URL
}
// retrieveLatestCharmInfo looks up the charm store to return the charm URLs for the
// latest revision of the deployed charms.
func (api *CharmRevisionUpdaterAPI) retrieveLatestCharmInfo() ([]latestCharmInfo, error) {
applications, err := api.state.AllApplications()
if err != nil {
return nil, errors.Trace(err)
}
model, err := api.state.Model()
if err != nil {
return nil, errors.Trace(err)
}
cfg, err := model.Config()
if err != nil {
return nil, errors.Trace(err)
}
telemetry := cfg.Telemetry()
// If there are no applications, exit now, check telemetry for additional work.
if len(applications) == 0 {
if !telemetry {
return nil, nil
}
return nil, errors.Trace(api.sendEmptyModelMetrics())
}
var (
charmhubIDs []charmhubID
charmhubApps []appInfo
)
for _, application := range applications {
cURL, _ := application.CharmURL()
curl, err := charm.ParseURL(*cURL)
if err != nil {
return nil, errors.Trace(err)
}
switch {
case charm.Local.Matches(curl.Schema):
continue
case charm.CharmHub.Matches(curl.Schema):
origin := application.CharmOrigin()
if origin == nil {
// If this fails, we have big problems, so make this Errorf
logger.Errorf("charm %s has no origin, skipping", curl)
continue
}
if origin.ID == "" || origin.Revision == nil || origin.Channel == nil || origin.Platform == nil {
logger.Errorf("charm %s has missing id(%s), revision (%p), channel (%p), or platform (%p), skipping",
curl, origin.Revision, origin.Channel, origin.Platform)
continue
}
channel, err := charm.MakeChannel(origin.Channel.Track, origin.Channel.Risk, origin.Channel.Branch)
if err != nil {
return nil, errors.Trace(err)
}
series, err := corebase.GetSeriesFromChannel(origin.Platform.OS, origin.Platform.Channel)
if err != nil {
return nil, errors.Trace(err)
}
cid := charmhubID{
id: origin.ID,
revision: *origin.Revision,
channel: channel.String(),
os: strings.ToLower(origin.Platform.OS), // charmhub API requires lowercase OS key
series: series,
arch: origin.Platform.Architecture,
instanceKey: charmhub.CreateInstanceKey(application.ApplicationTag(), model.ModelTag()),
}
if telemetry {
cid.metrics = map[charmmetrics.MetricKey]string{
charmmetrics.NumUnits: strconv.Itoa(application.UnitCount()),
}
}
charmhubIDs = append(charmhubIDs, cid)
charmhubApps = append(charmhubApps, appInfo{
id: application.ApplicationTag().Id(),
charmURL: curl,
})
default:
return nil, errors.NotValidf("charm schema %q", curl.Schema)
}
}
var (
charmhubErr error
latest []latestCharmInfo
)
if len(charmhubIDs) > 0 {
if telemetry {
charmhubIDs, err = api.addMetricsToCharmhubInfos(charmhubIDs, charmhubApps)
if err != nil {
// It's fine to error out here, as this is a state backed
// request and should be transitive.
return nil, errors.Trace(err)
}
}
hubLatest, err := api.fetchCharmhubInfos(cfg, charmhubIDs, charmhubApps)
if err != nil {
charmhubErr = err
} else {
latest = append(latest, hubLatest...)
}
}
if charmhubErr != nil {
return nil, errors.Errorf("charmhub: %v", charmhubErr)
}
return latest, nil
}
func (api *CharmRevisionUpdaterAPI) addMetricsToCharmhubInfos(ids []charmhubID, appInfos []appInfo) ([]charmhubID, error) {
relationKeys := api.state.AliveRelationKeys()
if len(relationKeys) == 0 {
return ids, nil
}
for k, v := range convertRelations(appInfos, relationKeys) {
for i, app := range appInfos {
if app.id != k {
continue
}
if ids[i].metrics == nil {
ids[i].metrics = map[charmmetrics.MetricKey]string{}
}
ids[i].metrics[charmmetrics.Relations] = strings.Join(v, ",")
}
}
return ids, nil
}
// sendEmptyModelMetrics sends the controller and model metrics
// for an empty model. This is highly likely for juju 2.9. A
// controller charm was introduced for juju 3.0. Allows us to get
// the controller version etc.
func (api *CharmRevisionUpdaterAPI) sendEmptyModelMetrics() error {
requestMetrics, err := charmhubRequestMetadata(api.state)
if err != nil {
return errors.Trace(err)
}
client, err := api.newCharmhubClient(api.state)
if err != nil {
return errors.Trace(err)
}
ctx, cancel := context.WithTimeout(context.TODO(), charmhub.RefreshTimeout)
defer cancel()
return errors.Trace(client.RefreshWithMetricsOnly(ctx, requestMetrics))
}
// convertRelations converts the list of relations by application name to charm name
// for the metrics response.
func convertRelations(appInfos []appInfo, relationKeys []string) map[string][]string {
// Map application names to its charm name.
appToCharm := make(map[string]string)
for _, v := range appInfos {
if v.charmURL != nil {
appToCharm[v.id] = v.charmURL.Name
}
}
// relationsByAppName is a map of relation providers, by application name,
// to a slice of requirers, by application name.
relationsByAppName := make(map[string][]string)
for _, key := range relationKeys {
one, two, use := relationApplicationNames(key)
if !use {
continue
}
values, _ := relationsByAppName[one]
values = append(values, two)
relationsByAppName[one] = values
rValues, _ := relationsByAppName[two]
rValues = append(rValues, one)
relationsByAppName[two] = rValues
}
// Put them together to create a map of relations, by
// application name, to a slice of relations, by charm name.
relations := make(map[string][]string)
for appName, appNameRels := range relationsByAppName {
// It is possible the same charm is deployed more than once with
// different names.
c := relations[appName]
relatedTo := set.NewStrings(c...)
// Using a set, also ensures there are no duplicates.
for _, v := range appNameRels {
relatedTo.Add(appToCharm[v])
}
relations[appName] = relatedTo.SortedValues()
}
return relations
}
// relationApplicationNames returns the application names in the relation.
// Peer relations are filtered out, not needed for metrics.
// Keys are in the format: "appName:endpoint appName:endpoint"
func relationApplicationNames(str string) (string, string, bool) {
endpoints := strings.Split(str, " ")
if len(endpoints) != 2 {
return "", "", false
}
one := strings.Split(endpoints[0], ":")
if len(one) != 2 {
return "", "", false
}
two := strings.Split(endpoints[1], ":")
if len(two) != 2 {
return "", "", false
}
return one[0], two[0], true
}
func (api *CharmRevisionUpdaterAPI) fetchCharmhubInfos(cfg *config.Config, ids []charmhubID, appInfos []appInfo) ([]latestCharmInfo, error) {
var requestMetrics map[charmmetrics.MetricKey]map[charmmetrics.MetricKey]string
if cfg.Telemetry() {
var err error
requestMetrics, err = charmhubRequestMetadata(api.state)
if err != nil {
return nil, errors.Trace(err)
}
}
client, err := api.newCharmhubClient(api.state)
if err != nil {
return nil, errors.Trace(err)
}
results, err := charmhubLatestCharmInfo(client, requestMetrics, ids, api.clock.Now().UTC())
if err != nil {
return nil, errors.Trace(err)
}
var latest []latestCharmInfo
for i, result := range results {
if i >= len(appInfos) {
logger.Errorf("retrieved more results (%d) than charmhub applications (%d)",
i, len(appInfos))
break
}
if result.error != nil {
logger.Errorf("retrieving charm info for ID %s: %v", ids[i].id, result.error)
continue
}
appInfo := appInfos[i]
latest = append(latest, latestCharmInfo{
url: appInfo.charmURL.WithRevision(result.revision),
timestamp: result.timestamp,
revision: result.revision,
resources: result.resources,
appID: appInfo.id,
})
}
return latest, nil
}
// charmhubRequestMetadata returns a map containing metadata key/value pairs to
// send to the charmhub for tracking metrics.
func charmhubRequestMetadata(st State) (map[charmmetrics.MetricKey]map[charmmetrics.MetricKey]string, error) {
model, err := st.Model()
if err != nil {
return nil, errors.Trace(err)
}
metrics, err := model.Metrics()
if err != nil {
return nil, errors.Trace(err)
}
metadata := map[charmmetrics.MetricKey]map[charmmetrics.MetricKey]string{
charmmetrics.Controller: {
charmmetrics.JujuVersion: version.Current.String(),
charmmetrics.UUID: metrics.ControllerUUID,
},
charmmetrics.Model: {
charmmetrics.Cloud: metrics.CloudName,
charmmetrics.UUID: metrics.UUID,
charmmetrics.NumApplications: metrics.ApplicationCount,
charmmetrics.NumMachines: metrics.MachineCount,
charmmetrics.NumUnits: metrics.UnitCount,
charmmetrics.Provider: metrics.Provider,
charmmetrics.Region: metrics.CloudRegion,
},
}
return metadata, nil
}