This repository has been archived by the owner on May 28, 2021. It is now read-only.
/
validation.go
271 lines (211 loc) · 8.93 KB
/
validation.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
// Copyright 2018 Oracle and/or its affiliates. All rights reserved.
//
// 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 v1alpha1
import (
"fmt"
"strconv"
"github.com/coreos/go-semver/semver"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/validation/field"
"github.com/oracle/mysql-operator/pkg/constants"
)
func validateCluster(c *Cluster) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateClusterMetadata(c.ObjectMeta, field.NewPath("metadata"))...)
allErrs = append(allErrs, validateClusterSpec(c.Spec, field.NewPath("spec"))...)
allErrs = append(allErrs, validateClusterStatus(c.Status, field.NewPath("status"))...)
return allErrs
}
func validateClusterMetadata(m metav1.ObjectMeta, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateName(m.Name, fldPath.Child("name"))...)
return allErrs
}
func validateName(name string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if len(name) > ClusterNameMaxLen {
msg := fmt.Sprintf("longer than maximum supported length %d (see: https://bugs.mysql.com/bug.php?id=90601)", ClusterNameMaxLen)
allErrs = append(allErrs, field.Invalid(fldPath, name, msg))
}
return allErrs
}
func validateClusterSpec(s ClusterSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateVersion(s.Version, fldPath.Child("version"))...)
allErrs = append(allErrs, validateMembers(s.Members, fldPath.Child("members"))...)
allErrs = append(allErrs, validateBaseServerID(s.BaseServerID, fldPath.Child("baseServerId"))...)
return allErrs
}
func validateClusterStatus(s ClusterStatus, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
return allErrs
}
func validateVersion(version string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
min, err := semver.NewVersion(MinimumMySQLVersion)
if err != nil {
allErrs = append(allErrs, field.InternalError(fldPath, fmt.Errorf("unable to parse minimum MySQL version: %v", err)))
}
given, err := semver.NewVersion(version)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, version, fmt.Sprintf("unable to parse MySQL version: %v", err)))
}
if len(allErrs) == 0 {
if given.Compare(*min) == -1 {
allErrs = append(allErrs, field.Invalid(fldPath, version, fmt.Sprintf("minimum supported MySQL version is %s", MinimumMySQLVersion)))
}
}
return allErrs
}
func validateBaseServerID(baseServerID uint32, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if baseServerID <= maxBaseServerID {
return allErrs
}
return append(allErrs, field.Invalid(fldPath, strconv.FormatUint(uint64(baseServerID), 10), "invalid baseServerId specified"))
}
func validateMembers(members int32, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if members < 1 || members > MaxInnoDBClusterMembers {
allErrs = append(allErrs, field.Invalid(fldPath, members, "InnoDB clustering supports between 1-9 members"))
}
return allErrs
}
func validateS3StorageProvider(s3 *S3StorageProvider, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if s3.Region == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("region"), ""))
}
if s3.Endpoint == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("endpoint"), ""))
}
if s3.Bucket == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("bucket"), ""))
}
if s3.CredentialsSecret == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("credentialsSecret"), ""))
} else if s3.CredentialsSecret.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("credentialsSecret").Child("name"), ""))
}
return allErrs
}
func validateStorageProvider(storage StorageProvider, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if storage.S3 == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("s3"), "S3 (compatible) is currently the only supported storage provider"))
} else {
allErrs = append(allErrs, validateS3StorageProvider(storage.S3, fldPath.Child("s3"))...)
}
return allErrs
}
// ExecutorProviders denotes the list of valid backup executor providers.
var ExecutorProviders = []string{"mysqldump"}
func isValidExecutorProvider(provider string) bool {
for _, ex := range ExecutorProviders {
if provider == ex {
return true
}
}
return false
}
func validateDatabase(database Database, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if database.Name == "" {
allErrs = append(allErrs, field.Invalid(fldPath.Child("name"), database.Name, ""))
}
return allErrs
}
func validateMySQLDumpExecutor(executor *MySQLDumpBackupExecutor, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if executor.Databases == nil || len(executor.Databases) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("databases"), executor, ""))
}
for i, database := range executor.Databases {
allErrs = append(allErrs, validateDatabase(database, fldPath.Index(i))...)
}
return allErrs
}
func validateExecutor(executor BackupExecutor, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if executor.MySQLDump == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("mysqldump"), "mysqldump is currently the only supported backup mechanism"))
} else {
allErrs = append(allErrs, validateMySQLDumpExecutor(executor.MySQLDump, fldPath.Child("mysqldump"))...)
}
return allErrs
}
func validateBackup(backup *Backup) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateBackupLabels(backup.Labels, field.NewPath("labels"))...)
allErrs = append(allErrs, validateBackupSpec(backup.Spec, field.NewPath("spec"))...)
return allErrs
}
func validateBackupLabels(labels map[string]string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if labels[constants.MySQLOperatorVersionLabel] == "" {
errorStr := fmt.Sprintf("no '%s' present.", constants.MySQLOperatorVersionLabel)
allErrs = append(allErrs, field.Invalid(fldPath, labels, errorStr))
}
return allErrs
}
func validateBackupSpec(spec BackupSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateExecutor(spec.Executor, field.NewPath("executor"))...)
allErrs = append(allErrs, validateStorageProvider(spec.StorageProvider, field.NewPath("storageProvider"))...)
if spec.Cluster == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("cluster"), "missing cluster"))
}
return allErrs
}
func validateBackupSchedule(bs *BackupSchedule) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateBackupScheduleSpec(bs.Spec, field.NewPath("spec"))...)
return allErrs
}
func validateBackupScheduleSpec(spec BackupScheduleSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if &spec.BackupTemplate == nil {
allErrs = append(allErrs, field.Required(fldPath.Child("backupTemplate"), "missing backup template"))
} else {
allErrs = append(allErrs, validateBackupSpec(spec.BackupTemplate, field.NewPath("backupTemplate"))...)
}
return allErrs
}
func validateRestore(restore *Restore) field.ErrorList {
allErrs := field.ErrorList{}
allErrs = append(allErrs, validateRestoreSpec(restore.Spec, field.NewPath("spec"))...)
// FIXME(apryde): The version label is a piece of internal bookkeeping,
// however, we're validating with a user-facing error here. Should test
// we're applying the label in unit tests and remove this validation.
value, ok := restore.Labels[constants.MySQLOperatorVersionLabel]
if !ok {
errorStr := fmt.Sprintf("no '%s' present.", constants.MySQLOperatorVersionLabel)
allErrs = append(allErrs, field.Invalid(field.NewPath("labels"), restore.Labels, errorStr))
}
if value == "" {
errorStr := fmt.Sprintf("empty '%s' present.", constants.MySQLOperatorVersionLabel)
allErrs = append(allErrs, field.Invalid(field.NewPath("labels"), restore.Labels, errorStr))
}
return allErrs
}
func validateRestoreSpec(s RestoreSpec, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
if s.Cluster == nil || s.Cluster.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("cluster").Child("name"), "a cluster to restore into is required"))
}
if s.Backup == nil || s.Backup.Name == "" {
allErrs = append(allErrs, field.Required(fldPath.Child("backup").Child("name"), "a backup to restore is required"))
}
return allErrs
}