mirror of
https://github.com/fluxcd/flux2.git
synced 2026-05-23 09:55:55 +00:00
Merge e8d8562b9e into fa7cd5f847
This commit is contained in:
commit
cea456040e
5 changed files with 71 additions and 9 deletions
|
|
@ -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{
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
14
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml
vendored
Normal file
14
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml
vendored
Normal 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
|
||||
14
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml.golden
vendored
Normal file
14
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml.golden
vendored
Normal 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
|
||||
6
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml.output.golden
vendored
Normal file
6
cmd/flux/testdata/migrate/file-system/non-adjacent-kind.yaml.output.golden
vendored
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue