forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
version.go
76 lines (70 loc) · 1.42 KB
/
version.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
package kube
import (
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetVersion returns the version from the labels on the deployment if it can be deduced
func GetVersion(r *metav1.ObjectMeta) string {
if r != nil {
labels := r.Labels
if labels != nil {
v := labels["version"]
if v != "" {
return v
}
v = labels["chart"]
if v != "" {
arr := strings.Split(v, "-")
last := arr[len(arr)-1]
if last != "" {
return last
}
return v
}
}
}
return ""
}
// GetName returns the app name
func GetName(r *metav1.ObjectMeta) string {
if r != nil {
name := r.Name
ns := r.Namespace
if ns != "" {
// for helm deployments which prefix the namespace in the name lets strip it
prefix := ns + "-"
if strings.HasPrefix(name, prefix) {
return strings.TrimPrefix(name, prefix)
}
}
labels := r.Labels
if labels != nil {
v := labels["app"]
if v != "" {
return v
}
}
return name
}
return ""
}
// GetCommitSha returns the git commit sha
func GetCommitSha(r *metav1.ObjectMeta) string {
if r != nil {
annotations := r.Annotations
if annotations != nil {
return annotations["jenkins.io/git-sha"]
}
}
return ""
}
// GetCommitURL returns the git commit URL
func GetCommitURL(r *metav1.ObjectMeta) string {
if r != nil {
annotations := r.Annotations
if annotations != nil {
return annotations["jenkins.io/git-url"]
}
}
return ""
}