-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathdetector.go
459 lines (405 loc) · 13.5 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
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
// Copyright 2020 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 kubernetes
import (
"context"
"fmt"
"path/filepath"
"sort"
"strings"
"time"
"go.uber.org/zap"
provider "github.com/pipe-cd/pipe/pkg/app/piped/cloudprovider/kubernetes"
"github.com/pipe-cd/pipe/pkg/app/piped/diff"
"github.com/pipe-cd/pipe/pkg/app/piped/livestatestore/kubernetes"
"github.com/pipe-cd/pipe/pkg/cache"
"github.com/pipe-cd/pipe/pkg/config"
"github.com/pipe-cd/pipe/pkg/git"
"github.com/pipe-cd/pipe/pkg/model"
)
type applicationLister interface {
ListByCloudProvider(name string) []*model.Application
}
type deploymentLister interface {
ListAppHeadDeployments() map[string]*model.Deployment
}
type gitClient interface {
Clone(ctx context.Context, repoID, remote, branch, destination string) (git.Repo, error)
}
type reporter interface {
ReportApplicationSyncState(ctx context.Context, appID string, state model.ApplicationSyncState) error
}
type detector struct {
provider config.PipedCloudProvider
appLister applicationLister
deploymentLister deploymentLister
gitClient gitClient
stateGetter kubernetes.Getter
reporter reporter
appManifestsCache cache.Cache
interval time.Duration
config *config.PipedSpec
logger *zap.Logger
gitRepos map[string]git.Repo
syncStates map[string]model.ApplicationSyncState
}
func NewDetector(
cp config.PipedCloudProvider,
appLister applicationLister,
deploymentLister deploymentLister,
gitClient gitClient,
stateGetter kubernetes.Getter,
reporter reporter,
appManifestsCache cache.Cache,
cfg *config.PipedSpec,
logger *zap.Logger,
) *detector {
logger = logger.Named("kubernetes-detector").With(
zap.String("cloud-provider", cp.Name),
)
return &detector{
provider: cp,
appLister: appLister,
deploymentLister: deploymentLister,
gitClient: gitClient,
stateGetter: stateGetter,
reporter: reporter,
appManifestsCache: appManifestsCache,
interval: time.Minute,
config: cfg,
gitRepos: make(map[string]git.Repo),
syncStates: make(map[string]model.ApplicationSyncState),
logger: logger,
}
}
func (d *detector) Run(ctx context.Context) error {
d.logger.Info("start running drift detector for kubernetes applications")
ticker := time.NewTicker(d.interval)
defer ticker.Stop()
L:
for {
select {
case <-ticker.C:
d.check(ctx)
case <-ctx.Done():
break L
}
}
d.logger.Info("drift detector for kubernetes applications has been stopped")
return nil
}
func (d *detector) check(ctx context.Context) error {
var (
err error
applications = d.listApplications()
headDeployments = d.deploymentLister.ListAppHeadDeployments()
)
for repoID, apps := range applications {
var notDeployingApps []*model.Application
// Firstly, handle all deploying applications
// and remove them from the list.
for _, app := range apps {
headDeployment, ok := headDeployments[app.Id]
if !ok {
notDeployingApps = append(notDeployingApps, app)
continue
}
state := makeDeployingState(headDeployment)
if err := d.reporter.ReportApplicationSyncState(ctx, app.Id, state); err != nil {
d.logger.Error("failed to report application sync state", zap.Error(err))
}
}
if len(notDeployingApps) == 0 {
continue
}
// Next, we have to clone the lastest commit of repository
// to compare the states.
gitRepo, ok := d.gitRepos[repoID]
if !ok {
// Clone repository for the first time.
repoCfg, ok := d.config.GetRepository(repoID)
if !ok {
d.logger.Error(fmt.Sprintf("repository %s was not found in piped configuration", repoID))
continue
}
gitRepo, err = d.gitClient.Clone(ctx, repoID, repoCfg.Remote, repoCfg.Branch, "")
if err != nil {
d.logger.Error("failed to clone repository",
zap.String("repo-id", repoID),
zap.Error(err),
)
continue
}
d.gitRepos[repoID] = gitRepo
}
// Fetch to update the repository.
branch := gitRepo.GetClonedBranch()
if err := gitRepo.Pull(ctx, branch); err != nil {
d.logger.Error("failed to update 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
}
for _, app := range notDeployingApps {
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))
}
}
}
return nil
}
func (d *detector) checkApplication(ctx context.Context, app *model.Application, repo git.Repo, headCommit git.Commit) error {
watchingResourceKinds := d.stateGetter.GetWatchingResourceKinds()
headManifests, err := d.loadHeadManifests(ctx, app, repo, headCommit, watchingResourceKinds)
if err != nil {
return err
}
headManifests = filterIgnoringManifests(headManifests)
d.logger.Info(fmt.Sprintf("application %s has %d manifests at commit %s", app.Id, len(headManifests), headCommit.Hash))
liveManifests := d.stateGetter.GetAppLiveManifests(app.Id)
liveManifests = filterIgnoringManifests(liveManifests)
d.logger.Info(fmt.Sprintf("application %s has %d live manifests", app.Id, len(liveManifests)))
// Divide manifests into separate groups.
adds, deletes, headInters, liveInters := groupManifests(headManifests, liveManifests)
// Now we will go to check the diff intersection group.
changes := make(map[provider.Manifest]*diff.Result)
for i := 0; i < len(headInters); i++ {
result, err := provider.Diff(headInters[i], liveInters[i], diff.WithIgnoreAddingMapKeys())
if err != nil {
d.logger.Error("failed to calculate the diff of manifests", zap.Error(err))
return err
}
if !result.HasDiff() {
continue
}
changes[headInters[i]] = result
}
// No diffs means this application is in SYNCED state.
if len(adds) == 0 && len(deletes) == 0 && len(changes) == 0 {
state := makeSyncedState()
return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
}
state := makeOutOfSyncState(adds, deletes, changes, headCommit.Hash)
return d.reporter.ReportApplicationSyncState(ctx, app.Id, state)
}
func (d *detector) loadHeadManifests(ctx context.Context, app *model.Application, repo git.Repo, headCommit git.Commit, watchingResourceKinds []provider.APIVersionKind) ([]provider.Manifest, error) {
var (
manifestCache = provider.AppManifestsCache{
AppID: app.Id,
Cache: d.appManifestsCache,
Logger: d.logger,
}
repoDir = repo.GetPath()
appDir = filepath.Join(repoDir, app.GitPath.Path)
)
manifests, ok := manifestCache.Get(headCommit.Hash)
if !ok {
// When the manifests were not in the cache we have to load them.
cfg, err := d.loadDeploymentConfiguration(repoDir, app)
if err != nil {
err = fmt.Errorf("failed to load deployment configuration: %w", err)
return nil, err
}
loader := provider.NewManifestLoader(app.Name, appDir, repoDir, app.GitPath.ConfigFilename, cfg.KubernetesDeploymentSpec.Input, d.logger)
manifests, err = loader.LoadManifests(ctx)
if err != nil {
err = fmt.Errorf("failed to load new manifests: %w", err)
return nil, err
}
manifestCache.Put(headCommit.Hash, manifests)
}
watchingMap := make(map[provider.APIVersionKind]struct{}, len(watchingResourceKinds))
for _, k := range watchingResourceKinds {
watchingMap[k] = struct{}{}
}
filtered := make([]provider.Manifest, 0, len(manifests))
for _, m := range manifests {
_, ok := watchingMap[provider.APIVersionKind{
APIVersion: m.Key.APIVersion,
Kind: m.Key.Kind,
}]
if ok {
filtered = append(filtered, m)
}
}
return filtered, nil
}
// listApplications retrieves all applications those should be handled by this director
// and then groups them by repoID.
func (d *detector) listApplications() map[string][]*model.Application {
var (
apps = d.appLister.ListByCloudProvider(d.provider.Name)
m = make(map[string][]*model.Application)
)
for _, app := range apps {
repoID := app.GitPath.Repo.Id
if _, ok := m[repoID]; !ok {
m[repoID] = []*model.Application{app}
} else {
m[repoID] = append(m[repoID], app)
}
}
return m
}
func (d *detector) loadDeploymentConfiguration(repoPath string, app *model.Application) (*config.Config, error) {
path := filepath.Join(repoPath, app.GitPath.GetDeploymentConfigFilePath())
cfg, err := config.LoadFromYAML(path)
if err != nil {
return nil, err
}
if appKind, ok := config.ToApplicationKind(cfg.Kind); !ok || appKind != app.Kind {
return nil, fmt.Errorf("application in deployment configuration file is not match, got: %s, expected: %s", appKind, app.Kind)
}
return cfg, nil
}
func (d *detector) ProviderName() string {
return d.provider.Name
}
// groupManifests compares the given head and live manifests to divide them into three groups:
// - adds: contains all manifests that appear in lives but not in heads
// - deletes: contains all manifests that appear in heads but not in lives
// - inters: pairs of manifests that appear in both heads and lives.
func groupManifests(heads, lives []provider.Manifest) (adds, deletes, headInters, liveInters []provider.Manifest) {
// Sort the manifests before comparing.
sort.Slice(heads, func(i, j int) bool {
return heads[i].Key.IsLessWithIgnoringNamespace(heads[j].Key)
})
sort.Slice(lives, func(i, j int) bool {
return lives[i].Key.IsLessWithIgnoringNamespace(lives[j].Key)
})
var h, l int
for {
if h >= len(heads) || l >= len(lives) {
break
}
if heads[h].Key.IsEqualWithIgnoringNamespace(lives[l].Key) {
headInters = append(headInters, heads[h])
liveInters = append(liveInters, lives[l])
h++
l++
continue
}
// Has in head but not in live so this should be a deleted one.
if heads[h].Key.IsLessWithIgnoringNamespace(lives[l].Key) {
deletes = append(deletes, heads[h])
h++
continue
}
// Has in live but not in head so this should be an added one.
adds = append(adds, lives[l])
l++
}
if len(heads) > h {
deletes = append(deletes, heads[h:]...)
}
if len(lives) > l {
adds = append(adds, lives[l:]...)
}
return
}
func makeDeployingState(deployment *model.Deployment) model.ApplicationSyncState {
var (
shortReason = "A deployment of this application is running"
reason = deployment.Summary
)
if reason == "" {
reason = shortReason
}
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_DEPLOYING,
ShortReason: shortReason,
Reason: reason,
HeadDeploymentId: deployment.Id,
Timestamp: time.Now().Unix(),
}
}
func makeSyncedState() model.ApplicationSyncState {
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_SYNCED,
ShortReason: "",
Reason: "",
Timestamp: time.Now().Unix(),
}
}
func makeOutOfSyncState(adds, deletes []provider.Manifest, changes map[provider.Manifest]*diff.Result, commit string) model.ApplicationSyncState {
total := len(adds) + len(deletes) + len(changes)
shortReason := fmt.Sprintf("There are %d manifests are not synced (%d adds, %d deletes, %d changes)", total, len(adds), len(deletes), len(changes))
var b strings.Builder
if len(commit) >= 7 {
commit = commit[:7]
}
b.WriteString(fmt.Sprintf("Diff between the running resources and the definitions in Git at commit %q:\n", commit))
b.WriteString("--- Git\n+++ Cluster\n\n")
index := 0
for _, delete := range deletes {
index++
b.WriteString(fmt.Sprintf("- %d. %s\n\n", index, delete.Key.ReadableString()))
}
for _, add := range adds {
index++
b.WriteString(fmt.Sprintf("+ %d. %s\n\n", index, add.Key.ReadableString()))
}
const maxPrintDiffs = 3
var prints = 0
for m, d := range changes {
opts := []diff.RenderOption{
diff.WithLeftPadding(1),
}
switch {
case m.Key.IsSecret():
opts = append(opts, diff.WithRedactPath("data", "***secret-data-in-git***", "***secret-data-in-cluster***"))
case m.Key.IsConfigMap():
opts = append(opts, diff.WithRedactPath("data", "***config-data-in-git***", "***config-data-in-cluster***"))
}
renderer := diff.NewRenderer(opts...)
index++
b.WriteString(fmt.Sprintf("* %d. %s\n\n", index, m.Key.ReadableString()))
b.WriteString(renderer.Render(d.Nodes()))
b.WriteString("\n")
prints++
if prints >= maxPrintDiffs {
break
}
}
if prints < len(changes) {
b.WriteString(fmt.Sprintf("... (diffs from %d other manifests are omitted\n", len(changes)-prints))
}
return model.ApplicationSyncState{
Status: model.ApplicationSyncStatus_OUT_OF_SYNC,
ShortReason: shortReason,
Reason: b.String(),
Timestamp: time.Now().Unix(),
}
}
func filterIgnoringManifests(manifests []provider.Manifest) []provider.Manifest {
out := make([]provider.Manifest, 0, len(manifests))
for _, m := range manifests {
annotations := m.GetAnnotations()
if annotations[provider.LabelIgnoreDriftDirection] == provider.IgnoreDriftDetectionTrue {
continue
}
out = append(out, m)
}
return out
}