forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_applications.go
190 lines (171 loc) · 4.88 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
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"
)
// GetApplicationsOptions containers the CLI options
type GetApplicationsOptions struct {
CommonOptions
Namespace string
Environment string
HideUrl bool
HidePod bool
}
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 cmdutil.Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GetApplicationsOptions{
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.HideUrl, "url", "u", false, "Hide the URLs")
cmd.Flags().BoolVarP(&options.HidePod, "pod", "p", false, "Hide the pod counts")
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
}
type EnvApps struct {
Environment v1.Environment
Apps map[string]v1beta1.Deployment
}
// Run implements this command
func (o *GetApplicationsOptions) 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 &&
(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)
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 {
titles = append(titles, strings.ToUpper(ea.Environment.Name))
if !o.HidePod {
titles = append(titles, "PODS")
}
if !o.HideUrl {
titles = append(titles, "URL")
}
}
table.AddRow(titles...)
for _, appName := range apps {
row := []string{appName}
for _, ea := range envApps {
version := ""
d := ea.Apps[appName]
version = kube.GetVersion(&d.ObjectMeta)
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, _ := kube.FindServiceURL(kubeClient, d.Namespace, appName)
if url == "" {
url, _ = kube.FindServiceURL(kubeClient, d.Namespace, d.Name)
}
row = append(row, url)
}
}
table.AddRow(row...)
}
table.Render()
return nil
}