/
import_db.go
185 lines (169 loc) · 4.77 KB
/
import_db.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
/*
Copyright The CloudNativePG Contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils
import (
"fmt"
"os"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
apiv1 "github.com/cloudnative-pg/cloudnative-pg/api/v1"
)
// ImportDatabaseMicroservice creates a cluster, starting from an external cluster
// using microservice approach
// NOTE: the application user on the source Cluster needs to be granted with
// REPLICATION permissions, which are not set by default
func ImportDatabaseMicroservice(
namespace,
sourceClusterName,
importedClusterName,
imageName,
databaseName string,
env *TestingEnvironment,
) (*apiv1.Cluster, error) {
if imageName == "" {
imageName = os.Getenv("POSTGRES_IMG")
}
storageClassName := os.Getenv("E2E_DEFAULT_STORAGE_CLASS")
host, err := GetHostName(namespace, sourceClusterName, env)
if err != nil {
return nil, err
}
appUserSecretName := sourceClusterName + apiv1.ApplicationUserSecretSuffix
restoreCluster := &apiv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: importedClusterName,
Namespace: namespace,
},
Spec: apiv1.ClusterSpec{
Instances: 3,
ImageName: imageName,
StorageConfiguration: apiv1.StorageConfiguration{
Size: "1Gi",
StorageClass: &storageClassName,
},
Bootstrap: &apiv1.BootstrapConfiguration{
InitDB: &apiv1.BootstrapInitDB{
Import: &apiv1.Import{
Type: "microservice",
Databases: []string{databaseName},
Source: apiv1.ImportSource{
ExternalCluster: sourceClusterName,
},
PostImportApplicationSQL: []string{"SELECT 1"},
},
},
},
ExternalClusters: []apiv1.ExternalCluster{
{
Name: sourceClusterName,
ConnectionParameters: map[string]string{
"host": host,
"user": AppUser,
"dbname": AppDBName,
},
Password: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: appUserSecretName,
},
Key: "password",
},
},
},
},
}
obj, err := CreateObject(env, restoreCluster)
if err != nil {
return nil, err
}
cluster, ok := obj.(*apiv1.Cluster)
if !ok {
return nil, fmt.Errorf("created object is not of cluster type: %T %v", obj, obj)
}
return cluster, nil
}
// ImportDatabasesMonolith creates a new cluster spec importing from a sourceCluster
// using the Monolith approach.
// Imports all the specified `databaseNames` and `roles` from the source cluster
// NOTE: enableSuperuserAccess needs to be enabled
func ImportDatabasesMonolith(
namespace,
sourceClusterName,
importedClusterName,
imageName string,
databaseNames []string,
roles []string,
env *TestingEnvironment,
) (*apiv1.Cluster, error) {
if imageName == "" {
imageName = os.Getenv("POSTGRES_IMG")
}
storageClassName := os.Getenv("E2E_DEFAULT_STORAGE_CLASS")
host, err := GetHostName(namespace, sourceClusterName, env)
if err != nil {
return nil, err
}
superUserSecretName := sourceClusterName + apiv1.SuperUserSecretSuffix
targetCluster := &apiv1.Cluster{
ObjectMeta: v1.ObjectMeta{
Name: importedClusterName,
Namespace: namespace,
},
Spec: apiv1.ClusterSpec{
Instances: 3,
ImageName: imageName,
EnableSuperuserAccess: ptr.To(true),
StorageConfiguration: apiv1.StorageConfiguration{
Size: "1Gi",
StorageClass: &storageClassName,
},
Bootstrap: &apiv1.BootstrapConfiguration{
InitDB: &apiv1.BootstrapInitDB{
Import: &apiv1.Import{
Type: "monolith",
Databases: databaseNames,
Roles: roles,
Source: apiv1.ImportSource{
ExternalCluster: sourceClusterName,
},
},
},
},
ExternalClusters: []apiv1.ExternalCluster{
{
Name: sourceClusterName,
ConnectionParameters: map[string]string{
"host": host,
"user": PostgresUser,
"dbname": PostgresDBName,
},
Password: &corev1.SecretKeySelector{
LocalObjectReference: corev1.LocalObjectReference{
Name: superUserSecretName,
},
Key: "password",
},
},
},
},
}
obj, err := CreateObject(env, targetCluster)
if err != nil {
return nil, err
}
cluster, ok := obj.(*apiv1.Cluster)
if !ok {
return nil, fmt.Errorf("created object is not of cluster type: %T %v", obj, obj)
}
return cluster, nil
}