Skip to content

Commit 82ce947

Browse files
author
Nikolas De Giorgis
authored
CLOUDP-85412: refactor_multiple_persistence_options (#399)
1 parent 3ba183c commit 82ce947

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

api/v1/mongodbcommunity_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ func (m MongoDBCommunity) getPreviousVersion() string {
542542
return annotations.GetAnnotation(&m, annotations.LastAppliedMongoDBVersion)
543543
}
544544

545+
func (m MongoDBCommunity) HasSeparateDataAndLogsVolumes() bool {
546+
return true
547+
}
548+
545549
func (m MongoDBCommunity) GetAnnotations() map[string]string {
546550
return m.Annotations
547551
}

controllers/construct/mongodbstatefulset.go

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,23 @@ const (
5050

5151
// MongoDBStatefulSetOwner is an interface which any resource which generates a MongoDB StatefulSet should implement.
5252
type MongoDBStatefulSetOwner interface {
53+
// ServiceName returns the name of the K8S service the operator will create.
5354
ServiceName() string
55+
// GetName returns the name of the resource.
5456
GetName() string
57+
// GetNamespace returns the namespace the resource is defined in.
5558
GetNamespace() string
59+
// GetMongoDBVersion returns the version of MongoDB to be used for this resource
5660
GetMongoDBVersion() string
61+
// AutomationConfigSecretName returns the name of the secret which will contain the automation config.
5762
AutomationConfigSecretName() string
63+
// GetUpdateStrategyType returns the UpdateStrategyType of the statefulset.
5864
GetUpdateStrategyType() appsv1.StatefulSetUpdateStrategyType
65+
// Persistent returns whether or not the statefulset should have persistent volumes added.
5966
Persistent() bool
67+
// HasSeparateDataAndLogsVolumes returns whether or not the volumes for data and logs would need to be different.
68+
HasSeparateDataAndLogsVolumes() bool
69+
// GetAgentScramKeyfileSecretNamespacedName returns the NamespacedName of the secret which stores the keyfile for the agent.
6070
GetAgentKeyfileSecretNamespacedName() types.NamespacedName
6171
}
6272

@@ -86,23 +96,34 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
8696
automationConfigVolume := statefulset.CreateVolumeFromSecret("automation-config", mdb.AutomationConfigSecretName())
8797
automationConfigVolumeMount := statefulset.CreateVolumeMount(automationConfigVolume.Name, "/var/lib/automation/config", statefulset.WithReadOnly(true))
8898

89-
logVolumeMount := statefulset.CreateVolumeMount(logVolumeName, automationconfig.DefaultAgentLogPath)
90-
9199
keyFileNsName := mdb.GetAgentKeyfileSecretNamespacedName()
92100
keyFileVolume := statefulset.CreateVolumeFromEmptyDir(keyFileNsName.Name)
93101
keyFileVolumeVolumeMount := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false))
94102
keyFileVolumeVolumeMountMongod := statefulset.CreateVolumeMount(keyFileVolume.Name, "/var/lib/mongodb-mms-automation/authentication", statefulset.WithReadOnly(false))
95103

96-
mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, automationConfigVolumeMount, scriptsVolumeMount, logVolumeMount, keyFileVolumeVolumeMount}
97-
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, hooksVolumeMount, logVolumeMount, keyFileVolumeVolumeMountMongod}
98-
dataVolumeClaim := func(s *appsv1.StatefulSet) {}
104+
mongodbAgentVolumeMounts := []corev1.VolumeMount{agentHealthStatusVolumeMount, automationConfigVolumeMount, scriptsVolumeMount, keyFileVolumeVolumeMount}
105+
mongodVolumeMounts := []corev1.VolumeMount{mongodHealthStatusVolumeMount, hooksVolumeMount, keyFileVolumeVolumeMountMongod}
106+
dataVolumeClaim := statefulset.NOOP()
107+
logVolumeClaim := statefulset.NOOP()
108+
singleModeVolumeClaim := func(s *appsv1.StatefulSet) {}
99109
if mdb.Persistent() {
100-
dataVolumeMount := statefulset.CreateVolumeMount(dataVolumeName, "/data")
101-
dataVolumeClaim = statefulset.WithVolumeClaim(dataVolumeName, dataPvc())
102-
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, dataVolumeMount)
103-
mongodVolumeMounts = append(mongodVolumeMounts, dataVolumeMount)
110+
if mdb.HasSeparateDataAndLogsVolumes() {
111+
logVolumeMount := statefulset.CreateVolumeMount(logVolumeName, automationconfig.DefaultAgentLogPath)
112+
dataVolumeMount := statefulset.CreateVolumeMount(dataVolumeName, "/data")
113+
dataVolumeClaim = statefulset.WithVolumeClaim(dataVolumeName, dataPvc())
114+
logVolumeClaim = statefulset.WithVolumeClaim(logVolumeName, logsPvc())
115+
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, dataVolumeMount, logVolumeMount)
116+
mongodVolumeMounts = append(mongodVolumeMounts, dataVolumeMount, logVolumeMount)
117+
} else {
118+
mounts := []corev1.VolumeMount{
119+
statefulset.CreateVolumeMount(dataVolumeName, "/data", statefulset.WithSubPath("data")),
120+
statefulset.CreateVolumeMount(dataVolumeName, automationconfig.DefaultAgentLogPath, statefulset.WithSubPath("logs")),
121+
}
122+
mongodbAgentVolumeMounts = append(mongodbAgentVolumeMounts, mounts...)
123+
mongodVolumeMounts = append(mongodVolumeMounts, mounts...)
124+
singleModeVolumeClaim = statefulset.WithVolumeClaim(dataVolumeName, dataPvc())
125+
}
104126
}
105-
106127
return statefulset.Apply(
107128
statefulset.WithName(mdb.GetName()),
108129
statefulset.WithNamespace(mdb.GetNamespace()),
@@ -112,7 +133,8 @@ func BuildMongoDBReplicaSetStatefulSetModificationFunction(mdb MongoDBStatefulSe
112133
statefulset.WithReplicas(scale.ReplicasThisReconciliation(scaler)),
113134
statefulset.WithUpdateStrategyType(mdb.GetUpdateStrategyType()),
114135
dataVolumeClaim,
115-
statefulset.WithVolumeClaim(logVolumeName, logsPvc()),
136+
logVolumeClaim,
137+
singleModeVolumeClaim,
116138
statefulset.WithPodSpecTemplate(
117139
podtemplatespec.Apply(
118140
podtemplatespec.WithSecurityContext(podtemplatespec.DefaultPodSecurityContext()),

pkg/kube/statefulset/statefulset.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ func CreateVolumeMount(name, path string, options ...func(*corev1.VolumeMount))
135135
return *volumeMount
136136
}
137137

138+
// NOOP is a valid Modification which applies no changes
139+
func NOOP() Modification {
140+
return func(sts *appsv1.StatefulSet) {}
141+
}
142+
138143
func WithSecretDefaultMode(mode *int32) func(*corev1.Volume) {
139144
return func(v *corev1.Volume) {
140145
if v.VolumeSource.Secret == nil {

0 commit comments

Comments
 (0)