-
Notifications
You must be signed in to change notification settings - Fork 787
/
get_applications.go
298 lines (269 loc) · 8.33 KB
/
get_applications.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
package cmd
import (
"github.com/jenkins-x/jx/pkg/kube/services"
"github.com/jenkins-x/jx/pkg/table"
"github.com/pkg/errors"
"io"
"k8s.io/client-go/kubernetes"
"os/user"
"sort"
"strings"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"k8s.io/api/apps/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetApplicationsOptions containers the CLI options
type GetApplicationsOptions struct {
CommonOptions
Namespace string
Environment string
HideUrl bool
HidePod bool
Previews bool
Results GetApplicationsResults
}
// GetApplicationsResults contains the data result from invoking this command
type GetApplicationsResults struct {
EnvApps []EnvApps
EnvNames []string
// Applications is a map indexed by the application name then the environment name
Applications map[string]map[string]*ApplicationEnvironmentInfo
}
// EnvApps contains data about app deployments in an environment
type EnvApps struct {
Environment v1.Environment
Apps map[string]v1beta1.Deployment
}
// ApplicationEnvironmentInfo contains the results of an app for an environment
type ApplicationEnvironmentInfo struct {
Deployment *v1beta1.Deployment
Environment *v1.Environment
Version string
URL string
}
var (
get_version_long = templates.LongDesc(`
Display applications across environments.
`)
get_version_example = templates.Examples(`
# List applications, their URL and pod counts for all environments
jx get apps
# List applications only in the Staging environment
jx get apps -e staging
# List applications only in the Production environment
jx get apps -e production
# List applications only in a specific namespace
jx get apps -n jx-staging
# List applications hiding the URLs
jx get apps -u
# List applications just showing the versions (hiding urls and pod counts)
jx get apps -u -p
`)
)
// NewCmdGetApplications creates the new command for: jx get version
func NewCmdGetApplications(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GetApplicationsOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "applications",
Short: "Display one or more Applications and their versions",
Aliases: []string{"app", "apps", "version", "versions"},
Long: get_version_long,
Example: get_version_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.HideUrl, "url", "u", false, "Hide the URLs")
cmd.Flags().BoolVarP(&options.HidePod, "pod", "p", false, "Hide the pod counts")
cmd.Flags().BoolVarP(&options.Previews, "preview", "w", false, "Show preview environments only")
cmd.Flags().StringVarP(&options.Environment, "env", "e", "", "Filter applications in the given environment")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "Filter applications in the given namespace")
return cmd
}
// Run implements this command
func (o *GetApplicationsOptions) Run() error {
kubeClient, _, err := o.KubeClient()
if err != nil {
return err
}
namespaces, envApps, envNames, apps, err := o.getAppData(kubeClient)
if err != nil {
return err
}
util.ReverseStrings(namespaces)
if len(apps) == 0 {
log.Infof("No applications found in environments %s\n", strings.Join(envNames, ", "))
return nil
}
sort.Strings(apps)
o.Results.EnvApps = envApps
o.Results.EnvNames = envNames
table := o.generateTable(apps, envApps, kubeClient)
table.Render()
return nil
}
func (o *GetApplicationsOptions) generateTable(apps []string, envApps []EnvApps, kubeClient kubernetes.Interface) table.Table {
table := o.generateTableHeaders(envApps)
appEnvMap := map[string]map[string]*ApplicationEnvironmentInfo{}
for _, appName := range apps {
row := []string{appName}
for _, ea := range envApps {
version := ""
d, ok := ea.Apps[appName]
if ok {
appMap := appEnvMap[appName]
if appMap == nil {
appMap = map[string]*ApplicationEnvironmentInfo{}
appEnvMap[appName] = appMap
}
version = kube.GetVersion(&d.ObjectMeta)
appEnvInfo := &ApplicationEnvironmentInfo{
Deployment: &d,
Environment: &ea.Environment,
Version: version,
}
appMap[ea.Environment.Name] = appEnvInfo
if ea.Environment.Spec.Kind != v1.EnvironmentKindTypePreview {
row = append(row, version)
}
if !o.HidePod {
pods := ""
replicas := ""
ready := d.Status.ReadyReplicas
if d.Spec.Replicas != nil && ready > 0 {
replicas = formatInt32(*d.Spec.Replicas)
pods = formatInt32(ready) + "/" + replicas
}
row = append(row, pods)
}
if !o.HideUrl {
url, _ := services.FindServiceURL(kubeClient, d.Namespace, appName)
if url == "" {
url, _ = services.FindServiceURL(kubeClient, d.Namespace, d.Name)
}
if url == "" {
// handle helm3
chart, ok := d.Labels["chart"]
if ok {
idx := strings.LastIndex(chart, "-")
if idx > 0 {
svcName := chart[0:idx]
if svcName != appName && svcName != d.Name {
url, _ = services.FindServiceURL(kubeClient, d.Namespace, svcName)
}
}
}
}
row = append(row, url)
appEnvInfo.URL = url
}
}
}
table.AddRow(row...)
}
o.Results.Applications = appEnvMap
return table
}
func (o *GetApplicationsOptions) getAppData(kubeClient kubernetes.Interface) (namespaces []string, envApps []EnvApps, envNames, apps []string, err error) {
f := o.Factory
client, currentNs, err := f.CreateJXClient()
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "getting jx client")
}
u, err := user.Current()
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "getting current user")
}
ns, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "getting current dev namespace")
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return nil, nil, nil, nil, errors.Wrap(err, "listing environments")
}
kube.SortEnvironments(envList.Items)
namespaces, envNames, apps = []string{}, []string{}, []string{}
envApps = []EnvApps{}
for _, env := range envList.Items {
isPreview := env.Spec.Kind == v1.EnvironmentKindTypePreview
shouldShow := isPreview
if !o.Previews {
shouldShow = !shouldShow
}
if shouldShow &&
(o.Environment == "" || o.Environment == env.Name) &&
(o.Namespace == "" || o.Namespace == env.Spec.Namespace) {
ens := env.Spec.Namespace
namespaces = append(namespaces, ens)
if ens != "" && env.Name != kube.LabelValueDevEnvironment {
envNames = append(envNames, env.Name)
m, err := kube.GetDeployments(kubeClient, ens)
if err == nil {
envApp := EnvApps{
Environment: env,
Apps: map[string]v1beta1.Deployment{},
}
envApps = append(envApps, envApp)
for k, d := range m {
appName := kube.GetAppName(k, ens)
if env.Spec.Kind == v1.EnvironmentKindTypeEdit {
if appName == kube.DeploymentExposecontrollerService || env.Spec.PreviewGitSpec.User.Username != u.Username {
continue
}
appName = kube.GetEditAppName(appName)
} else if env.Spec.Kind == v1.EnvironmentKindTypePreview {
appName = env.Spec.PullRequestURL
}
envApp.Apps[appName] = d
if util.StringArrayIndex(apps, appName) < 0 {
apps = append(apps, appName)
}
}
}
}
}
}
return
}
func (o *GetApplicationsOptions) generateTableHeaders(envApps []EnvApps) table.Table {
t := o.CreateTable()
title := "APPLICATION"
if o.Previews {
title = "PULL REQUESTS"
}
titles := []string{title}
for _, ea := range envApps {
envName := ea.Environment.Name
if ea.Environment.Spec.Kind == v1.EnvironmentKindTypeEdit {
envName = "Edit"
}
if ea.Environment.Spec.Kind != v1.EnvironmentKindTypePreview {
titles = append(titles, strings.ToUpper(envName))
}
if !o.HidePod {
titles = append(titles, "PODS")
}
if !o.HideUrl {
titles = append(titles, "URL")
}
}
t.AddRow(titles...)
return t
}