-
Notifications
You must be signed in to change notification settings - Fork 787
/
gc_releases.go
143 lines (124 loc) · 3.35 KB
/
gc_releases.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
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/log"
)
// GCReleasesOptions contains the CLI options for this command
type GCReleasesOptions struct {
*CommonOptions
RevisionHistoryLimit int
}
var (
GCReleasesLong = templates.LongDesc(`
Garbage collect the Jenkins X Activity Custom Resource Definitions
`)
GCReleasesExample = templates.Examples(`
jx garbage collect releases
jx gc releases
`)
)
// NewCmd s a command object for the "step" command
func NewCmdGCReleases(commonOpts *CommonOptions) *cobra.Command {
options := &GCReleasesOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "releases",
Short: "garbage collection for Releases",
Long: GCReleasesLong,
Example: GCReleasesExample,
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 Releases per application to keep")
return cmd
}
// Run implements this command
func (o *GCReleasesOptions) Run() error {
err := o.registerReleaseCRD()
if err != nil {
return err
}
client, ns, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
// cannot use field selectors like `spec.kind=Preview` on CRDs so list all environments
releaseInterface := client.JenkinsV1().Releases(ns)
releases, err := releaseInterface.List(metav1.ListOptions{})
if err != nil {
return err
}
if len(releases.Items) == 0 {
// no preview environments found so lets return gracefully
if o.Verbose {
log.Info("no releases found\n")
}
return nil
}
jenkinsClient, err := o.JenkinsClient()
if err != nil {
return err
}
jobs, err := jenkinsClient.GetJobs()
if err != nil {
return err
}
var jobNames []string
for _, j := range jobs {
err = o.getAllPipelineJobNames(jenkinsClient, &jobNames, j.Name)
if err != nil {
return err
}
}
pipelineReleases := make(map[string][]v1.Release)
for _, a := range releases.Items {
owner := a.Spec.GitOwner
repo := a.Spec.GitRepository
pipeline := owner + "/" + repo + "/master"
// if activity has no job in Jenkins delete it
matched := true
if owner != "" && repo != "" {
matched = false
for _, j := range jobNames {
if pipeline == j {
matched = true
break
}
}
}
if !matched {
err = releaseInterface.Delete(a.Name, metav1.NewDeleteOptions(0))
if err != nil {
return err
} else {
log.Infof("Deleting Release %s as it no longer has a pipeline for %s\n", a.Name, pipeline)
}
}
// collect all releases for a pipeline
pipelineReleases[pipeline] = append(pipelineReleases[pipeline], a)
}
for _, releases := range pipelineReleases {
kube.SortReleases(releases)
// iterate over the old releases and remove them
for i := o.RevisionHistoryLimit + 1; i < len(releases); i++ {
name := releases[i].Name
err = releaseInterface.Delete(name, metav1.NewDeleteOptions(0))
if err != nil {
return fmt.Errorf("failed to delete Release %s in namespace %s: %v\n", name, ns, err)
} else {
log.Infof("Deleting old Release %s\n", name)
}
}
}
return nil
}