diff --git a/pkg/kptfile/kptfileutil/util.go b/pkg/kptfile/kptfileutil/util.go index 2f99d3f04a..29acee7085 100644 --- a/pkg/kptfile/kptfileutil/util.go +++ b/pkg/kptfile/kptfileutil/util.go @@ -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 @@ -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) { @@ -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 diff --git a/pkg/kptfile/kptfileutil/util_test.go b/pkg/kptfile/kptfileutil/util_test.go index f28e83aba4..a42c59061d 100644 --- a/pkg/kptfile/kptfileutil/util_test.go +++ b/pkg/kptfile/kptfileutil/util_test.go @@ -1,4 +1,4 @@ -// Copyright 2020 The kpt Authors +// Copyright 2020, 2026 The kpt Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -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 @@ -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)) + }) + } +}