Skip to content

fix(update): preserve Kptfile formatting during upgrades#4427

Open
Jaisheesh-2006 wants to merge 25 commits intokptdev:mainfrom
Jaisheesh-2006:fix/4306-prevent-kptfile-upgrade
Open

fix(update): preserve Kptfile formatting during upgrades#4427
Jaisheesh-2006 wants to merge 25 commits intokptdev:mainfrom
Jaisheesh-2006:fix/4306-prevent-kptfile-upgrade

Conversation

@Jaisheesh-2006
Copy link
Copy Markdown
Contributor

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 kptops helpers.

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

  • Refactored upgrade-path Kptfile read/update operations to use SDK-backed Kptfile handling in kptfileutil.
  • Removed legacy custom Kptfile rewrite behavior in pkg/lib/kptops helper flows, reusing the centralized updater.
  • Hardened tests for cross-platform consistency:
    • line ending normalization where needed
    • environment-aware symlink assertions
    • Kptfile-focused preservation coverage

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=1
  • go test ./pkg/lib/kptops -count=1
  • go test ./commands/pkg/update -count=1
  • go test ./internal/util/update/... -count=1

Issue

Fixes #4306

Copilot AI review requested due to automatic review settings March 6, 2026 07:09
@netlify
Copy link
Copy Markdown

netlify Bot commented Mar 6, 2026

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 4560791
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/69eb79d02bf9c90008420e10
😎 Deploy Preview https://deploy-preview-4427--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dosubot dosubot Bot added size:XL This PR changes 500-999 lines, ignoring generated files. bug Something isn't working go Pull requests that update Go code labels Mar 6, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 kptops Kptfile update helpers to use kptfileutil.UpdateKptfileContent rather than re-marshalling via kyaml.
  • Adds SDK-based Kptfile write/update logic in kptfileutil to 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.

Comment thread internal/builtins/pkg_context_test.go Outdated
Comment thread internal/util/get/get_test.go
Comment thread pkg/kptfile/kptfileutil/util_test.go
Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread pkg/kptfile/kptfileutil/util.go Outdated
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/4306-prevent-kptfile-upgrade branch from e6f980d to ffd9c47 Compare March 6, 2026 07:52
Copilot AI review requested due to automatic review settings March 6, 2026 09:59
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/4306-prevent-kptfile-upgrade branch from ffd9c47 to a4335c5 Compare March 6, 2026 09:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread pkg/kptfile/kptfileutil/util.go
Copilot AI review requested due to automatic review settings April 8, 2026 16:15
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/4306-prevent-kptfile-upgrade branch from 5f117d1 to d622237 Compare April 8, 2026 16:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/kptfile/kptfileutil/util.go Outdated
Comment thread internal/util/get/get_test.go Outdated
Comment thread pkg/lib/kptops/clone_test.go Outdated
Comment thread pkg/kptfile/kptfileutil/util.go Outdated
Copilot AI review requested due to automatic review settings April 8, 2026 16:36
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread internal/util/get/get_test.go
Comment thread pkg/kptfile/kptfileutil/util.go
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread internal/util/render/executor_test.go Outdated
Comment thread internal/util/render/executor_test.go
Comment thread internal/util/render/executor_test.go Outdated
Comment thread pkg/kptfile/kptfileutil/util.go Outdated
Comment thread pkg/kptfile/kptfileutil/util.go Outdated
Comment thread pkg/lib/kptops/clone_test.go Outdated
Comment thread internal/builtins/pkg_context_test.go Outdated
Comment thread pkg/lib/kptops/render_test.go Outdated
Comment thread pkg/kptfile/kptfileutil/util_test.go Outdated
Comment thread pkg/kptfile/kptfileutil/util_test.go Outdated
Copilot AI review requested due to automatic review settings April 10, 2026 05:54
@dosubot dosubot Bot removed the size:XL This PR changes 500-999 lines, ignoring generated files. label Apr 10, 2026
@Jaisheesh-2006
Copy link
Copy Markdown
Contributor Author

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

Jaisheesh-2006 and others added 24 commits April 24, 2026 19:03
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>
Copilot AI review requested due to automatic review settings April 24, 2026 13:50
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/4306-prevent-kptfile-upgrade branch from 6fa5e09 to 5e352f1 Compare April 24, 2026 13:50
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/test/runner/runner.go
Comment thread pkg/test/runner/runner.go Outdated
Comment thread pkg/kptfile/kptfileutil/util.go
Comment thread pkg/kptfile/kptfileutil/util.go
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>
@Jaisheesh-2006
Copy link
Copy Markdown
Contributor Author

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working go Pull requests that update Go code lgtm size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Prevent formatting the kptfile during upgrades

4 participants