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
154 lines (144 loc) · 3.3 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
package migration
import (
"context"
"errors"
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"
plancnt "github.com/konveyor/forklift-controller/pkg/controller/plan"
"github.com/konveyor/forklift-controller/pkg/controller/provider/web"
k8serr "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
)
//
// Types
const (
PlanNotValid = "PlanNotValid"
PlanNotReady = "PlanNotReady"
VMNotFound = "VMNotFound"
VMNotUnique = "VMNotUnique"
Running = "Running"
Executing = plancnt.Executing
Succeeded = plancnt.Succeeded
Failed = plancnt.Failed
Canceled = plancnt.Canceled
)
//
// Categories
const (
Required = libcnd.Required
Advisory = libcnd.Advisory
Critical = libcnd.Critical
Error = libcnd.Error
Warn = libcnd.Warn
)
// Reasons
const (
NotSet = "NotSet"
NotFound = "NotFound"
Ambiguous = "Ambiguous"
)
// Statuses
const (
True = libcnd.True
False = libcnd.False
)
//
// Validate the migration resource.
func (r *Reconciler) validate(migration *api.Migration) (plan *api.Plan, err error) {
newCnd := libcnd.Condition{
Type: PlanNotValid,
Status: True,
Reason: NotSet,
Category: Critical,
Message: "The `plan` is not valid.",
}
ref := migration.Spec.Plan
if !libref.RefSet(&ref) {
migration.Status.SetCondition(newCnd)
return
}
plan = &api.Plan{}
key := client.ObjectKey{
Namespace: ref.Namespace,
Name: ref.Name,
}
err = r.Get(context.TODO(), key, plan)
if k8serr.IsNotFound(err) {
err = nil
newCnd.Reason = NotFound
migration.Status.SetCondition(newCnd)
return
}
if err != nil {
err = liberr.Wrap(err)
return
}
if !plan.Status.HasCondition(libcnd.Ready) {
migration.Status.SetCondition(
libcnd.Condition{
Type: PlanNotReady,
Status: True,
Reason: NotFound,
Category: Critical,
Message: "The `plan` does not have Ready condition.",
})
return
}
// Validate the refs in the Cancel array
notFound := libcnd.Condition{
Type: VMNotFound,
Status: True,
Reason: NotFound,
Category: Warn,
Message: "VM not found.",
Items: []string{},
}
ambiguous := libcnd.Condition{
Type: VMNotUnique,
Status: True,
Reason: Ambiguous,
Category: Warn,
Message: "VM reference is ambiguous.",
Items: []string{},
}
source := plan.Spec.Provider.Source
provider := &api.Provider{}
key = client.ObjectKey{
Namespace: source.Namespace,
Name: source.Name,
}
err = r.Get(context.TODO(), key, provider)
if err != nil {
err = liberr.Wrap(err)
return
}
inventory, err := web.NewClient(provider)
if err != nil {
return
}
for _, ref := range migration.Spec.Cancel {
_, err = inventory.VM(&ref)
if err != nil {
if errors.As(err, &web.NotFoundError{}) {
notFound.Items = append(notFound.Items, ref.String())
err = nil
continue
}
if errors.As(err, &web.RefNotUniqueError{}) {
ambiguous.Items = append(ambiguous.Items, ref.String())
err = nil
continue
}
return
}
}
if len(notFound.Items) > 0 {
migration.Status.SetCondition(notFound)
}
if len(ambiguous.Items) > 0 {
migration.Status.SetCondition(ambiguous)
}
return
}