-
Notifications
You must be signed in to change notification settings - Fork 787
/
gc_activities.go
183 lines (156 loc) · 4.54 KB
/
gc_activities.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
package cmd
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/spf13/cobra"
"gopkg.in/AlecAivazis/survey.v1/terminal"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jenkins-x/golang-jenkins"
"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 GCActivitiesOptions struct {
CommonOptions
RevisionHistoryLimit int
PullRequestHours int
jclient gojenkins.JenkinsClient
}
var (
GCActivitiesLong = templates.LongDesc(`
Garbage collect the Jenkins X Activity Custom Resource Definitions
`)
GCActivitiesExample = templates.Examples(`
jx garbage collect activities
jx gc activities
`)
)
// NewCmd s a command object for the "step" command
func NewCmdGCActivities(f Factory, in terminal.FileReader, out terminal.FileWriter, errOut io.Writer) *cobra.Command {
options := &GCActivitiesOptions{
CommonOptions: CommonOptions{
Factory: f,
In: in,
Out: out,
Err: errOut,
},
}
cmd := &cobra.Command{
Use: "activities",
Short: "garbage collection for activities",
Long: GCActivitiesLong,
Example: GCActivitiesExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().IntVarP(&options.RevisionHistoryLimit, "revision-history-limit", "l", 5, "Minimum number of Activities per application to keep")
cmd.Flags().IntVarP(&options.PullRequestHours, "pull-request-hours", "p", 48, "Number of hours to keep pull request activities for")
options.addCommonFlags(cmd)
return cmd
}
// Run implements this command
func (o *GCActivitiesOptions) Run() error {
kubeClient, err := o.KubeClient()
if err != nil {
return err
}
client, currentNs, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
// cannot use field selectors like `spec.kind=Preview` on CRDs so list all environments
activities, err := client.JenkinsV1().PipelineActivities(currentNs).List(metav1.ListOptions{})
if err != nil {
return err
}
if len(activities.Items) == 0 {
// no preview environments found so lets return gracefully
if o.Verbose {
log.Info("no activities found\n")
}
return nil
}
prowEnabled, err := kube.IsProwEnabled(kubeClient, currentNs)
if err != nil {
return err
}
var jobNames []string
if !prowEnabled {
o.jclient, err = o.JenkinsClient()
if err != nil {
return err
}
jobs, err := o.jclient.GetJobs()
if err != nil {
return err
}
for _, j := range jobs {
err = o.getAllPipelineJobNames(o.jclient, &jobNames, j.Name)
if err != nil {
return err
}
}
}
activityBuilds := make(map[string][]int)
for _, a := range activities.Items {
// if the activity is a PR and has completed over a week ago lets GC it
if strings.Contains(a.Name, "-pr-") {
if a.Spec.CompletedTimestamp != nil && a.Spec.CompletedTimestamp.Add(time.Duration(o.PullRequestHours)*time.Hour).Before(time.Now()) {
err = client.JenkinsV1().PipelineActivities(currentNs).Delete(a.Name, metav1.NewDeleteOptions(0))
if err != nil {
return err
}
continue
}
}
if !prowEnabled {
// if activity has no job in Jenkins delete it
matched := false
for _, j := range jobNames {
if a.Spec.Pipeline == j {
matched = true
break
}
}
if !matched {
err = client.JenkinsV1().PipelineActivities(currentNs).Delete(a.Name, metav1.NewDeleteOptions(0))
if err != nil {
return err
}
}
}
buildNumber, err := strconv.Atoi(a.Spec.Build)
if err != nil {
return err
}
// collect all activities for a pipeline
activityBuilds[a.Spec.Pipeline] = append(activityBuilds[a.Spec.Pipeline], buildNumber)
}
for pipeline, builds := range activityBuilds {
sort.Ints(builds)
// iterate over the build numbers and delete any while the activity is under the RevisionHistoryLimit
i := 0
for i < len(builds)-o.RevisionHistoryLimit {
activityName := fmt.Sprintf("%s-%v", pipeline, builds[i])
activityName = strings.Replace(activityName, "/", "-", -1)
activityName = strings.Replace(activityName, "_", "-", -1)
activityName = strings.ToLower(activityName)
err = client.JenkinsV1().PipelineActivities(currentNs).Delete(activityName, metav1.NewDeleteOptions(0))
if err != nil {
return fmt.Errorf("failed to delete activity %s: %v\n", activityName, err)
}
i++
}
}
return nil
}