-
Notifications
You must be signed in to change notification settings - Fork 72
/
policy_cleaner.go
130 lines (111 loc) · 3.43 KB
/
policy_cleaner.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
package cleaner
import (
"fmt"
"policy-server/models"
"code.cloudfoundry.org/lager"
)
//go:generate counterfeiter -o fakes/uua_client.go --fake-name UAAClient . uaaClient
type uaaClient interface {
GetToken() (string, error)
}
//go:generate counterfeiter -o fakes/cc_client.go --fake-name CCClient . ccClient
type ccClient interface {
GetLiveAppGUIDs(token string, appGUIDs []string) (map[string]struct{}, error)
}
//go:generate counterfeiter -o fakes/store.go --fake-name Store . store
type store interface {
All() ([]models.Policy, error)
Delete([]models.Policy) error
}
type PolicyCleaner struct {
Logger lager.Logger
Store store
UAAClient uaaClient
CCClient ccClient
CCAppRequestChunkSize int
}
func (p *PolicyCleaner) DeleteStalePolicies() ([]models.Policy, error) {
policies, err := p.Store.All()
if err != nil {
p.Logger.Error("store-list-policies-failed", err)
return nil, fmt.Errorf("database read failed: %s", err)
}
token, err := p.UAAClient.GetToken()
if err != nil {
p.Logger.Error("get-uaa-token-failed", err)
return nil, fmt.Errorf("get UAA token failed: %s", err)
}
stalePolicies := []models.Policy{}
appGUIDs := policyAppGUIDs(policies)
appGUIDchunks := getChunks(appGUIDs, p.CCAppRequestChunkSize)
for _, appGUIDchunk := range appGUIDchunks {
liveAppGUIDs, err := p.CCClient.GetLiveAppGUIDs(token, appGUIDchunk)
if err != nil {
p.Logger.Error("cc-get-app-guids-failed", err)
return nil, fmt.Errorf("get app guids from Cloud-Controller failed: %s", err)
}
staleAppGUIDs := getStaleAppGUIDs(liveAppGUIDs, appGUIDchunk)
toDelete := getStalePolicies(policies, staleAppGUIDs)
stalePolicies = append(stalePolicies, toDelete...)
p.Logger.Info("deleting stale policies:", lager.Data{
"total_policies": len(stalePolicies),
"stale_policies": stalePolicies,
})
err = p.Store.Delete(toDelete)
if err != nil {
p.Logger.Error("store-delete-policies-failed", err)
return nil, fmt.Errorf("database write failed: %s", err)
}
}
return stalePolicies, nil
}
func (p *PolicyCleaner) DeleteStalePoliciesWrapper() error {
_, err := p.DeleteStalePolicies()
return err
}
func getStaleAppGUIDs(liveAppGUIDs map[string]struct{}, appGUIDs []string) map[string]struct{} {
staleAppGUIDs := make(map[string]struct{})
for _, guid := range appGUIDs {
if _, ok := liveAppGUIDs[guid]; !ok {
staleAppGUIDs[guid] = struct{}{}
}
}
return staleAppGUIDs
}
func getStalePolicies(policyList []models.Policy, staleAppGUIDs map[string]struct{}) []models.Policy {
stalePolicies := []models.Policy{}
for _, p := range policyList {
_, foundSrc := staleAppGUIDs[p.Source.ID]
_, foundDst := staleAppGUIDs[p.Destination.ID]
if foundSrc || foundDst {
stalePolicies = append(stalePolicies, p)
}
}
return stalePolicies
}
func policyAppGUIDs(policyList []models.Policy) []string {
appGUIDset := make(map[string]struct{})
for _, p := range policyList {
appGUIDset[p.Source.ID] = struct{}{}
appGUIDset[p.Destination.ID] = struct{}{}
}
var appGUIDs []string
for guid, _ := range appGUIDset {
appGUIDs = append(appGUIDs, guid)
}
return appGUIDs
}
func getChunks(appGuids []string, chunkSize int) [][]string {
if chunkSize < 1 {
chunkSize = 100
}
var chunks [][]string
for i := 0; i < len(appGuids); i += chunkSize {
last := i + chunkSize
if last > len(appGuids) {
last = len(appGuids)
}
chunks = append(chunks, appGuids[i:last])
}
return chunks
}