/
helpers.go
303 lines (276 loc) · 7.93 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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package describe
import (
"bytes"
"fmt"
"regexp"
"strings"
"text/tabwriter"
"time"
"github.com/docker/docker/pkg/units"
"k8s.io/kubernetes/pkg/api"
"k8s.io/kubernetes/pkg/labels"
"k8s.io/kubernetes/pkg/util/sets"
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)
buf := &bytes.Buffer{}
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("%v", 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 formatMapStringString(out *tabwriter.Writer, label string, items map[string]string) {
keys := sets.NewString()
for k := range items {
keys.Insert(k)
}
if keys.Len() == 0 {
formatString(out, label, "")
return
}
for i, key := range keys.List() {
if i == 0 {
formatString(out, label, fmt.Sprintf("%s=%s", key, items[key]))
} else {
fmt.Fprintf(out, "%s\t%s=%s\n", "", key, items[key])
}
}
}
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])
}
formatMapStringString(out, prefix+"Annotations", annotations)
}
var timeNowFn = func() time.Time {
return time.Now()
}
func formatRelativeTime(t time.Time) string {
return units.HumanDuration(timeNowFn().Sub(t))
}
// FormatRelativeTime converts a time field into a human readable age string (hours, minutes, days).
func FormatRelativeTime(t time.Time) string {
return formatRelativeTime(t)
}
func formatMeta(out *tabwriter.Writer, m api.ObjectMeta) {
formatString(out, "Name", m.Name)
if !m.CreationTimestamp.IsZero() {
formatTime(out, "Created", m.CreationTimestamp.Time)
}
formatMapStringString(out, "Labels", 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.Spec.Triggers {
whTrigger := ""
switch trigger.Type {
case buildapi.GitHubWebHookBuildTriggerType:
whTrigger = trigger.GitHubWebHook.Secret
case buildapi.GenericWebHookBuildTriggerType:
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
}
var reLongImageID = regexp.MustCompile(`[a-f0-9]{60,}$`)
// shortenImagePullSpec returns a version of the pull spec intended for display, which may
// result in the image not being usable via cut-and-paste for users.
func shortenImagePullSpec(spec string) string {
if reLongImageID.MatchString(spec) {
return spec[:len(spec)-50] + "..."
}
return spec
}
func formatImageStreamTags(out *tabwriter.Writer, stream *imageapi.ImageStream) {
if len(stream.Status.Tags) == 0 && len(stream.Spec.Tags) == 0 {
fmt.Fprintf(out, "Tags:\t<none>\n")
return
}
fmt.Fprint(out, "\nTag\tSpec\tCreated\tPullSpec\tImage\n")
sortedTags := []string{}
for k := range stream.Status.Tags {
sortedTags = append(sortedTags, k)
}
for k := range stream.Spec.Tags {
if _, ok := stream.Status.Tags[k]; !ok {
sortedTags = append(sortedTags, k)
}
}
hasScheduled, hasInsecure := false, false
imageapi.PrioritizeTags(sortedTags)
for _, tag := range sortedTags {
tagRef, ok := stream.Spec.Tags[tag]
specTag := ""
scheduled := false
insecure := false
if ok {
if tagRef.From != nil {
namePair := ""
if len(tagRef.From.Namespace) > 0 && tagRef.From.Namespace != stream.Namespace {
namePair = fmt.Sprintf("%s/%s", tagRef.From.Namespace, tagRef.From.Name)
} else {
namePair = tagRef.From.Name
}
switch tagRef.From.Kind {
case "ImageStreamTag", "ImageStreamImage":
specTag = namePair
case "DockerImage":
specTag = tagRef.From.Name
default:
specTag = fmt.Sprintf("<unknown %s> %s", tagRef.From.Kind, namePair)
}
}
scheduled, insecure = tagRef.ImportPolicy.Scheduled, tagRef.ImportPolicy.Insecure
hasScheduled = hasScheduled || scheduled
hasInsecure = hasScheduled || insecure
} else {
specTag = "<pushed>"
}
if taglist, ok := stream.Status.Tags[tag]; ok {
if len(taglist.Conditions) > 0 {
var lastTime time.Time
summary := []string{}
for _, condition := range taglist.Conditions {
if condition.LastTransitionTime.After(lastTime) {
lastTime = condition.LastTransitionTime.Time
}
switch condition.Type {
case imageapi.ImportSuccess:
if condition.Status == api.ConditionFalse {
summary = append(summary, fmt.Sprintf("import failed: %s", condition.Message))
}
default:
summary = append(summary, string(condition.Type))
}
}
if len(summary) > 0 {
description := strings.Join(summary, ", ")
if len(description) > 70 {
description = strings.TrimSpace(description[:70-3]) + "..."
}
d := timeNowFn().Sub(lastTime)
fmt.Fprintf(out, "%s\t%s\t%s ago\t%s\t%v\n",
tag,
shortenImagePullSpec(specTag),
units.HumanDuration(d),
"",
description)
}
}
for i, event := range taglist.Items {
d := timeNowFn().Sub(event.Created.Time)
image := event.Image
ref, err := imageapi.ParseDockerImageReference(event.DockerImageReference)
if err == nil {
if ref.ID == image {
image = "<same>"
}
}
pullSpec := event.DockerImageReference
if pullSpec == specTag {
pullSpec = "<same>"
} else {
pullSpec = shortenImagePullSpec(pullSpec)
}
specTag = shortenImagePullSpec(specTag)
if i != 0 {
tag, specTag = "", ""
} else {
extra := ""
if scheduled {
extra += "*"
}
if insecure {
extra += "!"
}
if len(extra) > 0 {
specTag += " " + extra
}
}
fmt.Fprintf(out, "%s\t%s\t%s ago\t%s\t%v\n",
tag,
specTag,
units.HumanDuration(d),
pullSpec,
image)
}
} else {
fmt.Fprintf(out, "%s\t%s\t\t<not available>\t<not available>\n", tag, specTag)
}
}
if hasInsecure || hasScheduled {
fmt.Fprintln(out)
if hasScheduled {
fmt.Fprintf(out, " * tag is scheduled for periodic import\n")
}
if hasInsecure {
fmt.Fprintf(out, " ! tag is insecure and can be imported over HTTP or self-signed HTTPS\n")
}
}
}