-
Notifications
You must be signed in to change notification settings - Fork 141
/
diff.go
298 lines (258 loc) · 7.49 KB
/
diff.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
// 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 kubernetes
import (
"bytes"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"go.uber.org/zap"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"github.com/pipe-cd/pipecd/pkg/diff"
)
const (
diffCommand = "diff"
)
type DiffListResult struct {
Adds []Manifest
Deletes []Manifest
Changes []DiffListChange
}
func (r *DiffListResult) NoChange() bool {
return len(r.Adds)+len(r.Deletes)+len(r.Changes) == 0
}
type DiffListChange struct {
Old Manifest
New Manifest
Diff *diff.Result
}
func Diff(old, new Manifest, logger *zap.Logger, opts ...diff.Option) (*diff.Result, error) {
if old.Key.IsSecret() && new.Key.IsSecret() {
var err error
old.u, err = normalizeNewSecret(old.u, new.u)
if err != nil {
return nil, err
}
}
key := old.Key.String()
normalized, err := remarshal(new.u)
if err != nil {
logger.Info("compare manifests directly since it was unable to remarshal Kubernetes manifest to normalize special fields", zap.Error(err))
return diff.DiffUnstructureds(*old.u, *new.u, key, opts...)
}
return diff.DiffUnstructureds(*old.u, *normalized, key, opts...)
}
func DiffList(olds, news []Manifest, logger *zap.Logger, opts ...diff.Option) (*DiffListResult, error) {
adds, deletes, newChanges, oldChanges := groupManifests(olds, news)
cr := &DiffListResult{
Adds: adds,
Deletes: deletes,
Changes: make([]DiffListChange, 0, len(newChanges)),
}
for i := 0; i < len(newChanges); i++ {
result, err := Diff(oldChanges[i], newChanges[i], logger, opts...)
if err != nil {
return nil, err
}
if !result.HasDiff() {
continue
}
cr.Changes = append(cr.Changes, DiffListChange{
Old: oldChanges[i],
New: newChanges[i],
Diff: result,
})
}
return cr, nil
}
func normalizeNewSecret(old, new *unstructured.Unstructured) (*unstructured.Unstructured, error) {
var o, n v1.Secret
runtime.DefaultUnstructuredConverter.FromUnstructured(old.Object, &o)
runtime.DefaultUnstructuredConverter.FromUnstructured(new.Object, &n)
// Move as much as possible fields from `o.Data` to `o.StringData` to make `o` close to `n` to minimize the diff.
for k, v := range o.Data {
// Skip if the field also exists in StringData.
if _, ok := o.StringData[k]; ok {
continue
}
if _, ok := n.StringData[k]; !ok {
continue
}
if o.StringData == nil {
o.StringData = make(map[string]string)
}
// If the field is existing in `n.StringData`, we should move that field from `o.Data` to `o.StringData`
o.StringData[k] = string(v)
delete(o.Data, k)
}
newO, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&o)
if err != nil {
return nil, err
}
return &unstructured.Unstructured{Object: newO}, nil
}
type DiffRenderOptions struct {
MaskSecret bool
MaskConfigMap bool
// Maximum number of changed manifests should be shown.
// Zero means rendering all.
MaxChangedManifests int
// If true, use "diff" command to render.
UseDiffCommand bool
}
func (r *DiffListResult) Render(opt DiffRenderOptions) string {
var b strings.Builder
index := 0
for _, delete := range r.Deletes {
index++
b.WriteString(fmt.Sprintf("- %d. %s\n\n", index, delete.Key.ReadableString()))
}
for _, add := range r.Adds {
index++
b.WriteString(fmt.Sprintf("+ %d. %s\n\n", index, add.Key.ReadableString()))
}
maxPrintDiffs := len(r.Changes)
if opt.MaxChangedManifests != 0 && opt.MaxChangedManifests < maxPrintDiffs {
maxPrintDiffs = opt.MaxChangedManifests
}
var prints = 0
for _, change := range r.Changes {
key := change.Old.Key
opts := []diff.RenderOption{
diff.WithLeftPadding(1),
}
needMaskValue := false
if opt.MaskSecret && key.IsSecret() {
opts = append(opts, diff.WithMaskPath("data"))
needMaskValue = true
} else if opt.MaskConfigMap && key.IsConfigMap() {
opts = append(opts, diff.WithMaskPath("data"))
needMaskValue = true
}
renderer := diff.NewRenderer(opts...)
index++
b.WriteString(fmt.Sprintf("# %d. %s\n\n", index, key.ReadableString()))
// Use our diff check in one of the following cases:
// - not explicit set useDiffCommand option.
// - requires masking secret or configmap value.
if !opt.UseDiffCommand || needMaskValue {
b.WriteString(renderer.Render(change.Diff.Nodes()))
} else {
// TODO: Find a way to mask values in case of using unix `diff` command.
d, err := diffByCommand(diffCommand, change.Old, change.New)
if err != nil {
b.WriteString(fmt.Sprintf("An error occurred while rendering diff (%v)", err))
} else {
b.Write(d)
}
}
b.WriteString("\n")
prints++
if prints >= maxPrintDiffs {
break
}
}
if prints < len(r.Changes) {
b.WriteString(fmt.Sprintf("... (omitted %d other changed manifests\n", len(r.Changes)-prints))
}
return b.String()
}
func diffByCommand(command string, old, new Manifest) ([]byte, error) {
oldBytes, err := old.YamlBytes()
if err != nil {
return nil, err
}
newBytes, err := new.YamlBytes()
if err != nil {
return nil, err
}
oldFile, err := os.CreateTemp("", "old")
if err != nil {
return nil, err
}
defer os.Remove(oldFile.Name())
if _, err := oldFile.Write(oldBytes); err != nil {
return nil, err
}
newFile, err := os.CreateTemp("", "new")
if err != nil {
return nil, err
}
defer os.Remove(newFile.Name())
if _, err := newFile.Write(newBytes); err != nil {
return nil, err
}
var stdout, stderr bytes.Buffer
cmd := exec.Command(command, "-u", "-N", oldFile.Name(), newFile.Name())
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if stdout.Len() > 0 {
// diff exits with a non-zero status when the files don't match.
// Ignore that failure as long as we get output.
err = nil
}
if err != nil {
return nil, fmt.Errorf("failed to run diff, err = %w, %s", err, stderr.String())
}
// Remove two-line header from output.
data := bytes.TrimSpace(stdout.Bytes())
rows := bytes.SplitN(data, []byte("\n"), 3)
if len(rows) == 3 {
return rows[2], nil
}
return data, nil
}
func groupManifests(olds, news []Manifest) (adds, deletes, newChanges, oldChanges []Manifest) {
// Sort the manifests before comparing.
sort.Slice(news, func(i, j int) bool {
return news[i].Key.IsLessWithIgnoringNamespace(news[j].Key)
})
sort.Slice(olds, func(i, j int) bool {
return olds[i].Key.IsLessWithIgnoringNamespace(olds[j].Key)
})
var n, o int
for {
if n >= len(news) || o >= len(olds) {
break
}
if news[n].Key.IsEqualWithIgnoringNamespace(olds[o].Key) {
newChanges = append(newChanges, news[n])
oldChanges = append(oldChanges, olds[o])
n++
o++
continue
}
// Has in news but not in olds so this should be a added one.
if news[n].Key.IsLessWithIgnoringNamespace(olds[o].Key) {
adds = append(adds, news[n])
n++
continue
}
// Has in olds but not in news so this should be an deleted one.
deletes = append(deletes, olds[o])
o++
}
if len(news) > n {
adds = append(adds, news[n:]...)
}
if len(olds) > o {
deletes = append(deletes, olds[o:]...)
}
return
}