-
Notifications
You must be signed in to change notification settings - Fork 832
/
instanceprofile.go
144 lines (122 loc) · 4.81 KB
/
instanceprofile.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
/*
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/config"
"github.com/aws/aws-sdk-go-v2/service/iam"
iamtypes "github.com/aws/aws-sdk-go-v2/service/iam/types"
"github.com/samber/lo"
"go.uber.org/multierr"
"golang.org/x/exp/slices"
v1 "k8s.io/api/core/v1"
)
type InstanceProfile struct {
iamClient *iam.Client
}
func NewInstanceProfile(iamClient *iam.Client) *InstanceProfile {
return &InstanceProfile{iamClient: iamClient}
}
func (ip *InstanceProfile) String() string {
return "InstanceProfile"
}
func (ip *InstanceProfile) Global() bool {
return true
}
func (ip *InstanceProfile) GetExpired(ctx context.Context, expirationTime time.Time, excludedClusters []string) (names []string, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
if err != nil {
return names, err
}
errs := make([]error, len(out.InstanceProfiles))
for i := range out.InstanceProfiles {
profiles, err := ip.iamClient.ListInstanceProfileTags(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: out.InstanceProfiles[i].InstanceProfileName,
})
if err != nil {
errs[i] = err
continue
}
clusterName, _ := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == karpenterTestingTag
})
// Checking to make sure we are only list resources in the given region
region, _ := lo.Find(profiles.Tags, func(tag iamtypes.Tag) bool {
return lo.FromPtr(tag.Key) == v1.LabelTopologyZone
})
if slices.Contains(excludedClusters, lo.FromPtr(clusterName.Value)) || lo.FromPtr(region.Value) != lo.Must(config.LoadDefaultConfig(ctx)).Region {
continue
}
for _, t := range profiles.Tags {
// Since we can only get the date of the instance profile (not the exact time the instance profile was created)
// we add a day to the time that it was created to account for the worst-case of the instance profile being created
// at 23:59:59 and being marked with a time of 00:00:00 due to only capturing the date and not the time
if lo.FromPtr(t.Key) == karpenterTestingTag && out.InstanceProfiles[i].CreateDate.Add(time.Hour*24).Before(expirationTime) {
names = append(names, lo.FromPtr(out.InstanceProfiles[i].InstanceProfileName))
}
}
}
return names, multierr.Combine(errs...)
}
func (ip *InstanceProfile) CountAll(ctx context.Context) (count int, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
if err != nil {
return count, err
}
return len(out.InstanceProfiles), nil
}
func (ip *InstanceProfile) Get(ctx context.Context, clusterName string) (names []string, err error) {
out, err := ip.iamClient.ListInstanceProfiles(ctx, &iam.ListInstanceProfilesInput{})
if err != nil {
return names, err
}
errs := make([]error, len(out.InstanceProfiles))
for i := range out.InstanceProfiles {
profiles, err := ip.iamClient.ListInstanceProfileTags(ctx, &iam.ListInstanceProfileTagsInput{
InstanceProfileName: out.InstanceProfiles[i].InstanceProfileName,
})
if err != nil {
errs[i] = err
continue
}
for _, t := range profiles.Tags {
if lo.FromPtr(t.Key) == karpenterTestingTag && lo.FromPtr(t.Value) == clusterName {
names = append(names, lo.FromPtr(out.InstanceProfiles[i].InstanceProfileName))
}
}
}
return names, multierr.Combine(errs...)
}
// Cleanup any old instance profiles that were managed by Karpenter or 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 (ip *InstanceProfile) Cleanup(ctx context.Context, names []string) ([]string, error) {
var deleted []string
var errs error
for i := range names {
out, _ := ip.iamClient.GetInstanceProfile(ctx, &iam.GetInstanceProfileInput{InstanceProfileName: lo.ToPtr(names[i])})
if len(out.InstanceProfile.Roles) != 0 {
_, _ = ip.iamClient.RemoveRoleFromInstanceProfile(ctx, &iam.RemoveRoleFromInstanceProfileInput{
InstanceProfileName: lo.ToPtr(names[i]),
RoleName: out.InstanceProfile.Roles[0].RoleName,
})
}
_, err := ip.iamClient.DeleteInstanceProfile(ctx, &iam.DeleteInstanceProfileInput{
InstanceProfileName: lo.ToPtr(names[i]),
})
if err != nil {
errs = multierr.Append(errs, err)
}
deleted = append(deleted, names[i])
}
return deleted, errs
}