Skip to content

cmd/flux: search for kind within the YAML document instead of line+1#5860

Open
SAY-5 wants to merge 1 commit into
fluxcd:mainfrom
SAY-5:fix/migrate-kind-search-5839
Open

cmd/flux: search for kind within the YAML document instead of line+1#5860
SAY-5 wants to merge 1 commit into
fluxcd:mainfrom
SAY-5:fix/migrate-kind-search-5839

Conversation

@SAY-5
Copy link
Copy Markdown

@SAY-5 SAY-5 commented Apr 22, 2026

Summary

Fixes #5839.

cmd/flux/migrate.go's detectFileUpgrades assumed kind: always lives on the line immediately following apiVersion:. In practice YAML fields can be ordered arbitrarily, so a manifest with metadata: between the two fields (or with apiVersion: last) produced a silent no custom resources found that require migration even when the apiVersion was deprecated.

Fix

Search within the current YAML document (delimited by --- separators) rather than hard-coding line+1: scan backwards to the previous separator / start-of-file, then forwards to the next separator. The first matching kind: line wins. The order (backwards first) keeps the happy-path scan tight for the conventional apiVersion: then kind: layout; the forward fallback covers reversed-order documents.

Tests

Adds a regression fixture testdata/migrate/file-system/non-adjacent-kind.yaml exercising three shapes in one file:

  • apiVersion: first, kind: after metadata:
  • kind: first, apiVersion: after metadata:
  • kind: first, apiVersion: last

All three migrate to v1 as expected. The matching .golden + .output.golden fixtures pin the behaviour. go test -run TestFileSystemMigrator ./cmd/flux/... green; go vet ./cmd/flux/... clean.

detectFileUpgrades hard-coded the kind: lookup at apiVersion line+1,
so manifests with any other field (metadata:, comments, annotations)
or reversed field order silently returned "no migration needed" even
when apiVersion was deprecated (fluxcd#5839).

Search within the current YAML document boundaries: scan backwards
to the previous '---' separator or start-of-file, then forwards to
the next '---' separator. First kind: line wins. The order is
backwards-then-forwards so that for a conventional apiVersion/kind
ordering we only scan the single line + the document boundary; the
forward fallback covers the reversed-order case.

Adds a regression fixture with three documents covering:
  - apiVersion first, kind after metadata:
  - kind first, apiVersion after metadata:
  - kind first, apiVersion last

Fixes fluxcd#5839

Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant