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:
- When an
apiVersion: line is found, search backwards to the top of the file or the previous --- separator to find the kind:.
- 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
-
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
-
Run the migrate command targeting a version that should trigger a bump to v1:
flux migrate -f repro.yaml --version=2.7
-
Observed Result:
The CLI output says: ✔ no custom resources found that require migration. The file remains unchanged.
-
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
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
Describe the bug
The
flux migratecommand 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 incmd/flux/migrate.gois too rigid and fails to detect resources if thekind:field is not on the line immediately followingapiVersion:.Since Kubernetes YAML allows fields to be ordered arbitrarily, it is common for
metadata:or comments to be placed betweenapiVersion:andkind:. In these cases,flux migratereports that no migration is needed, even if the resources are using deprecated API versions.Proposed Fix
The detection logic in
detectFileUpgradesshould be updated to search for thekind:identifier within the boundaries of the current YAML document, rather than assuming it is on the next line (line+1).Planned Implementation:
Modify
detectFileUpgradesto perform a bidirectional search:apiVersion:line is found, search backwards to the top of the file or the previous---separator to find thekind:.---separator.This ensures that regardless of the field order, the resource identity is correctly resolved.
Steps to reproduce
Create a manifest where
metadatais placed between the version and the kind:Run the migrate command targeting a version that should trigger a bump to
v1:Observed Result:
The CLI output says:
✔ no custom resources found that require migration. The file remains unchanged.Expected Result:
The CLI should detect the
Kustomizationkind, realize thatv1beta2is old, and upgrade it tov1.Expected behavior
Screenshots and recordings
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
migrate_test.go.Code of Conduct