Skip to content

Commit

Permalink
✨ Enable the builtin postgres configuration (#1327)
Browse files Browse the repository at this point in the history
* add postgres configuration

Signed-off-by: myan <[email protected]>

* add new line and configuration

Signed-off-by: myan <[email protected]>

* remove the useless

Signed-off-by: myan <[email protected]>

---------

Signed-off-by: myan <[email protected]>
  • Loading branch information
yanmxa authored Jan 15, 2025
1 parent 90c6926 commit 166e7e6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ data:
pg_stat_statements.max = 10000
pg_stat_statements.track = all
{{- end }}
{{- .PostgresCustomizedConfig | indent 4 }}
37 changes: 33 additions & 4 deletions operator/pkg/controllers/storage/postgres_statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
builtinPostgresInitName = fmt.Sprintf("%s-init", BuiltinPostgresName)
builtinPartialPostgresURI = fmt.Sprintf("%s.%s.svc:5432/hoh?sslmode=verify-ca", BuiltinPostgresName,
utils.GetDefaultNamespace())
BuiltinPostgresCustomizedConfigName = "multicluster-global-hub-custom-postgresql-config"
)

type postgresCredential struct {
Expand All @@ -53,6 +54,13 @@ func InitPostgresByStatefulset(ctx context.Context, mgh *globalhubv1alpha4.Multi
imagePullPolicy = mgh.Spec.ImagePullPolicy
}

// postgres configurable
customizedConfig, err := getPostgresCustomizedConfig(ctx, mgh, mgr.GetClient())
if err != nil {
return nil, err
}
log.Infof("the postgres customized config: %s", customizedConfig)

// get the postgres objects
postgresRenderer, postgresDeployer := renderer.NewHoHRenderer(stsPostgresFS), deployer.NewHoHDeployer(mgr.GetClient())
postgresObjects, err := postgresRenderer.Render("manifests.sts", "",
Expand Down Expand Up @@ -81,6 +89,7 @@ func InitPostgresByStatefulset(ctx context.Context, mgh *globalhubv1alpha4.Multi
EnableMetrics bool
EnablePostgresMetrics bool
EnableInventoryAPI bool
PostgresCustomizedConfig []byte
}{
Name: BuiltinPostgresName,
Namespace: mgh.GetNamespace(),
Expand All @@ -105,13 +114,15 @@ func InitPostgresByStatefulset(ctx context.Context, mgh *globalhubv1alpha4.Multi
PostgresURI: strings.ReplaceAll(builtinPartialPostgresURI, "sslmode=verify-ca", "sslmode=disable"),
Resources: operatorutils.GetResources(operatorconstants.Postgres,
mgh.Spec.AdvancedSpec),
EnableMetrics: mgh.Spec.EnableMetrics,
EnablePostgresMetrics: (!config.IsBYOPostgres()) && mgh.Spec.EnableMetrics,
EnableInventoryAPI: config.WithInventory(mgh),
EnableMetrics: mgh.Spec.EnableMetrics,
EnablePostgresMetrics: (!config.IsBYOPostgres()) && mgh.Spec.EnableMetrics,
EnableInventoryAPI: config.WithInventory(mgh),
PostgresCustomizedConfig: []byte(customizedConfig),
}, nil
})
if err != nil {
return nil, fmt.Errorf("failed to render postgres manifests: %w", err)
log.Errorf("failed to render postgres manifests: %w", err)
return nil, err
}

// create restmapper for deployer to find GVR
Expand Down Expand Up @@ -164,6 +175,24 @@ func getPostgresCredential(ctx context.Context, mgh *globalhubv1alpha4.Multiclus
}, nil
}

func getPostgresCustomizedConfig(ctx context.Context, mgh *globalhubv1alpha4.MulticlusterGlobalHub,
c client.Client,
) (string, error) {
cm := &corev1.ConfigMap{}
err := c.Get(ctx, types.NamespacedName{
Name: BuiltinPostgresCustomizedConfigName,
Namespace: mgh.Namespace,
}, cm)
if err != nil && !errors.IsNotFound(err) {
return "", fmt.Errorf("failed to get the postgres customized config: %v", err)
}
customizedConfig := ""
if !errors.IsNotFound(err) {
customizedConfig = fmt.Sprintf("\n%s", cm.Data["postgresql.conf"])
}
return customizedConfig, nil
}

