Skip to content

Commit ec99af9

Browse files
committed
Only strip the config- prefix from ConfigMap names that have it
This change was written with the assistance of generative AI. I have reviewed and understand every change in it. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
1 parent f8d5140 commit ec99af9

2 files changed

Lines changed: 36 additions & 3 deletions

File tree

‎pkg/reconciler/common/config_maps.go‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package common
1818

1919
import (
20+
"strings"
21+
2022
mf "github.com/manifestival/manifestival"
2123
"go.uber.org/zap"
2224
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -31,9 +33,12 @@ func ConfigMapTransform(config base.ConfigMapData, log *zap.SugaredLogger) mf.Tr
3133
if data, ok := config[u.GetName()]; ok {
3234
return UpdateConfigMap(u, data, log)
3335
}
34-
// The "config-" prefix is optional
35-
if data, ok := config[u.GetName()[len(`config-`):]]; ok {
36-
return UpdateConfigMap(u, data, log)
36+
// The "config-" prefix is optional, so only drop it when it is
37+
// really there. Names shorter than the prefix must be left alone.
38+
if trimmed, found := strings.CutPrefix(u.GetName(), `config-`); found {
39+
if data, ok := config[trimmed]; ok {
40+
return UpdateConfigMap(u, data, log)
41+
}
3742
}
3843
}
3944
return nil

‎pkg/reconciler/common/config_maps_test.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,34 @@ func createConfigMapTests(t *testing.T) []updateConfigMapTest {
7575
"loglevel.webhook": "debug",
7676
}),
7777
},
78+
{
79+
// A ConfigMap name shorter than len("config-") must not be sliced.
80+
name: "short-name-without-config-prefix",
81+
config: makeconfigMapData("logging", map[string]string{
82+
"loglevel.controller": "debug",
83+
}),
84+
configMap: createConfigMap("kafka", map[string]string{
85+
"bootstrap.servers": "my-cluster:9092",
86+
}),
87+
expected: createConfigMap("kafka", map[string]string{
88+
"bootstrap.servers": "my-cluster:9092",
89+
}),
90+
},
91+
{
92+
// Only a real "config-" prefix may be dropped. "kourier-bootstrap"
93+
// starts with "kourier", so the first seven bytes are not a prefix
94+
// and the remainder is not a ConfigMap key.
95+
name: "unprefixed-name-keeps-all-of-its-bytes",
96+
config: makeconfigMapData("-bootstrap", map[string]string{
97+
"injected": "yes",
98+
}),
99+
configMap: createConfigMap("kourier-bootstrap", map[string]string{
100+
"orig": "keep",
101+
}),
102+
expected: createConfigMap("kourier-bootstrap", map[string]string{
103+
"orig": "keep",
104+
}),
105+
},
78106
{
79107
name: "change-using-real-configmap-name",
80108
config: makeconfigMapData("config-logging", map[string]string{

0 commit comments

Comments
 (0)