-
Notifications
You must be signed in to change notification settings - Fork 143
/
detector.go
340 lines (295 loc) · 9.92 KB
/
detector.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
// Copyright 2024 The PipeCD 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 cloudrun
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/app/piped/livestatestore/cloudrun"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/cloudrun"
"github.com/pipe-cd/pipecd/pkg/app/piped/sourceprocesser"
"github.com/pipe-cd/pipecd/pkg/cache"
"github.com/pipe-cd/pipecd/pkg/config"
"github.com/pipe-cd/pipecd/pkg/diff"
"github.com/pipe-cd/pipecd/pkg/git"
"github.com/pipe-cd/pipecd/pkg/model"
)
type applicationLister interface {
ListByPlatformProvider(name string) []*model.Application
}
type gitClient interface {
Clone(ctx context.Context, repoID, remote, branch, destination string) (git.Repo, error)
}
type secretDecrypter interface {
Decrypt(string) (string, error)
}
type reporter interface {
ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error
}
type Detector interface {
Run(ctx context.Context) error
ProviderName() string
}
type detector struct {
provider config.PipedPlatformProvider
appLister applicationLister
gitClient gitClient
stateGetter cloudrun.Getter
reporter reporter
appManifestsCache cache.Cache
interval time.Duration
config *config.PipedSpec
secretDecrypter secretDecrypter
logger *zap.Logger
gitRepos map[string]git.Repo
}
func NewDetector(
cp config.PipedPlatformProvider,
appLister applicationLister,
gitClient gitClient,
stateGetter cloudrun.Getter,
reporter reporter,
appManifestsCache cache.Cache,
cfg *config.PipedSpec,
sd secretDecrypter,
logger *zap.Logger,
) Detector {
logger = logger.Named("cloudrun-detector").With(
zap.String("platform-provider", cp.Name),
)
return &detector{
provider: cp,
appLister: appLister,
gitClient: gitClient,
stateGetter: stateGetter,
reporter: reporter,
appManifestsCache: appManifestsCache,
interval: time.Minute,
config: cfg,
secretDecrypter: sd,
gitRepos: make(map[string]git.Repo),
logger: logger,
}
}
func (d *detector) Run(ctx context.Context) error {
d.logger.Info("start running drift detector for cloudrun applications")
ticker := time.NewTicker(d.interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
d.logger.Info("drift detector for cloudrun applications has been stopped")
return nil
case <-ticker.C:
d.check(ctx)
}
}
}
func (d *detector) ProviderName() string {
return d.provider.Name
}
func (d *detector) check(ctx context.Context) {
appsByRepo := d.listGroupedApplication()
for repoID, apps := range appsByRepo {
gitRepo, ok := d.gitRepos[repoID]
if !ok {
// Clone repository for the first time.
gr, err := d.cloneGitRepository(ctx, repoID)
if err != nil {
d.logger.Error("failed to clone git repository",
zap.String("repo-id", repoID),
zap.Error(err),
)
continue
}
gitRepo = gr
d.gitRepos[repoID] = gitRepo
}
// Fetch the latest commit to compare the states.
branch := gitRepo.GetClonedBranch()
if err := gitRepo.Pull(ctx, branch); err != nil {
d.logger.Error("failed to pull repository branch",
zap.String("repo-id", repoID),
zap.Error(err),
)
continue
}
// Get the head commit of the repository.
headCommit, err := gitRepo.GetLatestCommit(ctx)
if err != nil {
d.logger.Error("failed to get head commit hash",
zap.String("repo-id", repoID),
zap.Error(err),
)
continue
}
// Start checking all applications in this repository.
for _, app := range apps {
if err := d.checkApplication(ctx, app, gitRepo, headCommit); err != nil {
d.logger.Error(fmt.Sprintf("failed to check application: %s", app.Id), zap.Error(err))
}
}
}
}
func (d *detector) cloneGitRepository(ctx context.Context, repoID string) (git.Repo, error) {
repoCfg, ok := d.config.GetRepository(repoID)
if !ok {
return nil, fmt.Errorf("repository %s was not found in piped configuration", repoID)
}
return d.gitClient.Clone(ctx, repoID, repoCfg.Remote, repoCfg.Branch, "")
}
// listGroupedApplication retrieves all applications those should be handled by this director
// and then groups them by repoID.
func (d *detector) listGroupedApplication() map[string][]*model.Application {
var (
apps = d.appLister.ListByPlatformProvider(d.provider.Name)
m = make(map[string][]*model.Application)
)
for _, app := range apps {
repoID := app.GitPath.Repo.Id
m[repoID] = append(m[repoID], app)
}
return m
}
func (d *detector) checkApplication(ctx context.Context, app *model.Application, repo git.Repo, headCommit git.Commit) error {
headManifest, err := d.loadHeadServiceManifest(app, repo, headCommit)
if err != nil {
return err
}
d.logger.Info(fmt.Sprintf("application %s has a service manifest at commit %s", app.Id, headCommit.Hash))
liveManifest, ok := d.stateGetter.GetServiceManifest(app.Id)
if !ok {
return fmt.Errorf("failed to get live service manifest")
}
d.logger.Info(fmt.Sprintf("application %s has a live service manifest", app.Id))
result, err := provider.Diff(
liveManifest,
headManifest,
diff.WithEquateEmpty(),
diff.WithIgnoreAddingMapKeys(),
diff.WithCompareNumberAndNumericString(),
)
if err != nil {
return err
}
state := makeSyncState(result, headCommit.Hash)
return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
}
func (d *detector) loadHeadServiceManifest(app *model.Application, repo git.Repo, headCommit git.Commit) (provider.ServiceManifest, error) {
var (
manifestCache = provider.ServiceManifestCache{
AppID: app.Id,
Cache: d.appManifestsCache,
Logger: d.logger,
}
repoDir = repo.GetPath()
appDir = filepath.Join(repoDir, app.GitPath.Path)
)
manifest, ok := manifestCache.Get(headCommit.Hash)
if !ok {
// When the manifests were not in the cache we have to load them.
cfg, err := d.loadApplicationConfiguration(repoDir, app)
if err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to load application configuration: %w", err)
}
gds, ok := cfg.GetGenericApplication()
if !ok {
return provider.ServiceManifest{}, fmt.Errorf("unsupport application kind %s", cfg.Kind)
}
var (
encryptionUsed = d.secretDecrypter != nil && gds.Encryption != nil
attachmentUsed = gds.Attachment != nil
)
// We have to copy repository into another directory because
// decrypting the sealed secrets or attaching files might change the git repository.
if attachmentUsed || encryptionUsed {
dir, err := os.MkdirTemp("", "detector-git-processing")
if err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to prepare a temporary directory for git repository (%w)", err)
}
defer os.RemoveAll(dir)
repo, err = repo.Copy(filepath.Join(dir, "repo"))
if err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to copy the cloned git repository (%w)", err)
}
repoDir := repo.GetPath()
appDir = filepath.Join(repoDir, app.GitPath.Path)
}
// Decrypting secrets to manifests.
if encryptionUsed {
if err := sourceprocesser.DecryptSecrets(appDir, *gds.Encryption, d.secretDecrypter); err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to decrypt secrets (%w)", err)
}
}
// Then attaching configurated files to manifests.
if attachmentUsed {
if err := sourceprocesser.AttachData(appDir, *gds.Attachment); err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to attach files (%w)", err)
}
}
var manifestFile string
if cfg.CloudRunApplicationSpec != nil {
manifestFile = cfg.CloudRunApplicationSpec.Input.ServiceManifestFile
}
manifest, err = provider.LoadServiceManifest(appDir, manifestFile)
if err != nil {
return provider.ServiceManifest{}, fmt.Errorf("failed to load new service manifest: %w", err)
}
manifestCache.Put(headCommit.Hash, manifest)
}
return manifest, nil
}
func (d *detector) loadApplicationConfiguration(repoPath string, app *model.Application) (*config.Config, error) {
path := filepath.Join(repoPath, app.GitPath.GetApplicationConfigFilePath())
cfg, err := config.LoadFromYAML(path)
if err != nil {
return nil, err
}
if appKind, ok := cfg.Kind.ToApplicationKind(); !ok || appKind != app.Kind {
return nil, fmt.Errorf("application in application configuration file is not match, got: %s, expected: %s", appKind, app.Kind)
}
return cfg, nil
}
func makeSyncState(r *provider.DiffResult, commit string) model.ApplicationSyncState {
if r.NoChange() {
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
Timestamp: time.Now().Unix(),
}
}
shortReason := "The service manifest doesn't be synced"
if len(commit) >= 7 {
commit = commit[:7]
}
var b strings.Builder
b.WriteString(fmt.Sprintf("Diff between the defined state in Git at commit %s and actual live state:\n\n", commit))
b.WriteString("--- Actual (LiveState)\n+++ Expected (Git)\n\n")
details := r.Render(provider.DiffRenderOptions{
// Currently, we do not use the diff command to render the result
// because CloudRun adds a large number of default values to the
// running manifest that causes a wrong diff text.
UseDiffCommand: false,
})
b.WriteString(details)
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Timestamp: time.Now().Unix(),
}
}