-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
355 lines (305 loc) · 9.31 KB
/
list.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
"fmt"
"io"
"strings"
"github.com/ghodss/yaml"
"github.com/gosuri/uitable"
"github.com/spf13/cobra"
"k8s.io/helm/pkg/helm"
"k8s.io/helm/pkg/proto/hapi/release"
"k8s.io/helm/pkg/proto/hapi/services"
"k8s.io/helm/pkg/timeconv"
)
var listHelp = `
This command lists all of the releases.
By default, it lists only releases that are deployed or failed. Flags like
'--deleted' and '--all' will alter this behavior. Such flags can be combined:
'--deleted --failed'.
By default, items are sorted alphabetically. Use the '-d' flag to sort by
release date.
If an argument is provided, it will be treated as a filter. Filters are
regular expressions (Perl compatible) that are applied to the list of releases.
Only items that match the filter will be returned.
$ helm list 'ara[a-z]+'
NAME UPDATED CHART
maudlin-arachnid Mon May 9 16:07:08 2016 alpine-0.1.0
If no results are found, 'helm list' will exit 0, but with no output (or in
the case of no '-q' flag, only headers).
By default, up to 256 items may be returned. To limit this, use the '--max' flag.
Setting '--max' to 0 will not return all results. Rather, it will return the
server's default, which may be much higher than 256. Pairing the '--max'
flag with the '--offset' flag allows you to page through results.
`
type listCmd struct {
filter string
short bool
limit int
offset string
byDate bool
sortDesc bool
out io.Writer
all bool
deleted bool
deleting bool
deployed bool
failed bool
namespace string
superseded bool
pending bool
client helm.Interface
colWidth uint
output string
byChartName bool
}
type listResult struct {
Next string
Releases []listRelease
}
type listRelease struct {
Name string
Revision int32
Updated string
Status string
Chart string
AppVersion string
Namespace string
}
func newListCmd(client helm.Interface, out io.Writer) *cobra.Command {
list := &listCmd{
out: out,
client: client,
}
cmd := &cobra.Command{
Use: "list [flags] [FILTER]",
Short: "List releases",
Long: listHelp,
Aliases: []string{"ls"},
PreRunE: func(_ *cobra.Command, _ []string) error { return setupConnection() },
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
list.filter = strings.Join(args, " ")
}
if list.client == nil {
list.client = newClient()
}
return list.run()
},
}
f := cmd.Flags()
settings.AddFlagsTLS(f)
f.BoolVarP(&list.short, "short", "q", false, "Output short (quiet) listing format")
f.BoolVarP(&list.byDate, "date", "d", false, "Sort by release date")
f.BoolVarP(&list.sortDesc, "reverse", "r", false, "Reverse the sort order")
f.IntVarP(&list.limit, "max", "m", 256, "Maximum number of releases to fetch")
f.StringVarP(&list.offset, "offset", "o", "", "Next release name in the list, used to offset from start value")
f.BoolVarP(&list.all, "all", "a", false, "Show all releases, not just the ones marked DEPLOYED")
f.BoolVar(&list.deleted, "deleted", false, "Show deleted releases")
f.BoolVar(&list.deleting, "deleting", false, "Show releases that are currently being deleted")
f.BoolVar(&list.deployed, "deployed", false, "Show deployed releases. If no other is specified, this will be automatically enabled")
f.BoolVar(&list.failed, "failed", false, "Show failed releases")
f.BoolVar(&list.pending, "pending", false, "Show pending releases")
f.StringVar(&list.namespace, "namespace", "", "Show releases within a specific namespace")
f.UintVar(&list.colWidth, "col-width", 60, "Specifies the max column width of output")
f.StringVar(&list.output, "output", "", "Output the specified format (json or yaml)")
f.BoolVarP(&list.byChartName, "chart-name", "c", false, "Sort by chart name")
// TODO: Do we want this as a feature of 'helm list'?
//f.BoolVar(&list.superseded, "history", true, "show historical releases")
// set defaults from environment
settings.InitTLS(f)
return cmd
}
func (l *listCmd) run() error {
sortBy := services.ListSort_NAME
if l.byDate {
sortBy = services.ListSort_LAST_RELEASED
}
if l.byChartName {
sortBy = services.ListSort_CHART_NAME
}
sortOrder := services.ListSort_ASC
if l.sortDesc {
sortOrder = services.ListSort_DESC
}
stats := l.statusCodes()
res, err := l.client.ListReleases(
helm.ReleaseListLimit(l.limit),
helm.ReleaseListOffset(l.offset),
helm.ReleaseListFilter(l.filter),
helm.ReleaseListSort(int32(sortBy)),
helm.ReleaseListOrder(int32(sortOrder)),
helm.ReleaseListStatuses(stats),
helm.ReleaseListNamespace(l.namespace),
)
if err != nil {
return prettyError(err)
}
if res == nil {
return nil
}
rels := filterList(res.GetReleases())
result := getListResult(rels, res.Next)
output, err := formatResult(l.output, l.short, result, l.colWidth)
if err != nil {
return prettyError(err)
}
fmt.Fprintln(l.out, output)
return nil
}
// filterList returns a list scrubbed of old releases.
func filterList(rels []*release.Release) []*release.Release {
idx := map[string]int32{}
for _, r := range rels {
name, version := r.GetName(), r.GetVersion()
if max, ok := idx[name]; ok {
// check if we have a greater version already
if max > version {
continue
}
}
idx[name] = version
}
uniq := make([]*release.Release, 0, len(idx))
for _, r := range rels {
if idx[r.GetName()] == r.GetVersion() {
uniq = append(uniq, r)
}
}
return uniq
}
// statusCodes gets the list of status codes that are to be included in the results.
func (l *listCmd) statusCodes() []release.Status_Code {
if l.all {
return []release.Status_Code{
release.Status_UNKNOWN,
release.Status_DEPLOYED,
release.Status_DELETED,
release.Status_DELETING,
release.Status_FAILED,
release.Status_PENDING_INSTALL,
release.Status_PENDING_UPGRADE,
release.Status_PENDING_ROLLBACK,
}
}
status := []release.Status_Code{}
if l.deployed {
status = append(status, release.Status_DEPLOYED)
}
if l.deleted {
status = append(status, release.Status_DELETED)
}
if l.deleting {
status = append(status, release.Status_DELETING)
}
if l.failed {
status = append(status, release.Status_FAILED)
}
if l.superseded {
status = append(status, release.Status_SUPERSEDED)
}
if l.pending {
status = append(status, release.Status_PENDING_INSTALL, release.Status_PENDING_UPGRADE, release.Status_PENDING_ROLLBACK)
}
// Default case.
if len(status) == 0 {
status = append(status, release.Status_DEPLOYED, release.Status_FAILED)
}
return status
}
func getListResult(rels []*release.Release, next string) listResult {
listReleases := []listRelease{}
for _, r := range rels {
md := r.GetChart().GetMetadata()
t := "-"
if tspb := r.GetInfo().GetLastDeployed(); tspb != nil {
t = timeconv.String(tspb)
}
lr := listRelease{
Name: r.GetName(),
Revision: r.GetVersion(),
Updated: t,
Status: r.GetInfo().GetStatus().GetCode().String(),
Chart: fmt.Sprintf("%s-%s", md.GetName(), md.GetVersion()),
AppVersion: md.GetAppVersion(),
Namespace: r.GetNamespace(),
}
listReleases = append(listReleases, lr)
}
return listResult{
Releases: listReleases,
Next: next,
}
}
func shortenListResult(result listResult) []string {
names := []string{}
for _, r := range result.Releases {
names = append(names, r.Name)
}
return names
}
func formatResult(format string, short bool, result listResult, colWidth uint) (string, error) {
var output string
var err error
var shortResult []string
var finalResult interface{}
if short {
shortResult = shortenListResult(result)
finalResult = shortResult
} else {
finalResult = result
}
switch format {
case "":
if short {
output = formatTextShort(shortResult)
} else {
output = formatText(result, colWidth)
}
case "json":
o, e := json.Marshal(finalResult)
if e != nil {
err = fmt.Errorf("Failed to Marshal JSON output: %s", e)
} else {
output = string(o)
}
case "yaml":
o, e := yaml.Marshal(finalResult)
if e != nil {
err = fmt.Errorf("Failed to Marshal YAML output: %s", e)
} else {
output = string(o)
}
default:
err = fmt.Errorf("Unknown output format \"%s\"", format)
}
return output, err
}
func formatText(result listResult, colWidth uint) string {
nextOutput := ""
if result.Next != "" {
nextOutput = fmt.Sprintf("\tnext: %s\n", result.Next)
}
table := uitable.New()
table.MaxColWidth = colWidth
table.AddRow("NAME", "REVISION", "UPDATED", "STATUS", "CHART", "APP VERSION", "NAMESPACE")
for _, lr := range result.Releases {
table.AddRow(lr.Name, lr.Revision, lr.Updated, lr.Status, lr.Chart, lr.AppVersion, lr.Namespace)
}
return fmt.Sprintf("%s%s", nextOutput, table.String())
}
func formatTextShort(shortResult []string) string {
return strings.Join(shortResult, "\n")
}