forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edges.go
132 lines (117 loc) · 5.17 KB
/
edges.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
package graph
import (
"github.com/gonum/graph"
kapi "k8s.io/kubernetes/pkg/api"
osgraph "github.com/openshift/origin/pkg/api/graph"
buildapi "github.com/openshift/origin/pkg/build/apis/build"
buildgraph "github.com/openshift/origin/pkg/build/graph/nodes"
imageapi "github.com/openshift/origin/pkg/image/apis/image"
imagegraph "github.com/openshift/origin/pkg/image/graph/nodes"
)
const (
// BuildTriggerImageEdgeKind is an edge from an ImageStream to a BuildConfig that
// represents a trigger connection. Changes to the ImageStream will trigger a new build
// from the BuildConfig.
BuildTriggerImageEdgeKind = "BuildTriggerImage"
// BuildInputImageEdgeKind is an edge from an ImageStream to a BuildConfig, where the
// ImageStream is the source image for the build (builder in S2I builds, FROM in Docker builds,
// custom builder in Custom builds). The same ImageStream can also have a trigger
// relationship with the BuildConfig, but not necessarily.
BuildInputImageEdgeKind = "BuildInputImage"
// BuildOutputEdgeKind is an edge from a BuildConfig to an ImageStream. The ImageStream will hold
// the ouptut of the Builds created with that BuildConfig.
BuildOutputEdgeKind = "BuildOutput"
// BuildInputEdgeKind is an edge from a source repository to a BuildConfig. The source repository is the
// input source for the build.
BuildInputEdgeKind = "BuildInput"
// BuildEdgeKind goes from a BuildConfigNode to a BuildNode and indicates that the buildConfig owns the build
BuildEdgeKind = "Build"
)
// AddBuildEdges adds edges that connect a BuildConfig to Builds to the given graph
func AddBuildEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNode) {
for _, n := range g.(graph.Graph).Nodes() {
if buildNode, ok := n.(*buildgraph.BuildNode); ok {
if buildNode.Build.Namespace != node.BuildConfig.Namespace {
continue
}
if belongsToBuildConfig(node.BuildConfig, buildNode.Build) {
g.AddEdge(node, buildNode, BuildEdgeKind)
}
}
}
}
// AddAllBuildEdges adds build edges to all BuildConfig nodes in the given graph
func AddAllBuildEdges(g osgraph.MutableUniqueGraph) {
for _, node := range g.(graph.Graph).Nodes() {
if bcNode, ok := node.(*buildgraph.BuildConfigNode); ok {
AddBuildEdges(g, bcNode)
}
}
}
func imageRefNode(g osgraph.MutableUniqueGraph, ref *kapi.ObjectReference, bc *buildapi.BuildConfig) graph.Node {
if ref == nil {
return nil
}
switch ref.Kind {
case "DockerImage":
if ref, err := imageapi.ParseDockerImageReference(ref.Name); err == nil {
tag := ref.Tag
ref.Tag = ""
return imagegraph.EnsureDockerRepositoryNode(g, ref.String(), tag)
}
case "ImageStream":
return imagegraph.FindOrCreateSyntheticImageStreamTagNode(g, imagegraph.MakeImageStreamTagObjectMeta(defaultNamespace(ref.Namespace, bc.Namespace), ref.Name, imageapi.DefaultImageTag))
case "ImageStreamTag":
return imagegraph.FindOrCreateSyntheticImageStreamTagNode(g, imagegraph.MakeImageStreamTagObjectMeta2(defaultNamespace(ref.Namespace, bc.Namespace), ref.Name))
case "ImageStreamImage":
return imagegraph.FindOrCreateSyntheticImageStreamImageNode(g, imagegraph.MakeImageStreamImageObjectMeta(defaultNamespace(ref.Namespace, bc.Namespace), ref.Name))
}
return nil
}
// AddOutputEdges links the build config to its output image node.
func AddOutputEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNode) {
if node.BuildConfig.Spec.Output.To == nil {
return
}
out := imageRefNode(g, node.BuildConfig.Spec.Output.To, node.BuildConfig)
g.AddEdge(node, out, BuildOutputEdgeKind)
}
// AddInputEdges links the build config to its input image and source nodes.
func AddInputEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNode) {
if in := buildgraph.EnsureSourceRepositoryNode(g, node.BuildConfig.Spec.Source); in != nil {
g.AddEdge(in, node, BuildInputEdgeKind)
}
inputImage := buildapi.GetInputReference(node.BuildConfig.Spec.Strategy)
if input := imageRefNode(g, inputImage, node.BuildConfig); input != nil {
g.AddEdge(input, node, BuildInputImageEdgeKind)
}
}
// AddTriggerEdges links the build config to its trigger input image nodes.
func AddTriggerEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNode) {
for _, trigger := range node.BuildConfig.Spec.Triggers {
if trigger.Type != buildapi.ImageChangeBuildTriggerType {
continue
}
from := trigger.ImageChange.From
if trigger.ImageChange.From == nil {
from = buildapi.GetInputReference(node.BuildConfig.Spec.Strategy)
}
triggerNode := imageRefNode(g, from, node.BuildConfig)
g.AddEdge(triggerNode, node, BuildTriggerImageEdgeKind)
}
}
// AddInputOutputEdges links the build config to other nodes for the images and source repositories it depends on.
func AddInputOutputEdges(g osgraph.MutableUniqueGraph, node *buildgraph.BuildConfigNode) *buildgraph.BuildConfigNode {
AddInputEdges(g, node)
AddTriggerEdges(g, node)
AddOutputEdges(g, node)
return node
}
// AddAllInputOutputEdges adds input and output edges for all BuildConfigs in the given graph
func AddAllInputOutputEdges(g osgraph.MutableUniqueGraph) {
for _, node := range g.(graph.Graph).Nodes() {
if bcNode, ok := node.(*buildgraph.BuildConfigNode); ok {
AddInputOutputEdges(g, bcNode)
}
}
}