This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
validation.go
337 lines (315 loc) · 7.31 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package provider
import (
"context"
"fmt"
"net/url"
libcnd "github.com/konveyor/controller/pkg/condition"
liberr "github.com/konveyor/controller/pkg/error"
libref "github.com/konveyor/controller/pkg/ref"
api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1"
"github.com/konveyor/forklift-controller/pkg/controller/provider/container"
"github.com/konveyor/forklift-controller/pkg/controller/provider/container/ovirt"
core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//
// Types
const (
UrlNotValid = "UrlNotValid"
TypeNotSupported = "ProviderTypeNotSupported"
SecretNotValid = "SecretNotValid"
SettingsNotValid = "SettingsNotValid"
Validated = "Validated"
ConnectionTestSucceeded = "ConnectionTestSucceeded"
ConnectionTestFailed = "ConnectionTestFailed"
InventoryCreated = "InventoryCreated"
LoadInventory = "LoadInventory"
ConnectionInsecure = "ConnectionInsecure"
)
//
// Categories
const (
Required = libcnd.Required
Advisory = libcnd.Advisory
Critical = libcnd.Critical
Error = libcnd.Error
Warn = libcnd.Warn
)
//
// Reasons
const (
NotSet = "NotSet"
NotFound = "NotFound"
NotSupported = "NotSupported"
DataErr = "DataErr"
Malformed = "Malformed"
Completed = "Completed"
Tested = "Tested"
Started = "Started"
SkipTLSVerification = "SkipTLSVerification"
)
//
// Statuses
const (
True = libcnd.True
False = libcnd.False
)
//
// Validate the provider resource.
func (r *Reconciler) validate(provider *api.Provider) error {
err := r.validateType(provider)
if err != nil {
return liberr.Wrap(err)
}
err = r.validateURL(provider)
if err != nil {
return liberr.Wrap(err)
}
secret, err := r.validateSecret(provider)
if err != nil {
return liberr.Wrap(err)
}
err = r.validateSettings(provider)
if err != nil {
return liberr.Wrap(err)
}
err = r.testConnection(provider, secret)
if err != nil {
return liberr.Wrap(err)
}
err = r.inventoryCreated(provider)
if err != nil {
return liberr.Wrap(err)
}
if !provider.Status.HasBlockerCondition() {
provider.Status.SetCondition(
libcnd.Condition{
Type: Validated,
Status: True,
Reason: Completed,
Category: Advisory,
Message: "Validation has been completed.",
})
}
return nil
}
//
// Validate types.
func (r *Reconciler) validateType(provider *api.Provider) error {
for _, p := range api.ProviderTypes {
if p == provider.Type() {
return nil
}
}
provider.Status.SetCondition(
libcnd.Condition{
Type: TypeNotSupported,
Status: True,
Reason: NotSupported,
Category: Critical,
Message: fmt.Sprintf("The `type` must be: %s", api.ProviderTypes),
})
return nil
}
//
// Validate the URL.
func (r *Reconciler) validateURL(provider *api.Provider) error {
if provider.IsHost() {
return nil
}
if provider.Spec.URL == "" {
provider.Status.SetCondition(
libcnd.Condition{
Type: UrlNotValid,
Status: True,
Reason: NotSet,
Category: Critical,
Message: "The `url` is not valid.",
})
}
_, err := url.Parse(provider.Spec.URL)
if err != nil {
provider.Status.SetCondition(
libcnd.Condition{
Type: UrlNotValid,
Status: True,
Reason: Malformed,
Category: Critical,
Message: fmt.Sprintf("The `url` is malformed: %s", err.Error()),
})
}
return nil
}
//
// Validate secret (ref).
// 1. The references is complete.
// 2. The secret exists.
// 3. the content of the secret is valid.
func (r *Reconciler) validateSecret(provider *api.Provider) (secret *core.Secret, err error) {
if provider.IsHost() {
return
}
// NotSet
newCnd := libcnd.Condition{
Type: SecretNotValid,
Status: True,
Reason: NotSet,
Category: Critical,
Message: "The `secret` is not valid.",
}
ref := provider.Spec.Secret
if !libref.RefSet(&ref) {
provider.Status.SetCondition(newCnd)
return
}
// NotFound
secret = &core.Secret{}
key := client.ObjectKey{
Namespace: ref.Namespace,
Name: ref.Name,
}
err = r.Get(context.TODO(), key, secret)
if errors.IsNotFound(err) {
err = nil
newCnd.Reason = NotFound
provider.Status.SetCondition(newCnd)
return
}
if err != nil {
err = liberr.Wrap(err)
}
// DataErr
keyList := []string{}
switch provider.Type() {
case api.OpenShift:
keyList = []string{"token"}
case api.VSphere:
keyList = []string{
"user",
"password",
"thumbprint",
}
case api.OVirt:
if ovirt.GetInsecureSkipVerifyFlag(secret) {
provider.Status.SetCondition(libcnd.Condition{
Type: ConnectionInsecure,
Status: True,
Reason: SkipTLSVerification,
Category: Warn,
Message: "TLS is susceptible to machine-in-the-middle attacks when certificate verification is skipped.",
})
keyList = []string{
"user",
"password",
}
} else {
keyList = []string{
"user",
"password",
"cacert",
}
}
}
for _, key := range keyList {
if _, found := secret.Data[key]; !found {
newCnd.Items = append(newCnd.Items, key)
}
}
if len(newCnd.Items) > 0 {
newCnd.Reason = DataErr
provider.Status.SetCondition(newCnd)
}
return
}
//
// Validate provider settings.
func (r *Reconciler) validateSettings(provider *api.Provider) (err error) {
newCnd := libcnd.Condition{
Type: SettingsNotValid,
Status: True,
Reason: DataErr,
Category: Critical,
Message: "The `settings` are not valid.",
}
keyList := []string{}
switch provider.Type() {
case api.VSphere:
keyList = []string{"vddkInitImage"}
}
for _, key := range keyList {
if _, found := provider.Spec.Settings[key]; !found {
newCnd.Items = append(newCnd.Items, key)
}
}
if len(newCnd.Items) > 0 {
provider.Status.SetCondition(newCnd)
}
return
}
//
// Test connection.
func (r *Reconciler) testConnection(provider *api.Provider, secret *core.Secret) error {
if provider.Status.HasBlockerCondition() {
return nil
}
rl := container.Build(nil, provider, secret)
err := rl.Test()
if err == nil {
log.Info(
"Connection test succeeded.")
provider.Status.SetCondition(
libcnd.Condition{
Type: ConnectionTestSucceeded,
Status: True,
Reason: Tested,
Category: Required,
Message: "Connection test, succeeded.",
})
} else {
log.Info(
"Connection test failed.",
"reason",
err.Error())
provider.Status.SetCondition(
libcnd.Condition{
Type: ConnectionTestFailed,
Status: True,
Reason: Tested,
Category: Critical,
Message: fmt.Sprintf(
"Connection test, failed: %s",
err.Error()),
})
}
return nil
}
//
// Validate inventory created.
func (r *Reconciler) inventoryCreated(provider *api.Provider) error {
if provider.Status.HasBlockerCondition() {
return nil
}
if r, found := r.container.Get(provider); found {
if r.HasParity() {
provider.Status.SetCondition(
libcnd.Condition{
Type: InventoryCreated,
Status: True,
Reason: Completed,
Category: Required,
Message: "The inventory has been loaded.",
})
} else {
provider.Status.SetCondition(
libcnd.Condition{
Type: LoadInventory,
Status: True,
Reason: Started,
Category: Advisory,
Message: "Loading the inventory.",
})
}
}
return nil
}