Skip to content

Commit 440e030

Browse files
Fs02chengfang
authored andcommitted
fix: Skip commit for kustomize update if nothing changed
Signed-off-by: Fs02 <[email protected]>
1 parent 13f01ec commit 440e030

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pkg/argocd/git.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -329,40 +329,51 @@ func writeKustomization(app *v1alpha1.Application, wbc *WriteBackConfig, gitC gi
329329
return err, false
330330
}
331331

332-
if err = updateKustomizeFile(filterFunc, kustFile); err != nil {
333-
return err, false
334-
}
335-
336-
return nil, false
332+
return updateKustomizeFile(filterFunc, kustFile)
337333
}
338334

339335
// updateKustomizeFile reads the kustomization file at path, applies the filter to it, and writes the result back
340336
// to the file. This is the same behavior as kyaml.UpdateFile, but it preserves the original order
341337
// of YAML fields to minimize git diffs.
342-
func updateKustomizeFile(filter kyaml.Filter, path string) error {
338+
func updateKustomizeFile(filter kyaml.Filter, path string) (error, bool) {
343339
// Read the yaml
344340
y, err := kyaml.ReadFile(path)
345341
if err != nil {
346-
return err
342+
return err, false
343+
}
344+
345+
originalData, err := y.String()
346+
if err != nil {
347+
return err, false
347348
}
348349

349350
// Update the yaml
350351
yCpy := y.Copy()
351352
if err := yCpy.PipeE(filter); err != nil {
352-
return err
353+
return err, false
353354
}
354355

355356
// Preserve the original order of fields
356357
if err := order.SyncOrder(y, yCpy); err != nil {
357-
return err
358+
return err, false
359+
}
360+
361+
override, err := yCpy.String()
362+
if err != nil {
363+
return err, false
364+
}
365+
366+
if originalData == override {
367+
log.Debugf("target parameter file and marshaled data are the same, skipping commit.")
368+
return nil, true
358369
}
359370

360371
// Write the yaml
361-
if err := kyaml.WriteFile(yCpy, path); err != nil {
362-
return err
372+
if err := os.WriteFile(path, []byte(override), 0600); err != nil {
373+
return err, false
363374
}
364375

365-
return nil
376+
return nil, false
366377
}
367378

368379
func imagesFilter(images v1alpha1.KustomizeImages) (kyaml.Filter, error) {

pkg/argocd/git_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func Test_updateKustomizeFile(t *testing.T) {
274274
for _, tt := range tests {
275275
t.Run(tt.name, func(t *testing.T) {
276276
path := makeTmpKustomization(t, []byte(tt.content))
277-
err := updateKustomizeFile(tt.filter, path)
277+
err, _ := updateKustomizeFile(tt.filter, path)
278278
if tt.wantErr {
279279
assert.Error(t, err)
280280
} else {

0 commit comments

Comments
 (0)