Skip to content

Commit 2ece40f

Browse files
committed
feat: customize connection string secret annotations
1 parent 3108a94 commit 2ece40f

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

api/v1/mongodbcommunity_types.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ type MongoDBUser struct {
486486
// +optional
487487
ConnectionStringSecretNamespace string `json:"connectionStringSecretNamespace,omitempty"`
488488

489+
// ConnectionStringSecretAnnotations is the annotations of the secret object created by the operator which exposes the connection strings for the user.
490+
// +optional
491+
ConnectionStringSecretAnnotations map[string]string `json:"connectionStringSecretAnnotations,omitempty"`
492+
489493
// Additional options to be appended to the connection string.
490494
// These options apply only to this user and will override any existing options in the resource.
491495
// +kubebuilder:validation:Type=object
@@ -786,12 +790,13 @@ func (m *MongoDBCommunity) GetAuthUsers() []authtypes.User {
786790
}
787791

788792
users[i] = authtypes.User{
789-
Username: u.Name,
790-
Database: u.DB,
791-
Roles: roles,
792-
ConnectionStringSecretName: u.GetConnectionStringSecretName(m.Name),
793-
ConnectionStringSecretNamespace: u.GetConnectionStringSecretNamespace(m.Namespace),
794-
ConnectionStringOptions: u.AdditionalConnectionStringConfig.Object,
793+
Username: u.Name,
794+
Database: u.DB,
795+
Roles: roles,
796+
ConnectionStringSecretName: u.GetConnectionStringSecretName(m.Name),
797+
ConnectionStringSecretNamespace: u.GetConnectionStringSecretNamespace(m.Namespace),
798+
ConnectionStringSecretAnnotations: u.ConnectionStringSecretAnnotations,
799+
ConnectionStringOptions: u.AdditionalConnectionStringConfig.Object,
795800
}
796801

797802
if u.DB != constants.ExternalDB {

config/crd/bases/mongodbcommunity.mongodb.com_mongodbcommunity.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,13 @@ spec:
518518
nullable: true
519519
type: object
520520
x-kubernetes-preserve-unknown-fields: true
521+
connectionStringSecretAnnotations:
522+
additionalProperties:
523+
type: string
524+
description: ConnectionStringSecretAnnotations is the annotations
525+
of the secret object created by the operator which exposes
526+
the connection strings for the user.
527+
type: object
521528
connectionStringSecretName:
522529
description: |-
523530
ConnectionStringSecretName is the name of the secret object created by the operator which exposes the connection strings for the user.

controllers/mongodb_users.go

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (r ReplicaSetReconciler) updateConnectionStringSecrets(ctx context.Context,
7777
SetField("username", user.Username).
7878
SetField("password", pwd).
7979
SetOwnerReferences(mdb.GetOwnerReferences()).
80+
SetAnnotations(user.ConnectionStringSecretAnnotations).
8081
Build()
8182

8283
if err := secret.CreateOrUpdate(ctx, r.client, connectionStringSecret); err != nil {

pkg/authentication/authtypes/authtypes.go

+4
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ type User struct {
7171
ConnectionStringSecretName string
7272

7373
// ConnectionStringSecretNamespace is the namespace of the secret object created by the operator which exposes the connection strings for the user.
74+
// Note: there will be one secret with connection strings per user created.
7475
ConnectionStringSecretNamespace string `json:"connectionStringSecretNamespace,omitempty"`
7576

77+
// ConnectionStringSecretAnnotations is the annotations of the secret object created by the operator which exposes the connection strings for the user.
78+
ConnectionStringSecretAnnotations map[string]string
79+
7680
// ConnectionStringOptions contains connection string options for this user
7781
// These options will be appended at the end of the connection string and
7882
// will override any existing options from the resources.

pkg/kube/secret/secret_builder.go

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type builder struct {
1111
labels map[string]string
1212
name string
1313
namespace string
14+
annotations map[string]string
1415
ownerReferences []metav1.OwnerReference
1516
}
1617

@@ -24,6 +25,11 @@ func (b *builder) SetNamespace(namespace string) *builder {
2425
return b
2526
}
2627

28+
func (b *builder) SetAnnotations(annotations map[string]string) *builder {
29+
b.annotations = annotations
30+
return b
31+
}
32+
2733
func (b *builder) SetField(key, value string) *builder {
2834
b.data[key] = []byte(value)
2935
return b
@@ -72,6 +78,7 @@ func (b builder) Build() corev1.Secret {
7278
Namespace: b.namespace,
7379
OwnerReferences: b.ownerReferences,
7480
Labels: b.labels,
81+
Annotations: b.annotations,
7582
},
7683
Data: b.data,
7784
Type: b.dataType,

0 commit comments

Comments
 (0)