-
Notifications
You must be signed in to change notification settings - Fork 786
/
gc_previews.go
138 lines (119 loc) · 3.47 KB
/
gc_previews.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
package cmd
import (
"fmt"
"io"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"strconv"
"strings"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/gits"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
)
// GetOptions is the start of the data required to perform the operation. As new fields are added, add them here instead of
// referencing the cmd.Flags()
type GCPreviewsOptions struct {
CommonOptions
DisableImport bool
OutDir string
}
var (
GCPreviewsLong = templates.LongDesc(`
Garbage collect Jenkins X preview environments. If a pull request is merged or closed the associated preview
environment will be deleted.
`)
GCPreviewsExample = templates.Examples(`
jx garbage collect previews
jx gc previews
`)
)
// NewCmd s a command object for the "step" command
func NewCmdGCPreviews(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GCPreviewsOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "previews",
Short: "garbage collection for preview environments",
Long: GCPreviewsLong,
Example: GCPreviewsExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
options.addCommonFlags(cmd)
return cmd
}
// Run implements this command
func (o *GCPreviewsOptions) Run() error {
f := o.Factory
client, currentNs, err := f.CreateJXClient()
if err != nil {
return err
}
// cannot use field selectors like `spec.kind=Preview` on CRDs so list all environments
envs, err := client.JenkinsV1().Environments(currentNs).List(metav1.ListOptions{})
if err != nil {
return err
}
if len(envs.Items) == 0 {
// no preview environments found so lets return gracefully
if o.Verbose {
log.Info("no preview environments found\n")
}
return nil
}
for _, e := range envs.Items {
if e.Spec.Kind == v1.EnvironmentKindTypePreview {
gitInfo, err := gits.ParseGitURL(e.Spec.Source.URL)
if err != nil {
return err
}
// we need pull request info to include
authConfigSvc, err := o.CreateGitAuthConfigService()
if err != nil {
return err
}
gitKind, err := o.GitServerKind(gitInfo)
if err != nil {
return err
}
gitProvider, err := gitInfo.CreateProvider(authConfigSvc, gitKind, o.Git(), o.BatchMode, o.In, o.Out, o.Err)
if err != nil {
return err
}
prNum, err := strconv.Atoi(e.Spec.PreviewGitSpec.Name)
if err != nil {
log.Warn("Unable to convert PR " + e.Spec.PreviewGitSpec.Name + " to a number" + "\n")
}
pullRequest, err := gitProvider.GetPullRequest(gitInfo.Organisation, gitInfo, prNum)
if err != nil {
return err
}
lowerState := strings.ToLower(*pullRequest.State)
if strings.HasPrefix(lowerState, "clos") || strings.HasPrefix(lowerState, "merged") || strings.HasPrefix(lowerState, "superseded") || strings.HasPrefix(lowerState, "declined") {
// lets delete the preview environment
deleteOpts := DeleteEnvOptions{
DeleteNamespace: true,
CommonOptions: o.CommonOptions,
}
deleteOpts.CommonOptions.Args = []string{e.Name}
err = deleteOpts.Run()
if err != nil {
return fmt.Errorf("failed to delete preview environment %s: %v\n", e.Name, err)
}
}
}
}
return nil
}