Skip to content

Commit 5e7c161

Browse files
committed
add back api
1 parent 4b2d8cd commit 5e7c161

File tree

4 files changed

+49
-23
lines changed

4 files changed

+49
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
This package is forked from
22
`sigs.k8s.io/kustomize/api/`
3-
with version `api/0.8.11`.
3+
with version `api/v0.12.1`.

functions/go/set-image/third_party/sigs.k8s.io/kustomize/api/image/image.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,53 @@ func IsImageMatched(s, t string) bool {
1414
// Tag values are limited to [a-zA-Z0-9_.{}-].
1515
// Some tools like Bazel rules_k8s allow tag patterns with {} characters.
1616
// More info: https://github.com/bazelbuild/rules_k8s/pull/423
17-
pattern, _ := regexp.Compile("^" + t + "(@sha256)?(:[a-zA-Z0-9_.{}-]*)?$")
17+
pattern, _ := regexp.Compile("^" + t + "(:[a-zA-Z0-9_.{}-]*)?(@sha256:[a-zA-Z0-9_.{}-]*)?$")
1818
return pattern.MatchString(s)
1919
}
2020

2121
// Split separates and returns the name and tag parts
2222
// from the image string using either colon `:` or at `@` separators.
23-
// Note that the returned tag keeps its separator.
24-
func Split(imageName string) (name string, tag string) {
23+
// image reference pattern: [[host[:port]/]component/]component[:tag][@digest]
24+
func Split(imageName string) (name string, tag string, digest string) {
2525
// check if image name contains a domain
2626
// if domain is present, ignore domain and check for `:`
27-
ic := -1
28-
if slashIndex := strings.Index(imageName, "/"); slashIndex < 0 {
29-
ic = strings.LastIndex(imageName, ":")
27+
searchName := imageName
28+
slashIndex := strings.Index(imageName, "/")
29+
if slashIndex > 0 {
30+
searchName = imageName[slashIndex:]
3031
} else {
31-
lastIc := strings.LastIndex(imageName[slashIndex:], ":")
32-
// set ic only if `:` is present
33-
if lastIc > 0 {
34-
ic = slashIndex + lastIc
35-
}
32+
slashIndex = 0
3633
}
37-
ia := strings.LastIndex(imageName, "@")
38-
if ic < 0 && ia < 0 {
39-
return imageName, ""
34+
35+
id := strings.Index(searchName, "@")
36+
ic := strings.Index(searchName, ":")
37+
38+
// no tag or digest
39+
if ic < 0 && id < 0 {
40+
return imageName, "", ""
41+
}
42+
43+
// digest only
44+
if id >= 0 && (id < ic || ic < 0) {
45+
id += slashIndex
46+
name = imageName[:id]
47+
digest = strings.TrimPrefix(imageName[id:], "@")
48+
return name, "", digest
4049
}
4150

42-
i := ic
43-
if ia > 0 {
44-
i = ia
51+
// tag and digest
52+
if id >= 0 && ic >= 0 {
53+
id += slashIndex
54+
ic += slashIndex
55+
name = imageName[:ic]
56+
tag = strings.TrimPrefix(imageName[ic:id], ":")
57+
digest = strings.TrimPrefix(imageName[id:], "@")
58+
return name, tag, digest
4559
}
4660

47-
name = imageName[:i]
48-
tag = imageName[i:]
49-
return
61+
// tag only
62+
ic += slashIndex
63+
name = imageName[:ic]
64+
tag = strings.TrimPrefix(imageName[ic:], ":")
65+
return name, tag, ""
5066
}

functions/go/set-image/third_party/sigs.k8s.io/kustomize/api/types/image.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ type Image struct {
1212
// NewName is the value used to replace the original name.
1313
NewName string `json:"newName,omitempty" yaml:"newName,omitempty"`
1414

15+
// TagSuffix is the value used to suffix the original tag
16+
// If Digest and NewTag is present an error is thrown
17+
TagSuffix string `json:"tagSuffix,omitempty" yaml:"tagSuffix,omitempty"`
18+
1519
// NewTag is the value used to replace the original tag.
1620
NewTag string `json:"newTag,omitempty" yaml:"newTag,omitempty"`
1721

functions/go/set-image/transformer/images_transformer.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ func (t *SetImage) hasPodContainers(o *fn.KubeObject) bool {
175175

176176
// getNewImageName return the new name for image field
177177
func getNewImageName(oldValue string, newImage Image) string {
178-
name, tag := image.Split(oldValue)
178+
name, tag, digest := image.Split(oldValue)
179179
if newImage.NewName != "" {
180180
name = newImage.NewName
181181
}
@@ -185,7 +185,13 @@ func getNewImageName(oldValue string, newImage Image) string {
185185
if newImage.Digest != "" {
186186
tag = "@" + newImage.Digest
187187
}
188-
newName := name + tag
188+
var newName string
189+
if tag == "" {
190+
newName = name + digest
191+
} else {
192+
newName = name + tag
193+
}
194+
189195
return newName
190196
}
191197

0 commit comments

Comments
 (0)