/
creation_processor.go
239 lines (214 loc) · 6.91 KB
/
creation_processor.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
// Copyright 2018 Istio Authors
//
// 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 dependency
import (
"fmt"
"reflect"
"time"
"istio.io/istio/pkg/test/framework/api/component"
"istio.io/istio/pkg/test/framework/api/lifecycle"
)
// creationProcessor is used by Manager to resolve creation order for components.
type creationProcessor struct {
scope lifecycle.Scope
mgr *Manager
// The entries that need to be processed.
required map[namedID]*reqEntry
}
// newCreationProcessor creates a new creation processor for the Manager.
func newCreationProcessor(mgr *Manager, scope lifecycle.Scope) *creationProcessor {
return &creationProcessor{
scope: scope,
mgr: mgr,
required: make(map[namedID]*reqEntry),
}
}
// A struct representing a named ID. This is used as the key to what requirements need to be
// created, as the Variant is not important, just the ID and Name.
type namedID struct {
Name string
ID component.ID
}
// A struct representing a single parsed requirement.
type reqEntry struct {
id namedID
desc *component.Descriptor
config component.Configuration
// Child entries that need to be processed before this entry can be created.
children map[namedID]bool
}
func (p *creationProcessor) ProcessRequirements(reqs []component.Requirement) component.RequirementError {
for _, req := range reqs {
entry, err := parseRequirement(req)
if err != nil {
return err
}
if err := p.addRequirement(entry); err != nil {
return err
}
}
return nil
}
// Parses a requirement into a requirement entry. This takes care of unwrapping the requirement
// envelope into a single flat type that we don't need to reflect over.
func parseRequirement(req component.Requirement) (r *reqEntry, err component.RequirementError) {
if c, ok := req.(*component.ConfiguredRequirement); ok {
if r, err = parseRequirement(c.GetRequirement()); err != nil {
return
}
r.id.Name = c.GetName()
r.config = c.GetConfiguration()
return
}
if id, ok := req.(*component.ID); ok {
r = &reqEntry{
id: namedID{"", *id},
children: make(map[namedID]bool),
}
return
}
if d, ok := req.(*component.Descriptor); ok {
r = &reqEntry{
id: namedID{"", d.ID},
desc: d,
children: make(map[namedID]bool),
}
return
}
err = resolutionError(fmt.Errorf("unsupported requirement type: %v", req))
return
}
// Adds a requirement to our map of requirements. This verifies the requirement is not overwriting
// a requirement of the same key with mismatched contents. We allow more specific overwrites.
func (p *creationProcessor) addRequirement(entry *reqEntry) component.RequirementError {
// First load up the children into the entry if it has a descriptor.
if err := p.loadChildren(entry); err != nil {
return err
}
// Now check if there is an existing entry, and if so compare them.
if oldEntry, ok := p.required[entry.id]; ok {
override, err := compareEntries(oldEntry, entry)
if err != nil {
return err
}
if !override {
return nil
}
}
p.required[entry.id] = entry
// If the entry has a descriptor, process all of the child requirements.
if entry.desc != nil {
return p.ProcessRequirements(entry.desc.Requires)
}
return nil
}
// Compare two entries, returning true if the new entry should override the old one.
func compareEntries(oldEntry *reqEntry, entry *reqEntry) (override bool, err component.RequirementError) {
override = false
if reflect.DeepEqual(oldEntry, entry) {
return
}
// First compare descriptors, and check if we need to merge or override the descriptor.
if oldEntry.desc == nil {
if entry.desc != nil {
override = true
}
} else if entry.desc == nil {
entry.desc = oldEntry.desc
} else if !reflect.DeepEqual(oldEntry.desc, entry.desc) {
err = resolutionError(fmt.Errorf("required mismatched descriptors for %v: %v, %v", entry.id, oldEntry.desc, entry.desc))
return
}
// Next compare config, and do the same check, do we need to merge or override.
if oldEntry.config == nil {
if entry.config != nil {
override = true
}
} else if entry.config == nil {
entry.config = oldEntry.config
} else if !reflect.DeepEqual(oldEntry.config, entry.config) {
err = resolutionError(fmt.Errorf("required mismatched configuration for %v: %v, %v", entry.id, oldEntry, entry))
}
return
}
func (p *creationProcessor) loadChildren(entry *reqEntry) component.RequirementError {
if entry.desc == nil {
return nil
}
for _, childReq := range entry.desc.Requires {
child, err := parseRequirement(childReq)
if err != nil {
return err
}
entry.children[child.id] = true
}
return nil
}
// For any required entry that does not have a descriptor, find a default and add that as a
// requirement. This will replace the entry with just an ID with one with a descriptor, as well as
// adding any child requirements.
func (p *creationProcessor) ApplyDefaults() component.RequirementError {
done := false
var toProcess []component.Requirement
for !done {
for _, entry := range p.required {
if entry.desc == nil {
desc, err := p.mgr.GetDefaultDescriptor(entry.id.ID)
if err != nil {
return resolutionError(err)
}
toProcess = append(toProcess, component.NewNamedRequirement(entry.id.Name, &desc))
}
}
done = len(toProcess) == 0
if !done {
err := p.ProcessRequirements(toProcess)
if err != nil {
return err
}
toProcess = nil
}
time.Sleep(time.Second)
}
return nil
}
// CreateComponents contained in this processor in the appropriate order.
func (p *creationProcessor) CreateComponents() component.RequirementError {
for len(p.required) > 0 {
progress := false
for _, entry := range p.required {
// Remove requirements for any components that have been created.
for childID := range entry.children {
if p.mgr.GetComponent(childID.Name, childID.ID) != nil {
delete(entry.children, childID)
}
}
// If all the requirements have been satisified, create the component.
if len(entry.children) == 0 {
progress = true
// Mark this requirement as satisfied.
delete(p.required, entry.id)
// Create the component.
if _, err := p.mgr.requireComponent(entry.id.Name, *entry.desc, p.scope); err != nil {
return err
}
}
}
// If we failed to make process on any of the required entries, report an error.
if !progress {
return resolutionError(fmt.Errorf("unable to determine creation order for required components, remaining requirements: %v", p.required))
}
}
return nil
}