-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
111 lines (94 loc) · 3.43 KB
/
helpers.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
package graph
import (
"sort"
kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
osgraph "github.com/openshift/origin/pkg/api/graph"
kubegraph "github.com/openshift/origin/pkg/api/kubegraph/nodes"
deployapi "github.com/openshift/origin/pkg/deploy/api"
deploygraph "github.com/openshift/origin/pkg/deploy/graph/nodes"
deployutil "github.com/openshift/origin/pkg/deploy/util"
imageapi "github.com/openshift/origin/pkg/image/api"
)
// RelevantDeployments returns the active deployment and a list of inactive deployments (in order from newest to oldest)
func RelevantDeployments(g osgraph.Graph, dcNode *deploygraph.DeploymentConfigNode) (*kubegraph.ReplicationControllerNode, []*kubegraph.ReplicationControllerNode) {
allDeployments := []*kubegraph.ReplicationControllerNode{}
uncastDeployments := g.SuccessorNodesByEdgeKind(dcNode, DeploymentEdgeKind)
if len(uncastDeployments) == 0 {
return nil, []*kubegraph.ReplicationControllerNode{}
}
for i := range uncastDeployments {
allDeployments = append(allDeployments, uncastDeployments[i].(*kubegraph.ReplicationControllerNode))
}
sort.Sort(RecentDeploymentReferences(allDeployments))
if dcNode.DeploymentConfig.LatestVersion == deployutil.DeploymentVersionFor(allDeployments[0]) {
return allDeployments[0], allDeployments[1:]
}
return nil, allDeployments
}
func BelongsToDeploymentConfig(config *deployapi.DeploymentConfig, b *kapi.ReplicationController) bool {
if b.Annotations != nil {
return config.Name == deployutil.DeploymentConfigNameFor(b)
}
return false
}
type RecentDeploymentReferences []*kubegraph.ReplicationControllerNode
func (m RecentDeploymentReferences) Len() int { return len(m) }
func (m RecentDeploymentReferences) Swap(i, j int) { m[i], m[j] = m[j], m[i] }
func (m RecentDeploymentReferences) Less(i, j int) bool {
return deployutil.DeploymentVersionFor(m[i].ReplicationController) > deployutil.DeploymentVersionFor(m[j].ReplicationController)
}
// TODO: move to deploy/api/helpers.go
type TemplateImage struct {
Image string
Ref *imageapi.DockerImageReference
From *kapi.ObjectReference
FromTag string
}
func EachTemplateImage(pod *kapi.PodSpec, triggerFn TriggeredByFunc, fn func(TemplateImage, error)) {
for _, container := range pod.Containers {
var ref imageapi.DockerImageReference
if trigger, ok := triggerFn(&container); ok {
trigger.Image = container.Image
fn(trigger, nil)
continue
}
ref, err := imageapi.ParseDockerImageReference(container.Image)
if err != nil {
fn(TemplateImage{Image: container.Image}, err)
continue
}
fn(TemplateImage{Image: container.Image, Ref: &ref}, nil)
}
}
type TriggeredByFunc func(container *kapi.Container) (TemplateImage, bool)
func DeploymentConfigHasTrigger(config *deployapi.DeploymentConfig) TriggeredByFunc {
return func(container *kapi.Container) (TemplateImage, bool) {
for _, trigger := range config.Triggers {
params := trigger.ImageChangeParams
if params == nil {
continue
}
for _, name := range params.ContainerNames {
if container.Name == name {
if len(params.From.Name) == 0 {
continue
}
tag := params.Tag
if len(tag) == 0 {
tag = imageapi.DefaultImageTag
}
from := params.From
if len(from.Namespace) == 0 {
from.Namespace = config.Namespace
}
return TemplateImage{
Image: container.Image,
From: &from,
FromTag: tag,
}, true
}
}
}
return TemplateImage{}, false
}
}