-
Notifications
You must be signed in to change notification settings - Fork 142
/
kubernetesdiff.go
135 lines (117 loc) · 3.6 KB
/
kubernetesdiff.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
// 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 planpreview
import (
"bytes"
"context"
"fmt"
"io"
"go.uber.org/zap"
"github.com/pipe-cd/pipecd/pkg/app/piped/deploysource"
provider "github.com/pipe-cd/pipecd/pkg/app/piped/platformprovider/kubernetes"
"github.com/pipe-cd/pipecd/pkg/cache"
"github.com/pipe-cd/pipecd/pkg/diff"
"github.com/pipe-cd/pipecd/pkg/model"
)
func (b *builder) kubernetesDiff(
ctx context.Context,
app *model.Application,
targetDSP deploysource.Provider,
lastSuccessfulCommit string,
buf *bytes.Buffer,
) (*diffResult, error) {
var oldManifests, newManifests []provider.Manifest
var err error
newManifests, err = loadKubernetesManifests(ctx, *app, targetDSP, b.appManifestsCache, b.gitClient, b.logger)
if err != nil {
fmt.Fprintf(buf, "failed to load kubernetes manifests at the head commit (%v)\n", err)
return nil, err
}
if lastSuccessfulCommit != "" {
runningDSP := deploysource.NewProvider(
b.workingDir,
deploysource.NewGitSourceCloner(b.gitClient, b.repoCfg, "running", lastSuccessfulCommit),
*app.GitPath,
b.secretDecrypter,
)
oldManifests, err = loadKubernetesManifests(ctx, *app, runningDSP, b.appManifestsCache, b.gitClient, b.logger)
if err != nil {
fmt.Fprintf(buf, "failed to load kubernetes manifests at the running commit (%v)\n", err)
return nil, err
}
}
result, err := provider.DiffList(
oldManifests,
newManifests,
b.logger,
diff.WithEquateEmpty(),
diff.WithCompareNumberAndNumericString(),
)
if err != nil {
fmt.Fprintf(buf, "failed to compare manifests (%v)\n", err)
return nil, err
}
if result.NoChange() {
fmt.Fprintln(buf, "No changes were detected")
return &diffResult{
summary: "No changes were detected",
noChange: true,
}, nil
}
summary := fmt.Sprintf("%d added manifests, %d changed manifests, %d deleted manifests", len(result.Adds), len(result.Changes), len(result.Deletes))
details := result.Render(provider.DiffRenderOptions{
MaskSecret: true,
UseDiffCommand: true,
})
fmt.Fprintf(buf, "--- Last Deploy\n+++ Head Commit\n\n%s\n", details)
return &diffResult{
summary: summary,
}, nil
}
func loadKubernetesManifests(ctx context.Context, app model.Application, dsp deploysource.Provider, manifestsCache cache.Cache, gc gitClient, logger *zap.Logger) (manifests []provider.Manifest, err error) {
commit := dsp.Revision()
cache := provider.AppManifestsCache{
AppID: app.Id,
Cache: manifestsCache,
Logger: logger,
}
manifests, ok := cache.Get(commit)
if ok {
return manifests, nil
}
// When the manifests were not in the cache we have to load them.
ds, err := dsp.Get(ctx, io.Discard)
if err != nil {
return nil, err
}
appCfg := ds.ApplicationConfig.KubernetesApplicationSpec
if appCfg == nil {
return nil, fmt.Errorf("malformed application configuration file")
}
loader := provider.NewLoader(
app.Name,
ds.AppDir,
ds.RepoDir,
app.GitPath.ConfigFilename,
appCfg.Input,
gc,
logger,
)
manifests, err = loader.LoadManifests(ctx)
if err != nil {
return nil, err
}
cache.Put(commit, manifests)
return manifests, nil
}