-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathcleanup.go
More file actions
176 lines (147 loc) · 4.08 KB
/
cleanup.go
File metadata and controls
176 lines (147 loc) · 4.08 KB
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
package cli
import (
"errors"
"flag"
"fmt"
"math"
"strconv"
"time"
"github.com/spf13/cobra"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
command "github.com/rancher/fleet/internal/cmd"
"github.com/rancher/fleet/internal/cmd/cli/cleanup"
)
// NewCleanup returns a subcommand to `cleanup` cluster registrations
func NewCleanUp() *cobra.Command {
cleanup := command.Command(&Cleanup{}, cobra.Command{
Short: "Clean up outdated resources",
SilenceUsage: true,
SilenceErrors: true,
})
cleanup.AddCommand(
NewClusterRegistration(),
NewGitjob(),
)
return cleanup
}
func NewClusterRegistration() *cobra.Command {
cmd := command.Command(&ClusterRegistration{}, cobra.Command{
Use: "clusterregistration [flags]",
Short: "Clean up outdated cluster registrations",
SilenceUsage: true,
SilenceErrors: true,
})
fs := flag.NewFlagSet("", flag.ExitOnError)
zopts.BindFlags(fs)
ctrl.RegisterFlags(fs)
cmd.Flags().AddGoFlagSet(fs)
return cmd
}
func NewGitjob() *cobra.Command {
cmd := command.Command(&Gitjob{}, cobra.Command{
Use: "gitjob [flags]",
Short: "Clean up outdated git jobs",
SilenceUsage: true,
SilenceErrors: true,
})
fs := flag.NewFlagSet("", flag.ExitOnError)
zopts.BindFlags(fs)
ctrl.RegisterFlags(fs)
cmd.Flags().AddGoFlagSet(fs)
return cmd
}
type Cleanup struct {
}
func (c *Cleanup) Run(cmd *cobra.Command, args []string) error {
return cmd.Help()
}
type ClusterRegistration struct {
FleetClient
Min string `usage:"Minimum delay between deletes (default: 10ms)" name:"min"`
Max string `usage:"Maximum delay between deletes (default: 5s)" name:"max"`
Factor string `usage:"Factor to increase delay between deletes (default: 1.1)" name:"factor"`
}
func (r *ClusterRegistration) PersistentPre(_ *cobra.Command, _ []string) error {
if err := r.SetupDebug(); err != nil {
return fmt.Errorf("failed to set up debug logging: %w", err)
}
return nil
}
func (a *ClusterRegistration) Run(cmd *cobra.Command, args []string) error {
var err error
min := 10 * time.Millisecond
if a.Min != "" {
min, err = time.ParseDuration(a.Min)
if err != nil {
return err
}
if min <= 0 {
return errors.New("min cannot be zero or less")
}
}
max := 3 * time.Second
if a.Max != "" {
max, err = time.ParseDuration(a.Max)
if err != nil {
return err
}
if max <= 0 {
return errors.New("max cannot be zero or less")
}
}
if max < min {
return errors.New("max cannot be less than min")
}
factor := 1.05
if a.Factor != "" {
factor, err = strconv.ParseFloat(a.Factor, 64)
if err != nil {
return err
}
if factor <= 1 || math.IsInf(factor, 0) || math.IsNaN(factor) {
return errors.New("factor must be greater than 1 and finite")
}
}
opts := cleanup.Options{
Min: min,
Max: max,
Factor: factor,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctx := log.IntoContext(cmd.Context(), ctrl.Log)
cfg := ctrl.GetConfigOrDie()
client, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
return err
}
fmt.Printf("Cleaning up outdated cluster registrations: %#v\n", opts)
return cleanup.ClusterRegistrations(ctx, client, opts)
}
type Gitjob struct {
FleetClient
BatchSize int `usage:"Number of git jobs to retrieve at once" name:"batch-size" default:"5000"`
}
func (r *Gitjob) PersistentPre(_ *cobra.Command, _ []string) error {
if err := r.SetupDebug(); err != nil {
return fmt.Errorf("failed to set up debug logging: %w", err)
}
return nil
}
func (r *Gitjob) Run(cmd *cobra.Command, args []string) error {
bs := r.BatchSize
if bs <= 1 {
return errors.New("factor must be greater than 1")
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&zopts)))
ctx := log.IntoContext(cmd.Context(), ctrl.Log)
cfg := ctrl.GetConfigOrDie()
client, err := client.New(cfg, client.Options{Scheme: scheme})
if err != nil {
return err
}
fmt.Printf("Cleaning up outdated git jobs, batch size at %d\n", bs)
return cleanup.GitJobs(ctx, client, bs)
}