Skip to content

flux migrate silently skips resources when apiVersion and kind are non-adjacent #5839

@Iam-Karan-Suresh

Description

@Iam-Karan-Suresh

Describe the bug

The flux migrate command is designed to help users transition their manifests to the latest API versions before a Flux minor version upgrade. However, the current implementation of the manifest scanner in cmd/flux/migrate.go is too rigid and fails to detect resources if the kind: field is not on the line immediately following apiVersion:.

Since Kubernetes YAML allows fields to be ordered arbitrarily, it is common for metadata: or comments to be placed between apiVersion: and kind:. In these cases, flux migrate reports that no migration is needed, even if the resources are using deprecated API versions.

Proposed Fix

The detection logic in detectFileUpgrades should be updated to search for the kind: identifier within the boundaries of the current YAML document, rather than assuming it is on the next line (line+1).

Planned Implementation:
Modify detectFileUpgrades to perform a bidirectional search:

  1. When an apiVersion: line is found, search backwards to the top of the file or the previous --- separator to find the kind:.
  2. If not found, search forwards until the end of the file or the next --- separator.

This ensures that regardless of the field order, the resource identity is correctly resolved.

// Proposed logic snippet:
var kind string
for j := line; j >= 0; j-- {
    if strings.HasPrefix(lines[j], "---") { break }
    if strings.Contains(lines[j], "kind: ") {
        // extract kind...
        break
    }
}
// ... repeat forward ...

Steps to reproduce

  1. Create a manifest where metadata is placed between the version and the kind:

    # repro.yaml
    apiVersion: kustomize.toolkit.fluxcd.io/v1beta2
    metadata:
      name: podinfo
    kind: Kustomization
  2. Run the migrate command targeting a version that should trigger a bump to v1:

    flux migrate -f repro.yaml --version=2.7
  3. Observed Result:
    The CLI output says: ✔ no custom resources found that require migration. The file remains unchanged.

  4. Expected Result:
    The CLI should detect the Kustomization kind, realize that v1beta2 is old, and upgrade it to v1.

Expected behavior

  • The output should show that the version was upgraded to v1: apiVersion: kustomize.toolkit.fluxcd.io/v1

Screenshots and recordings

Image

OS / Distro

Ubuntu 25.10

Flux version

flux: v2.8.5

Flux check

N/A

Git provider

No response

Container Registry provider

No response

Additional context

  • This issue leads to "Silent Failures" where users believe they have migrated their manifests, but they are actually still using deprecated APIs that will break after a Flux upgrade.
  • I have already verified this fix locally with a new test case in migrate_test.go.

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions