woodpecker/pipeline/frontend/yaml/utils/image.go

108 lines
2.7 KiB
Go
Raw Normal View History

// Copyright 2023 Woodpecker Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package utils
2017-03-05 07:56:08 +00:00
import (
"strings"
"github.com/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
}
// 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.
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
// MatchImageDynamic check if image is in list based on list.
// If an list entry has a tag specified it only will match if both are the same, else the tag is ignored.
func MatchImageDynamic(from string, to ...string) bool {
fullFrom := expandImage(from)
trimFrom := trimImage(from)
for _, match := range to {
if imageHasTag(match) {
if fullFrom == expandImage(match) {
return true
}
} else {
if trimFrom == trimImage(match) {
return true
}
}
}
return false
}
func imageHasTag(name string) bool {
return strings.Contains(name, ":")
}
// ParseNamed parses an image as a reference to validate it then parses it as a named reference.
func ParseNamed(image string) (reference.Named, error) {
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 nil, err
2017-04-06 16:04:25 +00:00
}
return reference.ParseNamed(ref.String())
}
// MatchHostname returns true if the image hostname
// matches the specified hostname.
func MatchHostname(image, hostname string) bool {
named, err := ParseNamed(image)
2020-05-18 14:46:13 +00:00
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
}