fix(update): preserve Kptfile formatting during upgrades#4427
fix(update): preserve Kptfile formatting during upgrades#4427Jaisheesh-2006 wants to merge 18 commits intokptdev:mainfrom
Conversation
✅ Deploy Preview for kptdocs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
There was a problem hiding this comment.
Pull request overview
This PR addresses Kptfile formatting/comment drift during kpt pkg update by switching upgrade-time Kptfile mutations to an SDK-backed read/modify/write flow intended to preserve YAML structure and comments.
Changes:
- Refactors
kptopsKptfile update helpers to usekptfileutil.UpdateKptfileContentrather than re-marshalling via kyaml. - Adds SDK-based Kptfile write/update logic in
kptfileutilto better preserve formatting/comments. - Hardens tests for cross-platform behavior (CRLF normalization, environment-aware symlink assertions) and adds Kptfile preservation coverage.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| pkg/lib/kptops/render_test.go | Normalizes CRLF/LF in golden comparisons to reduce cross-platform diffs. |
| pkg/lib/kptops/clone_test.go | Adds tests asserting UpdateUpstream/UpdateName preserve comments/formatting. |
| pkg/lib/kptops/clone.go | Routes Kptfile mutations through kptfileutil.UpdateKptfileContent to preserve formatting. |
| pkg/kptfile/kptfileutil/util_test.go | Adds tests for comment/format preservation during UpdateKptfile. |
| pkg/kptfile/kptfileutil/util.go | Introduces SDK-based Kptfile write/update utilities and updates decode flow. |
| internal/util/get/get_test.go | Makes symlink assertions conditional on whether symlinks materialize in the test environment. |
| internal/builtins/pkg_context_test.go | Normalizes CRLF/LF for deterministic output comparison. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
e6f980d to
ffd9c47
Compare
ffd9c47 to
a4335c5
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
5f117d1 to
d622237
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ab5870c to
9d4fa6c
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ly expectation Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…Kptfile Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 12 out of 12 changed files in this pull request and generated 1 comment.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…rtions Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…iene Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…harden edge cases - Add format-preserving path to WriteKptfileToFS, matching the existing WriteFile behavior. This ensures kpt fn render (via setRenderStatus in executor.go) no longer reformats the Kptfile, stripping comments and custom formatting. - Guard reflect.ValueOf(val).IsZero() with rv.IsValid() check in setOrRemoveNestedField to prevent panics on invalid reflect values. - Add round-trip idempotency test (TestWriteFile_RoundTripIdempotency) and filesystem format preservation test (TestWriteKptfileToFS_PreservesFormatting). - Fix bug in migratecmd.go where migrateKptfileToRG used err (from p.Kptfile()) instead of gFileErr (from os.Stat()) when checking ResourceGroup file existence. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
-Fix staticcheck QF1008: simplify kf.ObjectMeta.Annotations to kf.Annotations (ObjectMeta is embedded). -Fix YAML unicode escapes in test fixture: quote scalar so \u00fc is interpreted by the YAML parser. -Add require.NoError for success path in TestPathRelToRoot to catch regressions returning unexpected errors. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
… expectations Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
928930c to
ed019bb
Compare
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…apshots Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…snapshot drift Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Please address the comments from Copilot and @mozesl-nokia and fix the tests. |
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
- Refactored ormalizeDiff to fix gocyclo cyclomatic complexity. The heavy inline closures are now distinct methods bound to a diffNormalizer struct, drastically lowering branch complexity per function. - Addressed ineffassign failure by removing the ineffective i++ statement before the explicit i = j assignment inside the sortChunk block. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Description
This PR fixes Kptfile formatting drift during package upgrades by switching upgrade-time Kptfile handling to an SDK-backed read/update flow and removing legacy rewrite logic from
kptopshelpers.Motivation
During
kpt pkg update, Kptfile rewrites could alter user formatting/comments in ways that were not intended.Issue #4306 tracks this regression and expects upgrade behavior to preserve user-authored Kptfile structure as much as possible.
What Changed
kptfileutil.pkg/lib/kptopshelper flows, reusing the centralized updater.Scope
This change targets upgrade and related Kptfile mutation paths only, with no intended functional change to unrelated package/resource update behavior.
Testing
Validated with targeted and package-level tests, including:
go test ./pkg/kptfile/... -count=1go test ./pkg/lib/kptops -count=1go test ./commands/pkg/update -count=1go test ./internal/util/update/... -count=1Issue
Fixes #4306