-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
postgresql_replica.go
255 lines (208 loc) · 10.5 KB
/
postgresql_replica.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package image_ecosystem
import (
"fmt"
"time"
g "github.com/onsi/ginkgo"
o "github.com/onsi/gomega"
"github.com/openshift/api/template"
exutil "github.com/openshift/origin/test/extended/util"
"github.com/openshift/origin/test/extended/util/db"
testutil "github.com/openshift/origin/test/util"
// kapiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kcoreclient "k8s.io/client-go/kubernetes/typed/core/v1"
e2e "k8s.io/kubernetes/test/e2e/framework"
)
var (
postgreSQLReplicationTemplate = "https://raw.githubusercontent.com/sclorg/postgresql-container/master/examples/replica/postgresql_replica.json"
postgreSQLEphemeralTemplate = exutil.FixturePath("..", "..", "examples", "db-templates", "postgresql-ephemeral-template.json")
postgreSQLHelperName = "postgresql-helper"
postgreSQLImages = []string{
"postgresql:9.6",
}
)
/*
var _ = g.Describe("[image_ecosystem][postgresql][Slow][local] openshift postgresql replication", func() {
defer g.GinkgoRecover()
g.Skip("db replica tests are currently flaky and disabled")
var oc = exutil.NewCLI("postgresql-replication", exutil.KubeConfigPath())
var pvs = []*kapiv1.PersistentVolume{}
var nfspod = &kapiv1.Pod{}
var cleanup = func() {
// per k8s e2e volume_util.go:VolumeTestCleanup, nuke any client pods
// before nfs server to assist with umount issues; as such, need to clean
// up prior to the AfterEach processing, to guaranteed deletion order
g.By("start cleanup")
if g.CurrentGinkgoTestDescription().Failed {
exutil.DumpPodStates(oc)
exutil.DumpPodLogsStartingWith("", oc)
exutil.DumpImageStreams(oc)
exutil.DumpPersistentVolumeInfo(oc)
}
client := oc.AsAdmin().KubeFramework().ClientSet
g.By("removing postgresql")
exutil.RemoveDeploymentConfigs(oc, "postgresql-master", "postgresql-slave")
g.By("deleting PVCs")
exutil.DeletePVCsForDeployment(client, oc, "postgre")
g.By("removing nfs pvs")
for _, pv := range pvs {
e2e.DeletePersistentVolume(client, pv.Name)
}
g.By("removing nfs pod")
e2e.DeletePodWithWait(oc.AsAdmin().KubeFramework(), client, nfspod)
}
g.Context("", func() {
g.BeforeEach(func() {
exutil.PreTestDump()
g.By("waiting for default service account")
err := exutil.WaitForServiceAccount(oc.KubeClient().Core().ServiceAccounts(oc.Namespace()), "default")
o.Expect(err).NotTo(o.HaveOccurred())
g.By("PV/PVC dump before setup")
exutil.DumpPersistentVolumeInfo(oc)
nfspod, pvs, err = exutil.SetupK8SNFSServerAndVolume(oc, 8)
o.Expect(err).NotTo(o.HaveOccurred())
})
for _, image := range postgreSQLImages {
g.It(fmt.Sprintf("postgresql replication works for %s", image), PostgreSQLReplicationTestFactory(oc, image, cleanup))
}
})
})
*/
// CreatePostgreSQLReplicationHelpers creates a set of PostgreSQL helpers for master,
// slave an en extra helper that is used for remote login test.
func CreatePostgreSQLReplicationHelpers(c kcoreclient.PodInterface, masterDeployment, slaveDeployment, helperDeployment string, slaveCount int) (exutil.Database, []exutil.Database, exutil.Database) {
podNames, err := exutil.WaitForPods(c, exutil.ParseLabelsOrDie(fmt.Sprintf("deployment=%s", masterDeployment)), exutil.CheckPodIsRunning, 1, 4*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
masterPod := podNames[0]
slavePods, err := exutil.WaitForPods(c, exutil.ParseLabelsOrDie(fmt.Sprintf("deployment=%s", slaveDeployment)), exutil.CheckPodIsRunning, slaveCount, 6*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
// Create PostgreSQL helper for master
master := db.NewPostgreSQL(masterPod, "")
// Create PostgreSQL helpers for slaves
slaves := make([]exutil.Database, len(slavePods))
for i := range slavePods {
slave := db.NewPostgreSQL(slavePods[i], masterPod)
slaves[i] = slave
}
helperNames, err := exutil.WaitForPods(c, exutil.ParseLabelsOrDie(fmt.Sprintf("deployment=%s", helperDeployment)), exutil.CheckPodIsRunning, 1, 4*time.Minute)
o.Expect(err).NotTo(o.HaveOccurred())
helper := db.NewPostgreSQL(helperNames[0], masterPod)
return master, slaves, helper
}
func PostgreSQLReplicationTestFactory(oc *exutil.CLI, image string, cleanup func()) func() {
return func() {
// per k8s e2e volume_util.go:VolumeTestCleanup, nuke any client pods
// before nfs server to assist with umount issues; as such, need to clean
// up prior to the AfterEach processing, to guaranteed deletion order
defer cleanup()
err := testutil.WaitForPolicyUpdate(oc.KubeClient().AuthorizationV1(), oc.Namespace(), "create", template.Resource("templates"), true)
o.Expect(err).NotTo(o.HaveOccurred())
exutil.WaitForOpenShiftNamespaceImageStreams(oc)
err = oc.Run("create").Args("-f", postgreSQLReplicationTemplate).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("new-app").Args("--template", "pg-replica-example", "-p", fmt.Sprintf("IMAGESTREAMTAG=%s", image)).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("new-app").Args("-f", postgreSQLEphemeralTemplate, "-p", fmt.Sprintf("DATABASE_SERVICE_NAME=%s", postgreSQLHelperName)).Execute()
o.Expect(err).NotTo(o.HaveOccurred())
g.By("PV/PVC dump after setup")
exutil.DumpPersistentVolumeInfo(oc)
// oc.KubeFramework().WaitForAnEndpoint currently will wait forever; for now, prefacing with our WaitForADeploymentToComplete,
// which does have a timeout, since in most cases a failure in the service coming up stems from a failed deployment
err = exutil.WaitForDeploymentConfig(oc.KubeClient(), oc.AppsClient().AppsV1(), oc.Namespace(), postgreSQLHelperName, 1, true, oc)
o.Expect(err).NotTo(o.HaveOccurred())
err = e2e.WaitForEndpoint(oc.KubeFramework().ClientSet, oc.Namespace(), postgreSQLHelperName)
o.Expect(err).NotTo(o.HaveOccurred())
tableCounter := 0
assertReplicationIsWorking := func(masterDeployment, slaveDeployment string, slaveCount int) (exutil.Database, []exutil.Database, exutil.Database) {
check := func(err error) {
if err != nil {
exutil.DumpApplicationPodLogs("postgresql-master", oc)
exutil.DumpApplicationPodLogs("postgresql-slave", oc)
}
o.ExpectWithOffset(1, err).NotTo(o.HaveOccurred())
}
tableCounter++
table := fmt.Sprintf("table_%0.2d", tableCounter)
master, slaves, helper := CreatePostgreSQLReplicationHelpers(oc.KubeClient().CoreV1().Pods(oc.Namespace()), masterDeployment, slaveDeployment, fmt.Sprintf("%s-1", postgreSQLHelperName), slaveCount)
err := exutil.WaitUntilAllHelpersAreUp(oc, []exutil.Database{master, helper})
if err != nil {
exutil.DumpApplicationPodLogs("postgresql-master", oc)
exutil.DumpApplicationPodLogs("postgresql-helper", oc)
}
o.ExpectWithOffset(1, err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilAllHelpersAreUp(oc, slaves)
check(err)
// Test if we can query as admin
err = e2e.WaitForEndpoint(oc.KubeFramework().ClientSet, oc.Namespace(), "postgresql-master")
check(err)
err = helper.TestRemoteLogin(oc, "postgresql-master")
check(err)
// Create a new table with random name
_, err = master.Query(oc, fmt.Sprintf("CREATE TABLE %s (col1 VARCHAR(20), col2 VARCHAR(20));", table))
check(err)
// Write new data to the table through master
_, err = master.Query(oc, fmt.Sprintf("INSERT INTO %s (col1, col2) VALUES ('val1', 'val2');", table))
check(err)
// Make sure data is present on master
err = exutil.WaitForQueryOutputContains(oc, master, 10*time.Second, false,
fmt.Sprintf("SELECT * FROM %s;", table),
"col1 | val1\ncol2 | val2")
check(err)
// Make sure data was replicated to all slaves
for _, slave := range slaves {
err = exutil.WaitForQueryOutputContains(oc, slave, 90*time.Second, false,
fmt.Sprintf("SELECT * FROM %s;", table),
"col1 | val1\ncol2 | val2")
check(err)
}
return master, slaves, helper
}
g.By("after initial deployment")
master, _, _ := assertReplicationIsWorking("postgresql-master-1", "postgresql-slave-1", 1)
g.By("after master is restarted by changing the Deployment Config")
err = oc.Run("set", "env").Args("dc", "postgresql-master", "POSTGRESQL_ADMIN_PASSWORD=newpass").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeClient().CoreV1().Pods(oc.Namespace()), master.PodName(), 2*time.Minute)
if err != nil {
e2e.Logf("Checking if pod %s still exists", master.PodName())
oc.Run("get").Args("pod", master.PodName(), "-o", "yaml").Execute()
}
o.Expect(err).NotTo(o.HaveOccurred())
master, _, _ = assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)
g.By("after master is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=postgresql-master-2").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeClient().CoreV1().Pods(oc.Namespace()), master.PodName(), 2*time.Minute)
if err != nil {
e2e.Logf("Checking if pod %s still exists", master.PodName())
oc.Run("get").Args("pod", master.PodName(), "-o", "yaml").Execute()
}
o.Expect(err).NotTo(o.HaveOccurred())
_, slaves, _ := assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)
g.By("after slave is restarted by deleting the pod")
err = oc.Run("delete").Args("pod", "-l", "deployment=postgresql-slave-1").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeClient().CoreV1().Pods(oc.Namespace()), slaves[0].PodName(), 2*time.Minute)
if err != nil {
e2e.Logf("Checking if pod %s still exists", slaves[0].PodName())
oc.Run("get").Args("pod", slaves[0].PodName(), "-o", "yaml").Execute()
}
o.Expect(err).NotTo(o.HaveOccurred())
assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 1)
pods, err := oc.KubeClient().CoreV1().Pods(oc.Namespace()).List(metav1.ListOptions{LabelSelector: exutil.ParseLabelsOrDie("deployment=postgresql-slave-1").String()})
o.Expect(err).NotTo(o.HaveOccurred())
o.Expect(len(pods.Items)).To(o.Equal(1))
g.By("after slave is scaled to 0 and then back to 4 replicas")
err = oc.Run("scale").Args("dc", "postgresql-slave", "--replicas=0").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
err = exutil.WaitUntilPodIsGone(oc.KubeClient().CoreV1().Pods(oc.Namespace()), pods.Items[0].Name, 2*time.Minute)
if err != nil {
e2e.Logf("Checking if pod %s still exists", pods.Items[0].Name)
oc.Run("get").Args("pod", pods.Items[0].Name, "-o", "yaml").Execute()
}
o.Expect(err).NotTo(o.HaveOccurred())
err = oc.Run("scale").Args("dc", "postgresql-slave", "--replicas=4").Execute()
o.Expect(err).NotTo(o.HaveOccurred())
assertReplicationIsWorking("postgresql-master-2", "postgresql-slave-1", 4)
}
}