|
1 | 1 | package image |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "fmt" |
5 | | - "regexp" |
6 | | - "strings" |
7 | | - |
8 | | - "github.com/knadh/koanf/parsers/yaml" |
9 | | - "github.com/knadh/koanf/providers/file" |
10 | | - "github.com/knadh/koanf/v2" |
11 | | - "github.com/rs/zerolog/log" |
| 4 | + "fmt" |
| 5 | + "regexp" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/knadh/koanf/parsers/yaml" |
| 9 | + "github.com/knadh/koanf/providers/file" |
| 10 | + "github.com/knadh/koanf/v2" |
| 11 | + "github.com/rs/zerolog/log" |
12 | 12 | ) |
13 | 13 |
|
14 | 14 | // Images represents the structure of values.yaml. |
15 | 15 | type Images struct { |
16 | | - ImagesMap map[string]ImageDetails |
17 | | - K *koanf.Koanf |
| 16 | + ImagesMap map[string]ImageDetails |
| 17 | + K *koanf.Koanf |
18 | 18 | } |
19 | 19 |
|
20 | 20 | // ImageDetails represents details for each image. |
21 | 21 | type ImageDetails struct { |
22 | | - Repository string `yaml:"repository"` |
23 | | - Tag string `yaml:"tag"` |
24 | | - Version string |
25 | | - Link string |
26 | | - // Add other fields as needed |
| 22 | + Repository string `yaml:"repository"` |
| 23 | + Tag string `yaml:"tag"` |
| 24 | + Version string |
| 25 | + Link string |
| 26 | + // Add other fields as needed |
27 | 27 | } |
28 | 28 |
|
29 | 29 | var imageRegex = regexp.MustCompile(`^image|[a-zA-Z0-9]+Image$`) |
30 | 30 |
|
31 | 31 | func (i *Images) LoadValuesFile(filename string) error { |
32 | | - // Initialize koanf instance |
33 | | - i.K = koanf.New(".") |
34 | | - |
35 | | - // Load YAML file using koanf |
36 | | - if err := i.K.Load(file.Provider(filename), yaml.Parser()); err != nil { |
37 | | - return err |
38 | | - } |
39 | | - |
40 | | - // List only root-level keys that match the criteria |
41 | | - keys := getFilteredRootLevelKeys(i.K) |
42 | | - i.ImagesMap = make(map[string]ImageDetails) |
43 | | - for _, key := range keys { |
44 | | - // Extract relevant fields from the loaded configuration |
45 | | - var img ImageDetails |
46 | | - if err := i.K.Unmarshal(key, &img); err != nil { |
47 | | - return err |
48 | | - } |
49 | | - |
50 | | - // Set the Link field based on the repository |
51 | | - img.Link = constructLink(img.Repository) |
52 | | - |
53 | | - // Set the Version field based on the tag |
54 | | - version, err := CleanTag(img.Tag) |
55 | | - if err != nil { |
56 | | - log.Error().Err(err).Msg("❌ Failed to clean tag") |
57 | | - } |
58 | | - |
59 | | - img.Version = version |
60 | | - |
61 | | - // Save the extracted values to the struct |
62 | | - i.ImagesMap[key] = img |
63 | | - } |
64 | | - |
65 | | - return nil |
| 32 | + // Initialize koanf instance |
| 33 | + i.K = koanf.New(".") |
| 34 | + |
| 35 | + // Load YAML file using koanf |
| 36 | + if err := i.K.Load(file.Provider(filename), yaml.Parser()); err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + // List only root-level keys that match the criteria |
| 41 | + keys := getFilteredRootLevelKeys(i.K) |
| 42 | + i.ImagesMap = make(map[string]ImageDetails) |
| 43 | + for _, key := range keys { |
| 44 | + // Extract relevant fields from the loaded configuration |
| 45 | + var img ImageDetails |
| 46 | + if err := i.K.Unmarshal(key, &img); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + // Set the Link field based on the repository |
| 51 | + img.Link = constructLink(img.Repository) |
| 52 | + |
| 53 | + // Set the Version field based on the tag |
| 54 | + version, err := CleanTag(img.Tag) |
| 55 | + if err != nil { |
| 56 | + log.Error().Err(err).Msg("❌ Failed to clean tag") |
| 57 | + } |
| 58 | + |
| 59 | + img.Version = version |
| 60 | + |
| 61 | + // Save the extracted values to the struct |
| 62 | + i.ImagesMap[key] = img |
| 63 | + } |
| 64 | + |
| 65 | + return nil |
66 | 66 | } |
67 | 67 |
|
68 | 68 | func getFilteredRootLevelKeys(k *koanf.Koanf) []string { |
69 | | - filteredKeys := []string{} |
70 | | - |
71 | | - // k.Raw() returns a map[string]interface{} with all the keys and their values |
72 | | - // This means the keys will only be the root-level keys, we can drill into the |
73 | | - // values later if we want the nested keys. |
74 | | - for key := range k.Raw() { |
75 | | - if key == "imageSelector" { |
76 | | - log.Error().Msg("❌ Found [imageSelector] in top level keys, this is not supported.") |
77 | | - continue |
78 | | - } |
79 | | - // Filter keys that match the regex |
80 | | - if imageRegex.MatchString(key) { |
81 | | - filteredKeys = append(filteredKeys, key) |
82 | | - } |
83 | | - } |
84 | | - |
85 | | - return filteredKeys |
| 69 | + filteredKeys := []string{} |
| 70 | + |
| 71 | + // k.Raw() returns a map[string]interface{} with all the keys and their values |
| 72 | + // This means the keys will only be the root-level keys, we can drill into the |
| 73 | + // values later if we want the nested keys. |
| 74 | + for key := range k.Raw() { |
| 75 | + if key == "imageSelector" { |
| 76 | + log.Error().Msg("❌ Found [imageSelector] in top level keys, this is not supported.") |
| 77 | + continue |
| 78 | + } |
| 79 | + // Filter keys that match the regex |
| 80 | + if imageRegex.MatchString(key) { |
| 81 | + filteredKeys = append(filteredKeys, key) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return filteredKeys |
86 | 86 | } |
87 | 87 |
|
88 | 88 | // constructLink constructs a link based on the repository using the logic from the main function. |
89 | 89 | func constructLink(repository string) string { |
90 | | - prefix := "" |
91 | | - |
92 | | - switch { |
93 | | - case strings.HasPrefix(repository, "lscr.io/linuxserver/"): |
94 | | - prefix = "https://fleet.linuxserver.io/image?name=" |
95 | | - repository = strings.TrimPrefix(repository, "lscr.io/") |
96 | | - case strings.HasPrefix(repository, "oci.trueforge.org/tccr/"): |
97 | | - prefix = "https://github.com/truecharts/containers/tree/master/apps/" |
98 | | - repository = strings.TrimPrefix(repository, "oci.trueforge.org/tccr/") |
99 | | - case strings.HasPrefix(repository, "mcr.microsoft.com/"): |
100 | | - prefix = "https://mcr.microsoft.com/en-us/product/" |
101 | | - repository = strings.TrimPrefix(repository, "mcr.microsoft.com/") |
102 | | - case strings.HasPrefix(repository, "public.ecr.aws/"): |
103 | | - prefix = "https://gallery.ecr.aws/" |
104 | | - repository = strings.TrimPrefix(repository, "public.ecr.aws/") |
105 | | - case strings.HasPrefix(repository, "ghcr.io/"): |
106 | | - prefix = "https://" |
107 | | - case strings.HasPrefix(repository, "quay.io/"): |
108 | | - prefix = "https://" |
109 | | - case strings.HasPrefix(repository, "gcr.io/"): |
110 | | - prefix = "https://" |
111 | | - case strings.Contains(repository, ".azurecr.io/"): |
112 | | - reg := fmt.Sprintf(`%s.azurecr.io/`, strings.Split(repository, ".")[0]) |
113 | | - prefix = fmt.Sprintf("https://%s", reg) |
114 | | - repository = strings.TrimPrefix(repository, reg) |
115 | | - case strings.Contains(repository, ".ocir.io/"): |
116 | | - prefix = "" |
117 | | - default: |
118 | | - // Docker Hub or unknown registry |
119 | | - prefix = "https://hub.docker.com/r/" |
120 | | - repository = strings.TrimPrefix(repository, "docker.io/") |
121 | | - repository = strings.TrimPrefix(repository, "index.docker.io/") |
122 | | - repository = strings.TrimPrefix(repository, "registry-1.docker.io/") |
123 | | - repository = strings.TrimPrefix(repository, "registry.hub.docker.com/") |
124 | | - |
125 | | - // Check for Docker Official Image |
126 | | - if strings.Count(repository, "/") == 0 || strings.HasPrefix(repository, "library/") { |
127 | | - prefix = "https://hub.docker.com/_/" |
128 | | - repository = strings.TrimPrefix(repository, "library/") |
129 | | - } |
130 | | - |
131 | | - // Avoid creating a bad link if the image name has more than 1 slash |
132 | | - slashes := strings.Count(repository, "/") |
133 | | - if slashes > 1 { |
134 | | - prefix = "" |
135 | | - log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository) |
136 | | - } |
137 | | - } |
138 | | - |
139 | | - if prefix == "" { |
140 | | - log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository) |
141 | | - return "" |
142 | | - } |
143 | | - |
144 | | - containerURL := fmt.Sprintf("%s%s", prefix, repository) |
145 | | - return containerURL |
| 90 | + prefix := "" |
| 91 | + |
| 92 | + switch { |
| 93 | + case strings.HasPrefix(repository, "lscr.io/linuxserver/"): |
| 94 | + prefix = "https://fleet.linuxserver.io/image?name=" |
| 95 | + repository = strings.TrimPrefix(repository, "lscr.io/") |
| 96 | + case strings.HasPrefix(repository, "oci.trueforge.org/containerforge/"): |
| 97 | + prefix = "https://github.com/trueforge-org/containers/tree/main/apps/" |
| 98 | + repository = strings.TrimPrefix(repository, "oci.trueforge.org/tccr/") |
| 99 | + case strings.HasPrefix(repository, "mcr.microsoft.com/"): |
| 100 | + prefix = "https://mcr.microsoft.com/en-us/product/" |
| 101 | + repository = strings.TrimPrefix(repository, "mcr.microsoft.com/") |
| 102 | + case strings.HasPrefix(repository, "public.ecr.aws/"): |
| 103 | + prefix = "https://gallery.ecr.aws/" |
| 104 | + repository = strings.TrimPrefix(repository, "public.ecr.aws/") |
| 105 | + case strings.HasPrefix(repository, "ghcr.io/"): |
| 106 | + prefix = "https://" |
| 107 | + case strings.HasPrefix(repository, "quay.io/"): |
| 108 | + prefix = "https://" |
| 109 | + case strings.HasPrefix(repository, "gcr.io/"): |
| 110 | + prefix = "https://" |
| 111 | + case strings.Contains(repository, ".azurecr.io/"): |
| 112 | + reg := fmt.Sprintf(`%s.azurecr.io/`, strings.Split(repository, ".")[0]) |
| 113 | + prefix = fmt.Sprintf("https://%s", reg) |
| 114 | + repository = strings.TrimPrefix(repository, reg) |
| 115 | + case strings.Contains(repository, ".ocir.io/"): |
| 116 | + prefix = "" |
| 117 | + default: |
| 118 | + // Docker Hub or unknown registry |
| 119 | + prefix = "https://hub.docker.com/r/" |
| 120 | + repository = strings.TrimPrefix(repository, "docker.io/") |
| 121 | + repository = strings.TrimPrefix(repository, "index.docker.io/") |
| 122 | + repository = strings.TrimPrefix(repository, "registry-1.docker.io/") |
| 123 | + repository = strings.TrimPrefix(repository, "registry.hub.docker.com/") |
| 124 | + |
| 125 | + // Check for Docker Official Image |
| 126 | + if strings.Count(repository, "/") == 0 || strings.HasPrefix(repository, "library/") { |
| 127 | + prefix = "https://hub.docker.com/_/" |
| 128 | + repository = strings.TrimPrefix(repository, "library/") |
| 129 | + } |
| 130 | + |
| 131 | + // Avoid creating a bad link if the image name has more than 1 slash |
| 132 | + slashes := strings.Count(repository, "/") |
| 133 | + if slashes > 1 { |
| 134 | + prefix = "" |
| 135 | + log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if prefix == "" { |
| 140 | + log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository) |
| 141 | + return "" |
| 142 | + } |
| 143 | + |
| 144 | + containerURL := fmt.Sprintf("%s%s", prefix, repository) |
| 145 | + return containerURL |
146 | 146 | } |
0 commit comments