-
Notifications
You must be signed in to change notification settings - Fork 832
/
securitygroup.go
156 lines (136 loc) · 3.79 KB
/
securitygroup.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
/*
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 resourcetypes
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/samber/lo"
"go.uber.org/multierr"
"golang.org/x/exp/slices"
)
type SecurityGroup struct {
ec2Client *ec2.Client
}
func NewSecurityGroup(ec2Client *ec2.Client) *SecurityGroup {
return &SecurityGroup{ec2Client: ec2Client}
}
func (sg *SecurityGroup) String() string {
return "SecurityGroup"
}
func (sg *SecurityGroup) Global() bool {
return false
}
func (sg *SecurityGroup) GetExpired(ctx context.Context, expirationTime time.Time, excludedClusters []string) (ids []string, err error) {
var nextToken *string
for {
out, err := sg.ec2Client.DescribeSecurityGroups(ctx, &ec2.DescribeSecurityGroupsInput{
Filters: []ec2types.Filter{
{
Name: lo.ToPtr("group-name"),
Values: []string{"security-group-drift"},
},
},
NextToken: nextToken,
})
if err != nil {
return ids, err
}
for _, sgroup := range out.SecurityGroups {
clusterName, found := lo.Find(sgroup.Tags, func(tag ec2types.Tag) bool {
return *tag.Key == karpenterTestingTag
})
if found && slices.Contains(excludedClusters, lo.FromPtr(clusterName.Value)) {
continue
}
creationDate, found := lo.Find(sgroup.Tags, func(tag ec2types.Tag) bool {
return *tag.Key == "creation-date"
})
if !found {
continue
}
time, err := time.Parse(time.RFC3339, *creationDate.Value)
if err != nil {
continue
}
if time.Before(expirationTime) {
ids = append(ids, lo.FromPtr(sgroup.GroupId))
}
}
nextToken = out.NextToken
if nextToken == nil {
break
}
}
return ids, err
}
func (sg *SecurityGroup) CountAll(ctx context.Context) (count int, err error) {
var nextToken *string
for {
out, err := sg.ec2Client.DescribeSecurityGroups(ctx, &ec2.DescribeSecurityGroupsInput{
NextToken: nextToken,
})
if err != nil {
return count, err
}
count += len(out.SecurityGroups)
nextToken = out.NextToken
if nextToken == nil {
break
}
}
return count, err
}
func (sg *SecurityGroup) Get(ctx context.Context, clusterName string) (ids []string, err error) {
var nextToken *string
for {
out, err := sg.ec2Client.DescribeSecurityGroups(ctx, &ec2.DescribeSecurityGroupsInput{
Filters: []ec2types.Filter{
{
Name: lo.ToPtr("tag:" + karpenterSecurityGroupTag),
Values: []string{clusterName},
},
},
NextToken: nextToken,
})
if err != nil {
return ids, err
}
for _, sgroup := range out.SecurityGroups {
ids = append(ids, lo.FromPtr(sgroup.GroupId))
}
nextToken = out.NextToken
if nextToken == nil {
break
}
}
return ids, err
}
// Cleanup any old security groups that were provisioned as part of testing
// We execute these in serial since we will most likely get rate limited if we try to delete these too aggressively
func (sg *SecurityGroup) Cleanup(ctx context.Context, ids []string) ([]string, error) {
var deleted []string
var errs error
for i := range ids {
_, err := sg.ec2Client.DeleteSecurityGroup(ctx, &ec2.DeleteSecurityGroupInput{
GroupId: aws.String(ids[i]),
})
if err != nil {
errs = multierr.Append(errs, err)
continue
}
deleted = append(deleted, ids[i])
}
return deleted, errs
}