Skip to content

Commit 5191f32

Browse files
authored
Place RabbitMQ configuration in confd (#548)
* Place RabbitMQ configuration in confd Files places in confd are loaded after 'rabbitmq.conf' is loaded, allowing the user configuration to take precedence. The ConfigMap has keys without any numeric prefix because file naming is done in StatefulSet's pod template volume mounts. This allows flexibility in the future, shall we decide we rename or change the numeric prefixes. This commit also adds a condition to decide whether to include the server ConfigMap as a volume. This happens if RabbitMQ env or advanced config are set in the RabbitmqCluster Spec. The server conf is partially projected into a volume that is always specified. * Use a string builder to generate RMQ configuration String builder is more efficient than a byte buffer for strings. * Rename additionalConfig to userDefinedConfiguration The previous name could set an expectation to be a 1-to-1 mapping between the file and the contents in `.spec.rabbitmq.additionalConfig`. This was not intended, as this file can contain other configuration, generated based on the user input. Reverted the change that used string concatenation instead of multi-line string in test expectations.
1 parent bba233e commit 5191f32

File tree

6 files changed

+368
-310
lines changed

6 files changed

+368
-310
lines changed

controllers/rabbitmqcluster_controller_test.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -886,22 +886,28 @@ var _ = Describe("RabbitmqClusterController", func() {
886886
},
887887
},
888888
},
889+
{
890+
ConfigMap: &corev1.ConfigMapProjection{
891+
LocalObjectReference: corev1.LocalObjectReference{
892+
Name: "rabbitmq-sts-override-server-conf",
893+
},
894+
Items: []corev1.KeyToPath{
895+
{
896+
Key: "operatorDefaults.conf",
897+
Path: "operatorDefaults.conf",
898+
},
899+
{
900+
Key: "userDefinedConfiguration.conf",
901+
Path: "userDefinedConfiguration.conf",
902+
},
903+
},
904+
},
905+
},
889906
},
890907
DefaultMode: &defaultMode,
891908
},
892909
},
893910
},
894-
corev1.Volume{
895-
Name: "server-conf",
896-
VolumeSource: corev1.VolumeSource{
897-
ConfigMap: &corev1.ConfigMapVolumeSource{
898-
DefaultMode: &defaultMode,
899-
LocalObjectReference: corev1.LocalObjectReference{
900-
Name: "rabbitmq-sts-override-server-conf",
901-
},
902-
},
903-
},
904-
},
905911
corev1.Volume{
906912
Name: "plugins-conf",
907913
VolumeSource: corev1.VolumeSource{

internal/resource/configmap.go

Lines changed: 46 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
package resource
1111

1212
import (
13-
"bytes"
1413
"fmt"
14+
"strings"
15+
1516
"sigs.k8s.io/controller-runtime/pkg/client"
1617

1718
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
@@ -80,135 +81,146 @@ func (builder *ServerConfigMapBuilder) Update(object client.Object) error {
8081
configMap := object.(*corev1.ConfigMap)
8182

8283
ini.PrettySection = false // Remove trailing new line because rabbitmq.conf has only a default section.
83-
cfg, err := ini.Load([]byte(defaultRabbitmqConf))
84+
operatorConfiguration, err := ini.Load([]byte(defaultRabbitmqConf))
8485
if err != nil {
8586
return err
8687
}
87-
defaultSection := cfg.Section("")
88+
defaultSection := operatorConfiguration.Section("")
8889

8990
if _, err := defaultSection.NewKey("cluster_name", builder.Instance.Name); err != nil {
9091
return err
9192
}
9293

94+
userConfiguration := ini.Empty(ini.LoadOptions{})
95+
userConfigurationSection := userConfiguration.Section("")
96+
9397
if builder.Instance.TLSEnabled() {
94-
if err := cfg.Append([]byte(defaultTLSConf)); err != nil {
98+
if err := userConfiguration.Append([]byte(defaultTLSConf)); err != nil {
9599
return err
96100
}
97101
if builder.Instance.DisableNonTLSListeners() {
98-
if _, err := defaultSection.NewKey("listeners.tcp", "none"); err != nil {
102+
if _, err := userConfigurationSection.NewKey("listeners.tcp", "none"); err != nil {
99103
return err
100104
}
101105
} else {
102106
// management plugin does not have a *.listeners.tcp settings like other plugins
103107
// management tcp listener can be disabled by setting management.ssl.port without setting management.tcp.port
104108
// we set management tcp listener only if tls is enabled and disableNonTLSListeners is false
105-
if _, err := defaultSection.NewKey("management.tcp.port", "15672"); err != nil {
109+
if _, err := userConfigurationSection.NewKey("management.tcp.port", "15672"); err != nil {
106110
return err
107111
}
108112

109-
if _, err := defaultSection.NewKey("prometheus.tcp.port", "15692"); err != nil {
113+
if _, err := userConfigurationSection.NewKey("prometheus.tcp.port", "15692"); err != nil {
110114
return err
111115
}
112116
}
113117
if builder.Instance.AdditionalPluginEnabled("rabbitmq_mqtt") {
114-
if _, err := defaultSection.NewKey("mqtt.listeners.ssl.default", "8883"); err != nil {
118+
if _, err := userConfigurationSection.NewKey("mqtt.listeners.ssl.default", "8883"); err != nil {
115119
return err
116120
}
117121
if builder.Instance.DisableNonTLSListeners() {
118-
if _, err := defaultSection.NewKey("mqtt.listeners.tcp", "none"); err != nil {
122+
if _, err := userConfigurationSection.NewKey("mqtt.listeners.tcp", "none"); err != nil {
119123
return err
120124
}
121125
}
122126
}
123127
if builder.Instance.AdditionalPluginEnabled("rabbitmq_stomp") {
124-
if _, err := defaultSection.NewKey("stomp.listeners.ssl.1", "61614"); err != nil {
128+
if _, err := userConfigurationSection.NewKey("stomp.listeners.ssl.1", "61614"); err != nil {
125129
return err
126130
}
127131
if builder.Instance.DisableNonTLSListeners() {
128-
if _, err := defaultSection.NewKey("stomp.listeners.tcp", "none"); err != nil {
132+
if _, err := userConfigurationSection.NewKey("stomp.listeners.tcp", "none"); err != nil {
129133
return err
130134
}
131135
}
132136
}
133137
}
134138

135139
if builder.Instance.MutualTLSEnabled() {
136-
if _, err := defaultSection.NewKey("ssl_options.cacertfile", caCertPath); err != nil {
140+
if _, err := userConfigurationSection.NewKey("ssl_options.cacertfile", caCertPath); err != nil {
137141
return err
138142
}
139-
if _, err := defaultSection.NewKey("ssl_options.verify", "verify_peer"); err != nil {
143+
if _, err := userConfigurationSection.NewKey("ssl_options.verify", "verify_peer"); err != nil {
140144
return err
141145
}
142146

143-
if _, err := defaultSection.NewKey("management.ssl.cacertfile", caCertPath); err != nil {
147+
if _, err := userConfigurationSection.NewKey("management.ssl.cacertfile", caCertPath); err != nil {
144148
return err
145149
}
146150

147-
if _, err := defaultSection.NewKey("prometheus.ssl.cacertfile", caCertPath); err != nil {
151+
if _, err := userConfigurationSection.NewKey("prometheus.ssl.cacertfile", caCertPath); err != nil {
148152
return err
149153
}
150154

151155
if builder.Instance.AdditionalPluginEnabled("rabbitmq_web_mqtt") {
152-
if _, err := defaultSection.NewKey("web_mqtt.ssl.port", "15676"); err != nil {
156+
if _, err := userConfigurationSection.NewKey("web_mqtt.ssl.port", "15676"); err != nil {
153157
return err
154158
}
155-
if _, err := defaultSection.NewKey("web_mqtt.ssl.cacertfile", caCertPath); err != nil {
159+
if _, err := userConfigurationSection.NewKey("web_mqtt.ssl.cacertfile", caCertPath); err != nil {
156160
return err
157161
}
158-
if _, err := defaultSection.NewKey("web_mqtt.ssl.certfile", tlsCertPath); err != nil {
162+
if _, err := userConfigurationSection.NewKey("web_mqtt.ssl.certfile", tlsCertPath); err != nil {
159163
return err
160164
}
161-
if _, err := defaultSection.NewKey("web_mqtt.ssl.keyfile", tlsKeyPath); err != nil {
165+
if _, err := userConfigurationSection.NewKey("web_mqtt.ssl.keyfile", tlsKeyPath); err != nil {
162166
return err
163167
}
164168
if builder.Instance.DisableNonTLSListeners() {
165-
if _, err := defaultSection.NewKey("web_mqtt.tcp.listener", "none"); err != nil {
169+
if _, err := userConfigurationSection.NewKey("web_mqtt.tcp.listener", "none"); err != nil {
166170
return err
167171
}
168172
}
169173
}
170174
if builder.Instance.AdditionalPluginEnabled("rabbitmq_web_stomp") {
171-
if _, err := defaultSection.NewKey("web_stomp.ssl.port", "15673"); err != nil {
175+
if _, err := userConfigurationSection.NewKey("web_stomp.ssl.port", "15673"); err != nil {
172176
return err
173177
}
174-
if _, err := defaultSection.NewKey("web_stomp.ssl.cacertfile", caCertPath); err != nil {
178+
if _, err := userConfigurationSection.NewKey("web_stomp.ssl.cacertfile", caCertPath); err != nil {
175179
return err
176180
}
177-
if _, err := defaultSection.NewKey("web_stomp.ssl.certfile", tlsCertPath); err != nil {
181+
if _, err := userConfigurationSection.NewKey("web_stomp.ssl.certfile", tlsCertPath); err != nil {
178182
return err
179183
}
180-
if _, err := defaultSection.NewKey("web_stomp.ssl.keyfile", tlsKeyPath); err != nil {
184+
if _, err := userConfigurationSection.NewKey("web_stomp.ssl.keyfile", tlsKeyPath); err != nil {
181185
return err
182186
}
183187
if builder.Instance.DisableNonTLSListeners() {
184-
if _, err := defaultSection.NewKey("web_stomp.tcp.listener", "none"); err != nil {
188+
if _, err := userConfigurationSection.NewKey("web_stomp.tcp.listener", "none"); err != nil {
185189
return err
186190
}
187191
}
188192
}
189193
}
190194

191195
if builder.Instance.MemoryLimited() {
192-
if _, err := defaultSection.NewKey("total_memory_available_override_value", fmt.Sprintf("%d", removeHeadroom(builder.Instance.Spec.Resources.Limits.Memory().Value()))); err != nil {
196+
if _, err := userConfigurationSection.NewKey("total_memory_available_override_value", fmt.Sprintf("%d", removeHeadroom(builder.Instance.Spec.Resources.Limits.Memory().Value()))); err != nil {
193197
return err
194198
}
195199
}
196200

197-
rmqProperties := builder.Instance.Spec.Rabbitmq
198-
if err := cfg.Append([]byte(rmqProperties.AdditionalConfig)); err != nil {
199-
return fmt.Errorf("failed to append spec.rabbitmq.additionalConfig: %w", err)
200-
}
201-
202-
var rmqConfBuffer bytes.Buffer
203-
if _, err := cfg.WriteTo(&rmqConfBuffer); err != nil {
201+
var rmqConfBuffer strings.Builder
202+
if _, err := operatorConfiguration.WriteTo(&rmqConfBuffer); err != nil {
204203
return err
205204
}
206205

207206
if configMap.Data == nil {
208207
configMap.Data = make(map[string]string)
209208
}
210209

211-
configMap.Data["rabbitmq.conf"] = rmqConfBuffer.String()
210+
configMap.Data["operatorDefaults.conf"] = rmqConfBuffer.String()
211+
212+
rmqConfBuffer.Reset()
213+
214+
rmqProperties := builder.Instance.Spec.Rabbitmq
215+
if err := userConfiguration.Append([]byte(rmqProperties.AdditionalConfig)); err != nil {
216+
return fmt.Errorf("failed to append spec.rabbitmq.additionalConfig: %w", err)
217+
}
218+
219+
if _, err := userConfiguration.WriteTo(&rmqConfBuffer); err != nil {
220+
return err
221+
}
222+
223+
configMap.Data["userDefinedConfiguration.conf"] = rmqConfBuffer.String()
212224

213225
updateProperty(configMap.Data, "advanced.config", rmqProperties.AdvancedConfig)
214226
updateProperty(configMap.Data, "rabbitmq-env.conf", rmqProperties.EnvConfig)

0 commit comments

Comments
 (0)