2023-07-09 21:41:13 +00:00
|
|
|
package utils
|
2017-03-05 07:56:08 +00:00
|
|
|
|
2020-05-18 14:46:13 +00:00
|
|
|
import "github.com/docker/distribution/reference"
|
2017-03-05 07:56:08 +00:00
|
|
|
|
|
|
|
// trimImage returns the short image name without tag.
|
|
|
|
func trimImage(name string) string {
|
2020-05-18 14:46:13 +00:00
|
|
|
ref, err := reference.ParseAnyReference(name)
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
2020-05-18 14:46:13 +00:00
|
|
|
named, err := reference.ParseNamed(ref.String())
|
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
named = reference.TrimNamed(named)
|
|
|
|
return reference.FamiliarName(named)
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// expandImage returns the fully qualified image name.
|
|
|
|
func expandImage(name string) string {
|
2020-05-18 14:46:13 +00:00
|
|
|
ref, err := reference.ParseAnyReference(name)
|
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
named, err := reference.ParseNamed(ref.String())
|
2017-03-05 07:56:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
2020-05-18 14:46:13 +00:00
|
|
|
named = reference.TagNameOnly(named)
|
|
|
|
return named.String()
|
2017-03-05 07:56:08 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 21:41:13 +00:00
|
|
|
// MatchImage returns true if the image name matches
|
2017-03-05 07:56:08 +00:00
|
|
|
// an image in the list. Note the image tag is not used
|
|
|
|
// in the matching logic.
|
2023-07-09 21:41:13 +00:00
|
|
|
func MatchImage(from string, to ...string) bool {
|
2017-03-05 07:56:08 +00:00
|
|
|
from = trimImage(from)
|
|
|
|
for _, match := range to {
|
2017-04-10 16:27:34 +00:00
|
|
|
if from == trimImage(match) {
|
2017-03-05 07:56:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2017-04-06 16:04:25 +00:00
|
|
|
|
2023-07-09 21:41:13 +00:00
|
|
|
// MatchHostname returns true if the image hostname
|
2017-04-06 16:04:25 +00:00
|
|
|
// matches the specified hostname.
|
2023-07-09 21:41:13 +00:00
|
|
|
func MatchHostname(image, hostname string) bool {
|
2020-05-18 14:46:13 +00:00
|
|
|
ref, err := reference.ParseAnyReference(image)
|
2017-04-06 16:04:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2020-05-18 14:46:13 +00:00
|
|
|
named, err := reference.ParseNamed(ref.String())
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if hostname == "index.docker.io" {
|
|
|
|
hostname = "docker.io"
|
|
|
|
}
|
|
|
|
return reference.Domain(named) == hostname
|
2017-04-06 16:04:25 +00:00
|
|
|
}
|