Files
truecharts/clustertool/pkg/charts/image/image.go
T
Kjeld SchoutenandGitHub fd8f1add2a Change tccr.io into oci.trueforge.org (#38868)
**Description**
We're moving from tccr.io to oci.trueforge.org

TCCR meant "TrueCharts Container Repository" however, it mostly stores
chart artefacts.
At the same time, we want to more clearly allow us to store
containers/artifacts not related to truecharts.

Its also worth noting that an additional .io domain carries additional
costs.

**⚙️ Type of change**

- [ ] ⚙️ Feature/App addition
- [ ] 🪛 Bugfix
- [ ] ⚠️ Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] 🔃 Refactor of current code
- [ ] 📜 Documentation Changes

**🧪 How Has This Been Tested?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

**📃 Notes:**
<!-- Please enter any other relevant information here -->

**✔️ Checklist:**

- [ ] ⚖️ My code follows the style guidelines of this project
- [ ] 👀 I have performed a self-review of my own code
- [ ] #️⃣ I have commented my code, particularly in hard-to-understand
areas
- [ ] 📄 I have made changes to the documentation
- [ ] 🧪 I have added tests to this description that prove my fix is
effective or that my feature works
- [ ] ⬆️ I increased versions for any altered app according to semantic
versioning
- [ ] I made sure the title starts with `feat(chart-name):`,
`fix(chart-name):`, `chore(chart-name):`, `docs(chart-name):` or
`fix(docs):`

** App addition**

If this PR is an app addition please make sure you have done the
following.

- [ ] 🖼️ I have added an icon in the Chart's root directory called
`icon.png`

---

_Please don't blindly check all the boxes. Read them and only check
those that apply.
Those checkboxes are there for the reviewer to see what is this all
about and
the status of this PR with a quick glance._
2025-08-29 14:44:36 +02:00

147 lines
5.0 KiB
Go

package image
import (
"fmt"
"regexp"
"strings"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/v2"
"github.com/rs/zerolog/log"
)
// Images represents the structure of values.yaml.
type Images struct {
ImagesMap map[string]ImageDetails
K *koanf.Koanf
}
// ImageDetails represents details for each image.
type ImageDetails struct {
Repository string `yaml:"repository"`
Tag string `yaml:"tag"`
Version string
Link string
// Add other fields as needed
}
var imageRegex = regexp.MustCompile(`^image|[a-zA-Z0-9]+Image$`)
func (i *Images) LoadValuesFile(filename string) error {
// Initialize koanf instance
i.K = koanf.New(".")
// Load YAML file using koanf
if err := i.K.Load(file.Provider(filename), yaml.Parser()); err != nil {
return err
}
// List only root-level keys that match the criteria
keys := getFilteredRootLevelKeys(i.K)
i.ImagesMap = make(map[string]ImageDetails)
for _, key := range keys {
// Extract relevant fields from the loaded configuration
var img ImageDetails
if err := i.K.Unmarshal(key, &img); err != nil {
return err
}
// Set the Link field based on the repository
img.Link = constructLink(img.Repository)
// Set the Version field based on the tag
version, err := CleanTag(img.Tag)
if err != nil {
log.Error().Err(err).Msg("❌ Failed to clean tag")
}
img.Version = version
// Save the extracted values to the struct
i.ImagesMap[key] = img
}
return nil
}
func getFilteredRootLevelKeys(k *koanf.Koanf) []string {
filteredKeys := []string{}
// k.Raw() returns a map[string]interface{} with all the keys and their values
// This means the keys will only be the root-level keys, we can drill into the
// values later if we want the nested keys.
for key := range k.Raw() {
if key == "imageSelector" {
log.Error().Msg("❌ Found [imageSelector] in top level keys, this is not supported.")
continue
}
// Filter keys that match the regex
if imageRegex.MatchString(key) {
filteredKeys = append(filteredKeys, key)
}
}
return filteredKeys
}
// constructLink constructs a link based on the repository using the logic from the main function.
func constructLink(repository string) string {
prefix := ""
switch {
case strings.HasPrefix(repository, "lscr.io/linuxserver/"):
prefix = "https://fleet.linuxserver.io/image?name="
repository = strings.TrimPrefix(repository, "lscr.io/")
case strings.HasPrefix(repository, "oci.trueforge.org/tccr/"):
prefix = "https://github.com/truecharts/containers/tree/master/apps/"
repository = strings.TrimPrefix(repository, "oci.trueforge.org/tccr/")
case strings.HasPrefix(repository, "mcr.microsoft.com/"):
prefix = "https://mcr.microsoft.com/en-us/product/"
repository = strings.TrimPrefix(repository, "mcr.microsoft.com/")
case strings.HasPrefix(repository, "public.ecr.aws/"):
prefix = "https://gallery.ecr.aws/"
repository = strings.TrimPrefix(repository, "public.ecr.aws/")
case strings.HasPrefix(repository, "ghcr.io/"):
prefix = "https://"
case strings.HasPrefix(repository, "quay.io/"):
prefix = "https://"
case strings.HasPrefix(repository, "gcr.io/"):
prefix = "https://"
case strings.Contains(repository, ".azurecr.io/"):
reg := fmt.Sprintf(`%s.azurecr.io/`, strings.Split(repository, ".")[0])
prefix = fmt.Sprintf("https://%s", reg)
repository = strings.TrimPrefix(repository, reg)
case strings.Contains(repository, ".ocir.io/"):
prefix = ""
default:
// Docker Hub or unknown registry
prefix = "https://hub.docker.com/r/"
repository = strings.TrimPrefix(repository, "docker.io/")
repository = strings.TrimPrefix(repository, "index.docker.io/")
repository = strings.TrimPrefix(repository, "registry-1.docker.io/")
repository = strings.TrimPrefix(repository, "registry.hub.docker.com/")
// Check for Docker Official Image
if strings.Count(repository, "/") == 0 || strings.HasPrefix(repository, "library/") {
prefix = "https://hub.docker.com/_/"
repository = strings.TrimPrefix(repository, "library/")
}
// Avoid creating a bad link if the image name has more than 1 slash
slashes := strings.Count(repository, "/")
if slashes > 1 {
prefix = ""
log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository)
}
}
if prefix == "" {
log.Warn().Msgf("WARNING: Could not determine source repository url for [%s]", repository)
return ""
}
containerURL := fmt.Sprintf("%s%s", prefix, repository)
return containerURL
}