forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_activities.go
180 lines (152 loc) · 4.15 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
package cmd
import (
"fmt"
"io"
"sort"
"strconv"
"strings"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/spf13/cobra"
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
jclient *gojenkins.Jenkins
}
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, out io.Writer, errOut io.Writer) *cobra.Command {
options := &GCActivitiesOptions{
CommonOptions: CommonOptions{
Factory: f,
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", "", 5, "Minimum number of Activities per application to keep")
return cmd
}
// Run implements this command
func (o *GCActivitiesOptions) Run() error {
apisClient, err := o.CreateApiExtensionsClient()
if err != nil {
return err
}
err = kube.RegisterPipelineActivityCRD(apisClient)
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
}
o.jclient, err = o.JenkinsClient()
if err != nil {
return err
}
jobs, err := o.jclient.GetJobs()
if err != nil {
return err
}
var jobNames []string
for _, j := range jobs {
err = o.getAllPipelineJobNames(&jobNames, j.Name)
if err != nil {
return err
}
}
activityBuilds := make(map[string][]int)
for _, a := range activities.Items {
// 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
}
func (o *GCActivitiesOptions) getAllPipelineJobNames(jobNames *[]string, jobName string) error {
job, err := o.jclient.GetJob(jobName)
if err != nil {
return err
}
if len(job.Jobs) == 0 {
*jobNames = append(*jobNames, job.FullName)
}
for _, j := range job.Jobs {
err = o.getAllPipelineJobNames(jobNames, job.FullName+"/"+j.Name)
if err != nil {
return err
}
}
return nil
}