/
delete_application.go
548 lines (478 loc) · 17.1 KB
/
delete_application.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
package deletecmd
import (
"fmt"
"os/user"
"strings"
"time"
"k8s.io/apimachinery/pkg/fields"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/jenkins-x/jx/v2/pkg/client/clientset/versioned"
"github.com/jenkins-x/jx/v2/pkg/environments"
"k8s.io/helm/pkg/proto/hapi/chart"
"github.com/jenkins-x/jx/v2/pkg/prow"
"github.com/pkg/errors"
"github.com/jenkins-x/jx/v2/pkg/gits"
gojenkins "github.com/jenkins-x/golang-jenkins"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
jenkinsv1 "github.com/jenkins-x/jx/v2/pkg/client/clientset/versioned/typed/jenkins.io/v1"
"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/jenkins"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/log"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jenkins-x/jx/v2/pkg/util"
)
const (
optionPullRequestPollTime = "pull-request-poll-time"
)
var (
deleteApplicationLong = templates.LongDesc(`
Deletes one or more Applications
Note that this command does not remove the underlying Git Repositories.
For that see the [jx delete repo](https://jenkins-x.io/commands/jx_delete_repo/) command.
`)
deleteApplicationExample = templates.Examples(`
# prompt for the available applications to delete
jx delete application
# delete a specific app
jx delete application cheese
`)
)
// DeleteApplicationOptions are the flags for this delete commands
type DeleteApplicationOptions struct {
*opts.CommonOptions
SelectAll bool
SelectFilter string
IgnoreEnvironments bool
NoMergePullRequest bool
Timeout string
PullRequestPollTime string
Org string
AutoMerge bool
// calculated fields
TimeoutDuration *time.Duration
PullRequestPollDuration *time.Duration
// Used for testing
CloneDir string
}
// NewCmdDeleteApplication creates a command object for this command
func NewCmdDeleteApplication(commonOpts *opts.CommonOptions) *cobra.Command {
options := &DeleteApplicationOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "application",
Short: "Deletes one or more applications from Jenkins",
Long: deleteApplicationLong,
Example: deleteApplicationExample,
Aliases: []string{"applications"}, // FIXME - naming conflict with 'app'
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().BoolVarP(&options.IgnoreEnvironments, "no-env", "", false, "Do not remove the application from any of the Environments")
cmd.Flags().BoolVarP(&options.SelectAll, "all", "a", false, "Selects all the matched applications")
cmd.Flags().BoolVarP(&options.NoMergePullRequest, "no-merge", "", false, "Disables automatic merge of promote Pull Requests")
cmd.Flags().StringVarP(&options.SelectFilter, "filter", "f", "", "Filter the list of applications to those containing this text")
cmd.Flags().StringVarP(&options.Timeout, "timeout", "t", "1h", "The timeout to wait for the promotion to succeed in the underlying Environment. The command fails if the timeout is exceeded or the promotion does not complete")
cmd.Flags().StringVarP(&options.PullRequestPollTime, optionPullRequestPollTime, "", "20s", "Poll time when waiting for a Pull Request to merge")
cmd.Flags().StringVarP(&options.Org, "org", "o", "", "github organisation/project name that source code resides in")
cmd.Flags().BoolVarP(&options.AutoMerge, "auto-merge", "", false, "Automatically merge GitOps pull requests that pass CI")
return cmd
}
// Run implements this command
func (o *DeleteApplicationOptions) Run() error {
err := o.init()
if err != nil {
return errors.Wrap(err, "setting up context")
}
isProw, err := o.IsProw()
if err != nil {
return errors.Wrap(err, "getting prow config")
}
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
var deletedApplications []string
if isProw {
sourceRepositoryInterface := jxClient.JenkinsV1().SourceRepositories(ns)
deletedApplications, err = o.deleteProwApplication(sourceRepositoryInterface)
} else {
deletedApplications, err = o.deleteJenkinsApplication()
}
if err != nil {
return errors.Wrapf(err, "deleting application")
}
log.Logger().Infof("Deleted Application(s): %s", util.ColorInfo(strings.Join(deletedApplications, ",")))
return nil
}
func matchesWithAnyEnvironmentURL(s string, environments map[string]*v1.Environment) bool {
for _, environment := range environments {
if strings.Contains(environment.Spec.Source.URL, s) {
return true
}
}
return false
}
func removeEnvironments(jobs []string, environments map[string]*v1.Environment) []string {
var applications []string
for _, job := range jobs {
if !matchesWithAnyEnvironmentURL(job, environments) {
applications = append(applications, job)
}
}
return applications
}
func (o *DeleteApplicationOptions) deleteProwApplication(repoService jenkinsv1.SourceRepositoryInterface) (deletedApplications []string, err error) {
jxClient, _, err := o.JXClient()
if err != nil {
return deletedApplications, err
}
envMap, _, err := kube.GetOrderedEnvironments(jxClient, "")
username, err := o.GetUsername(o.Username)
if err != nil {
log.Logger().Warnf("could not get the current user: %s", err.Error())
}
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return deletedApplications, errors.Wrap(err, "getting kube client")
}
prowOptions := &prow.Options{
KubeClient: kubeClient,
NS: ns,
IgnoreBranch: true,
}
jobs, err := prowOptions.GetReleaseJobs()
if err != nil {
return deletedApplications, fmt.Errorf("Failed to get ProwJobs")
}
names := removeEnvironments(jobs, envMap)
if len(names) == 0 {
return deletedApplications, fmt.Errorf("There are no Applications in Jenkins")
}
srList, err := repoService.List(metav1.ListOptions{})
if err != nil {
return deletedApplications, errors.Wrapf(err, "error in sourcerepository service %s", err.Error())
}
if len(o.Args) == 0 {
o.Args, err = util.SelectNamesWithFilter(names, "Pick Applications to remove from Prow:", o.SelectAll, o.SelectFilter, "", o.GetIOFileHandles())
if err != nil {
return deletedApplications, err
}
if len(o.Args) == 0 {
return deletedApplications, fmt.Errorf("No application was picked to be removed from Prow")
}
} else {
for i := range o.Args {
arg := o.Args[i]
if util.StringArrayIndex(names, arg) < 0 {
org := o.Org
applicationName := arg
path := strings.SplitN(arg, "/", 2)
if len(path) >= 2 {
org = path[0]
applicationName = path[1]
}
if org == "" {
srObjects := []v1.SourceRepository{}
for sr := range srList.Items {
if srList.Items[sr].Spec.Repo == applicationName {
srObjects = append(srObjects, srList.Items[sr])
if len(srObjects) > 1 {
return deletedApplications, errors.Wrapf(err, "application %s exists in multiple orgs, use --org to specify the app to delete", util.ColorInfo(applicationName))
}
}
}
if len(srObjects) == 0 {
return deletedApplications, errors.Wrapf(err, "unable to determine org for %s. Please use --org to specify the app to delete", util.ColorInfo(applicationName))
}
// we only found a single sourcerepository resource, proceed
org = srObjects[0].Spec.Org
if org != "" {
o.Args[i] = fmt.Sprintf("%s/%s", org, applicationName)
}
}
}
}
}
for _, repo := range o.Args {
path := strings.SplitN(repo, "/", 2)
if len(path) < 2 {
return deletedApplications, fmt.Errorf("Invalid app name %s expecting owner/name syntax", repo)
}
org := path[0]
applicationName := path[1]
devEnv, settings, err := o.DevEnvAndTeamSettings()
if err != nil {
return nil, err
}
deletedApplications = append(deletedApplications, applicationName)
srName := naming.ToValidName(org + "-" + applicationName)
err = repoService.Delete(srName, nil)
if err != nil {
log.Logger().Warnf("Unable to find application metadata for %s to remove", applicationName)
}
if settings.IsSchedulerMode() {
err = o.GenerateProwConfig(ns, devEnv)
if err != nil {
return nil, errors.Wrapf(err, "generating prow config")
}
} else {
err = prow.DeleteApplication(kubeClient, []string{repo}, ns)
if err != nil {
log.Logger().Warnf("Unable to delete application %s from prow: %s", repo, err.Error())
}
}
err = o.deletePipelineActivitiesForSourceRepository(jxClient, ns, org, applicationName)
if err != nil {
log.Logger().Warnf("failed to remove PipelineActivities in namespace %s: %s", ns, err.Error())
}
for _, env := range envMap {
if env.Spec.Kind == v1.EnvironmentKindTypePermanent {
err = o.deleteApplicationFromEnvironment(env, applicationName, username)
if err != nil {
return deletedApplications, errors.Wrapf(err, "deleting application %s from environment %s", applicationName, env.Name)
}
}
}
}
return
}
func (o *DeleteApplicationOptions) deleteJenkinsApplication() (deletedApplications []string, err error) {
args := o.Args
jenk, err := o.JenkinsClient()
if err != nil {
return deletedApplications, err
}
jobs, err := jenkins.LoadAllJenkinsJobs(jenk)
if err != nil {
return deletedApplications, err
}
names := []string{}
m := map[string]*gojenkins.Job{}
for _, j := range jobs {
if jenkins.IsMultiBranchProject(j) {
name := j.Name
names = append(names, name)
m[name] = j
}
}
if len(names) == 0 {
return deletedApplications, fmt.Errorf("There are no Applications in Jenkins")
}
if len(args) == 0 {
args, err = util.SelectNamesWithFilter(names, "Pick Applications to remove from Jenkins:", o.SelectAll, o.SelectFilter, "", o.GetIOFileHandles())
if err != nil {
return deletedApplications, err
}
if len(args) == 0 {
return deletedApplications, fmt.Errorf("No application was picked to be removed from Jenkins")
}
} else {
for _, arg := range args {
if util.StringArrayIndex(names, arg) < 0 {
return deletedApplications, util.InvalidArg(arg, names)
}
}
}
deleteMessage := strings.Join(args, ", ")
if !o.BatchMode {
if answer, err := util.Confirm("You are about to delete these Applications from Jenkins: "+deleteMessage, false, "The list of Applications names to be deleted from Jenkins", o.GetIOFileHandles()); !answer {
return deletedApplications, err
}
}
for _, name := range args {
job := m[name]
if job != nil {
err = o.deleteApplication(jenk, name, job)
if err != nil {
return deletedApplications, err
}
deletedApplications = append(deletedApplications, name)
}
}
return deletedApplications, err
}
func (o *DeleteApplicationOptions) deleteApplication(jenkinsClient gojenkins.JenkinsClient, name string, job *gojenkins.Job) error {
jxClient, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
envMap, envNames, err := kube.GetOrderedEnvironments(jxClient, ns)
if err != nil {
return err
}
u, err := user.Current()
if err != nil {
return err
}
applicationName := o.applicationNameFromJenkinsJobName(name)
for _, envName := range envNames {
// TODO filter on environment names?
env := envMap[envName]
if env != nil && env.Spec.Kind == v1.EnvironmentKindTypePermanent {
err = o.deleteApplicationFromEnvironment(env, applicationName, u.Username)
if err != nil {
return err
}
}
}
// lets try delete the job from each environment first
return jenkinsClient.DeleteJob(*job)
}
func (o *DeleteApplicationOptions) applicationNameFromJenkinsJobName(name string) string {
path := strings.Split(name, "/")
return path[len(path)-1]
}
func (o *DeleteApplicationOptions) deleteApplicationFromEnvironment(env *v1.Environment, applicationName string, username string) error {
if o.IgnoreEnvironments {
return nil
}
if env.Spec.Source.URL == "" {
return nil
}
log.Logger().Infof("Removing application %s from environment %s", applicationName, env.Spec.Label)
modifyChartFn := func(requirements *helm.Requirements, metadata *chart.Metadata, values map[string]interface{},
templates map[string]string, dir string, info *gits.PullRequestDetails) error {
requirements.RemoveApplication(applicationName)
return nil
}
gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(env.Spec.Source.URL)
if err != nil {
return errors.Wrapf(err, "creating git provider for %s", env.Spec.Source.URL)
}
envDir := ""
if o.CloneDir != "" {
envDir = o.CloneDir
}
details := gits.PullRequestDetails{
BranchName: "delete-" + applicationName,
Title: "Delete application " + applicationName + " from this environment",
Message: "The command `jx delete application` was run by " + username + " and it generated this Pull Request",
}
options := environments.EnvironmentPullRequestOptions{
Gitter: o.Git(),
ModifyChartFn: modifyChartFn,
GitProvider: gitProvider,
}
info, err := options.Create(env, envDir, &details, nil, "", o.AutoMerge)
if err != nil {
return err
}
duration := *o.TimeoutDuration
end := time.Now().Add(duration)
return o.waitForGitOpsPullRequest(env, info, end, duration)
}
func (o *DeleteApplicationOptions) waitForGitOpsPullRequest(env *v1.Environment,
pullRequestInfo *gits.PullRequestInfo, end time.Time,
duration time.Duration) error {
if pullRequestInfo != nil {
logMergeFailure := false
pr := pullRequestInfo.PullRequest
log.Logger().Infof("Waiting for pull request %s to merge", pr.URL)
for {
gitProvider, _, err := o.CreateGitProviderForURLWithoutKind(env.Spec.Source.URL)
if err != nil {
return errors.Wrapf(err, "creating git provider for %s", env.Spec.Source.URL)
}
err = gitProvider.UpdatePullRequestStatus(pr)
if err != nil {
return fmt.Errorf("Failed to query the Pull Request status for %s %s", pr.URL, err)
}
if pr.Merged != nil && *pr.Merged {
log.Logger().Infof("Request %s is merged!", util.ColorInfo(pr.URL))
return nil
} else {
if pr.IsClosed() {
log.Logger().Warnf("Pull Request %s is closed", util.ColorInfo(pr.URL))
return fmt.Errorf("Promotion failed as Pull Request %s is closed without merging", pr.URL)
}
// lets try merge if the status is good
status, err := gitProvider.PullRequestLastCommitStatus(pr)
if err != nil {
log.Logger().Warnf("Failed to query the Pull Request last commit status for %s ref %s %s", pr.URL, pr.LastCommitSha, err)
} else {
if status == "success" {
if !(o.NoMergePullRequest) {
tideMerge := false
// Now check if tide is running or not
commitStatues, err := gitProvider.ListCommitStatus(pr.Owner, pr.Repo, pr.LastCommitSha)
if err != nil {
log.Logger().Warnf("unable to get commit statuses for %s", pr.URL)
} else {
for _, s := range commitStatues {
if s.State == "tide" {
tideMerge = true
break
}
}
}
if !tideMerge {
err = gitProvider.MergePullRequest(pr, "jx promote automatically merged promotion PR")
if err != nil {
if !logMergeFailure {
logMergeFailure = true
log.Logger().Warnf("Failed to merge the Pull Request %s due to %s maybe I don't have karma?", pr.URL, err)
}
}
}
}
} else if status == "error" || status == "failure" {
return fmt.Errorf("Pull request %s last commit has status %s for ref %s", pr.URL, status, pr.LastCommitSha)
}
}
}
if time.Now().After(end) {
return fmt.Errorf("Timed out waiting for pull request %s to merge. Waited %s", pr.URL, duration.String())
}
time.Sleep(*o.PullRequestPollDuration)
}
}
return nil
}
func (o *DeleteApplicationOptions) init() error {
_, _, err := o.JXClientAndDevNamespace()
if err != nil {
return errors.Wrap(err, "getting jx client")
}
if o.PullRequestPollTime != "" {
duration, err := time.ParseDuration(o.PullRequestPollTime)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.PullRequestPollTime, optionPullRequestPollTime, err)
}
o.PullRequestPollDuration = &duration
}
if o.Timeout != "" {
duration, err := time.ParseDuration(o.Timeout)
if err != nil {
return fmt.Errorf("Invalid duration format %s for option --%s: %s", o.Timeout, "timeout", err)
}
o.TimeoutDuration = &duration
}
return nil
}
func (o *DeleteApplicationOptions) deletePipelineActivitiesForSourceRepository(jxClient versioned.Interface, ns string, gitOwner string, gitRepo string) error {
gitOwnerSelector := fields.OneTermEqualSelector("spec.gitOwner", gitOwner)
gitRepoSelector := fields.OneTermEqualSelector("spec.gitRepository", gitRepo)
fieldSelector := fields.AndSelectors(gitOwnerSelector, gitRepoSelector)
pipelineInterface := jxClient.JenkinsV1().PipelineActivities(ns)
paList, err := kube.ListSelectedPipelineActivities(pipelineInterface, nil, fieldSelector)
if err != nil {
return errors.Wrapf(err, "failed to list PipelineActivity resource in namespace %s with selector %s", ns, fieldSelector.String())
}
for _, pa := range paList.Items {
err := pipelineInterface.Delete(pa.Name, &metav1.DeleteOptions{})
if err != nil {
return err
}
}
return nil
}