Skip to content

Commit 0f117a9

Browse files
Merge pull request #515 from lmiccini/extend_rabbitmqconfig
Extend RabbitMqConfig to store Cluster name and implement defaulting function
2 parents 4871011 + 545453f commit 0f117a9

2 files changed

Lines changed: 123 additions & 0 deletions

File tree

apis/rabbitmq/v1beta1/rabbitmq_common.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,28 @@ type RabbitMqConfig struct {
2525
// +kubebuilder:validation:Optional
2626
// Vhost - RabbitMQ vhost name
2727
Vhost string `json:"vhost,omitempty"`
28+
29+
// +kubebuilder:validation:Required
30+
// +kubebuilder:validation:MinLength=1
31+
// Name of the cluster
32+
Cluster string `json:"cluster"`
33+
}
34+
35+
// DefaultRabbitMqConfig sets default values for RabbitMqConfig if not specified.
36+
// This function should be called from service operators' webhooks to automatically
37+
// populate the Cluster from the legacy rabbitmqClusterName field, making
38+
// RabbitMqConfig the authoritative source for transportCreateOrUpdate.
39+
//
40+
// Example usage in a service operator's webhook:
41+
//
42+
// func (r *Cinder) Default() {
43+
// rabbitmqv1beta1.DefaultRabbitMqConfig(
44+
// &r.Spec.RabbitMqConfig,
45+
// r.Spec.RabbitmqClusterName,
46+
// )
47+
// }
48+
func DefaultRabbitMqConfig(config *RabbitMqConfig, defaultClusterName string) {
49+
if config.Cluster == "" && defaultClusterName != "" {
50+
config.Cluster = defaultClusterName
51+
}
2852
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
Copyright 2025.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1beta1
18+
19+
import "testing"
20+
21+
func TestDefaultRabbitMqConfig(t *testing.T) {
22+
tests := []struct {
23+
name string
24+
config *RabbitMqConfig
25+
defaultClusterName string
26+
wantClusterName string
27+
wantUser string
28+
wantVhost string
29+
}{
30+
{
31+
name: "should set Cluster when it's empty and defaultClusterName is provided",
32+
config: &RabbitMqConfig{
33+
User: "testuser",
34+
Vhost: "testvhost",
35+
// Cluster is empty
36+
},
37+
defaultClusterName: "default-rabbitmq",
38+
wantClusterName: "default-rabbitmq",
39+
wantUser: "testuser",
40+
wantVhost: "testvhost",
41+
},
42+
{
43+
name: "should not override Cluster when it's already set",
44+
config: &RabbitMqConfig{
45+
User: "testuser",
46+
Vhost: "testvhost",
47+
Cluster: "existing-cluster",
48+
},
49+
defaultClusterName: "default-rabbitmq",
50+
wantClusterName: "existing-cluster",
51+
wantUser: "testuser",
52+
wantVhost: "testvhost",
53+
},
54+
{
55+
name: "should not set Cluster when defaultClusterName is empty",
56+
config: &RabbitMqConfig{
57+
User: "testuser",
58+
Vhost: "testvhost",
59+
// Cluster is empty
60+
},
61+
defaultClusterName: "",
62+
wantClusterName: "",
63+
wantUser: "testuser",
64+
wantVhost: "testvhost",
65+
},
66+
{
67+
name: "should not set Cluster when both Cluster and defaultClusterName are empty",
68+
config: &RabbitMqConfig{
69+
User: "testuser",
70+
Vhost: "testvhost",
71+
// Cluster is empty
72+
},
73+
defaultClusterName: "",
74+
wantClusterName: "",
75+
wantUser: "testuser",
76+
wantVhost: "testvhost",
77+
},
78+
}
79+
80+
for _, tt := range tests {
81+
t.Run(tt.name, func(t *testing.T) {
82+
// Make a copy of config to avoid modifying the original
83+
configCopy := *tt.config
84+
config := &configCopy
85+
86+
DefaultRabbitMqConfig(config, tt.defaultClusterName)
87+
88+
if config.Cluster != tt.wantClusterName {
89+
t.Errorf("Cluster = %q, want %q", config.Cluster, tt.wantClusterName)
90+
}
91+
if config.User != tt.wantUser {
92+
t.Errorf("User = %q, want %q", config.User, tt.wantUser)
93+
}
94+
if config.Vhost != tt.wantVhost {
95+
t.Errorf("Vhost = %q, want %q", config.Vhost, tt.wantVhost)
96+
}
97+
})
98+
}
99+
}

0 commit comments

Comments
 (0)