forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
198 lines (176 loc) · 4.9 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package describe
import (
"bytes"
"fmt"
"sort"
"text/tabwriter"
"time"
"github.com/docker/docker/pkg/units"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
buildapi "github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/client"
imageapi "github.com/openshift/origin/pkg/image/api"
)
const emptyString = "<none>"
func tabbedString(f func(*tabwriter.Writer) error) (string, error) {
out := new(tabwriter.Writer)
b := make([]byte, 1024)
buf := bytes.NewBuffer(b)
out.Init(buf, 0, 8, 1, '\t', 0)
err := f(out)
if err != nil {
return "", err
}
out.Flush()
str := string(buf.String())
return str, nil
}
func toString(v interface{}) string {
value := fmt.Sprintf("%s", v)
if len(value) == 0 {
value = emptyString
}
return value
}
func bold(v interface{}) string {
return "\033[1m" + toString(v) + "\033[0m"
}
func convertEnv(env []api.EnvVar) map[string]string {
result := make(map[string]string, len(env))
for _, e := range env {
result[e.Name] = toString(e.Value)
}
return result
}
func formatEnv(env api.EnvVar) string {
if env.ValueFrom != nil && env.ValueFrom.FieldRef != nil {
return fmt.Sprintf("%s=<%s>", env.Name, env.ValueFrom.FieldRef.FieldPath)
}
return fmt.Sprintf("%s=%s", env.Name, env.Value)
}
func formatString(out *tabwriter.Writer, label string, v interface{}) {
fmt.Fprintf(out, fmt.Sprintf("%s:\t%s\n", label, toString(v)))
}
func formatTime(out *tabwriter.Writer, label string, t time.Time) {
fmt.Fprintf(out, fmt.Sprintf("%s:\t%s ago\n", label, formatRelativeTime(t)))
}
func formatLabels(labelMap map[string]string) string {
return labels.Set(labelMap).String()
}
func extractAnnotations(annotations map[string]string, keys ...string) ([]string, map[string]string) {
extracted := make([]string, len(keys))
remaining := make(map[string]string)
for k, v := range annotations {
remaining[k] = v
}
for i, key := range keys {
extracted[i] = remaining[key]
delete(remaining, key)
}
return extracted, remaining
}
func formatAnnotations(out *tabwriter.Writer, m api.ObjectMeta, prefix string) {
values, annotations := extractAnnotations(m.Annotations, "description")
if len(values[0]) > 0 {
formatString(out, prefix+"Description", values[0])
}
keys := util.NewStringSet()
for k := range annotations {
keys.Insert(k)
}
for i, key := range keys.List() {
if i == 0 {
formatString(out, prefix+"Annotations", fmt.Sprintf("%s=%s", key, annotations[key]))
} else {
fmt.Fprintf(out, "%s\t%s=%s\n", prefix, key, annotations[key])
}
}
}
var timeNowFn = func() time.Time {
return time.Now()
}
func formatRelativeTime(t time.Time) string {
return units.HumanDuration(timeNowFn().Sub(t))
}
func formatMeta(out *tabwriter.Writer, m api.ObjectMeta) {
formatString(out, "Name", m.Name)
if !m.CreationTimestamp.IsZero() {
formatTime(out, "Created", m.CreationTimestamp.Time)
}
formatString(out, "Labels", formatLabels(m.Labels))
formatAnnotations(out, m, "")
}
// webhookURL assembles map with of webhook type as key and webhook url and value
func webhookURL(c *buildapi.BuildConfig, cli client.BuildConfigsNamespacer) map[string]string {
result := map[string]string{}
for _, trigger := range c.Triggers {
whTrigger := ""
switch trigger.Type {
case "github":
whTrigger = trigger.GithubWebHook.Secret
case "generic":
whTrigger = trigger.GenericWebHook.Secret
}
if len(whTrigger) == 0 {
continue
}
out := ""
url, err := cli.BuildConfigs(c.Namespace).WebHookURL(c.Name, &trigger)
if err != nil {
out = fmt.Sprintf("<error: %s>", err.Error())
} else {
out = url.String()
}
result[string(trigger.Type)] = out
}
return result
}
func formatImageStreamTags(out *tabwriter.Writer, stream *imageapi.ImageStream) {
if len(stream.Status.Tags) == 0 {
fmt.Fprintf(out, "Tags:\t<none>\n")
return
}
fmt.Fprint(out, "Tags:\n Tag\tSpec\tCreated\tPullSpec\tImage\n")
sortedTags := []string{}
for k := range stream.Status.Tags {
sortedTags = append(sortedTags, k)
}
sort.Strings(sortedTags)
for _, tag := range sortedTags {
tagRef, ok := stream.Spec.Tags[tag]
specTag := ""
if ok {
if tagRef.From != nil {
specTag = fmt.Sprintf("%s/%s", tagRef.From.Namespace, tagRef.From.Name)
} else if len(tagRef.DockerImageReference) != 0 {
specTag = tagRef.DockerImageReference
}
} else {
specTag = "<pushed>"
}
for _, event := range stream.Status.Tags[tag].Items {
d := timeNowFn().Sub(event.Created.Time)
image := event.Image
ref, err := imageapi.ParseDockerImageReference(event.DockerImageReference)
if err == nil {
if ref.ID == image {
image = ""
}
}
fmt.Fprintf(out, " %s \t%s \t%s ago \t%s \t%v\n",
tag,
specTag,
units.HumanDuration(d),
event.DockerImageReference,
image)
if tag != "" {
tag = ""
}
if specTag != "" {
specTag = ""
}
}
}
}