forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_app.go
167 lines (153 loc) · 3.98 KB
/
get_app.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
package cmd
import (
"io"
"sort"
"strings"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
cmdutil "github.com/jenkins-x/jx/pkg/jx/cmd/util"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"k8s.io/api/apps/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// GetVersionOptions containers the CLI options
type GetVersionOptions struct {
CommonOptions
UrlMode bool
}
var (
get_version_long = templates.LongDesc(`
Display applications across environments.
`)
get_version_example = templates.Examples(`
# List applications for all environments
jx get apps
# List applications and their URLs for all environments
jx get apps -u
`)
)
// NewCmdGetVersion creates the new command for: jx get version
func NewCmdGetVersion(f cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetVersionOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "applications",
Short: "Display one or many 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()
cmdutil.CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.UrlMode, "url", "u", false, "Show the URLs rather than pod numbers")
return cmd
}
type EnvApps struct {
Environment v1.Environment
Apps map[string]v1beta1.Deployment
}
// Run implements this command
func (o *GetVersionOptions) Run() error {
f := o.Factory
client, currentNs, err := f.CreateJXClient()
if err != nil {
return err
}
kubeClient, _, err := o.Factory.CreateClient()
if err != nil {
return err
}
ns, _, err := kube.GetDevNamespace(kubeClient, currentNs)
if err != nil {
return err
}
envList, err := client.JenkinsV1().Environments(ns).List(metav1.ListOptions{})
if err != nil {
return err
}
kube.SortEnvironments(envList.Items)
namespaces := []string{}
envApps := []EnvApps{}
envNames := []string{}
apps := []string{}
for _, env := range envList.Items {
if env.Spec.Kind != v1.EnvironmentKindTypePreview {
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)
envApp.Apps[appName] = d
if util.StringArrayIndex(apps, appName) < 0 {
apps = append(apps, appName)
}
}
}
}
}
}
util.ReverseStrings(namespaces)
if len(apps) == 0 {
o.Printf("No applications found in environments %s\n", strings.Join(envNames, ", "))
return nil
}
sort.Strings(apps)
table := o.CreateTable()
titles := []string{"APPLICATION"}
for _, ea := range envApps {
title := "PODS"
if o.UrlMode {
title = "URL"
}
titles = append(titles, strings.ToUpper(ea.Environment.Name), title)
}
table.AddRow(titles...)
for _, appName := range apps {
row := []string{appName}
for _, ea := range envApps {
version := ""
pods := ""
d := ea.Apps[appName]
version = kube.GetVersion(&d.ObjectMeta)
if o.UrlMode {
url, _ := kube.FindServiceURL(kubeClient, d.Namespace, appName)
if url == "" {
url, _ = kube.FindServiceURL(kubeClient, d.Namespace, d.Name)
}
if url != "" {
pods = url
}
} else {
replicas := ""
ready := d.Status.ReadyReplicas
if d.Spec.Replicas != nil && ready > 0 {
replicas = formatInt32(*d.Spec.Replicas)
pods = formatInt32(ready) + "/" + replicas
}
}
row = append(row, version, pods)
}
table.AddRow(row...)
}
table.Render()
return nil
}