-
Notifications
You must be signed in to change notification settings - Fork 300
/
main.go
216 lines (200 loc) · 5.58 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/aws/aws-sdk-go/service/route53/route53iface"
)
const dryRun = false
func main() {
awsConfig := aws.NewConfig()
awsConfig.Region = aws.String("us-east-1")
awsSession := session.Must(session.NewSession())
route53client := route53.New(awsSession, awsConfig)
ec2client := ec2.New(awsSession, awsConfig)
ctx, cancel := context.WithCancel(context.Background())
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT)
go func() {
<-sigs
cancel()
}()
// List all VPCs
output, err := ec2client.DescribeVpcsWithContext(ctx, &ec2.DescribeVpcsInput{})
if err != nil {
log.Fatal(err)
}
var vpcs []string
for _, vpc := range output.Vpcs {
vpcs = append(vpcs, *vpc.VpcId)
}
// Find all zones in use by VPCs
zonesIdsInUse := make(map[string]string)
for _, vpc := range vpcs {
output, err := route53client.ListHostedZonesByVPCWithContext(ctx, &route53.ListHostedZonesByVPCInput{VPCId: aws.String(vpc), VPCRegion: aws.String("us-east-1")})
if err != nil {
log.Fatal(err)
}
for _, zoneSummary := range output.HostedZoneSummaries {
zonesIdsInUse[*zoneSummary.Name] = *zoneSummary.HostedZoneId
}
}
for name, id := range zonesIdsInUse {
log.Printf("%s %s in use", name, id)
}
examplePrivateZoneName := regexp.MustCompile("example-([a-z0-9]{5}).hypershift.local.")
publicZones := map[string]string{
"ci.hypershift.devcluster.openshift.com.": "",
"hive.hypershift.devcluster.openshift.com.": "",
}
// Delete unused private zones
err = route53client.ListHostedZonesPagesWithContext(ctx, &route53.ListHostedZonesInput{}, func(lhzo *route53.ListHostedZonesOutput, lastPage bool) bool {
for _, zone := range lhzo.HostedZones {
if zone == nil || zone.Name == nil || zone.Id == nil {
continue
}
zoneId := strings.TrimPrefix(*zone.Id, "/hostedzone/")
for zoneName := range publicZones {
if *zone.Name == zoneName {
publicZones[zoneName] = zoneId
}
}
if zone.Config == nil || zone.Config.PrivateZone == nil || !*zone.Config.PrivateZone {
continue
}
if !strings.HasSuffix(*zone.Name, ".ci.hypershift.devcluster.openshift.com.") &&
!strings.HasSuffix(*zone.Name, ".hive.hypershift.devcluster.openshift.com.") &&
!examplePrivateZoneName.MatchString(*zone.Name) {
continue
}
inUse := false
for _, zoneIdInUse := range zonesIdsInUse {
if zoneId == zoneIdInUse {
inUse = true
break
}
}
if inUse {
continue
}
log.Printf("deleting hosted zone %s with id %s", *zone.Name, *zone.Id)
deleteZone(ctx, strings.TrimSuffix(*zone.Name, "."), strings.TrimPrefix(*zone.Id, "/hostedzone/"), route53client)
}
return !lastPage
})
if err != nil {
log.Fatal(err)
}
// Delete unused api.* records from public zone
for _, publicZoneId := range publicZones {
if publicZoneId == "" {
continue
}
lrrsi := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(publicZoneId),
MaxItems: aws.String("200"),
}
output, err := route53client.ListResourceRecordSetsWithContext(ctx, lrrsi)
if err != nil {
log.Fatal(err)
}
if len(output.ResourceRecordSets) == 0 {
continue
}
var changeBatch route53.ChangeBatch
var deleteRequired bool
for _, rrs := range output.ResourceRecordSets {
if *rrs.Type != "A" {
continue
}
inUseRecord := false
for zoneNameInUse := range zonesIdsInUse {
if strings.HasSuffix(*rrs.Name, zoneNameInUse) {
inUseRecord = true
break
}
}
if inUseRecord {
continue
}
deleteRequired = true
log.Printf("deleting record %s", *rrs.Name)
changeBatch.Changes = append(changeBatch.Changes, &route53.Change{
Action: aws.String("DELETE"),
ResourceRecordSet: rrs,
})
}
if !deleteRequired {
continue
}
crrsi := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(publicZoneId),
ChangeBatch: &changeBatch,
}
if !dryRun {
_, err = route53client.ChangeResourceRecordSetsWithContext(ctx, crrsi)
if err != nil {
log.Fatal(err)
}
}
}
}
func deleteZone(ctx context.Context, zoneName string, zoneId string, client route53iface.Route53API) error {
err := deleteRecords(ctx, client, zoneId)
if err != nil {
return fmt.Errorf("failed to delete zone records")
}
if !dryRun {
_, err = client.DeleteHostedZoneWithContext(ctx, &route53.DeleteHostedZoneInput{
Id: aws.String(zoneId),
})
return err
}
return nil
}
func deleteRecords(ctx context.Context, client route53iface.Route53API, zoneId string) error {
lrrsi := &route53.ListResourceRecordSetsInput{
HostedZoneId: aws.String(zoneId),
}
output, err := client.ListResourceRecordSetsWithContext(ctx, lrrsi)
if err != nil {
return err
}
if len(output.ResourceRecordSets) == 0 {
return nil
}
var changeBatch route53.ChangeBatch
var deleteRequired bool
for _, rrs := range output.ResourceRecordSets {
if *rrs.Type != "A" && *rrs.Type != "CNAME" {
continue
}
deleteRequired = true
log.Printf("deleting record %s", *rrs.Name)
changeBatch.Changes = append(changeBatch.Changes, &route53.Change{
Action: aws.String("DELETE"),
ResourceRecordSet: rrs,
})
}
if !deleteRequired {
return nil
}
crrsi := &route53.ChangeResourceRecordSetsInput{
HostedZoneId: aws.String(zoneId),
ChangeBatch: &changeBatch,
}
if !dryRun {
_, err = client.ChangeResourceRecordSetsWithContext(ctx, crrsi)
return err
}
return nil
}