...
1
16
17 package image
18
19 import (
20 "bytes"
21 "fmt"
22 "io/fs"
23 "regexp"
24 "strings"
25
26 "k8s.io/apimachinery/pkg/util/yaml"
27 e2etestingmanifests "k8s.io/kubernetes/test/e2e/testing-manifests"
28 )
29
30
31 var imageRE = regexp.MustCompile(`^(.*)/([^/:]*):(.*)$`)
32
33
34
35
36 func appendCSIImageConfigs(configs map[ImageID]Config) {
37 embeddedFS := e2etestingmanifests.GetE2ETestingManifestsFS().EmbeddedFS
38
39
40 index := ImageID(0)
41 for i := range configs {
42 if i > index {
43 index = i
44 }
45 }
46
47 err := fs.WalkDir(embeddedFS, "storage-csi", func(path string, d fs.DirEntry, err error) error {
48 if err != nil {
49 return err
50 }
51 if d.IsDir() || !strings.HasSuffix(path, ".yaml") {
52 return nil
53 }
54 data, err := embeddedFS.ReadFile(path)
55 if err != nil {
56 return err
57 }
58
59
60
61
62
63
64
65
66
67 items := bytes.Split(data, []byte("\n---"))
68 for i, item := range items {
69
70
71 var object interface{}
72 if err := yaml.Unmarshal(item, &object); err != nil {
73 return fmt.Errorf("decode item #%d in %s: %v",
74 i, path, err)
75 }
76
77
78 visit := func(value string) {
79 parts := imageRE.FindStringSubmatch(value)
80 if parts == nil {
81 return
82 }
83 config := Config{parts[1], parts[2], parts[3]}
84 for _, otherConfig := range configs {
85 if otherConfig == config {
86 return
87 }
88 }
89 index++
90 configs[index] = config
91 }
92
93
94
95 findStrings(object, visit, "spec", "containers", "image")
96 findStrings(object, visit, "spec", "template", "spec", "containers", "image")
97
98 }
99 return nil
100
101 })
102 if err != nil {
103 panic(err)
104 }
105 }
106
107
108
109
110
111
112 func findStrings(object interface{}, visit func(value string), path ...string) {
113 if len(path) == 0 {
114
115 if object, ok := object.(string); ok {
116 visit(object)
117 }
118 return
119 }
120
121 switch object := object.(type) {
122 case []interface{}:
123
124 for _, child := range object {
125 findStrings(child, visit, path...)
126 }
127 case map[string]interface{}:
128
129 if child, ok := object[path[0]]; ok {
130 findStrings(child, visit, path[1:]...)
131 }
132 }
133 }
134
View as plain text