/
garbage_controller.go
309 lines (284 loc) · 8.63 KB
/
garbage_controller.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
Copyright 2020 Elotl Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"encoding/json"
"fmt"
"sync"
"time"
"github.com/elotl/kip/pkg/api"
"github.com/elotl/kip/pkg/server/cloud"
"github.com/elotl/kip/pkg/server/cloud/aws"
"github.com/elotl/kip/pkg/server/cloud/azure"
"github.com/elotl/kip/pkg/server/registry"
"github.com/elotl/kip/pkg/util/stats"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog"
)
var lastUnknownInstances map[string]bool
func init() {
lastUnknownInstances = make(map[string]bool)
}
type GarbageControllerConfig struct {
CleanTerminatedInterval time.Duration
CleanInstancesInterval time.Duration
CleanResourceGroupsInterval time.Duration
CleanDedicatedHostsInterval time.Duration
}
type GarbageController struct {
config GarbageControllerConfig
podRegistry *registry.PodRegistry
nodeRegistry *registry.NodeRegistry
cloudClient cloud.CloudClient
controllerID string
timer stats.LoopTimer
lastOrphanedAzureGroups sets.String
lastOldTaskDefs sets.String
}
func (c *GarbageController) Start(quit <-chan struct{}, wg *sync.WaitGroup) {
c.lastOrphanedAzureGroups = sets.NewString()
go c.GCLoop(quit, wg)
}
func (c *GarbageController) Dump() []byte {
b, err := json.MarshalIndent(c.timer, "", " ")
if err != nil {
klog.Errorln("Error dumping data from GarbageController", err)
return nil
}
return b
}
func (c *GarbageController) GCLoop(quit <-chan struct{}, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
cleanTermiantedTicker := time.NewTicker(c.config.CleanTerminatedInterval)
instancesTicker := time.NewTicker(c.config.CleanInstancesInterval)
cleanResourceGroupsTicker := time.NewTicker(c.config.CleanResourceGroupsInterval)
cleanDedicatedHostsTicker := time.NewTicker(c.config.CleanDedicatedHostsInterval)
defer cleanTermiantedTicker.Stop()
defer instancesTicker.Stop()
defer cleanResourceGroupsTicker.Stop()
defer cleanDedicatedHostsTicker.Stop()
for {
// The garbage controller takes a while to stop if we
// are timing out talking to etcd, lets give quit priority
select {
case <-quit:
klog.V(2).Info("Stopping GarbageController")
return
default:
}
select {
case <-instancesTicker.C:
c.timer.StartLoop()
c.CleanInstances()
c.CleanDanglingRoutes()
c.timer.EndLoop()
case <-cleanTermiantedTicker.C:
c.CleanTerminatedNodes()
case <-cleanResourceGroupsTicker.C:
c.CleanAzureResourceGroups()
case <-cleanDedicatedHostsTicker.C:
c.CleanDedicatedHosts()
case <-quit:
klog.V(2).Info("Stopping GarbageController")
return
}
}
}
func (c *GarbageController) CleanDanglingRoutes() {
err := c.cloudClient.RemoveRoute("", "")
if err != nil {
klog.Warningf("cleaning up dangling routes: %v", err)
}
}
func (c *GarbageController) CleanTerminatedNodes() {
nodes, err := c.nodeRegistry.ListNodes(func(n *api.Node) bool {
return n.Status.Phase == api.NodeTerminated
})
if err != nil {
klog.Errorln("Couldn't list terminated nodes", err)
}
now := api.Now()
for _, node := range nodes.Items {
if node.DeletionTimestamp == nil {
klog.Warningf("Found node with nil deletion timestamp")
_, _ = c.nodeRegistry.SetNodeDeletionTimestamp(node)
}
if node.DeletionTimestamp != nil &&
node.DeletionTimestamp.Add(30*time.Second).Before(now) {
_, err = c.nodeRegistry.PurgeNode(node)
if err != nil {
klog.Errorf("Error purging terminated nodes")
}
}
}
}
func (c *GarbageController) CleanInstances() {
unknownInstances := make(map[string]bool)
nodes, err := c.nodeRegistry.ListNodes(registry.MatchAllNodes)
if err != nil {
klog.Errorf("Error listing nodes in GC: %s", err.Error())
return
}
nodeSet := make(map[string]bool)
for _, node := range nodes.Items {
nodeSet[node.Name] = true
}
if err != nil {
klog.Errorf("Error listing nodes for instance cleaning: %s", err.Error())
return
}
instances, err := c.cloudClient.ListInstances()
if err != nil {
klog.Errorf("Error listing cloud instances: %s", err.Error())
return
}
for _, inst := range instances {
if !nodeSet[inst.NodeName] {
unknownInstances[inst.ID] = true
}
}
for iid, _ := range unknownInstances {
if lastUnknownInstances[iid] {
klog.Errorf("Stopping unknown cloud instance %s", iid)
go func() {
err := c.cloudClient.StopInstance(iid)
if err != nil {
klog.Error(err)
}
}()
}
}
lastUnknownInstances = unknownInstances
}
func (c *GarbageController) CleanDedicatedHosts() {
err := c.cloudClient.ReleaseDedicatedHosts()
if err != nil {
klog.Errorf("Error releasing dedicated hosts: %s", err.Error())
return
}
}
func (c *GarbageController) CleanAzureResourceGroups() {
az, ok := c.cloudClient.(*azure.AzureClient)
if !ok {
return
}
err := c.CleanAzureResourceGroupsHelper(az)
if err != nil {
klog.Error(err)
}
}
func (c *GarbageController) cleanFargateTaskDefs() {
client, ok := c.cloudClient.(*aws.AwsEC2)
if !ok {
return
}
if c.lastOldTaskDefs == nil {
c.lastOldTaskDefs = sets.NewString()
}
taskDefARNs, err := client.ListTaskDefinitions()
if err != nil {
klog.Errorln("Error listing ECS Fargate task definitions for cleanup:", err)
return
}
if len(taskDefARNs) == 0 {
return
}
pods, err := c.podRegistry.ListPods(registry.MatchAllPods)
if err != nil {
klog.Errorln("Error listing pods when cleaning up fargate task definitions", err)
}
podNames := sets.NewString()
for i := range pods.Items {
podNames.Insert(pods.Items[i].Name)
}
oldTaskDefs := getOutdatedTaskDefinitions(taskDefARNs, podNames, c.controllerID)
doomedTaskDefARNs := c.lastOldTaskDefs.Intersection(oldTaskDefs)
for _, taskDefARN := range doomedTaskDefARNs.List() {
err := client.DeregisterTaskDefinition(taskDefARN)
if err != nil {
klog.Errorln("Error cleaning up old task definition", taskDefARN)
}
}
c.lastOldTaskDefs = oldTaskDefs
}
// Task definitions have a taskName and taskRevision in the form
// taskname:revision. We should be deactivating all but the most
// recent task definions Here we find all the older task definition
// revisions.
func getOutdatedTaskDefinitions(taskDefARNs []string, podNames sets.String, controllerID string) sets.String {
oldTaskDefs := sets.NewString()
latestRevision := make(map[string]int)
for _, taskDef := range taskDefARNs {
name, revision := aws.SplitTaskDef(taskDef, controllerID)
if len(name) == 0 || revision == 0 {
continue
}
if revision > latestRevision[name] {
latestRevision[name] = revision
}
}
for _, taskDef := range taskDefARNs {
name, revision := aws.SplitTaskDef(taskDef, controllerID)
if !podNames.Has(name) {
oldTaskDefs.Insert(taskDef)
} else {
if revision < latestRevision[name] {
oldTaskDefs.Insert(taskDef)
}
}
}
return oldTaskDefs
}
type ResourceGrouper interface {
ListNodeResourceGroups() ([]string, error)
DeleteResourceGroup(string) error
}
func (c *GarbageController) CleanAzureResourceGroupsHelper(client ResourceGrouper) error {
nodes, err := c.nodeRegistry.ListNodes(func(n *api.Node) bool {
return (n.Status.Phase != api.NodeTerminating &&
n.Status.Phase != api.NodeTerminated)
})
if err != nil {
return fmt.Errorf("Error listing nodes for cleaning azure resource groups: %s", err.Error())
}
expectedGroups := sets.NewString()
for _, n := range nodes.Items {
if n.Status.InstanceID != "" {
expectedGroups.Insert(n.Status.InstanceID)
}
}
orphanedGroups := sets.NewString()
groups, err := client.ListNodeResourceGroups()
if err != nil {
return fmt.Errorf("Error listing azure resource groups, not cleaning orphaned groups: %s", err.Error())
}
for _, group := range groups {
if !expectedGroups.Has(group) {
orphanedGroups.Insert(group)
}
}
doomedGroups := c.lastOrphanedAzureGroups.Intersection(orphanedGroups)
newOrphaned := orphanedGroups.Difference(doomedGroups)
c.lastOrphanedAzureGroups = newOrphaned
if doomedGroups.Len() > 0 {
klog.Errorf("Deleting %d orphaned azure resource groups: %v", doomedGroups.Len(), doomedGroups.List())
}
for _, groupName := range doomedGroups.List() {
err := client.DeleteResourceGroup(groupName)
if err != nil {
klog.Errorf("Error deleting orphaned resource group: %s", err.Error())
}
}
return nil
}