func getPostgresCA(ctx context.Context, mgh *globalhubv1alpha4.MulticlusterGlobalHub, c client.Client) (string, error) {
ca := &corev1.ConfigMap{}
if err := c.Get(ctx, types.NamespacedName{
Expand Down
17 changes: 16 additions & 1 deletion operator/pkg/controllers/storage/storage_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ var WatchedSecret = sets.NewString(

var WatchedConfigMap = sets.NewString(
BuiltinPostgresCAName,
BuiltinPostgresCustomizedConfigName,
)

var (
Expand Down Expand Up @@ -125,7 +126,7 @@ func (r *StorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
Watches(&corev1.Secret{},
&handler.EnqueueRequestForObject{}, builder.WithPredicates(secretPred)).
Watches(&corev1.ConfigMap{},
&handler.EnqueueRequestForObject{}, builder.WithPredicates(config.GeneralPredicate)).
&handler.EnqueueRequestForObject{}, builder.WithPredicates(configMapPredicate)).
Watches(&appsv1.StatefulSet{},
&handler.EnqueueRequestForObject{}, builder.WithPredicates(statefulSetPred)).
Watches(&corev1.ServiceAccount{},
Expand All @@ -137,6 +138,20 @@ func (r *StorageReconciler) SetupWithManager(mgr ctrl.Manager) error {
Complete(r)
}

var configMapPredicate = predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return WatchedConfigMap.Has(e.Object.GetName())
},
UpdateFunc: func(e event.UpdateEvent) bool {
return WatchedConfigMap.Has(e.ObjectNew.GetName()) ||
e.ObjectNew.GetLabels()[constants.GlobalHubOwnerLabelKey] == constants.GHOperatorOwnerLabelVal
},
DeleteFunc: func(e event.DeleteEvent) bool {
return WatchedConfigMap.Has(e.Object.GetName()) ||
e.Object.GetLabels()[constants.GlobalHubOwnerLabelKey] == constants.GHOperatorOwnerLabelVal
},
}

var statefulSetPred = predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return e.Object.GetNamespace() == commonutils.GetDefaultNamespace() &&
Expand Down
19 changes: 19 additions & 0 deletions test/integration/operator/controllers/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,18 @@ var _ = Describe("storage", Ordered, func() {
Expect(runtimeClient.Create(ctx, mgh)).To(Succeed())
Expect(runtimeClient.Get(ctx, client.ObjectKeyFromObject(mgh), mgh)).To(Succeed())

// add customized postgres configuration
cm := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "multicluster-global-hub-custom-postgresql-config",
Namespace: namespace,
},
Data: map[string]string{
"postgresql.conf": "wal_level = logical\nmax_wal_size = 2GB\n",
},
}
Expect(runtimeClient.Create(ctx, cm)).To(Succeed())

storageReconciler := storage.NewStorageReconciler(runtimeManager, true, false)

// blocking until get the connection
Expand All @@ -247,6 +259,13 @@ var _ = Describe("storage", Ordered, func() {
return nil
}, 10*time.Second, 100*time.Millisecond).ShouldNot(HaveOccurred())

// verify the customized configuration
cm = &corev1.ConfigMap{}
err = runtimeClient.Get(ctx, types.NamespacedName{Namespace: mgh.Namespace, Name: "multicluster-global-hub-postgresql-config"}, cm)
Expect(err).To(Succeed())
Expect(cm.Data["postgresql.conf"]).To(ContainSubstring("max_wal_size = 2GB"))
utils.PrettyPrint(cm.Data["postgresql.conf"])

// cleanup
Eventually(func() error {
if err := testutils.DeleteMgh(ctx, runtimeClient, mgh); err != nil {
Expand Down

0 comments on commit 166e7e6

Please sign in to comment.