This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
forked from trek10inc/awsets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
266 lines (236 loc) · 6.32 KB
/
main.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
package awspelunk
import (
"context"
"fmt"
"reflect"
"strings"
"sync"
context2 "github.com/trek10inc/awsets/context"
"github.com/aws/aws-sdk-go-v2/aws/external"
"github.com/trek10inc/awsets/lister"
"github.com/trek10inc/awsets/resource"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
)
type ListerName string
func Types(include []string, exclude []string) []resource.ResourceType {
typeMap := make(map[resource.ResourceType]struct{})
if len(include) == 0 {
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
typeMap[t] = struct{}{}
}
}
} else {
for _, name := range include {
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
if strings.HasPrefix(t.String(), name) {
typeMap[t] = struct{}{}
}
}
}
}
}
for _, name := range exclude {
if len(name) == 0 {
continue
}
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
if strings.HasPrefix(t.String(), name) {
delete(typeMap, t)
}
}
}
}
ret := make([]resource.ResourceType, 0)
for k := range typeMap {
ret = append(ret, k)
}
return ret
}
func Listers(include []string, exclude []string) []ListerName {
idxMap := make(map[ListerName]struct{}, 0)
if len(include) == 0 {
for _, v := range lister.AllListers() {
idxMap[ListerName(reflect.TypeOf(v).Name())] = struct{}{}
}
} else {
for _, name := range include {
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
if strings.HasPrefix(t.String(), name) {
idxMap[ListerName(reflect.TypeOf(v).Name())] = struct{}{}
}
}
}
}
}
for _, name := range exclude {
if len(name) == 0 {
continue
}
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
if strings.HasPrefix(t.String(), name) {
delete(idxMap, ListerName(reflect.TypeOf(v).Name()))
}
}
}
}
ret := make([]ListerName, 0)
for v := range idxMap {
ret = append(ret, v)
}
return ret
}
func GetByName(name ListerName) (lister.Lister, error) {
for _, v := range lister.AllListers() {
if name == ListerName(reflect.TypeOf(v).Name()) {
return v, nil
}
}
return nil, fmt.Errorf("no lister found for %s", name)
}
func GetByType(kind resource.ResourceType) (lister.Lister, error) {
for _, v := range lister.AllListers() {
for _, t := range v.Types() {
if t == kind {
return v, nil
}
}
}
return nil, fmt.Errorf("no lister found for %s", kind)
}
func Regions(prefixes ...string) ([]string, error) {
ctx, err := external.LoadDefaultAWSConfig()
if err != nil {
return nil, fmt.Errorf("failed to load aws config: %w", err)
}
ctx.Region = "us-east-1"
regionMap := make(map[string]struct{}, 0)
ec2svc := ec2.New(ctx)
regionsRes, err := ec2svc.DescribeRegionsRequest(&ec2.DescribeRegionsInput{
AllRegions: aws.Bool(true),
}).Send(context.TODO())
if err != nil {
return nil, fmt.Errorf("failed to query regions: %w", err)
}
for _, r := range regionsRes.Regions {
if r.OptInStatus != nil && *r.OptInStatus == "not-opted-in" {
continue
}
if len(prefixes) == 0 {
regionMap[*r.RegionName] = struct{}{}
} else {
for _, p := range prefixes {
if strings.HasPrefix(*r.RegionName, p) {
regionMap[*r.RegionName] = struct{}{}
}
}
}
}
regions := make([]string, 0)
for k := range regionMap {
regions = append(regions, k)
}
return regions, nil
}
func List(ctx context2.AWSetsCtx, regions []string, listers []ListerName, cache Cacher) (*resource.Group, error) {
if cache == nil {
cache = NoopCache{}
}
jobs := make(chan job, 0)
rg := resource.NewGroup()
wg := &sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
go func(id int, workQueue <-chan job) {
//var j job
defer func() {
ctx.Logger.Infof("%d: finished worker\n", id)
wg.Done()
}()
//defer func() {
// if r := recover(); r != nil {
// fmt.Printf("%d: Paniced in %s - %s. Error: %v\n", id, j.region, j.kind, r)
// fmt.Printf("%d: stacktrace from panic: %v\n", id, string(debug.Stack()))
// }
//}()
for {
select {
case job, more := <-workQueue:
if !more {
return
}
//j = job
ctx.Logger.Infof("%d: processing: %s - %s\n", id, job.region, job.lister)
if cache.IsCached(ctx.AccountId, job.region, job.lister) {
ctx.Logger.Infof("%d: cached: %s - %s\n", id, job.region, job.lister)
group, err := cache.LoadGroup(job.region, job.lister)
if err != nil {
ctx.Logger.Errorf("failed to load group: %v", err)
}
rg.Merge(group)
} else {
ctx.Logger.Infof("%d: not cached: %s - %s\n", id, job.region, job.lister)
lister, err := GetByName(job.lister)
if err != nil {
ctx.Logger.Errorf("failed to get lister by name: %v", err)
continue
}
ctxcp := ctx.Copy(job.region)
group, err := lister.List(ctxcp)
if err != nil {
// indicates service is not supported in a region, likely a better way to do this though
// eks returns "AccessDenied" if the service isn't in the region though
if strings.Contains(err.Error(), "no such host") {
continue
}
ctx.Logger.Errorf("%d: failed job %s - %s\n with error: %v\n", id, job.region, job.lister, err)
continue
}
err = cache.SaveGroup(group, job.region, job.lister)
if err != nil {
ctx.Logger.Errorf("%d: failed to write cache for %s - %s: %v\n", id, job.region, job.lister, err)
}
rg.Merge(group)
}
ctx.Logger.Infof("%d: complete: %s - %s\n", id, job.region, job.lister)
}
}
}(i, jobs)
}
for _, k := range listers {
for _, r := range regions {
jobs <- job{
lister: k,
region: r,
}
}
}
close(jobs)
wg.Wait()
return rg, nil
}
type job struct {
lister ListerName
region string
}
type Cacher interface {
IsCached(account, region string, kind ListerName) bool
SaveGroup(group *resource.Group, region string, kind ListerName) error
LoadGroup(region string, kind ListerName) (*resource.Group, error)
}
type NoopCache struct {
}
func (c NoopCache) IsCached(account, region string, kind ListerName) bool {
return false
}
func (c NoopCache) SaveGroup(group *resource.Group, region string, kind ListerName) error {
return nil
}
func (c NoopCache) LoadGroup(region string, kind ListerName) (*resource.Group, error) {
return resource.NewGroup(), nil
}