Add rewrite support to hosts.toml loader

Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
This commit is contained in:
Brad Davidson
2023-11-30 18:03:22 +00:00
parent ba6b205d0f
commit f660f4424f
3 changed files with 56 additions and 18 deletions

View File

@@ -197,7 +197,7 @@ type Mirror struct {
// The scheme, host and path from the endpoint URL will be used.
Endpoints []string `toml:"endpoint" json:"endpoint"`
// Rewrites are repository rewrite rules for a namespace. When fetching image resources
// Rewrites is a map of repository rewrite rules for a namespace. When fetching image resources
// from an endpoint and a key matches the repository via regular expression matching
// it will be replaced with the corresponding value from the map in the resource request.
//

View File

@@ -437,14 +437,14 @@ func (c *CRIImageService) registryHosts(ctx context.Context, credentials func(ho
return func(host string) ([]docker.RegistryHost, error) {
var registries []docker.RegistryHost
endpoints, err := c.registryEndpoints(host)
if err != nil {
return nil, fmt.Errorf("get registry endpoints: %w", err)
}
rewrites, err := c.registryRewrites(host)
if err != nil {
return nil, fmt.Errorf("get registry rewrites: %w", err)
}
endpoints, err := c.registryEndpoints(host)
if err != nil {
return nil, fmt.Errorf("get registry endpoints: %w", err)
}
for _, e := range endpoints {
u, err := url.Parse(e)
if err != nil {
@@ -570,17 +570,13 @@ func (c *CRIImageService) registryEndpoints(host string) ([]string, error) {
}
func (c *CRIImageService) registryRewrites(host string) (map[string]string, error) {
var rewrites map[string]string
_, ok := c.config.Registry.Mirrors[host]
if ok {
rewrites = c.config.Registry.Mirrors[host].Rewrites
} else {
rewrites = c.config.Registry.Mirrors["*"].Rewrites
hosts := []string{host, "*"}
for _, host := range hosts {
if host, ok := c.config.Registry.Mirrors[host]; ok {
return host.Rewrites, nil
}
}
if rewrites == nil {
rewrites = map[string]string{}
}
return rewrites, nil
return nil, nil
}
// newTransport returns a new HTTP transport used to pull image.