/
config_snapshot.go
149 lines (130 loc) · 4.24 KB
/
config_snapshot.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
/*
Copyright 2022 The TestGrid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package snapshot
import (
"context"
"fmt"
"time"
"cloud.google.com/go/storage"
"github.com/GoogleCloudPlatform/testgrid/config"
configpb "github.com/GoogleCloudPlatform/testgrid/pb/config"
"github.com/GoogleCloudPlatform/testgrid/util/gcs"
"github.com/sethvargo/go-retry"
"github.com/sirupsen/logrus"
)
// Config is a mapped representation of the config at a particular point in time.
// Not concurrency-safe; meant for reading only
type Config struct {
DashboardGroups map[string]*configpb.DashboardGroup
Dashboards map[string]*configpb.Dashboard
Groups map[string]*configpb.TestGroup
Attrs storage.ReaderObjectAttrs
}
// Observe reads the config at configPath and return a ConfigSnapshot initially and, if a ticker is supplied, when the config changes
// Returns an error instead if there is no file at configPath
func Observe(ctx context.Context, log logrus.FieldLogger, client gcs.ConditionalClient, configPath gcs.Path, ticker <-chan time.Time) (<-chan *Config, error) {
ch := make(chan *Config)
if log == nil {
log = logrus.New()
}
log = log.WithField("observed-path", configPath.String())
initialSnap, err := updateHash(ctx, client, configPath)
if err != nil {
return nil, fmt.Errorf("can't read %q: %w", configPath.String(), err)
}
cond := storage.Conditions{
GenerationNotMatch: initialSnap.Attrs.Generation,
}
client = client.If(&cond, nil)
go func() {
defer close(ch)
ch <- initialSnap
if ticker == nil {
return
}
nextTick:
for {
select {
case <-ctx.Done():
return
case <-ticker:
snap, err := updateHash(ctx, client, configPath)
if err != nil {
if !gcs.IsPreconditionFailed(err) {
log.WithError(err).Warning("Error fetching updated config")
}
continue nextTick
}
// Configuration changed
select {
case <-ctx.Done():
return
case ch <- snap:
cond.GenerationNotMatch = snap.Attrs.Generation
}
}
}
}()
return ch, nil
}
func updateHash(ctx context.Context, client gcs.Opener, configPath gcs.Path) (*Config, error) {
var cs Config
cfg, attrs, err := fetchConfig(ctx, client, configPath)
if err != nil {
return nil, err
}
namedDashboardGroups := make(map[string]*configpb.DashboardGroup, len(cfg.DashboardGroups))
for _, dg := range cfg.DashboardGroups {
namedDashboardGroups[dg.Name] = dg
}
namedDashboards := make(map[string]*configpb.Dashboard, len(cfg.Dashboards))
for _, d := range cfg.Dashboards {
namedDashboards[d.Name] = d
}
namedGroups := make(map[string]*configpb.TestGroup, len(cfg.TestGroups))
for _, tg := range cfg.TestGroups {
namedGroups[tg.Name] = tg
}
cs.DashboardGroups = namedDashboardGroups
cs.Dashboards = namedDashboards
cs.Groups = namedGroups
if attrs != nil {
cs.Attrs = *attrs
}
return &cs, nil
}
func fetchConfig(ctx context.Context, client gcs.Opener, configPath gcs.Path) (*configpb.Configuration, *storage.ReaderObjectAttrs, error) {
backoff := retry.WithMaxRetries(2, retry.NewExponential(5*time.Second))
var cfg *configpb.Configuration
var attrs *storage.ReaderObjectAttrs
err := retry.Do(ctx, backoff, func(innerCtx context.Context) error {
var onceErr error
cfg, attrs, onceErr = fetchConfigOnce(innerCtx, client, configPath)
if onceErr != nil {
return retry.RetryableError(onceErr)
}
return nil
})
return cfg, attrs, err
}
func fetchConfigOnce(ctx context.Context, client gcs.Opener, configPath gcs.Path) (*configpb.Configuration, *storage.ReaderObjectAttrs, error) {
r, attrs, err := client.Open(ctx, configPath)
if err != nil {
return nil, nil, fmt.Errorf("open: %w", err)
}
cfg, err := config.Unmarshal(r)
if err != nil {
return nil, nil, fmt.Errorf("unmarshal: %v", err)
}
return cfg, attrs, nil
}