-
Notifications
You must be signed in to change notification settings - Fork 102
/
phase1_verifying.go
313 lines (282 loc) · 10.6 KB
/
phase1_verifying.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package system
import (
"database/sql"
"fmt"
"os"
// this is the driver we are using for psql
_ "github.com/lib/pq"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/asaskevich/govalidator"
semver "github.com/coreos/go-semver/semver"
dockerref "github.com/docker/distribution/reference"
nbv1 "github.com/noobaa/noobaa-operator/v5/pkg/apis/noobaa/v1alpha1"
"github.com/noobaa/noobaa-operator/v5/pkg/options"
"github.com/noobaa/noobaa-operator/v5/pkg/util"
)
// ReconcilePhaseVerifying runs the reconcile verify phase
func (r *Reconciler) ReconcilePhaseVerifying() error {
r.SetPhase(
nbv1.SystemPhaseVerifying,
"SystemPhaseVerifying",
"noobaa operator started phase 1/4 - \"Verifying\"",
)
if err := r.CheckSystemCR(); err != nil {
return err
}
if r.JoinSecret != nil {
if err := r.CheckJoinSecret(); err != nil {
return err
}
}
if r.ExternalPgSecret != nil {
if r.ExternalPgSecret.StringData["db_url"] == "" {
return util.NewPersistentError("InvalidExternalPgSecert",
"ExternalPgSecret is missing db_url")
}
if r.ExternalPgSSLSecret != nil {
if r.ExternalPgSSLSecret.StringData["tls.key"] == "" ||
r.ExternalPgSSLSecret.StringData["tls.crt"] == "" {
return util.NewPersistentError("InvalidExternalPgCert",
fmt.Sprintf("%q is missing private key (must be tls.key)"+
" or missing cert key (must be tls.cert)", r.ExternalPgSSLSecret.Name))
}
err := os.WriteFile("/tmp/tls.key", []byte(r.ExternalPgSSLSecret.StringData["tls.key"]), 0600)
if err != nil {
return fmt.Errorf("failed to write k8s secret tls.key content to a file %v", err)
}
err = os.WriteFile("/tmp/tls.crt", []byte(r.ExternalPgSSLSecret.StringData["tls.crt"]), 0644)
if err != nil {
return fmt.Errorf("failed to write k8s secret tls.key content to a file %v", err)
}
os.Setenv("PGSSLKEY", "/tmp/tls.key")
os.Setenv("PGSSLCERT", "/tmp/tls.crt")
}
if err := r.checkExternalPg(r.ExternalPgSecret.StringData["db_url"]); err != nil {
return err
}
}
if r.NooBaa.Spec.BucketLogging.LoggingType == nbv1.BucketLoggingTypeGuaranteed {
if err := r.checkPersistentLoggingPVC(r.NooBaa.Spec.BucketLogging.BucketLoggingPVC, r.BucketLoggingPVC, "InvalidBucketLoggingConfiguration"); err != nil {
return err
}
}
if r.NooBaa.Spec.BucketNotifications.Enabled {
if err := r.checkPersistentLoggingPVC(r.NooBaa.Spec.BucketNotifications.PVC, r.BucketNotificationsPVC, "InvalidBucketNotificationConfiguration"); err != nil {
return err
}
}
return nil
}
// CheckSystemCR checks the validity of the system CR
// (i.e system.metadata.name and system.spec.image)
// and updates the status accordingly
func (r *Reconciler) CheckSystemCR() error {
log := r.Logger.WithField("func", "CheckSystemCR")
// we assume a single system per ns here
if r.NooBaa.Name != options.SystemName {
return util.NewPersistentError("InvalidSystemName",
fmt.Sprintf("Invalid system name %q expected %q", r.NooBaa.Name, options.SystemName))
}
specImage := options.ContainerImage
if os.Getenv("NOOBAA_CORE_IMAGE") != "" {
specImage = os.Getenv("NOOBAA_CORE_IMAGE")
}
if r.NooBaa.Spec.Image != nil {
specImage = *r.NooBaa.Spec.Image
}
// Parse the image spec as a docker image url
imageRef, err := dockerref.Parse(specImage)
// If the image cannot be parsed log the incident and mark as persistent error
// since we don't need to retry until the spec is updated.
if err != nil {
return util.NewPersistentError("InvalidImage",
fmt.Sprintf(`Invalid image requested %q %v`, specImage, err))
}
// Get the image name and tag
imageName := ""
imageTag := ""
switch image := imageRef.(type) {
case dockerref.NamedTagged:
log.Infof("Parsed image (NamedTagged) %v", image)
imageName = image.Name()
imageTag = image.Tag()
case dockerref.Tagged:
log.Infof("Parsed image (Tagged) %v", image)
imageTag = image.Tag()
case dockerref.Named:
log.Infof("Parsed image (Named) %v", image)
imageName = image.Name()
default:
log.Infof("Parsed image (unstructured) %v", image)
}
if imageName == options.ContainerImageName {
version, err := semver.NewVersion(imageTag)
if err == nil {
log.Infof("Parsed version %q from image tag %q", version.String(), imageTag)
minver := semver.New(options.ContainerImageSemverLowerBound)
maxver := semver.New(options.ContainerImageSemverUpperBound)
if version.Compare(*minver) != 1 || version.Compare(*maxver) != -1 {
return util.NewPersistentError("InvalidImageVersion",
fmt.Sprintf(`Invalid image version %q not matching version constraints: >=%v, <%v`,
imageRef, minver, maxver))
}
} else {
log.Infof("Using custom image %q", imageRef.String())
}
} else {
log.Infof("Using custom image name %q the default is %q", imageRef.String(), options.ContainerImageName)
}
// Set ActualImage to be updated in the noobaa status
r.NooBaa.Status.ActualImage = specImage
// Verify the endpoints spec
endpointsSpec := r.NooBaa.Spec.Endpoints
if endpointsSpec != nil {
if endpointsSpec.MinCount <= 0 {
return util.NewPersistentError("InvalidEndpointsConfiguration",
"Invalid endpoint min count (must be greater than 0)")
}
// Validate bounds on endpoint counts
if endpointsSpec.MinCount > endpointsSpec.MaxCount {
return util.NewPersistentError("InvalidEndpointsConfiguration",
"Invalid endpoint maximum count (must be higher than or equal to minimum count)")
}
// Validate that all virtual hosts are in FQDN format
for _, virtualHost := range endpointsSpec.AdditionalVirtualHosts {
if !govalidator.IsDNSName(virtualHost) {
return util.NewPersistentError("InvalidEndpointsConfiguration",
fmt.Sprintf(`Invalid virtual host %s, not a fully qualified DNS name`, virtualHost))
}
}
}
// Validate the DefaultBackingStore Spec
// nolint: staticcheck
if r.NooBaa.Spec.DefaultBackingStoreSpec != nil {
return util.NewPersistentError("Invalid, DefaultBackingStoreSpec was deprecated", fmt.Sprintf(`%s`, err))
}
if (r.NooBaa.Spec.Autoscaler.AutoscalerType == nbv1.AutoscalerTypeKeda ||
r.NooBaa.Spec.Autoscaler.AutoscalerType == nbv1.AutoscalerTypeHPAV2) &&
r.NooBaa.Spec.Autoscaler.PrometheusNamespace == "" {
return util.NewPersistentError("InvalidEndpointsAutoscalerConfiguration",
fmt.Sprintf("Autoscaler %s missing prometheusNamespace property ", r.NooBaa.Spec.Autoscaler.AutoscalerType))
}
return nil
}
// CheckJoinSecret checks that all need information to allow to join
// another noobaa clauster is specified in the join secret
func (r *Reconciler) CheckJoinSecret() error {
if r.JoinSecret.StringData["mgmt_addr"] == "" {
return util.NewPersistentError("InvalidJoinSecert",
"JoinSecret is missing mgmt_addr")
}
if r.JoinSecret.StringData["auth_token"] == "" {
return util.NewPersistentError("InvalidJoinSecert",
"JoinSecret is missing auth_token")
}
if !util.IsRemoteClientNoobaa(r.NooBaa.GetAnnotations()) {
if r.JoinSecret.StringData["bg_addr"] == "" {
return util.NewPersistentError("InvalidJoinSecert",
"JoinSecret is missing bg_addr")
}
if r.JoinSecret.StringData["md_addr"] == "" {
return util.NewPersistentError("InvalidJoinSecert",
"JoinSecret is missing md_addr")
}
if r.JoinSecret.StringData["hosted_agents_addr"] == "" {
return util.NewPersistentError("InvalidJoinSecert",
"JoinSecret is missing hosted_agents_addr")
}
}
return nil
}
func (r *Reconciler) checkExternalPg(postgresDbURL string) error {
dbURL := r.ExternalPgSecret.StringData["db_url"]
if r.NooBaa.Spec.ExternalPgSSLRequired {
if !r.NooBaa.Spec.ExternalPgSSLUnauthorized {
dbURL += "?sslmode=verify-full" // won't allow self-signed certs
} // when we want to allow self-signed we will use the default sslmode=require
} else {
dbURL += "?sslmode=disable" // don't use ssl - the default is to use it
}
db, err := sql.Open("postgres", dbURL)
if err != nil {
return util.NewPersistentError("InvalidExternalPgUrl",
fmt.Sprintf("failed openning a connection to external DB url: %q, error: %s",
dbURL, err))
}
defer db.Close()
err = db.Ping()
if err != nil {
return util.NewPersistentError("InvalidExternalPgUrl",
fmt.Sprintf("failed pinging external DB url: %q, error: %s",
dbURL, err))
}
// Query the PostgreSQL version
var version string
err = db.QueryRow("SELECT current_setting('server_version_num')::integer / 10000").Scan(&version)
if err != nil {
return util.NewPersistentError("InvalidExternalPgVersion",
fmt.Sprintf("failed getting version of external DB url: %q, error: %s",
dbURL, err))
}
// Check if the version is 15
if version != "15" {
return util.NewPersistentError("InvalidExternalPgVersion",
fmt.Sprintf("version of external DB %q, is not supported: %s",
dbURL, version))
}
// Query the database's collation
var collation string
err = db.QueryRow("SELECT datcollate FROM pg_database WHERE datname = current_database()").Scan(&collation)
if err != nil {
return util.NewPersistentError("InvalidExternalPgCollation",
fmt.Sprintf("failed getting database collation of external DB url: %q, error: %s",
dbURL, err))
}
// Check if the collation is "C"
if collation != "C" {
return util.NewPersistentError("InvalidExternalPgCollation",
fmt.Sprintf("collation of external DB url: %q, is not supported: %s",
dbURL, collation))
}
return nil
}
// checkPersistentLoggingPVC validates the configuration of pvc for persistent logging
func (r *Reconciler) checkPersistentLoggingPVC(
pvcName *string,
pvc *corev1.PersistentVolumeClaim,
errorName string) error {
if pvcName == nil {
sc := &storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{Kind: "StorageClass"},
ObjectMeta: metav1.ObjectMeta{Name: "ocs-storagecluster-cephfs"},
}
// Return nil as the operator running in ODF environment
if util.KubeCheck(sc) {
return nil
}
return util.NewPersistentError(errorName,
"Persistent Volume Claim (PVC) was not specified (and CephFS was not found for a defualt PVC)")
}
// Check if pvc exists in the cluster
PersistentLoggingPVC := &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{Kind: "PersistenVolumeClaim"},
ObjectMeta: metav1.ObjectMeta{
Name: *pvcName,
Namespace: r.Request.Namespace,
},
}
if !util.KubeCheck(PersistentLoggingPVC) {
return util.NewPersistentError(errorName,
fmt.Sprintf("The specified persistent logging pvc '%s' was not found", *pvcName))
}
// Check if pvc supports RWX access mode
for _, accessMode := range PersistentLoggingPVC.Spec.AccessModes {
if accessMode == corev1.ReadWriteMany {
return nil
}
}
return util.NewPersistentError(errorName,
fmt.Sprintf("The specified persistent logging pvc '%s' does not support RWX access mode", *pvcName))
}