This commit is contained in:
Sai Asish Y 2026-05-22 07:12:11 +08:00 committed by GitHub
commit cea456040e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 71 additions and 9 deletions

View file

@ -624,18 +624,35 @@ func (f *FileSystemMigrator) detectFileUpgrades(file string) ([]APIUpgrade, erro
continue
}
// Parse kind.
if line+1 >= len(lines) {
continue
}
kindLine := lines[line+1]
// Parse kind. YAML allows fields to be ordered arbitrarily, so the
// kind: line may appear before or after apiVersion: within the same
// document. Search within the current document boundaries (delimited
// by "---" separators) rather than hard-coding line+1.
const kindPrefix = "kind: "
idx = strings.Index(kindLine, kindPrefix)
if idx == -1 {
kind := ""
for j := line - 1; j >= 0; j-- {
if strings.HasPrefix(strings.TrimSpace(lines[j]), "---") {
break
}
if k := strings.Index(lines[j], kindPrefix); k != -1 {
kind = strings.Split(strings.TrimSpace(lines[j][k+len(kindPrefix):]), " ")[0]
break
}
}
if kind == "" {
for j := line + 1; j < len(lines); j++ {
if strings.HasPrefix(strings.TrimSpace(lines[j]), "---") {
break
}
if k := strings.Index(lines[j], kindPrefix); k != -1 {
kind = strings.Split(strings.TrimSpace(lines[j][k+len(kindPrefix):]), " ")[0]
break
}
}
}
if kind == "" {
continue
}
kindValuePrefix := strings.TrimSpace(kindLine[idx+len(kindPrefix):])
kind := strings.Split(kindValuePrefix, " ")[0]
// Build GroupKind.
gk := schema.GroupKind{

View file

@ -71,6 +71,17 @@ func TestFileSystemMigrator(t *testing.T) {
},
},
},
{
name: "migrate single file with non-adjacent apiVersion and kind",
path: "testdata/migrate/file-system/non-adjacent-kind.yaml",
outputGolden: "testdata/migrate/file-system/non-adjacent-kind.yaml.output.golden",
writtenFiles: []writtenFile{
{
file: "testdata/migrate/file-system/non-adjacent-kind.yaml",
goldenFile: "testdata/migrate/file-system/non-adjacent-kind.yaml.golden",
},
},
},
{
name: "migrate files in directory",
path: "testdata/migrate/file-system/dir",

View file

@ -0,0 +1,14 @@
apiVersion: image.toolkit.fluxcd.io/v1beta1
metadata:
name: podinfo
kind: ImageRepository
---
metadata:
name: alpine
kind: ImagePolicy
apiVersion: image.toolkit.fluxcd.io/v1beta2
---
kind: ImageUpdateAutomation
metadata:
name: flux-system
apiVersion: image.toolkit.fluxcd.io/v1beta1

View file

@ -0,0 +1,14 @@
apiVersion: image.toolkit.fluxcd.io/v1
metadata:
name: podinfo
kind: ImageRepository
---
metadata:
name: alpine
kind: ImagePolicy
apiVersion: image.toolkit.fluxcd.io/v1
---
kind: ImageUpdateAutomation
metadata:
name: flux-system
apiVersion: image.toolkit.fluxcd.io/v1

View file

@ -0,0 +1,6 @@
► starting migration of custom resources
✚ testdata/migrate/file-system/non-adjacent-kind.yaml:1: ImageRepository v1beta1 -> v1
✚ testdata/migrate/file-system/non-adjacent-kind.yaml:9: ImagePolicy v1beta2 -> v1
✚ testdata/migrate/file-system/non-adjacent-kind.yaml:14: ImageUpdateAutomation v1beta1 -> v1
✔ file testdata/migrate/file-system/non-adjacent-kind.yaml migrated successfully
✔ custom resources migrated successfully