Skip to content

Commit 3319929

Browse files
Merge pull request #818 from AlexNPavel/split-reg-type-graph
rehearse,registry: make explicit maps for type in NodeByName
2 parents e4628a9 + 8670a26 commit 3319929

File tree

5 files changed

+213
-111
lines changed

5 files changed

+213
-111
lines changed

cmd/pj-rehearse/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func rehearseMain() error {
183183
affectedJobs = jobs
184184
}
185185

186-
var changedRegistrySteps registry.NodeByName
186+
var changedRegistrySteps []registry.Node
187187
var refs registry.ReferenceByName
188188
var chains registry.ChainByName
189189
var workflows registry.WorkflowByName
@@ -214,8 +214,8 @@ func rehearseMain() error {
214214
}
215215
if len(changedRegistrySteps) != 0 {
216216
var names []string
217-
for step := range changedRegistrySteps {
218-
names = append(names, step)
217+
for _, step := range changedRegistrySteps {
218+
names = append(names, step.Name())
219219
}
220220
logger.Infof("found %d changed registry steps: %s", len(changedRegistrySteps), strings.Join(names, ", "))
221221
}

pkg/config/release.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -166,43 +166,41 @@ func GetChangedTemplates(path, baseRev string) ([]ConfigMapSource, error) {
166166
return ret, nil
167167
}
168168

169-
func loadRegistryStep(filename string, graph, changes registry.NodeByName) error {
169+
func loadRegistryStep(filename string, graph registry.NodeByName) (registry.Node, error) {
170170
// if a commands script changed, mark reference as changed
171171
filename = strings.ReplaceAll(filename, "-commands.sh", "-ref.yaml")
172-
name := ""
173-
if strings.HasSuffix(filename, "-ref.yaml") {
174-
name = strings.TrimSuffix(filename, "-ref.yaml")
172+
var node registry.Node
173+
var ok bool
174+
switch {
175+
case strings.HasSuffix(filename, "-ref.yaml"):
176+
node, ok = graph.References[strings.TrimSuffix(filename, "-ref.yaml")]
177+
case strings.HasSuffix(filename, "-chain.yaml"):
178+
node, ok = graph.Chains[strings.TrimSuffix(filename, "-chain.yaml")]
179+
case strings.HasSuffix(filename, "-workflow.yaml"):
180+
node, ok = graph.Workflows[strings.TrimSuffix(filename, "-workflow.yaml")]
181+
default:
182+
return nil, fmt.Errorf("invalid step filename: %s", filename)
175183
}
176-
if strings.HasSuffix(filename, "-chain.yaml") {
177-
name = strings.TrimSuffix(filename, "-chain.yaml")
178-
}
179-
if strings.HasSuffix(filename, "-workflow.yaml") {
180-
name = strings.TrimSuffix(filename, "-workflow.yaml")
181-
}
182-
if name == "" {
183-
return fmt.Errorf("invalid step filename: %s", filename)
184-
}
185-
node, ok := graph[name]
186184
if !ok {
187-
return fmt.Errorf("could not find registry component in registry graph: %s", name)
185+
return nil, fmt.Errorf("could not find registry component in registry graph: %s", filename)
188186
}
189-
changes[name] = node
190-
return nil
187+
return node, nil
191188
}
192189

193190
// GetChangedRegistrySteps identifies all registry components (refs, chains, and workflows) that changed.
194-
func GetChangedRegistrySteps(path, baseRev string, graph registry.NodeByName) (registry.NodeByName, error) {
195-
changes := make(registry.NodeByName)
191+
func GetChangedRegistrySteps(path, baseRev string, graph registry.NodeByName) ([]registry.Node, error) {
192+
var changes []registry.Node
196193
revChanges, err := getRevChanges(path, RegistryPath, baseRev, true)
197194
if err != nil {
198195
return changes, err
199196
}
200197
for _, c := range revChanges {
201198
if filepath.Ext(c.Filename) == ".yaml" || strings.HasSuffix(c.Filename, "-commands.sh") {
202-
err := loadRegistryStep(filepath.Base(c.Filename), graph, changes)
199+
node, err := loadRegistryStep(filepath.Base(c.Filename), graph)
203200
if err != nil {
204201
return changes, err
205202
}
203+
changes = append(changes, node)
206204
}
207205
}
208206
return changes, nil

pkg/registry/graph.go

Lines changed: 47 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,21 @@ type Node interface {
2222
// Type returns the type of the registry element a Node refers to
2323
Type() Type
2424
// AncestorNames returns a set of strings containing the names of all of the node's ancestors
25-
AncestorNames() sets.String
25+
Ancestors() []Node
2626
// DescendantNames returns a set of strings containing the names of all of the node's descendants
27-
DescendantNames() sets.String
27+
Descendants() []Node
2828
// ParentNames returns a set of strings containing the names of all the node's parents
29-
ParentNames() sets.String
29+
Parents() []Node
3030
// ChildrenNames returns a set of strings containing the names of all the node's children
31-
ChildrenNames() sets.String
31+
Childrens() []Node
3232
}
3333

3434
// NodeByName provides a mapping from node name to the Node interface
35-
type NodeByName map[string]Node
35+
type NodeByName struct {
36+
References map[string]Node
37+
Chains map[string]Node
38+
Workflows map[string]Node
39+
}
3640

3741
type nodeWithName struct {
3842
name string
@@ -107,51 +111,51 @@ func (*referenceNode) Type() Type {
107111
return Reference
108112
}
109113

110-
func (n *nodeWithParents) ParentNames() sets.String {
111-
parents := sets.NewString()
114+
func (n *nodeWithParents) Parents() []Node {
115+
var parents []Node
112116
for parent := range n.workflowParents {
113-
parents.Insert(parent.Name())
117+
parents = append(parents, parent)
114118
}
115119
for parent := range n.chainParents {
116-
parents.Insert(parent.Name())
120+
parents = append(parents, parent)
117121
}
118122
return parents
119123
}
120124

121-
func (*workflowNode) ParentNames() sets.String { return sets.NewString() }
125+
func (*workflowNode) Parents() []Node { return []Node{} }
122126

123-
func (n *nodeWithChildren) ChildrenNames() sets.String {
124-
children := sets.NewString()
127+
func (n *nodeWithChildren) Childrens() []Node {
128+
var children []Node
125129
for child := range n.referenceChildren {
126-
children.Insert(child.Name())
130+
children = append(children, child)
127131
}
128132
for child := range n.chainChildren {
129-
children.Insert(child.Name())
133+
children = append(children, child)
130134
}
131135
return children
132136
}
133137

134-
func (*referenceNode) ChildrenNames() sets.String { return sets.NewString() }
138+
func (*referenceNode) Childrens() []Node { return []Node{} }
135139

136-
func (n *nodeWithParents) AncestorNames() sets.String {
137-
ancestors := n.ParentNames()
140+
func (n *nodeWithParents) Ancestors() []Node {
141+
ancestors := n.Parents()
138142
for parent := range n.chainParents {
139-
ancestors.Insert(parent.AncestorNames().List()...)
143+
ancestors = append(ancestors, parent.Ancestors()...)
140144
}
141145
return ancestors
142146
}
143147

144-
func (*workflowNode) AncestorNames() sets.String { return sets.NewString() }
148+
func (*workflowNode) Ancestors() []Node { return []Node{} }
145149

146-
func (n *nodeWithChildren) DescendantNames() sets.String {
147-
descendants := n.ChildrenNames()
150+
func (n *nodeWithChildren) Descendants() []Node {
151+
descendants := n.Childrens()
148152
for child := range n.chainChildren {
149-
descendants.Insert(child.DescendantNames().List()...)
153+
descendants = append(descendants, child.Descendants()...)
150154
}
151155
return descendants
152156
}
153157

154-
func (*referenceNode) DescendantNames() sets.String { return sets.NewString() }
158+
func (*referenceNode) Descendants() []Node { return []Node{} }
155159

156160
func (n *workflowNode) addChainChild(child *chainNode) {
157161
n.chainChildren.insert(child)
@@ -216,7 +220,11 @@ func hasCycles(node *chainNode, ancestors sets.String, traversedPath []string) e
216220

217221
// NewGraph returns a NodeByType map representing the provided step references, chains, and workflows as a directed graph.
218222
func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsByName WorkflowByName) (NodeByName, error) {
219-
nodesByName := make(NodeByName)
223+
nodesByName := NodeByName{
224+
References: make(map[string]Node),
225+
Chains: make(map[string]Node),
226+
Workflows: make(map[string]Node),
227+
}
220228
// References can only be children; load them so they can be added as children by workflows and chains
221229
referenceNodes := make(referenceNodeByName)
222230
for name := range stepsByName {
@@ -225,10 +233,10 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
225233
nodeWithParents: newNodeWithParents(),
226234
}
227235
referenceNodes[name] = node
228-
nodesByName[name] = node
236+
nodesByName.References[name] = node
229237
}
230238
// since we may load the parent chain before a child chain, we need to make the parent->child links after loading all chains
231-
parentChildChain := make(map[*chainNode]string)
239+
parentChildChain := make(map[*chainNode][]string)
232240
chainNodes := make(chainNodeByName)
233241
for name, chain := range chainsByName {
234242
node := &chainNode{
@@ -237,29 +245,31 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
237245
nodeWithParents: newNodeWithParents(),
238246
}
239247
chainNodes[name] = node
240-
nodesByName[name] = node
248+
nodesByName.Chains[name] = node
241249
for _, step := range chain {
242250
if step.Reference != nil {
243251
if _, exists := referenceNodes[*step.Reference]; !exists {
244-
return nil, fmt.Errorf("Chain %s contains non-existent reference %s", name, *step.Reference)
252+
return nodesByName, fmt.Errorf("Chain %s contains non-existent reference %s", name, *step.Reference)
245253
}
246254
node.addReferenceChild(referenceNodes[*step.Reference])
247255
}
248256
if step.Chain != nil {
249-
parentChildChain[node] = *step.Chain
257+
parentChildChain[node] = append(parentChildChain[node], *step.Chain)
250258
}
251259
}
252260
}
253-
for parent, child := range parentChildChain {
254-
if _, exists := chainNodes[child]; !exists {
255-
return nil, fmt.Errorf("Chain %s contains non-existent chain %s", parent.Name(), child)
261+
for parent, children := range parentChildChain {
262+
for _, child := range children {
263+
if _, exists := chainNodes[child]; !exists {
264+
return nodesByName, fmt.Errorf("Chain %s contains non-existent chain %s", parent.Name(), child)
265+
}
266+
parent.addChainChild(chainNodes[child])
256267
}
257-
parent.addChainChild(chainNodes[child])
258268
}
259269
// verify that no cycles exist
260270
for _, chain := range chainNodes {
261271
if err := hasCycles(chain, sets.NewString(), []string{}); err != nil {
262-
return nil, err
272+
return nodesByName, err
263273
}
264274
}
265275
workflowNodes := make(workflowNodeByName)
@@ -269,18 +279,18 @@ func NewGraph(stepsByName ReferenceByName, chainsByName ChainByName, workflowsBy
269279
nodeWithChildren: newNodeWithChildren(),
270280
}
271281
workflowNodes[name] = node
272-
nodesByName[name] = node
282+
nodesByName.Workflows[name] = node
273283
steps := append(workflow.Pre, append(workflow.Test, workflow.Post...)...)
274284
for _, step := range steps {
275285
if step.Reference != nil {
276286
if _, exists := referenceNodes[*step.Reference]; !exists {
277-
return nil, fmt.Errorf("Workflow %s contains non-existent reference %s", name, *step.Reference)
287+
return nodesByName, fmt.Errorf("Workflow %s contains non-existent reference %s", name, *step.Reference)
278288
}
279289
node.addReferenceChild(referenceNodes[*step.Reference])
280290
}
281291
if step.Chain != nil {
282292
if _, exists := chainNodes[*step.Chain]; !exists {
283-
return nil, fmt.Errorf("Workflow %s contains non-existent chain %s", name, *step.Chain)
293+
return nodesByName, fmt.Errorf("Workflow %s contains non-existent chain %s", name, *step.Chain)
284294
}
285295
node.addChainChild(chainNodes[*step.Chain])
286296
}

0 commit comments

Comments
 (0)