Skip to content

Commit 13f2a1e

Browse files
authored
kc25/workshop/mcp: pgpassfile now includes 3 DBs to follow the exercise (#16)
1 parent 5e98c7e commit 13f2a1e

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

20250401-kubecon-london/workshop/kcp-multicluster-provider-example/internal/controller/application_controller.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"encoding/json"
2222
"fmt"
23+
"strings"
2324

2425
appsv1 "k8s.io/api/apps/v1"
2526
corev1 "k8s.io/api/core/v1"
@@ -97,7 +98,16 @@ func (r *ApplicationReconciler) Reconcile(ctx context.Context, req ctrl.Request)
9798
return ctrl.Result{}, err
9899
}
99100

100-
pgpass := newPgpassData(&db, secret, namespace)
101+
var dbCluster cnpgapiv1.Cluster
102+
err = r.ProviderClient.Get(ctx, types.NamespacedName{
103+
Namespace: namespace,
104+
Name: db.GetClusterRef().Name,
105+
}, &dbCluster)
106+
if err != nil {
107+
return ctrl.Result{}, err
108+
}
109+
110+
pgpass := newPgpassData(&db, &dbCluster, secret, namespace)
101111

102112
deployment, err := getApplicationDeployment(pgpass, app, namespace)
103113
if err != nil {
@@ -184,23 +194,28 @@ type pgpassData struct {
184194
PassFile string
185195
SSLMode string
186196
MaintenanceDB string
197+
initDb string
198+
createdDb string
187199
secret string
188200
}
189201

190202
func newPgpassData(
191203
db *cnpgapiv1.Database,
204+
dbCluster *cnpgapiv1.Cluster,
192205
secret corev1.Secret,
193206
namespace string,
194207
) *pgpassData {
195208
return &pgpassData{
196-
Name: db.Name,
209+
Name: dbCluster.Name,
197210
Group: "Servers",
198211
Host: pgsqlServerHost(db),
199212
Port: 5432,
200213
Username: string(secret.Data["username"]),
201214
PassFile: "/tmp/pgpassfile", // We don't have perms to write to /pgadmin4 where this normally would be.
202215
SSLMode: "prefer",
203216
MaintenanceDB: "postgres",
217+
initDb: dbCluster.GetApplicationDatabaseName(),
218+
createdDb: db.Spec.Name,
204219
secret: string(secret.Data["password"]),
205220
}
206221
}
@@ -217,9 +232,17 @@ func (data *pgpassData) toServersJson() ([]byte, error) {
217232
}
218233

219234
func (data *pgpassData) toPassfileContent() string {
220-
// See https://www.postgresql.org/docs/current/libpq-pgpass.html.
221-
return fmt.Sprintf("%s:%d:%s:%s:%s",
222-
data.Host, data.Port, data.MaintenanceDB, data.Username, data.secret)
235+
var sb strings.Builder
236+
for _, dbName := range []string{data.MaintenanceDB, data.initDb, data.createdDb} {
237+
if dbName == "" {
238+
continue
239+
}
240+
// See https://www.postgresql.org/docs/current/libpq-pgpass.html.
241+
sb.WriteString(fmt.Sprintf("%s:%d:%s:%s:%s\n",
242+
data.Host, data.Port, dbName, data.Username, data.secret))
243+
}
244+
245+
return sb.String()
223246
}
224247

225248
func getApplicationService(app *apisv1alpha1.Application, namespace string) (*corev1.Service, error) {

0 commit comments

Comments
 (0)