Skip to content

Commit 24d412a

Browse files
authored
generate spilo config can return error (with test) (zalando#570)
* fix: raise explicit error when failing to generate spilo config Signed-off-by: Stephane Tang <[email protected]>
1 parent 1f4267e commit 24d412a

File tree

2 files changed

+73
-9
lines changed

2 files changed

+73
-9
lines changed

pkg/cluster/k8sres.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func fillResourceList(spec acidv1.ResourceDescription, defaults acidv1.ResourceD
149149
return requests, nil
150150
}
151151

152-
func generateSpiloJSONConfiguration(pg *acidv1.PostgresqlParam, patroni *acidv1.Patroni, pamRoleName string, logger *logrus.Entry) string {
152+
func generateSpiloJSONConfiguration(pg *acidv1.PostgresqlParam, patroni *acidv1.Patroni, pamRoleName string, logger *logrus.Entry) (string, error) {
153153
config := spiloConfiguration{}
154154

155155
config.Bootstrap = pgBootstrap{}
@@ -249,12 +249,9 @@ PatroniInitDBParams:
249249
Options: []string{constants.RoleFlagCreateDB, constants.RoleFlagNoLogin},
250250
},
251251
}
252-
result, err := json.Marshal(config)
253-
if err != nil {
254-
logger.Errorf("cannot convert spilo configuration into JSON: %v", err)
255-
return ""
256-
}
257-
return string(result)
252+
253+
res, err := json.Marshal(config)
254+
return string(res), err
258255
}
259256

260257
func getLocalAndBoostrapPostgreSQLParameters(parameters map[string]string) (local, bootstrap map[string]string) {
@@ -780,7 +777,10 @@ func (c *Cluster) generateStatefulSet(spec *acidv1.PostgresSpec) (*v1beta1.State
780777
func(i, j int) bool { return customPodEnvVarsList[i].Name < customPodEnvVarsList[j].Name })
781778
}
782779

783-
spiloConfiguration := generateSpiloJSONConfiguration(&spec.PostgresqlParam, &spec.Patroni, c.OpConfig.PamRoleName, c.logger)
780+
spiloConfiguration, err := generateSpiloJSONConfiguration(&spec.PostgresqlParam, &spec.Patroni, c.OpConfig.PamRoleName, c.logger)
781+
if err != nil {
782+
return nil, fmt.Errorf("could not generate Spilo JSON configuration: %v", err)
783+
}
784784

785785
// generate environment variables for the spilo container
786786
spiloEnvVars := deduplicateEnvVars(

pkg/cluster/k8sres_test.go

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package cluster
33
import (
44
"k8s.io/api/core/v1"
55

6+
"testing"
7+
68
acidv1 "github.com/zalando/postgres-operator/pkg/apis/acid.zalan.do/v1"
79
"github.com/zalando/postgres-operator/pkg/util/config"
810
"github.com/zalando/postgres-operator/pkg/util/constants"
911
"github.com/zalando/postgres-operator/pkg/util/k8sutil"
10-
"testing"
1112
)
1213

1314
func True() *bool {
@@ -20,6 +21,69 @@ func False() *bool {
2021
return &b
2122
}
2223

24+
func TestGenerateSpiloJSONConfiguration(t *testing.T) {
25+
var cluster = New(
26+
Config{
27+
OpConfig: config.Config{
28+
ProtectedRoles: []string{"admin"},
29+
Auth: config.Auth{
30+
SuperUsername: superUserName,
31+
ReplicationUsername: replicationUserName,
32+
},
33+
},
34+
}, k8sutil.KubernetesClient{}, acidv1.Postgresql{}, logger)
35+
36+
testName := "TestGenerateSpiloConfig"
37+
tests := []struct {
38+
subtest string
39+
pgParam *acidv1.PostgresqlParam
40+
patroni *acidv1.Patroni
41+
role string
42+
opConfig config.Config
43+
result string
44+
}{
45+
{
46+
subtest: "Patroni default configuration",
47+
pgParam: &acidv1.PostgresqlParam{PgVersion: "9.6"},
48+
patroni: &acidv1.Patroni{},
49+
role: "zalandos",
50+
opConfig: config.Config{},
51+
result: `{"postgresql":{"bin_dir":"/usr/lib/postgresql/9.6/bin"},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"}],"users":{"zalandos":{"password":"","options":["CREATEDB","NOLOGIN"]}},"dcs":{}}}`,
52+
},
53+
{
54+
subtest: "Patroni configured",
55+
pgParam: &acidv1.PostgresqlParam{PgVersion: "11"},
56+
patroni: &acidv1.Patroni{
57+
InitDB: map[string]string{
58+
"encoding": "UTF8",
59+
"locale": "en_US.UTF-8",
60+
"data-checksums": "true",
61+
},
62+
PgHba: []string{"hostssl all all 0.0.0.0/0 md5", "host all all 0.0.0.0/0 md5"},
63+
TTL: 30,
64+
LoopWait: 10,
65+
RetryTimeout: 10,
66+
MaximumLagOnFailover: 33554432,
67+
Slots: map[string]map[string]string{"permanent_logical_1": {"type": "logical", "database": "foo", "plugin": "pgoutput"}},
68+
},
69+
role: "zalandos",
70+
opConfig: config.Config{},
71+
result: `{"postgresql":{"bin_dir":"/usr/lib/postgresql/11/bin","pg_hba":["hostssl all all 0.0.0.0/0 md5","host all all 0.0.0.0/0 md5"]},"bootstrap":{"initdb":[{"auth-host":"md5"},{"auth-local":"trust"},"data-checksums",{"encoding":"UTF8"},{"locale":"en_US.UTF-8"}],"users":{"zalandos":{"password":"","options":["CREATEDB","NOLOGIN"]}},"dcs":{"ttl":30,"loop_wait":10,"retry_timeout":10,"maximum_lag_on_failover":33554432,"slots":{"permanent_logical_1":{"database":"foo","plugin":"pgoutput","type":"logical"}}}}}`,
72+
},
73+
}
74+
for _, tt := range tests {
75+
cluster.OpConfig = tt.opConfig
76+
result, err := generateSpiloJSONConfiguration(tt.pgParam, tt.patroni, tt.role, logger)
77+
if err != nil {
78+
t.Errorf("Unexpected error: %v", err)
79+
}
80+
if tt.result != result {
81+
t.Errorf("%s %s: Spilo Config is %v, expected %v for role %#v and param %#v",
82+
testName, tt.subtest, result, tt.result, tt.role, tt.pgParam)
83+
}
84+
}
85+
}
86+
2387
func TestCreateLoadBalancerLogic(t *testing.T) {
2488
var cluster = New(
2589
Config{

0 commit comments

Comments
 (0)