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