forked from akuity/kargo-render
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.go
419 lines (379 loc) · 12 KB
/
service.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
package bookkeeper
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/pkg/errors"
uuid "github.com/satori/go.uuid"
log "github.com/sirupsen/logrus"
"github.com/akuity/bookkeeper/internal/kustomize"
"github.com/akuity/bookkeeper/internal/manifests"
"github.com/akuity/bookkeeper/pkg/git"
)
type ServiceOptions struct {
LogLevel LogLevel
}
// Service is an interface for components that can handle bookkeeping requests.
// Implementations of this interface are transport-agnostic.
type Service interface {
// RenderManifests handles a bookkeeping request.
RenderManifests(context.Context, RenderRequest) (RenderResponse, error)
}
type service struct {
logger *log.Logger
}
// NewService returns an implementation of the Service interface for
// handling bookkeeping requests.
func NewService(opts *ServiceOptions) Service {
if opts == nil {
opts = &ServiceOptions{}
}
if opts.LogLevel == 0 {
opts.LogLevel = LogLevelInfo
}
logger := log.New()
logger.SetLevel(log.Level(opts.LogLevel))
return &service{
logger: logger,
}
}
// nolint: gocyclo
func (s *service) RenderManifests(
ctx context.Context,
req RenderRequest,
) (RenderResponse, error) {
req.id = uuid.NewV4().String()
logger := s.logger.WithField("request", req.id)
startEndLogger := logger.WithFields(log.Fields{
"repo": req.RepoURL,
"targetBranch": req.TargetBranch,
})
startEndLogger.Debug("handling rendering request")
res := RenderResponse{}
var err error
if req, err = validateAndCanonicalizeRequest(req); err != nil {
return res, err
}
startEndLogger.Debug("validated rendering request")
rc := renderRequestContext{
logger: logger,
request: req,
}
if rc.repo, err = git.Clone(
rc.request.RepoURL,
git.RepoCredentials{
SSHPrivateKey: rc.request.RepoCreds.SSHPrivateKey,
Username: rc.request.RepoCreds.Username,
Password: rc.request.RepoCreds.Password,
},
); err != nil {
return res, errors.Wrap(err, "error cloning remote repository")
}
defer rc.repo.Close()
// TODO: Add some logging to this block
if rc.request.Ref == "" {
if rc.source.commit, err = rc.repo.LastCommitID(); err != nil {
return res, errors.Wrap(err, "error getting last commit ID")
}
} else {
if err = rc.repo.Checkout(rc.request.Ref); err != nil {
return res, errors.Wrapf(err, "error checking out %q", rc.request.Ref)
}
if rc.intermediate.branchMetadata, err =
loadBranchMetadata(rc.repo.WorkingDir()); err != nil {
return res, errors.Wrap(err, "error loading branch metadata")
}
if rc.intermediate.branchMetadata == nil {
// We're not on a target branch. We're sitting on the source commit.
if rc.source.commit, err = rc.repo.LastCommitID(); err != nil {
return res, errors.Wrap(err, "error getting last commit ID")
}
} else {
// Follow the branch metadata back to the real source commit
if err = rc.repo.Checkout(
rc.intermediate.branchMetadata.SourceCommit,
); err != nil {
return res, errors.Wrapf(
err,
"error checking out %q",
rc.intermediate.branchMetadata.SourceCommit,
)
}
rc.source.commit = rc.intermediate.branchMetadata.SourceCommit
}
}
repoConfig, err := loadRepoConfig(rc.repo.WorkingDir())
if err != nil {
return res,
errors.Wrap(err, "error loading Bookkeeper configuration from repo")
}
if rc.target.branchConfig, err =
repoConfig.GetBranchConfig(rc.request.TargetBranch); err != nil {
return res, errors.Wrapf(
err,
"error loading configuration for branch %q",
rc.request.TargetBranch,
)
}
if len(rc.target.branchConfig.AppConfigs) == 0 {
rc.target.branchConfig.AppConfigs = map[string]appConfig{
"app": {
ConfigManagement: configManagementConfig{
Kustomize: &kustomize.Config{
Path: rc.request.TargetBranch,
},
},
},
}
}
if rc.target.prerenderedManifests, err = preRender(ctx, rc); err != nil {
return res, errors.Wrap(err, "error pre-rendering manifests")
}
if err = switchToTargetBranch(rc); err != nil {
return res, errors.Wrap(err, "error switching to target branch")
}
oldTargetBranchMetadata, err := loadBranchMetadata(rc.repo.WorkingDir())
if err != nil {
return res, errors.Wrap(err, "error loading branch metadata")
}
rc.target.oldBranchMetadata = *oldTargetBranchMetadata
if rc.target.commit.branch, err = switchToCommitBranch(rc); err != nil {
return res, errors.Wrap(err, "error switching to commit branch")
}
if rc.target.commit.branch != rc.request.TargetBranch {
// The commit branch isn't the target branch and we should take into account
// any metadata that already exists in the commit branch, in case that
// branch already existed.
if rc.target.commit.oldBranchMetadata, err =
loadBranchMetadata(rc.repo.WorkingDir()); err != nil {
return res, errors.Wrap(err, "error loading branch metadata")
}
}
rc.target.newBranchMetadata.SourceCommit = rc.source.commit
if rc.target.newBranchMetadata.ImageSubstitutions,
rc.target.renderedManifests,
err =
renderLastMile(ctx, rc); err != nil {
return res, errors.Wrap(err, "error in last-mile manifest rendering")
}
// Write branch metadata
if err = writeBranchMetadata(
rc.target.newBranchMetadata,
rc.repo.WorkingDir(),
); err != nil {
return res, errors.Wrap(err, "error writing branch metadata")
}
logger.WithField("sourceCommit", rc.source.commit).
Debug("wrote branch metadata")
// Write the new fully-rendered manifests to the root of the repo
if err = writeAllManifests(rc); err != nil {
return res, err
}
logger.Debug("wrote all manifests")
// Before committing, check if we actually have any diffs from the head of
// this branch that are NOT just Bookkeeper metadata. We'd have an error if we
// tried to commit with no diffs!
diffPaths, err := rc.repo.GetDiffPaths()
if err != nil {
return res, errors.Wrap(err, "error checking for diffs")
}
if len(diffPaths) == 0 ||
(len(diffPaths) == 1 && diffPaths[0] == ".bookkeeper/metadata.yaml") {
logger.WithField("commitBranch", rc.target.commit.branch).Debug(
"manifests do not differ from the head of the " +
"commit branch; no further action is required",
)
res.ActionTaken = ActionTakenNone
res.CommitID, err = rc.repo.LastCommitID()
return res, errors.Wrap(
err,
"error getting last commit ID from the commit branch",
)
}
if rc.target.commit.message, err = buildCommitMessage(rc); err != nil {
return res, err
}
logger.Debug("prepared commit message")
// Commit the changes
if err = rc.repo.AddAllAndCommit(rc.target.commit.message); err != nil {
return res, errors.Wrapf(err, "error committing manifests")
}
if rc.target.commit.id, err = rc.repo.LastCommitID(); err != nil {
return res, errors.Wrap(
err,
"error getting last commit ID from the commit branch",
)
}
logger.WithFields(log.Fields{
"commitBranch": rc.target.commit.branch,
"commitID": rc.target.commit.id,
}).Debug("committed all changes")
// Push the commit branch to the remote
if err = rc.repo.Push(); err != nil {
return res, errors.Wrap(
err,
"error pushing commit branch to remote",
)
}
logger.WithField("commitBranch", rc.target.commit.branch).
Debug("pushed commit branch to remote")
// Open a PR if requested
if rc.target.branchConfig.PRs.Enabled {
if res.PullRequestURL, err = openPR(ctx, rc); err != nil {
return res,
errors.Wrap(err, "error opening pull request to the target branch")
}
if res.PullRequestURL == "" {
res.ActionTaken = ActionTakenUpdatedPR
logger.Debug("updated existing PR")
} else {
res.ActionTaken = ActionTakenOpenedPR
logger.WithField("prURL", res.PullRequestURL).Debug("opened PR")
}
} else {
res.ActionTaken = ActionTakenPushedDirectly
res.CommitID = rc.target.commit.id
}
startEndLogger.Debug("completed rendering request")
return res, nil
}
// buildCommitMessage builds a commit message for rendered manifests being
// written to a target branch by using the source commit's own commit message
// as a starting point. The message is then augmented with details about where
// Bookkeeper rendered it from (the source commit) and any image substitutions
// Bookkeeper made per the RenderRequest.
func buildCommitMessage(rc renderRequestContext) (string, error) {
var commitMsg string
if rc.request.CommitMessage != "" {
commitMsg = rc.request.CommitMessage
} else {
// Use the source commit's message as a starting point
var err error
if commitMsg, err = rc.repo.CommitMessage(rc.source.commit); err != nil {
return "", errors.Wrapf(
err,
"error getting commit message for commit %q",
rc.source.commit,
)
}
}
// Add the source commit's ID
formattedCommitMsg := fmt.Sprintf(
"%s\n\nBookkeeper created this commit by rendering manifests from %s",
commitMsg,
rc.source.commit,
)
// TODO: Tentatively removing the following because it simply results in too
// much noise in the repo history. Leaving it commented for now in case we
// decide to bring it back later.
//
// // Find all recent commits
// if rc.target.oldBranchMetadata.SourceCommit != "" {
// var memberCommitMsgs []string
// // Add info about member commits
// formattedCommitMsg = fmt.Sprintf(
// "%s\n\nThis includes the following changes (newest to oldest):",
// formattedCommitMsg,
// )
// var err error
// if memberCommitMsgs, err = rc.repo.CommitMessages(
// rc.target.oldBranchMetadata.SourceCommit,
// rc.source.commit,
// ); err != nil {
// return "", errors.Wrapf(
// err,
// "error getting commit messages between commit %q and %q",
// rc.target.oldBranchMetadata.SourceCommit,
// rc.source.commit,
// )
// }
// for _, memberCommitMsg := range memberCommitMsgs {
// formattedCommitMsg = fmt.Sprintf(
// "%s\n * %s",
// formattedCommitMsg,
// memberCommitMsg,
// )
// }
// }
if len(rc.target.newBranchMetadata.ImageSubstitutions) != 0 {
formattedCommitMsg = fmt.Sprintf(
"%s\n\nBookkeeper also incorporated the following images into this "+
"commit:\n",
formattedCommitMsg,
)
for _, image := range rc.target.newBranchMetadata.ImageSubstitutions {
formattedCommitMsg = fmt.Sprintf(
"%s\n * %s",
formattedCommitMsg,
image,
)
}
}
return formattedCommitMsg, nil
}
func writeAllManifests(rc renderRequestContext) error {
for appName, appConfig := range rc.target.branchConfig.AppConfigs {
appLogger := rc.logger.WithField("app", appName)
var outputDir string
if appConfig.OutputPath != "" {
outputDir = filepath.Join(rc.repo.WorkingDir(), appConfig.OutputPath)
} else {
outputDir = filepath.Join(rc.repo.WorkingDir(), appName)
}
var err error
if appConfig.CombineManifests {
appLogger.Debug("manifests will be combined into a single file")
err =
writeCombinedManifests(outputDir, rc.target.renderedManifests[appName])
} else {
appLogger.Debug("manifests will NOT be combined into a single file")
err = writeManifests(outputDir, rc.target.renderedManifests[appName])
}
appLogger.Debug("wrote manifests")
if err != nil {
return errors.Wrapf(
err, "error writing manifests for app %q to %q",
appName,
outputDir,
)
}
}
return nil
}
func writeManifests(dir string, yamlBytes []byte) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrapf(err, "error creating directory %q", dir)
}
manifestsByResourceTypeAndName, err := manifests.SplitYAML(yamlBytes)
if err != nil {
return err
}
for resourceTypeAndName, manifest := range manifestsByResourceTypeAndName {
fileName := filepath.Join(
dir,
fmt.Sprintf("%s.yaml", resourceTypeAndName),
)
// nolint: gosec
if err := os.WriteFile(fileName, manifest, 0644); err != nil {
return errors.Wrapf(
err,
"error writing manifest to %q",
fileName,
)
}
}
return nil
}
func writeCombinedManifests(dir string, manifests []byte) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrapf(err, "error creating directory %q", dir)
}
fileName := filepath.Join(dir, "all.yaml")
return errors.Wrapf(
// nolint: gosec
os.WriteFile(fileName, manifests, 0644),
"error writing manifests to %q",
fileName,
)
}