Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions pkg/kptfile/kptfileutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func shouldAddFnKeyUtil(fns []kptfilev1.Function) bool {
if fn.Exec != "" {
key = fn.Exec
} else {
key = strings.Split(fn.Image, ":")[0]
key = imageMergeKey(fn.Image)
}
if keySet.Has(key) {
return false
Expand All @@ -505,6 +505,16 @@ func shouldAddFnKeyUtil(fns []kptfilev1.Function) bool {
return true
}

// imageMergeKey returns the function image without its tag or digest.
// A colon before the last "/" is the port of the registry and is kept.
func imageMergeKey(image string) string {
image, _, _ = strings.Cut(image, "@")
if i := strings.LastIndex(image, ":"); i > strings.LastIndex(image, "/") {
return image[:i]
}
return image
}

// addNameForMerge adds name field for all the functions if empty
// name is primarily used as MergeKptfiles-key
func addNameForMerge(kfs ...*kptfilev1.KptFile) {
Expand All @@ -531,10 +541,7 @@ func addName(fn kptfilev1.Function) kptfilev1.Function {
if fn.Exec != "" {
key = fn.Exec
} else {
parts := strings.Split(fn.Image, ":")
if len(parts) > 0 {
key = parts[0]
}
key = imageMergeKey(fn.Image)
}
fn.Name = fmt.Sprintf("_kpt-merge_%s", key)
return fn
Expand Down
108 changes: 107 additions & 1 deletion pkg/kptfile/kptfileutil/util_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2020 The kpt Authors
// Copyright 2020-2026 The kpt Authors
Comment thread
aravindtga marked this conversation as resolved.
Resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -828,6 +828,71 @@ pipeline:
`,
},

"functions from a registry with a port are merged by image name": {
origin: `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: pipeline
pipeline:
mutators:
- image: registry.example.com:5000/krm/apply-setters:v0.2
configMap:
image: nginx
tag: 1.0.1
- image: registry.example.com:5000/krm/set-labels:v0.2
configMap:
app: demo
`,
update: `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: pipeline
pipeline:
mutators:
- image: registry.example.com:5000/krm/apply-setters:v0.2
configMap:
image: nginx
tag: 1.0.1
new-setter: new-setter-value // new setter is added
- image: registry.example.com:5000/krm/set-labels:v0.2
configMap:
app: demo
`,
local: `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: pipeline
pipeline:
mutators:
- image: registry.example.com:5000/krm/apply-setters:v0.2
configMap:
image: nginx
tag: 1.2.0 // value of tag is updated
- image: registry.example.com:5000/krm/set-labels:v0.2
configMap:
app: demo
`,
expected: `
apiVersion: kpt.dev/v1
kind: Kptfile
metadata:
name: pipeline
pipeline:
mutators:
- image: registry.example.com:5000/krm/apply-setters:v0.2
configMap:
image: nginx
new-setter: new-setter-value // new setter is added
tag: 1.2.0 // value of tag is updated
- image: registry.example.com:5000/krm/set-labels:v0.2
configMap:
app: demo
`,
},

"both upstream and local configPath is updated, take upstream": {
origin: `
apiVersion: kpt.dev/v1
Expand Down Expand Up @@ -1843,3 +1908,44 @@ func TestUpdateKptfileWithoutOrigin_errorPaths(t *testing.T) {
assert.Error(t, err)
})
}

func TestImageMergeKey(t *testing.T) {
testCases := map[string]struct {
image string
expected string
}{
"tag": {
image: "ghcr.io/kptdev/krm-functions-catalog/apply-setters:v0.2",
expected: "ghcr.io/kptdev/krm-functions-catalog/apply-setters",
},
"no tag": {
image: "ghcr.io/kptdev/krm-functions-catalog/apply-setters",
expected: "ghcr.io/kptdev/krm-functions-catalog/apply-setters",
},
"short name with tag": {
image: "foo:bar",
expected: "foo",
},
"registry with port and tag": {
image: "registry.example.com:5000/krm/apply-setters:v0.2",
expected: "registry.example.com:5000/krm/apply-setters",
},
"registry with port and no tag": {
image: "registry.example.com:5000/krm/apply-setters",
expected: "registry.example.com:5000/krm/apply-setters",
},
"digest": {
image: "ghcr.io/kptdev/krm-functions-catalog/apply-setters@sha256:0123456789abcdef",
expected: "ghcr.io/kptdev/krm-functions-catalog/apply-setters",
},
"registry with port, tag and digest": {
image: "registry.example.com:5000/krm/apply-setters:v0.2@sha256:0123456789abcdef",
expected: "registry.example.com:5000/krm/apply-setters",
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
assert.Equal(t, tc.expected, imageMergeKey(tc.image))
})
}
}
Loading