Skip to content

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

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

fix(update): preserve Kptfile formatting during upgrades#4427
Jaisheesh-2006 wants to merge 18 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 0c99aa7
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/69e90118277cc00008fa0eb7
😎 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
…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>
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 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.

Comment thread pkg/test/runner/runner.go
…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>
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/4306-prevent-kptfile-upgrade branch from 928930c to ed019bb Compare April 19, 2026 05:55
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Copilot AI review requested due to automatic review settings April 19, 2026 08:51
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 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.

Comment thread pkg/test/runner/runner.go Outdated
Comment thread pkg/test/runner/runner.go
…apshots

Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
…snapshot drift

Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Copilot AI review requested due to automatic review settings April 19, 2026 10:46
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 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.

Comment thread pkg/test/runner/runner.go
Comment thread pkg/test/runner/runner.go Outdated
@CsatariGergely
Copy link
Copy Markdown
Contributor

Please address the comments from Copilot and @mozesl-nokia and fix the tests.

@dosubot dosubot Bot added the lgtm label Apr 22, 2026
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>
Copilot AI review requested due to automatic review settings April 22, 2026 16:45
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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:L This PR changes 100-499 lines, ignoring generated files. labels Apr 22, 2026
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:XS This PR changes 0-9 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