-
Notifications
You must be signed in to change notification settings - Fork 785
/
get_apps.go
266 lines (238 loc) · 7.46 KB
/
get_apps.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
package get
import (
"fmt"
"github.com/jenkins-x/jx-logging/pkg/log"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/apps"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/helm"
"github.com/jenkins-x/jx/v2/pkg/io/secrets"
"github.com/jenkins-x/jx/v2/pkg/table"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"
)
// GetAppsOptions containers the CLI options
type GetAppsOptions struct {
GetOptions
Namespace string
ShowStatus bool
GitOps bool
DevEnv *v1.Environment
}
type appsResult struct {
AppOutput []appOutput `json:"items"`
}
type appOutput struct {
Name string `yaml:"appName" json:"appName"`
Version string `yaml:"version" json:"version"`
Description string `yaml:"description" json:"description"`
ChartRepository string `yaml:"chartRepository" json:"chartRepository"`
Status string `yaml:"status" json:"status"`
Namespace string `yaml:"namespace" json:"namespace"`
}
// HelmOutput is the representation of the Helm status command
type HelmOutput struct {
helmInfo `yaml:"info" json:"info"`
}
type helmInfo struct {
helmInfoStatus `yaml:"status" json:"status"`
}
type helmInfoStatus struct {
Resources string `json:"resources" json:"resources"`
}
var (
getAppsLong = templates.LongDesc(`
Display installed Apps (an app is similar to an addon)
`)
getAppsExample = templates.Examples(`
# List all apps
jx get apps
# Display details about the app called cheese
jx get app cheese
# Display detailed status info about the app called cheese
jx get app cheese --status
# Display detailed status info about the app called cheese in 'json' format
jx get app cheese --status -o json
# Display details about the app called cheese in 'yaml' format
jx get app cheese -o yaml
`)
)
// NewCmdGetApps creates the new command for: jx get version
func NewCmdGetApps(commonOpts *opts.CommonOptions) *cobra.Command {
options := &GetAppsOptions{
GetOptions: GetOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "apps",
Short: "Display one or more installed Apps (an app is similar to an addon)",
Aliases: []string{"app"},
Long: getAppsLong,
Example: getAppsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.AddGetFlags(cmd)
cmd.Flags().StringVarP(&options.Namespace, opts.OptionNamespace, "n", "", "The namespace where you want to search the apps in")
return cmd
}
// Run implements this command
func (o *GetAppsOptions) Run() error {
o.GitOps, o.DevEnv = o.GetDevEnv()
kubeClient, err := o.GetOptions.KubeClient()
if err != nil {
return err
}
jxClient, _, err := o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrapf(err, "getting jx client")
}
envsDir, err := o.GetOptions.EnvironmentsDir()
if err != nil {
return errors.Wrap(err, "getting the the GitOps environments dir")
}
installOptions := apps.InstallOptions{
IOFileHandles: o.GetIOFileHandles(),
DevEnv: o.DevEnv,
Verbose: o.Verbose,
GitOps: o.GitOps,
BatchMode: o.BatchMode,
Helmer: o.Helm(),
JxClient: jxClient,
EnvironmentCloneDir: envsDir,
}
if o.GetSecretsLocation() == secrets.VaultLocationKind {
teamName, _, err := o.TeamAndEnvironmentNames()
if err != nil {
return err
}
installOptions.TeamName = teamName
client, err := o.SystemVaultClient("")
if err != nil {
return err
}
installOptions.VaultClient = client
}
if o.GitOps {
msg := "Unable to specify --%s when using GitOps for your dev environment"
if o.Namespace != "" {
return util.InvalidOptionf(opts.OptionNamespace, o.ReleaseName, msg, opts.OptionNamespace)
}
gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(o.DevEnv.Spec.Source.URL)
if err != nil {
return errors.Wrapf(err, "creating git provider for %s", o.DevEnv.Spec.Source.URL)
}
installOptions.GitProvider = gitProvider
installOptions.Gitter = o.Git()
}
apps, err := installOptions.GetApps(o.Args)
if err != nil {
return err
}
if len(apps.Items) == 0 {
if len(o.Args) > 0 {
return errors.New("No Apps found")
}
fmt.Fprint(o.Out, "No Apps found\n")
return nil
}
if o.Output != "" {
appsResult := o.generateTableFormatted(apps)
return o.renderResult(appsResult, o.Output)
}
table := o.generateTable(apps, kubeClient)
table.Render()
return nil
}
func (o *GetAppsOptions) generateTableFormatted(apps *v1.AppList) appsResult {
releases, err := o.getAppsStatus(o.GitOps, o.Namespace, apps)
if err != nil {
log.Logger().Warnf("There was a problem obtaining the app status: %v", err)
}
results := appsResult{}
for _, app := range apps.Items {
if app.Labels != nil {
name := app.Labels[helm.LabelAppName]
if name != "" && app.Annotations != nil {
var status string
if releaseStatus, ok := releases[name]; ok {
status = releaseStatus
}
results.AppOutput = append(results.AppOutput, appOutput{
Name: name,
Version: app.Labels[helm.LabelAppVersion],
ChartRepository: app.Annotations[helm.AnnotationAppRepository],
Namespace: app.Namespace,
Status: status,
Description: app.Annotations[helm.AnnotationAppDescription],
})
}
}
}
return results
}
func (o *GetAppsOptions) generateTable(apps *v1.AppList, kubeClient kubernetes.Interface) table.Table {
table := o.generateTableHeaders(apps)
releases, err := o.getAppsStatus(o.GitOps, o.Namespace, apps)
if err != nil {
log.Logger().Warnf("There was a problem obtaining the app status: %v", err)
}
for _, app := range apps.Items {
if app.Labels != nil {
name := app.Labels[helm.LabelAppName]
if name != "" && app.Annotations != nil {
version := app.Labels[helm.LabelAppVersion]
description := app.Annotations[helm.AnnotationAppDescription]
repository := app.Annotations[helm.AnnotationAppRepository]
var status string
if releaseStatus, ok := releases[name]; ok {
status = releaseStatus
}
namespace := app.Namespace
row := []string{name, version, repository, namespace, status, description}
table.AddRow(row...)
}
}
}
return table
}
func (o *GetAppsOptions) generateTableHeaders(apps *v1.AppList) table.Table {
t := o.CreateTable()
t.Out = o.CommonOptions.Out
titles := []string{"Name", "Version", "Chart Repository", "Namespace", "Status", "Description"}
t.AddRow(titles...)
return t
}
func (o *GetAppsOptions) getAppsStatus(gitOps bool, namespace string, apps *v1.AppList) (map[string]string, error) {
appsStatus := make(map[string]string)
if o.GitOps {
for _, a := range apps.Items {
//In gitops, we can assume that if they app has a namespace, it has been deployed
//Otherwise, it has just been defined in the requirements.yaml and not deployed yet
appName := a.Labels[helm.LabelAppName]
if a.Namespace == "" {
appsStatus[appName] = "READY FOR DEPLOYMENT"
} else {
appsStatus[appName] = "DEPLOYED"
}
}
return appsStatus, nil
}
statusReleases, _, err := o.Helm().ListReleases(namespace)
if err != nil {
return nil, errors.Wrap(err, "there was a problem getting the status of the apps")
}
for _, v := range statusReleases {
appsStatus[v.Chart] = v.Status
}
return appsStatus, nil
}