|
1 | 1 | package utils |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "fmt" |
| 6 | + "io" |
5 | 7 | "io/fs" |
6 | 8 | "os" |
7 | 9 | "path/filepath" |
8 | 10 | "regexp" |
9 | 11 | "strings" |
10 | 12 |
|
11 | | - "sigs.k8s.io/yaml" |
| 13 | + yamlutil "k8s.io/apimachinery/pkg/util/yaml" |
12 | 14 |
|
13 | 15 | apiv1 "github.com/operator-framework/operator-controller/api/v1" |
14 | 16 | ) |
15 | 17 |
|
16 | 18 | // ParseImageRefsFromCatalog reads the catalogs from the files used and returns a list of image references. |
17 | 19 | func ParseImageRefsFromCatalog(catalogsPath string) ([]string, error) { |
18 | | - var images []string |
19 | | - |
20 | 20 | re := regexp.MustCompile(`{{.*}}`) |
21 | 21 |
|
22 | | - // Check if the directory exists first |
23 | | - if _, err := os.Stat(catalogsPath); os.IsNotExist(err) { |
24 | | - return nil, fmt.Errorf("catalogs path %s does not exist", catalogsPath) |
| 22 | + info, err := os.Stat(catalogsPath) |
| 23 | + if err != nil { |
| 24 | + if os.IsNotExist(err) { |
| 25 | + return nil, fmt.Errorf("catalogs path %s does not exist", catalogsPath) |
| 26 | + } |
| 27 | + return nil, err |
25 | 28 | } |
26 | 29 |
|
27 | | - err := filepath.WalkDir(catalogsPath, func(path string, d fs.DirEntry, err error) error { |
| 30 | + var images []string |
| 31 | + if info.IsDir() { |
| 32 | + err = filepath.WalkDir(catalogsPath, func(path string, d fs.DirEntry, err error) error { |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + if d.IsDir() || (!strings.HasSuffix(d.Name(), ".yaml") && |
| 38 | + !strings.HasSuffix(d.Name(), ".yml")) { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + content, err := os.ReadFile(path) |
| 43 | + if err != nil { |
| 44 | + return nil |
| 45 | + } |
| 46 | + |
| 47 | + refs, err := parseClusterCatalogs(content, re) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + images = append(images, refs...) |
| 52 | + return nil |
| 53 | + }) |
28 | 54 | if err != nil { |
29 | | - return err |
| 55 | + return nil, err |
30 | 56 | } |
31 | | - |
32 | | - if d.IsDir() || (!strings.HasSuffix(d.Name(), ".yaml") && |
33 | | - !strings.HasSuffix(d.Name(), ".yml")) { |
34 | | - return nil |
| 57 | + } else { |
| 58 | + content, err := os.ReadFile(catalogsPath) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
35 | 61 | } |
36 | | - |
37 | | - content, err := os.ReadFile(path) |
| 62 | + refs, err := parseClusterCatalogs(content, re) |
38 | 63 | if err != nil { |
39 | | - return nil |
| 64 | + return nil, err |
40 | 65 | } |
41 | | - // Replace any helm templating |
42 | | - content = re.ReplaceAll(content, []byte{}) |
| 66 | + images = append(images, refs...) |
| 67 | + } |
| 68 | + |
| 69 | + if len(images) == 0 { |
| 70 | + return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath) |
| 71 | + } |
43 | 72 |
|
| 73 | + return images, nil |
| 74 | +} |
| 75 | + |
| 76 | +func parseClusterCatalogs(content []byte, re *regexp.Regexp) ([]string, error) { |
| 77 | + // Replace any helm templating |
| 78 | + content = re.ReplaceAll(content, []byte{}) |
| 79 | + |
| 80 | + decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(content), 4096) |
| 81 | + |
| 82 | + var images []string |
| 83 | + for { |
44 | 84 | var catalog apiv1.ClusterCatalog |
45 | | - if err := yaml.Unmarshal(content, &catalog); err != nil { |
46 | | - return nil |
| 85 | + if err := decoder.Decode(&catalog); err != nil { |
| 86 | + if err == io.EOF { |
| 87 | + break |
| 88 | + } |
| 89 | + return nil, err |
47 | 90 | } |
48 | 91 |
|
49 | 92 | if catalog.Kind != "ClusterCatalog" { |
50 | | - return nil |
| 93 | + continue |
51 | 94 | } |
52 | 95 |
|
53 | 96 | if catalog.Spec.Source.Type == apiv1.SourceTypeImage && catalog.Spec.Source.Image != nil { |
54 | 97 | images = append(images, catalog.Spec.Source.Image.Ref) |
55 | 98 | } |
56 | | - |
57 | | - return nil |
58 | | - }) |
59 | | - |
60 | | - if err != nil { |
61 | | - return nil, err |
62 | | - } |
63 | | - |
64 | | - if len(images) == 0 { |
65 | | - return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath) |
66 | 99 | } |
67 | 100 |
|
68 | 101 | return images, nil |
|
0 commit comments