-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
image.go
141 lines (120 loc) · 4.46 KB
/
image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
Copyright 2020 The Kubernetes 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 container implements container utility functionality.
package container
import (
// Import the crypto sha256 algorithm for the docker image parser to work
_ "crypto/sha256"
// Import the crypto/sha512 algorithm for the docker image parser to work with 384 and 512 sha hashes
_ "crypto/sha512"
"fmt"
"path"
"regexp"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
)
var (
ociTagAllowedChars = regexp.MustCompile(`[^-a-zA-Z0-9_\.]`)
)
// Image type represents the container image details.
type Image struct {
Repository string
Name string
Tag string
Digest string
}
// ImageFromString parses a docker image string into three parts: repo, tag and digest.
func ImageFromString(image string) (Image, error) {
named, err := reference.ParseNamed(image)
if err != nil {
return Image{}, fmt.Errorf("couldn't parse image name: %v", err)
}
var repo, tag, digest string
_, nameOnly := path.Split(reference.Path(named))
if nameOnly != "" {
// split out the part of the name after the last /
lenOfCompleteName := len(named.Name())
repo = named.Name()[:lenOfCompleteName-len(nameOnly)-1]
}
tagged, ok := named.(reference.Tagged)
if ok {
tag = tagged.Tag()
}
digested, ok := named.(reference.Digested)
if ok {
digest = digested.Digest().String()
}
return Image{Repository: repo, Name: nameOnly, Tag: tag, Digest: digest}, nil
}
func (i Image) String() string {
// repo/name [ ":" tag ] [ "@" digest ]
ref := fmt.Sprintf("%s/%s", i.Repository, i.Name)
if i.Tag != "" {
ref = fmt.Sprintf("%s:%s", ref, i.Tag)
}
if i.Digest != "" {
ref = fmt.Sprintf("%s@%s", ref, i.Digest)
}
return ref
}
// ModifyImageRepository takes an imageName (e.g., repository/image:tag), and returns an image name with updated repository.
func ModifyImageRepository(imageName, repositoryName string) (string, error) {
image, err := ImageFromString(imageName)
if err != nil {
return "", errors.Wrap(err, "failed to parse image name")
}
nameUpdated, err := reference.WithName(path.Join(repositoryName, image.Name))
if err != nil {
return "", errors.Wrap(err, "failed to update repository name")
}
if image.Tag != "" {
retagged, err := reference.WithTag(nameUpdated, image.Tag)
if err != nil {
return "", errors.Wrap(err, "failed to parse image tag")
}
return reference.FamiliarString(retagged), nil
}
return "", errors.New("image must be tagged")
}
// ModifyImageTag takes an imageName (e.g., repository/image:tag), and returns an image name with updated tag.
func ModifyImageTag(imageName, tagName string) (string, error) {
normalisedTagName := SemverToOCIImageTag(tagName)
namedRef, err := reference.ParseNormalizedNamed(imageName)
if err != nil {
return "", errors.Wrap(err, "failed to parse image name")
}
// return error if images use digest as version instead of tag
if _, isCanonical := namedRef.(reference.Canonical); isCanonical {
return "", errors.New("image uses digest as version, cannot update tag ")
}
// update the image tag with tagName
namedTagged, err := reference.WithTag(namedRef, normalisedTagName)
if err != nil {
return "", errors.Wrap(err, "failed to update image tag")
}
return reference.FamiliarString(reference.TagNameOnly(namedTagged)), nil
}
// ImageTagIsValid ensures that a given image tag is compliant with the OCI spec.
func ImageTagIsValid(tagName string) bool {
return !ociTagAllowedChars.MatchString(tagName)
}
// SemverToOCIImageTag is a helper function that replaces all
// non-allowed symbols in tag strings with underscores.
// Image tag can only contain lowercase and uppercase letters, digits,
// underscores, periods and dashes.
// Current usage is for CI images where all of symbols except '+' are valid,
// but function is for generic usage where input can't be always pre-validated.
// Taken from k8s.io/cmd/kubeadm/app/util.
func SemverToOCIImageTag(version string) string {
return ociTagAllowedChars.ReplaceAllString(version, "_")
}