forked from mongodb/mongodb-kubernetes-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe2eutil.go
221 lines (202 loc) · 5.75 KB
/
e2eutil.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
package e2eutil
import (
"context"
"fmt"
"reflect"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/resource"
"github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/secret"
"github.com/mongodb/mongodb-kubernetes-operator/pkg/util/envvar"
mdbv1 "github.com/mongodb/mongodb-kubernetes-operator/api/v1"
corev1 "k8s.io/api/core/v1"
apiErrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
k8sClient "sigs.k8s.io/controller-runtime/pkg/client"
)
const testDataDirEnv = "TEST_DATA_DIR"
// TestLabels should be applied to all resources created by tests.
func TestLabels() map[string]string {
return map[string]string{
"e2e-test": "true",
}
}
// TestAnnotations create an annotations map
func TestAnnotations() map[string]string {
return map[string]string{
"e2e-test-annotated": "true",
}
}
func TestDataDir() string {
return envvar.GetEnvOrDefault(testDataDirEnv, "/workspace/testdata")
}
func TlsTestDataDir() string {
return fmt.Sprintf("%s/tls", TestDataDir())
}
// UpdateMongoDBResource applies the provided function to the most recent version of the MongoDB resource
// and retries when there are conflicts
func UpdateMongoDBResource(original *mdbv1.MongoDBCommunity, updateFunc func(*mdbv1.MongoDBCommunity)) error {
err := TestClient.Get(context.TODO(), types.NamespacedName{Name: original.Name, Namespace: original.Namespace}, original)
if err != nil {
return err
}
updateFunc(original)
return TestClient.Update(context.TODO(), original)
}
func NewTestMongoDB(ctx *Context, name string, namespace string) (mdbv1.MongoDBCommunity, mdbv1.MongoDBUser) {
mongodbNamespace := namespace
if mongodbNamespace == "" {
mongodbNamespace = OperatorNamespace
}
mdb := mdbv1.MongoDBCommunity{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: mongodbNamespace,
Labels: TestLabels(),
},
Spec: mdbv1.MongoDBCommunitySpec{
Members: 3,
Type: "ReplicaSet",
Version: "7.0.2",
Arbiters: 0,
Security: mdbv1.Security{
Authentication: mdbv1.Authentication{
Modes: []mdbv1.AuthMode{"SCRAM"},
},
},
Users: []mdbv1.MongoDBUser{
{
Name: fmt.Sprintf("%s-user", name),
PasswordSecretRef: mdbv1.SecretKeyReference{
Key: fmt.Sprintf("%s-password", name),
Name: fmt.Sprintf("%s-%s-password-secret", name, ctx.ExecutionId),
},
Roles: []mdbv1.Role{
// roles on testing db for general connectivity
{
DB: "testing",
Name: "readWrite",
},
{
DB: "testing",
Name: "clusterAdmin",
},
// admin roles for reading FCV
{
DB: "admin",
Name: "readWrite",
},
{
DB: "admin",
Name: "clusterAdmin",
},
{
DB: "admin",
Name: "userAdmin",
},
},
ScramCredentialsSecretName: fmt.Sprintf("%s-my-scram", name),
},
},
StatefulSetConfiguration: mdbv1.StatefulSetConfiguration{
SpecWrapper: mdbv1.StatefulSetSpecWrapper{
Spec: appsv1.StatefulSetSpec{
Template: corev1.PodTemplateSpec{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "mongod",
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
"cpu": resource.MustParse("1.0"),
"memory": resource.MustParse("200M"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
"cpu": resource.MustParse("0.1"),
"memory": resource.MustParse("200M"),
},
},
},
{
Name: "mongodb-agent",
Resources: corev1.ResourceRequirements{
Limits: map[corev1.ResourceName]resource.Quantity{
"cpu": resource.MustParse("1.0"),
"memory": resource.MustParse("200M"),
},
Requests: map[corev1.ResourceName]resource.Quantity{
"cpu": resource.MustParse("0.1"),
"memory": resource.MustParse("200M"),
},
},
},
},
},
},
}},
},
},
}
return mdb, mdb.Spec.Users[0]
}
func NewTestTLSConfig(optional bool) mdbv1.TLS {
return mdbv1.TLS{
Enabled: true,
Optional: optional,
CertificateKeySecret: corev1.LocalObjectReference{
Name: "tls-certificate",
},
CaCertificateSecret: &corev1.LocalObjectReference{
Name: "tls-ca-key-pair",
},
}
}
func NewPrometheusConfig(namespace string) *mdbv1.Prometheus {
sec := secret.Builder().
SetName("prom-secret").
SetNamespace(namespace).
SetField("password", "prom-password").
Build()
err := TestClient.Create(context.TODO(), &sec, &CleanupOptions{})
if err != nil {
if !apiErrors.IsAlreadyExists(err) {
panic(fmt.Sprintf("Error trying to create secret: %s", err))
}
}
return &mdbv1.Prometheus{
Username: "prom-user",
PasswordSecretRef: mdbv1.SecretKeyReference{
Name: "prom-secret",
},
}
}
func ensureObject(ctx *Context, obj k8sClient.Object) error {
key := k8sClient.ObjectKeyFromObject(obj)
obj.SetLabels(TestLabels())
err := TestClient.Get(context.TODO(), key, obj)
if err != nil {
if !apiErrors.IsNotFound(err) {
return err
}
err = TestClient.Create(context.TODO(), obj, &CleanupOptions{TestContext: ctx})
if err != nil {
return err
}
} else {
fmt.Printf("%s %s/%s already exists!\n", reflect.TypeOf(obj), key.Namespace, key.Name)
err = TestClient.Update(context.TODO(), obj)
if err != nil {
return err
}
}
return nil
}
// EnsureNamespace checks that the given namespace exists and creates it if not.
func EnsureNamespace(ctx *Context, namespace string) error {
return ensureObject(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
Labels: TestLabels(),
},
})
}