fix(update): preserve Kptfile formatting during upgrades#4427
fix(update): preserve Kptfile formatting during upgrades#4427Jaisheesh-2006 wants to merge 25 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.
|
Hi @CsatariGergely, was working on the issue with tests, a little more left to correct regarding podman test (network issue). I will resolve the podman issue and copilot suggestions within 1-2 days |
Use SDK-backed Kptfile read/update flow in upgrade paths, remove legacy rewrite behavior from kptops helpers, and harden tests for cross-platform stability. Fixes kptdev#4306 Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Reuse DecodeKptfile validation in UpdateKptfileContent so invalid, deprecated, or unknown-field Kptfiles fail before SDK processing. Strip SDK-internal metadata annotations before applying mutations to avoid writing internal config.kubernetes.io fields back to user Kptfiles. Add regression tests covering validation parity, annotation stripping, empty annotation cleanup, and nil-safe handling. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…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>
…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>
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…apshots Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…snapshot drift Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
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>
…strict Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Goldens committed in 0c99aa7 and e755c0d stripped leading YAML whitespace from every hunk line, but the runner's normalizer only stripped indent inside Kptfile diff blocks. Extend the same strip (preserving context lines and file headers) to non-Kptfile hunks so both the stripped goldens and properly-indented actual diffs normalize to the same canonical form. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Error strings must start lowercase (staticcheck ST1005) — rephrase the 'multiple YAML documents' guard from 'Kptfile must contain…' to 'expected exactly one YAML document in Kptfile, found %d' and update the matching test expectation. Also apply gofmt to the 'multiple yaml documents' test case and one other block that used spaces instead of tabs. Verified against the CI-pinned golangci-lint v2.11.4. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
The 0c99aa7/e755c0d74 hand-edits stripped indent AND silently dropped content lines from nine fn-{render,eval} goldens (resourceRef.name, metadata.name, newer Kptfile status schema fields). Indent drift was already handled by the normalizer extension in 027545b; this commit restores the missing content: - 6 goldens restored verbatim from main (content was correct there) - 4 render-failure goldens rebuilt from the CI log's post-normalization Actual block (main's versions pre-date the Kptfile status schema expansion); the normalizer is idempotent on its own output so these round-trip cleanly. Also fix two ST1005 staticcheck hits (error strings starting with the proper noun 'Kptfile') by rephrasing to 'expected exactly one YAML document in Kptfile', and apply gofmt to util_test.go (spaces→tabs in one test case and one block that slipped through an earlier edit). Verified: golangci-lint v2.11.4 (CI-pinned) 0 issues; all nine previously-failing goldens converge with the CI-captured actual diffs under the runner's normalizeDiff. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
The test relied on a live 'git ls-remote https://github.com/kptdev/kpt' to resolve the default branch; CI failed when github.com returned HTTP 500. Indirect default-branch lookup through a 'defaultBranchResolver' package variable (prod behavior unchanged) and swap in a stub in the test via t.Cleanup. Test now runs in 0 ms with zero network I/O. Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
6fa5e09 to
5e352f1
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 90 out of 133 changed files in this pull request and generated 4 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Four issues flagged by the kpt PR reviewer, each fixed + pinned by a
regression test:
1. isKptfileDiffHeader used strings.Fields, which mis-parses git's
c-quoted diff headers when paths contain spaces. Replaced with a
regex + c-style unquote helper. (TestIsKptfileDiffHeader_SpaceInPath)
2. The hardcoded non-Kptfile indent-strip is a LOSSY normalization that
hides genuine YAML nesting changes. Kept the behavior (removing it
would break ~99 legacy tests whose goldens were stripped by a prior
bad commit) but replaced the one-line comment with an explicit
tradeoff block pointing at KPT_E2E_UPDATE_EXPECTED as the cure.
3. setOrRemoveNestedField relied on reflect.Value.IsZero, which treats
only nil maps/slices as zero. Empty-but-non-nil containers leaked as
'labels: {}' / 'annotations: {}'. Added isEmptyContainer helper so
both forms are removed cleanly. (empty-but-non-nil_labels_map...)
4. DecodeKptfile silently stripped four SDK-synthesized annotations.
The behavior is correct (they're not user content) but was invisible
to readers. Documented on both the var and the decode function.
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
|
@CsatariGergely All the failing test issues and copilot issues have been resolved, kindly let me know if anything else to look into regarding this branch fix |
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