Skip to content
This repository was archived by the owner on Dec 12, 2025. It is now read-only.

Commit df0004a

Browse files
authored
Fix CRD Generation (#303)
1 parent 89794dd commit df0004a

File tree

8 files changed

+46
-8
lines changed

8 files changed

+46
-8
lines changed

deploy/crds/arbitrary_statefulset_configuration/mongodb.com_v1_custom_volume_cr.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ spec:
1919
db: admin
2020
- name: userAdminAnyDatabase
2121
db: admin
22+
scramCredentialsSecretName: my-scram
23+
2224
statefulSet:
2325
spec:
2426
template:

deploy/crds/mongodb.com_mongodbcommunity_crd.yaml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ spec:
4242
spec:
4343
description: MongoDBCommunitySpec defines the desired state of MongoDB
4444
properties:
45+
additionalMongodConfig:
46+
description: 'AdditionalMongodConfig is additional configuration that
47+
can be passed to each data-bearing mongod at runtime. Uses the same
48+
structure as the mongod configuration file: https://docs.mongodb.com/manual/reference/configuration-options/'
49+
type: object
4550
featureCompatibilityVersion:
4651
description: FeatureCompatibilityVersion configures the feature compatibility
4752
version that will be set for the deployment
@@ -205,6 +210,11 @@ spec:
205210
statefulSet:
206211
description: StatefulSetConfiguration holds the optional custom StatefulSet
207212
that should be merged into the operator created one.
213+
properties:
214+
spec:
215+
type: object
216+
required:
217+
- spec
208218
type: object
209219
type:
210220
description: Type defines which type of MongoDB deployment the resource
@@ -259,8 +269,8 @@ spec:
259269
description: ScramCredentialsSecretName appended by string "scram-credentials"
260270
is the name of the secret object created by the mongoDB operator
261271
for storing SCRAM credentials
262-
type: string
263272
pattern: ^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
273+
type: string
264274
required:
265275
- name
266276
- passwordSecretRef

pkg/apis/mongodb/v1/mongodb_types.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ type MongoDBCommunitySpec struct {
7575
// each data-bearing mongod at runtime. Uses the same structure as the mongod
7676
// configuration file: https://docs.mongodb.com/manual/reference/configuration-options/
7777
// +kubebuilder:validation:Type=object
78+
// +optional
7879
AdditionalMongodConfig MongodConfiguration `json:"additionalMongodConfig,omitempty"`
7980
}
8081

@@ -175,8 +176,30 @@ type AuthenticationRestriction struct {
175176
// StatefulSetConfiguration holds the optional custom StatefulSet
176177
// that should be merged into the operator created one.
177178
type StatefulSetConfiguration struct {
178-
// The StatefulSet override options for underlying StatefulSet
179-
Spec appsv1.StatefulSetSpec `json:"spec"` // TODO: this pollutes the crd generation
179+
SpecWrapper StatefulSetSpecWrapper `json:"spec"`
180+
}
181+
182+
// StatefulSetSpecWrapper is a wrapper around StatefulSetSpec with a custom implementation
183+
// of MarshalJSON and UnmarshalJSON which delegate to the underlying Spec to avoid CRD pollution.
184+
185+
type StatefulSetSpecWrapper struct {
186+
Spec appsv1.StatefulSetSpec `json:"-"`
187+
}
188+
189+
// MarshalJSON defers JSON encoding to the wrapped map
190+
func (m *StatefulSetSpecWrapper) MarshalJSON() ([]byte, error) {
191+
return json.Marshal(m.Spec)
192+
}
193+
194+
// UnmarshalJSON will decode the data into the wrapped map
195+
func (m *StatefulSetSpecWrapper) UnmarshalJSON(data []byte) error {
196+
return json.Unmarshal(data, &m.Spec)
197+
}
198+
199+
func (m *StatefulSetSpecWrapper) DeepCopy() *StatefulSetSpecWrapper {
200+
return &StatefulSetSpecWrapper{
201+
Spec: m.Spec,
202+
}
180203
}
181204

182205
// MongodConfiguration holds the optional mongod configuration
@@ -224,7 +247,7 @@ type MongoDBUser struct {
224247
Roles []Role `json:"roles"`
225248

226249
// ScramCredentialsSecretName appended by string "scram-credentials" is the name of the secret object created by the mongoDB operator for storing SCRAM credentials
227-
// +kubebuilder:validation:Pattern="^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$"
250+
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$
228251
ScramCredentialsSecretName string `json:"scramCredentialsSecretName"`
229252
}
230253

pkg/apis/mongodb/v1/zz_generated.deepcopy.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/mongodb/replica_set_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ func buildStatefulSetModificationFunction(mdb mdbv1.MongoDBCommunity) statefulse
799799
buildScramPodSpecModification(mdb),
800800
),
801801
),
802-
statefulset.WithCustomSpecs(mdb.Spec.StatefulSetConfiguration.Spec),
802+
statefulset.WithCustomSpecs(mdb.Spec.StatefulSetConfiguration.SpecWrapper.Spec),
803803
)
804804
}
805805

test/e2e/e2eutil.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,9 @@ func NewTestMongoDB(name string, namespace string) (mdbv1.MongoDBCommunity, mdbv
154154
Modes: []mdbv1.AuthMode{"SCRAM"},
155155
},
156156
},
157+
AdditionalMongodConfig: mdbv1.MongodConfiguration{
158+
Object: map[string]interface{}{},
159+
},
157160
Users: []mdbv1.MongoDBUser{
158161
{
159162
Name: fmt.Sprintf("%s-user", name),

test/e2e/statefulset_arbitrary_config/statefulset_arbitrary_config_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestStatefulSetArbitraryConfig(t *testing.T) {
3131
}
3232

3333
overrideSpec := v1.StatefulSetConfiguration{}
34-
overrideSpec.Spec.Template.Spec.Containers = []corev1.Container{
34+
overrideSpec.SpecWrapper.Spec.Template.Spec.Containers = []corev1.Container{
3535
{Name: "mongodb-agent", ReadinessProbe: &corev1.Probe{TimeoutSeconds: 100}}}
3636

3737
mdb.Spec.StatefulSetConfiguration = overrideSpec

test/e2e/statefulset_arbitrary_config_update/statefulset_arbitrary_config_update_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func TestStatefulSetArbitraryConfig(t *testing.T) {
4242
t.Run("AutomationConfig has the correct version", mongodbtests.AutomationConfigVersionHasTheExpectedVersion(&mdb, 1))
4343

4444
overrideSpec := v1.StatefulSetConfiguration{}
45-
overrideSpec.Spec.Template.Spec.Containers = []corev1.Container{
45+
overrideSpec.SpecWrapper.Spec.Template.Spec.Containers = []corev1.Container{
4646
{Name: "mongodb-agent", ReadinessProbe: &corev1.Probe{TimeoutSeconds: 100}}}
4747

4848
err = e2eutil.UpdateMongoDBResource(&mdb, func(mdb *v1.MongoDBCommunity) { mdb.Spec.StatefulSetConfiguration = overrideSpec })

0 commit comments

Comments
 (0)