K8SPSMDB-1562: fix creation of nonvoting hookscript#2200
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes an issue with the creation of nonvoting hookscript ConfigMap names by ensuring they are lowercase. The problem likely arises because Kubernetes resource names (including ConfigMaps) must be lowercase, and the ComponentNonVoting constant is "nonVoting" with camelCase, which would produce invalid ConfigMap names when used in the hookscript name.
Changes:
- Added
strings.ToLower()wrapper toHookScriptConfigMapNamefunction to ensure ConfigMap names are always lowercase
Comments suppressed due to low confidence (1)
pkg/naming/naming.go:86
- The
MongosHookScriptConfigMapNameandPBMHookScriptConfigMapNamefunctions should also applystrings.ToLower()to ensure consistency withHookScriptConfigMapNameand to guarantee Kubernetes-compliant lowercase names in casecr.Nameor components contain uppercase characters.
func MongosHookScriptConfigMapName(cr *psmdbv1.PerconaServerMongoDB) string {
return fmt.Sprintf("%s-%s-hookscript", cr.Name, ComponentMongos)
}
func PBMHookScriptConfigMapName(cr *psmdbv1.PerconaServerMongoDB) string {
return fmt.Sprintf("%s-pbm-hookscript", cr.Name)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Comments suppressed due to low confidence (2)
pkg/naming/naming.go:82
- The
MongosHookScriptConfigMapNamefunction should also applystrings.ToLower()for consistency withHookScriptConfigMapName. WhileComponentMongosis already lowercase,cr.Namecould theoretically contain uppercase characters in edge cases, and applying lowercase conversion would make the function more defensive and consistent with the fix applied toHookScriptConfigMapName.
func MongosHookScriptConfigMapName(cr *psmdbv1.PerconaServerMongoDB) string {
return fmt.Sprintf("%s-%s-hookscript", cr.Name, ComponentMongos)
}
pkg/naming/naming.go:86
- The
PBMHookScriptConfigMapNamefunction should also applystrings.ToLower()for consistency withHookScriptConfigMapName. While 'pbm' is already lowercase,cr.Namecould theoretically contain uppercase characters in edge cases, and applying lowercase conversion would make the function more defensive and consistent with the fix applied toHookScriptConfigMapName.
func PBMHookScriptConfigMapName(cr *psmdbv1.PerconaServerMongoDB) string {
return fmt.Sprintf("%s-pbm-hookscript", cr.Name)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| func HookScriptConfigMapName(cr *psmdbv1.PerconaServerMongoDB, rs *psmdbv1.ReplsetSpec, component string) string { | ||
| if rs == nil { | ||
| return fmt.Sprintf("%s-%s-hookscript", cr.Name, component) | ||
| return strings.ToLower(fmt.Sprintf("%s-%s-hookscript", cr.Name, component)) |
There was a problem hiding this comment.
let's add a unit test for this given that we had an issue:
package naming
import (
"testing"
psmdbv1 "github.com/percona/percona-server-mongodb-operator/pkg/apis/psmdb/v1"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestHookScriptConfigMapName(t *testing.T) {
tests := map[string]struct {
cr *psmdbv1.PerconaServerMongoDB
rs *psmdbv1.ReplsetSpec
component string
expected string
}{
"rs is nil": {
cr: &psmdbv1.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
},
},
component: "mongos",
expected: "my-cluster-mongos-hookscript",
},
"rs is not nil": {
cr: &psmdbv1.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{
Name: "my-cluster",
},
},
rs: &psmdbv1.ReplsetSpec{
Name: "rs0",
},
component: "mongod",
expected: "my-cluster-rs0-mongod-hookscript",
},
"mixed case cr name - should be lowercased": {
cr: &psmdbv1.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{
Name: "My-Cluster",
},
},
rs: nil,
component: "MONGOS",
expected: "my-cluster-mongos-hookscript",
},
"mixed case with rs - should be lowercased": {
cr: &psmdbv1.PerconaServerMongoDB{
ObjectMeta: metav1.ObjectMeta{
Name: "My-Cluster",
},
},
rs: &psmdbv1.ReplsetSpec{
Name: "RS0",
},
component: "NonVoting",
expected: "my-cluster-rs0-nonvoting-hookscript",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := HookScriptConfigMapName(tt.cr, tt.rs, tt.component)
assert.Equal(t, tt.expected, got)
})
}
}
commit: 9fe54ad |
https://perconadev.atlassian.net/browse/K8SPSMDB-1562
DESCRIPTION
Problem:
Operator fails to deploy a cluster when
.nonVoting.hookscript.scriptis specified:Cause:
The
HookScriptConfigMapNamefunction generates a hook script name using the component label value. For non-voting pods, this value isnonVoting, which contains an uppercase letter.Solution:
Force the
HookScriptConfigMapNamefunction to return a lowercase nameCHECKLIST
Jira
Needs Doc) and QA (Needs QA)?Tests
compare/*-oc.yml)?Config/Logging/Testability