-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
diff.go
331 lines (299 loc) · 10.8 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
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
package diff
import (
"fmt"
"github.com/go-logr/logr"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appv1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/argoproj/argo-cd/v2/util/argo"
"github.com/argoproj/argo-cd/v2/util/argo/managedfields"
appstatecache "github.com/argoproj/argo-cd/v2/util/cache/appstate"
"github.com/argoproj/gitops-engine/pkg/diff"
"github.com/argoproj/gitops-engine/pkg/utils/kube"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// DiffConfigBuilder is used as a safe way to create valid DiffConfigs.
type DiffConfigBuilder struct {
diffConfig *diffConfig
}
// NewDiffConfigBuilder create a new DiffConfigBuilder instance.
func NewDiffConfigBuilder() *DiffConfigBuilder {
return &DiffConfigBuilder{
diffConfig: &diffConfig{},
}
}
// WithDiffSettings will set the diff settings in the builder.
func (b *DiffConfigBuilder) WithDiffSettings(id []v1alpha1.ResourceIgnoreDifferences, o map[string]v1alpha1.ResourceOverride, ignoreAggregatedRoles bool) *DiffConfigBuilder {
ignores := id
if ignores == nil {
ignores = []v1alpha1.ResourceIgnoreDifferences{}
}
b.diffConfig.ignores = ignores
overrides := o
if overrides == nil {
overrides = make(map[string]appv1.ResourceOverride)
}
b.diffConfig.overrides = overrides
b.diffConfig.ignoreAggregatedRoles = ignoreAggregatedRoles
return b
}
// WithTrackingMethod sets the tracking in the diff config.
func (b *DiffConfigBuilder) WithTracking(appLabelKey, trackingMethod string) *DiffConfigBuilder {
b.diffConfig.appLabelKey = appLabelKey
b.diffConfig.trackingMethod = trackingMethod
return b
}
// WithNoCache sets the nocache in the diff config.
func (b *DiffConfigBuilder) WithNoCache() *DiffConfigBuilder {
b.diffConfig.noCache = true
return b
}
// WithCache sets the appstatecache.Cache and the appName in the diff config. Those the
// are two objects necessary to retrieve a cached diff.
func (b *DiffConfigBuilder) WithCache(s *appstatecache.Cache, appName string) *DiffConfigBuilder {
b.diffConfig.noCache = false
b.diffConfig.stateCache = s
b.diffConfig.appName = appName
return b
}
// WithLogger sets the logger in the diff config.
func (b *DiffConfigBuilder) WithLogger(l logr.Logger) *DiffConfigBuilder {
b.diffConfig.logger = &l
return b
}
// Build will first validate the current state of the diff config and return the
// DiffConfig implementation if no errors are found. Will return nil and the error
// details otherwise.
func (b *DiffConfigBuilder) Build() (DiffConfig, error) {
err := b.diffConfig.Validate()
if err != nil {
return nil, err
}
return b.diffConfig, nil
}
// DiffConfig defines methods to retrieve the configurations used while applying diffs
// and normalizing resources.
type DiffConfig interface {
// Validate will check if the current configurations are set properly.
Validate() error
// DiffFromCache will verify if it should retrieve the cached ResourceDiff based on this
// DiffConfig.
DiffFromCache(appName string) (bool, []*appv1.ResourceDiff)
// Ignores Application level ignore difference configurations.
Ignores() []v1alpha1.ResourceIgnoreDifferences
// Overrides is map of system configurations to override the Application ones.
// The key should follow the "group/kind" format.
Overrides() map[string]v1alpha1.ResourceOverride
AppLabelKey() string
TrackingMethod() string
// AppName the Application name. Used to retrieve the cached diff.
AppName() string
// NoCache defines if should retrieve the diff from cache.
NoCache() bool
// StateCache is used when retrieving the diff from the cache.
StateCache() *appstatecache.Cache
IgnoreAggregatedRoles() bool
// Logger used during the diff
Logger() *logr.Logger
}
// diffConfig defines the configurations used while applying diffs.
type diffConfig struct {
ignores []v1alpha1.ResourceIgnoreDifferences
overrides map[string]v1alpha1.ResourceOverride
appLabelKey string
trackingMethod string
appName string
noCache bool
stateCache *appstatecache.Cache
ignoreAggregatedRoles bool
logger *logr.Logger
}
func (c *diffConfig) Ignores() []v1alpha1.ResourceIgnoreDifferences {
return c.ignores
}
func (c *diffConfig) Overrides() map[string]v1alpha1.ResourceOverride {
return c.overrides
}
func (c *diffConfig) AppLabelKey() string {
return c.appLabelKey
}
func (c *diffConfig) TrackingMethod() string {
return c.trackingMethod
}
func (c *diffConfig) AppName() string {
return c.appName
}
func (c *diffConfig) NoCache() bool {
return c.noCache
}
func (c *diffConfig) StateCache() *appstatecache.Cache {
return c.stateCache
}
func (c *diffConfig) IgnoreAggregatedRoles() bool {
return c.ignoreAggregatedRoles
}
func (c *diffConfig) Logger() *logr.Logger {
return c.logger
}
// Validate will check the current state of this diffConfig and return
// error if it finds any required configuration missing.
func (c *diffConfig) Validate() error {
msg := "diffConfig validation error"
if c.ignores == nil {
return fmt.Errorf("%s: ResourceIgnoreDifferences can not be nil", msg)
}
if c.overrides == nil {
return fmt.Errorf("%s: ResourceOverride can not be nil", msg)
}
if !c.noCache {
if c.appName == "" {
return fmt.Errorf("%s: AppName must be set when retrieving from cache", msg)
}
if c.stateCache == nil {
return fmt.Errorf("%s: StateCache must be set when retrieving from cache", msg)
}
}
return nil
}
// NormalizationResult holds the normalized lives and target resources.
type NormalizationResult struct {
Lives []*unstructured.Unstructured
Targets []*unstructured.Unstructured
}
// StateDiff will apply all required normalizations and calculate the diffs between
// the live and the config/desired states.
func StateDiff(live, config *unstructured.Unstructured, diffConfig DiffConfig) (diff.DiffResult, error) {
results, err := StateDiffs([]*unstructured.Unstructured{live}, []*unstructured.Unstructured{config}, diffConfig)
if err != nil {
return diff.DiffResult{}, err
}
if len(results.Diffs) != 1 {
return diff.DiffResult{}, fmt.Errorf("StateDiff error: unexpected diff results: expected 1 got %d", len(results.Diffs))
}
return results.Diffs[0], nil
}
// StateDiffs will apply all required normalizations and calculate the diffs between
// the live and the config/desired states.
func StateDiffs(lives, configs []*unstructured.Unstructured, diffConfig DiffConfig) (*diff.DiffResultList, error) {
normResults, err := preDiffNormalize(lives, configs, diffConfig)
if err != nil {
return nil, err
}
diffNormalizer, err := newDiffNormalizer(diffConfig.Ignores(), diffConfig.Overrides())
if err != nil {
return nil, err
}
diffOpts := []diff.Option{
diff.WithNormalizer(diffNormalizer),
diff.IgnoreAggregatedRoles(diffConfig.IgnoreAggregatedRoles()),
}
if diffConfig.Logger() != nil {
diffOpts = append(diffOpts, diff.WithLogr(*diffConfig.Logger()))
}
useCache, cachedDiff := diffConfig.DiffFromCache(diffConfig.AppName())
if useCache && cachedDiff != nil {
return diffArrayCached(normResults.Targets, normResults.Lives, cachedDiff, diffOpts...)
}
return diff.DiffArray(normResults.Targets, normResults.Lives, diffOpts...)
}
func diffArrayCached(configArray []*unstructured.Unstructured, liveArray []*unstructured.Unstructured, cachedDiff []*appv1.ResourceDiff, opts ...diff.Option) (*diff.DiffResultList, error) {
numItems := len(configArray)
if len(liveArray) != numItems {
return nil, fmt.Errorf("left and right arrays have mismatched lengths")
}
diffByKey := map[kube.ResourceKey]*appv1.ResourceDiff{}
for i := range cachedDiff {
res := cachedDiff[i]
diffByKey[kube.NewResourceKey(res.Group, res.Kind, res.Namespace, res.Name)] = cachedDiff[i]
}
diffResultList := diff.DiffResultList{
Diffs: make([]diff.DiffResult, numItems),
}
for i := 0; i < numItems; i++ {
config := configArray[i]
live := liveArray[i]
resourceVersion := ""
var key kube.ResourceKey
if live != nil {
key = kube.GetResourceKey(live)
resourceVersion = live.GetResourceVersion()
} else {
key = kube.GetResourceKey(config)
}
var dr *diff.DiffResult
if cachedDiff, ok := diffByKey[key]; ok && cachedDiff.ResourceVersion == resourceVersion {
dr = &diff.DiffResult{
NormalizedLive: []byte(cachedDiff.NormalizedLiveState),
PredictedLive: []byte(cachedDiff.PredictedLiveState),
Modified: cachedDiff.Modified,
}
} else {
res, err := diff.Diff(configArray[i], liveArray[i], opts...)
if err != nil {
return nil, err
}
dr = res
}
if dr != nil {
diffResultList.Diffs[i] = *dr
if dr.Modified {
diffResultList.Modified = true
}
}
}
return &diffResultList, nil
}
// DiffFromCache will verify if it should retrieve the cached ResourceDiff based on this
// DiffConfig. Returns true and the cached ResourceDiff if configured to use the cache.
// Returns false and nil otherwise.
func (c *diffConfig) DiffFromCache(appName string) (bool, []*appv1.ResourceDiff) {
if c.noCache || c.stateCache == nil || appName == "" {
return false, nil
}
cachedDiff := make([]*appv1.ResourceDiff, 0)
if c.stateCache != nil && c.stateCache.GetAppManagedResources(appName, &cachedDiff) == nil {
return true, cachedDiff
}
return false, nil
}
// preDiffNormalize applies the normalization of live and target resources before invoking
// the diff. None of the attributes in the preDiffNormalizeParams will be modified.
func preDiffNormalize(lives, targets []*unstructured.Unstructured, diffConfig DiffConfig) (*NormalizationResult, error) {
if diffConfig == nil {
return nil, fmt.Errorf("preDiffNormalize error: diffConfig can not be nil")
}
err := diffConfig.Validate()
if err != nil {
return nil, fmt.Errorf("preDiffNormalize error: %s", err)
}
results := &NormalizationResult{}
for i := range targets {
target := safeDeepCopy(targets[i])
live := safeDeepCopy(lives[i])
resourceTracking := argo.NewResourceTracking()
_ = resourceTracking.Normalize(target, live, diffConfig.AppLabelKey(), diffConfig.TrackingMethod())
// just normalize on managed fields if live and target aren't nil as we just care
// about conflicting fields
if live != nil && target != nil {
gvk := target.GetObjectKind().GroupVersionKind()
idc := NewIgnoreDiffConfig(diffConfig.Ignores(), diffConfig.Overrides())
ok, ignoreDiff := idc.HasIgnoreDifference(gvk.Group, gvk.Kind, target.GetName(), target.GetNamespace())
if ok && len(ignoreDiff.ManagedFieldsManagers) > 0 {
var err error
live, target, err = managedfields.Normalize(live, target, ignoreDiff.ManagedFieldsManagers)
if err != nil {
return nil, err
}
}
}
results.Lives = append(results.Lives, live)
results.Targets = append(results.Targets, target)
}
return results, nil
}
// safeDeepCopy will return nil if given obj is nil.
func safeDeepCopy(obj *unstructured.Unstructured) *unstructured.Unstructured {
if obj == nil {
return nil
}
return obj.DeepCopy()
}