-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdater.go
265 lines (244 loc) · 7.81 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
package updater
import (
"context"
"errors"
"fmt"
"os"
"regexp"
"strings"
"github.com/edgedelta/updater/api"
"github.com/edgedelta/updater/core"
"github.com/edgedelta/updater/k8s"
"github.com/edgedelta/updater/log"
"github.com/go-yaml/yaml"
"k8s.io/client-go/rest"
)
var (
confVarRe = regexp.MustCompile(`{{\s*([^{} ]+)\s*}}`)
contextualVariableTemplateFormat = `{{ index .Vars "%s" }}`
)
type Updater struct {
config *core.UpdaterConfig
apiCli core.VersioningServiceClient
k8sCliOpts []k8s.NewClientOpt
k8sCli *k8s.Client
}
type NewClientOpt func(*Updater)
func WithK8sConfig(config *rest.Config) NewClientOpt {
return func(u *Updater) {
u.k8sCliOpts = append(u.k8sCliOpts, k8s.WithConfig(config))
}
}
func WithConfig(config *core.UpdaterConfig) NewClientOpt {
return func(u *Updater) {
u.config = config
}
}
func NewUpdater(ctx context.Context, configPath string, opts ...NewClientOpt) (*Updater, error) {
u := &Updater{k8sCliOpts: make([]k8s.NewClientOpt, 0)}
for _, o := range opts {
o(u)
}
if u.config == nil {
u.config = &core.UpdaterConfig{}
b, err := os.ReadFile(configPath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(b, u.config); err != nil {
return nil, err
}
}
cl, err := k8s.NewClient(u.k8sCliOpts...)
if err != nil {
return nil, err
}
u.k8sCli = cl
if err := u.evaluateConfigVars(ctx); err != nil {
return nil, fmt.Errorf("updater.Updater.evaluateConfigVars: %v", err)
}
if err := u.validateEntities(); err != nil {
return nil, fmt.Errorf("updater.Updater.validateEntities: %v", err)
}
u.apiCli = api.NewClient(&u.config.API)
if u.config.API.MetadataEndpoint != nil {
u.config.Metadata, err = u.apiCli.GetMetadata()
if err != nil {
return nil, fmt.Errorf("failed to fetch metadata, err: %v", err)
}
}
if err := u.evaluateMetadataConfigVars(); err != nil {
return nil, fmt.Errorf("updater.Updater.evaluateMetadataConfigVars: %v", err)
}
return u, nil
}
func (u *Updater) APIClient() *api.Client {
return u.apiCli.(*api.Client)
}
func (u *Updater) LogCustomTags() map[string]string {
m := make(map[string]string)
if u.config.Log == nil {
return m
}
for k, v := range u.config.Log.CustomTags {
m[k] = v
}
return m
}
func (u *Updater) LogUploaderEnabled() bool {
return u.config.API.LogUpload != nil && u.config.API.LogUpload.Enabled
}
// validateEntities function validates the given entities through the rules:
// - Each entity ID is unique
func (u *Updater) validateEntities() error {
if len(u.config.Entities) == 0 {
return errors.New("no entity is defined, need at least 1")
}
ids := make(map[string]bool)
for _, e := range u.config.Entities {
if _, ok := ids[e.ID]; ok {
return fmt.Errorf("entity ID %s is used at least twice", e.ID)
}
ids[e.ID] = true
}
return nil
}
func (u *Updater) Run(ctx context.Context) error {
u.logRunningConfig()
errors := core.NewErrors()
for _, entity := range u.config.Entities {
res, err := u.apiCli.GetLatestApplicableTag(entity.ID, entity.ImageName)
if err != nil {
errors.Addf("failed to get latest applicable tag from API for entity with ID %s, err: %v", entity.ID, err)
continue
}
if res.Tag == "" {
log.Info("No applicable tag found for entity with ID %s", entity.ID)
continue
}
log.Info("Latest applicable tag from API: %+v", res)
for _, path := range entity.K8sPaths {
if err := u.k8sCli.SetResourceKeyValue(ctx, path, res.URL); err != nil {
errors.Addf("failed to set K8s resource spec key/value for entity with ID %s (path: %s, value: %s), err: %v", entity.ID, path, res.URL, err)
continue
}
}
}
return errors.ErrorOrNil()
}
func (u *Updater) evaluateConfigVars(ctx context.Context) (err error) {
for index, entity := range u.config.Entities {
if u.config.Entities[index].ID, err = u.evaluateConfigVar(ctx, entity.ID); err != nil {
return
}
}
if u.config.API.BaseURL, err = u.evaluateConfigVar(ctx, u.config.API.BaseURL); err != nil {
return
}
if u.config.API.TopLevelAuth != nil {
if u.config.API.TopLevelAuth.HeaderValue, err = u.evaluateConfigVar(ctx, u.config.API.TopLevelAuth.HeaderValue); err != nil {
return
}
}
if u.config.API.LatestTagEndpoint.Endpoint, err = u.evaluateConfigVar(ctx, u.config.API.LatestTagEndpoint.Endpoint); err != nil {
return
}
if u.config.API.LatestTagEndpoint.Params != nil {
for k, v := range u.config.API.LatestTagEndpoint.Params.QueryParams {
if u.config.API.LatestTagEndpoint.Params.QueryParams[k], err = u.evaluateConfigVar(ctx, v); err != nil {
return
}
}
}
if u.config.API.LogUpload.PresignedUploadURLEndpoint.Endpoint, err = u.evaluateConfigVar(ctx, u.config.API.LogUpload.PresignedUploadURLEndpoint.Endpoint); err != nil {
return
}
if u.config.API.LogUpload.PresignedUploadURLEndpoint.Params != nil {
for k, v := range u.config.API.LogUpload.PresignedUploadURLEndpoint.Params.QueryParams {
if u.config.API.LogUpload.PresignedUploadURLEndpoint.Params.QueryParams[k], err = u.evaluateConfigVar(ctx, v); err != nil {
return
}
}
}
if u.config.Log != nil {
for k, v := range u.config.Log.CustomTags {
if u.config.Log.CustomTags[k], err = u.evaluateConfigVar(ctx, v); err != nil {
return
}
}
}
return nil
}
func (u *Updater) evaluateConfigVar(ctx context.Context, val string) (string, error) {
var err error
return confVarRe.ReplaceAllStringFunc(val, func(s string) string {
inner := confVarRe.FindStringSubmatch(s)[1]
if strings.HasPrefix(inner, ".k8s.secrets.") {
path := inner[13:] // .k8s.secrets.<KEY>
elms := strings.Split(path, ".")
if len(elms) != 2 {
err = fmt.Errorf("path should have pattern: .k8s.<NAMESPACE>.<SECRET-NAME>, got '%s' instead", path)
return ""
}
namespace := elms[0]
name := elms[1]
var secret string
secret, err = u.k8sCli.GetSecret(ctx, namespace, name)
return secret
}
if strings.HasPrefix(inner, ".env.") {
key := inner[5:] // .env.<KEY>
return os.Getenv(key)
}
if strings.HasPrefix(inner, ".ctx.") {
key := inner[5:] // .ctx.<KEY>
// Replace the contextual variable's key to a Go template map index key to later
// use inside the related function(s).
return fmt.Sprintf(contextualVariableTemplateFormat, key)
}
return s // If unmatching, just return itself
}), err
}
func (u *Updater) evaluateMetadataConfigVars() (err error) {
if u.config.Log != nil {
for k, v := range u.config.Log.CustomTags {
if u.config.Log.CustomTags[k], err = u.evaluateMetadataConfigVar(v); err != nil {
return
}
}
}
return nil
}
func (u *Updater) evaluateMetadataConfigVar(val string) (string, error) {
var err error
return confVarRe.ReplaceAllStringFunc(val, func(s string) string {
inner := confVarRe.FindStringSubmatch(s)[1]
if strings.HasPrefix(inner, ".meta.") {
key := inner[6:] // .meta.<KEY>
v, ok := u.config.Metadata[key]
if !ok {
err = fmt.Errorf("metadata with key %q is not found", key)
return ""
}
return v
}
return s // If unmatching, just return itself
}), err
}
func (u *Updater) logRunningConfig() {
var sb strings.Builder
entities := make([]string, 0)
for _, e := range u.config.Entities {
entities = append(entities, fmt.Sprintf("%s:%s", e.ImageName, e.ID))
}
sb.WriteString(fmt.Sprintf("Updater is running for entities %s with API base URL: %s, latest tag endpoint: %s, log uploader is", strings.Join(entities, ", "), u.config.API.BaseURL, u.config.API.LatestTagEndpoint.Endpoint))
if u.LogUploaderEnabled() {
sb.WriteString(fmt.Sprintf(" enabled with presigned URL endpoint: %s, encoding: %s, and compression: %s.", u.config.API.LogUpload.PresignedUploadURLEndpoint.Endpoint, u.config.API.LogUpload.Encoding.Type, u.config.API.LogUpload.Compression))
} else {
sb.WriteString(" disabled.")
}
if u.config.API.MetadataEndpoint != nil {
sb.WriteString(fmt.Sprintf(" Updater metadata: %+v", u.config.Metadata))
}
log.Debug(sb.String())
}