forked from openshift/origin
/
util.go
79 lines (61 loc) · 1.79 KB
/
util.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
package util
import (
"fmt"
"hash/adler32"
"strconv"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
deployapi "github.com/openshift/origin/pkg/deploy/api"
)
func LatestDeploymentIDForConfig(config *deployapi.DeploymentConfig) string {
return config.ID + "-" + strconv.Itoa(config.LatestVersion)
}
func ParamsForImageChangeTrigger(config *deployapi.DeploymentConfig, repoName string) *deployapi.DeploymentTriggerImageChangeParams {
if config == nil || config.Triggers == nil {
return nil
}
for _, trigger := range config.Triggers {
if trigger.Type == deployapi.DeploymentTriggerOnImageChange && trigger.ImageChangeParams.RepositoryName == repoName {
return trigger.ImageChangeParams
}
}
return nil
}
// Set a-b
func Difference(a, b util.StringSet) util.StringSet {
diff := util.StringSet{}
if a == nil || b == nil {
return diff
}
for _, s := range a.List() {
if !b.Has(s) {
diff.Insert(s)
}
}
return diff
}
// Returns a map of referenced image name to image version
func ReferencedImages(deployment *deployapi.Deployment) map[string]string {
result := make(map[string]string)
if deployment == nil {
return result
}
for _, container := range deployment.ControllerTemplate.PodTemplate.DesiredState.Manifest.Containers {
name, version := ParseContainerImage(container.Image)
result[name] = version
}
return result
}
func ParseContainerImage(image string) (string, string) {
tokens := strings.Split(image, ":")
return tokens[0], tokens[1]
}
func HashPodTemplate(t api.PodState) uint64 {
hash := adler32.New()
fmt.Fprintf(hash, "%#v", t)
return uint64(hash.Sum32())
}
func PodTemplatesEqual(a, b api.PodTemplate) bool {
return HashPodTemplate(a.DesiredState) == HashPodTemplate(b.DesiredState)
}