This repository has been archived by the owner on May 7, 2024. It is now read-only.
/
list_releases.go
247 lines (210 loc) · 6.41 KB
/
list_releases.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
package commands
import (
"fmt"
"net/http"
"os"
"sort"
"strings"
"github.com/Masterminds/semver"
"github.com/fatih/color"
"github.com/giantswarm/columnize"
"github.com/giantswarm/gsclientgen/models"
"github.com/giantswarm/microerror"
"github.com/spf13/cobra"
"github.com/giantswarm/gsctl/client/clienterror"
"github.com/giantswarm/gsctl/config"
"github.com/giantswarm/gsctl/util"
)
const (
listReleasesActivityName = "list-releases"
)
var (
// ListReleasesCommand performs the "list releases" function
ListReleasesCommand = &cobra.Command{
Use: "releases",
Short: "List releases to be used with clusters",
Long: `Prints detail on all available releases.
A release is a software bundle that constitutes a cluster. It is identified by its semantic version number.`,
PreRun: listReleasesPreRunOutput,
Run: listReleasesRunOutput,
}
)
// listReleasesArguments are the actual arguments used to call the
// listReleases() function.
type listReleasesArguments struct {
apiEndpoint string
token string
scheme string
}
// defaultListReleasesArguments returns a new listReleasesArguments struct
// based on global variables (= command line options from cobra).
func defaultListReleasesArguments() listReleasesArguments {
endpoint := config.Config.ChooseEndpoint(cmdAPIEndpoint)
token := config.Config.ChooseToken(endpoint, cmdToken)
scheme := config.Config.ChooseScheme(endpoint, cmdToken)
return listReleasesArguments{
apiEndpoint: endpoint,
token: token,
scheme: scheme,
}
}
func init() {
ListCommand.AddCommand(ListReleasesCommand)
}
// listReleasesPreRunOutput does our pre-checks and shows errors, in case
// something is missing.
func listReleasesPreRunOutput(cmd *cobra.Command, extraArgs []string) {
args := defaultListReleasesArguments()
err := listReleasesPreconditions(&args)
if err == nil {
return
}
handleCommonErrors(err)
fmt.Println(color.RedString(err.Error()))
os.Exit(1)
}
// listReleasesPreconditions validates our pre-conditions and returns an error in
// case something is missing.
func listReleasesPreconditions(args *listReleasesArguments) error {
if config.Config.Token == "" && args.token == "" {
return microerror.Mask(notLoggedInError)
}
return nil
}
// listReleasesRunOutput is the function called to list releases and display
// errors in case they happen
func listReleasesRunOutput(cmd *cobra.Command, extraArgs []string) {
args := defaultListReleasesArguments()
releases, err := listReleases(args)
if err != nil {
handleCommonErrors(err)
if clientErr, ok := err.(*clienterror.APIError); ok {
fmt.Println(color.RedString(clientErr.ErrorMessage))
if clientErr.ErrorDetails != "" {
fmt.Println(clientErr.ErrorDetails)
}
} else {
fmt.Println(color.RedString("Error: %s", err.Error()))
}
os.Exit(1)
}
// success
if len(releases) == 0 {
fmt.Println(color.RedString("No releases available."))
fmt.Println("We cannot find any releases. Please contact the Giant Swarm support team to find out if there is a problem to be solved.")
}
// table headers
output := []string{strings.Join([]string{
color.CyanString("VERSION"),
color.CyanString("STATUS"),
color.CyanString("CREATED"),
color.CyanString("KUBERNETES"),
color.CyanString("CONTAINERLINUX"),
color.CyanString("COREDNS"),
color.CyanString("CALICO"),
}, "|")}
var major int64
var status string
major = 0
status = "deprecated"
for i, release := range releases {
created := util.ShortDate(util.ParseDate(*release.Timestamp))
kubernetesVersion := "n/a"
containerLinuxVersion := "n/a"
coreDNSVersion := "n/a"
calicoVersion := "n/a"
// As long as the status information is not specific in the API
// we start with deprecated, find the active one and then switch
// to "wip" for each major version.
version, err := semver.NewVersion(*release.Version)
if err == nil {
if version.Major() > major {
// Found new major release.
major = version.Major()
// If this is a new major version and the last release
// likelihood is high that this is a wip release and
// not deprecated.
if i == len(releases)-1 {
status = "wip"
} else {
status = "deprecated"
}
}
if release.Active {
status = "active"
} else if status == "active" {
status = "wip"
}
} else {
// release version couldn't be parsed
major = 0
status = "-"
}
for _, component := range release.Components {
if *component.Name == "kubernetes" {
kubernetesVersion = *component.Version
}
if *component.Name == "containerlinux" {
containerLinuxVersion = *component.Version
}
if *component.Name == "coredns" {
coreDNSVersion = *component.Version
}
if *component.Name == "calico" {
calicoVersion = *component.Version
}
}
if status == "active" {
output = append(output, strings.Join([]string{
color.YellowString(*release.Version),
color.YellowString(status),
color.YellowString(created),
color.YellowString(kubernetesVersion),
color.YellowString(containerLinuxVersion),
color.YellowString(coreDNSVersion),
color.YellowString(calicoVersion),
}, "|"))
} else {
output = append(output, strings.Join([]string{
*release.Version,
status,
created,
kubernetesVersion,
containerLinuxVersion,
coreDNSVersion,
calicoVersion,
}, "|"))
}
}
fmt.Println(columnize.SimpleFormat(output))
}
// listReleases fetches releases and returns them as a structured result.
func listReleases(args listReleasesArguments) ([]*models.V4ReleaseListItem, error) {
auxParams := ClientV2.DefaultAuxiliaryParams()
auxParams.ActivityName = listReleasesActivityName
response, err := ClientV2.GetReleases(auxParams)
if err != nil {
// create specific error types for cases we care about
if clientErr, ok := err.(*clienterror.APIError); ok {
if clientErr.HTTPStatusCode >= http.StatusInternalServerError {
return nil, microerror.Maskf(internalServerError, err.Error())
} else if clientErr.HTTPStatusCode == http.StatusUnauthorized {
return nil, microerror.Mask(notAuthorizedError)
}
}
return nil, microerror.Mask(err)
}
// sort releases by version (ascending)
sort.Slice(response.Payload[:], func(i, j int) bool {
vi, err := semver.NewVersion(*response.Payload[i].Version)
if err != nil {
return false
}
vj, err := semver.NewVersion(*response.Payload[j].Version)
if err != nil {
return true
}
return vj.GreaterThan(vi)
})
return response.Payload, nil
}