Skip to content

Commit f514627

Browse files
rvanderp3faermanj
andcommitted
add dedicated hosts pruning to aws-janitor
Co-authored-by: Julio Faerman <[email protected]>
1 parent 8b694e7 commit f514627

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package resources
18+
19+
import (
20+
"fmt"
21+
"strings"
22+
"time"
23+
24+
"github.com/aws/aws-sdk-go/aws"
25+
"github.com/aws/aws-sdk-go/service/ec2"
26+
"github.com/pkg/errors"
27+
28+
"github.com/sirupsen/logrus"
29+
)
30+
31+
// Dedicated Hosts: https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#EC2.DescribeDedicatedHosts
32+
33+
type DedicatedHosts struct{}
34+
35+
func (DedicatedHosts) MarkAndSweep(opts Options, set *Set) error {
36+
logger := logrus.WithField("options", opts)
37+
svc := ec2.New(opts.Session, aws.NewConfig().WithRegion(opts.Region))
38+
39+
inp := &ec2.DescribeHostsInput{}
40+
41+
var toDelete []*string // Paged call, defer deletion until we have the whole list.
42+
43+
pageFunc := func(page *ec2.DescribeHostsOutput, _ bool) bool {
44+
for _, _host := range page.Hosts {
45+
h := &host{
46+
Account: opts.Account,
47+
Region: opts.Region,
48+
HostID: *_host.HostId,
49+
}
50+
tags := fromEC2Tags(_host.Tags)
51+
52+
if !set.Mark(opts, h, _host.AllocationTime, tags) {
53+
continue
54+
}
55+
56+
logger.Warningf("%s: deleting %T: %s (%s)", h.ARN(), _host, h.HostID, tags[NameTagKey])
57+
if !opts.DryRun {
58+
toDelete = append(toDelete, _host.HostId)
59+
}
60+
}
61+
return true
62+
}
63+
64+
if err := svc.DescribeHostsPages(inp, pageFunc); err != nil {
65+
return err
66+
}
67+
68+
if len(toDelete) > 0 {
69+
if _, err := svc.ReleaseHosts(&ec2.ReleaseHostsInput{HostIds: toDelete}); err != nil {
70+
logger.Warningf("Release failed for Hosts: %s : %v", strings.Join(aws.StringValueSlice(toDelete), ", "), err)
71+
}
72+
}
73+
return nil
74+
}
75+
76+
func (DedicatedHosts) ListAll(opts Options) (*Set, error) {
77+
svc := ec2.New(opts.Session, aws.NewConfig().WithRegion(opts.Region))
78+
set := NewSet(0)
79+
inp := &ec2.DescribeHostsInput{}
80+
81+
err := svc.DescribeHostsPages(inp, func(Hosts *ec2.DescribeHostsOutput, _ bool) bool {
82+
for _, _host := range Hosts.Hosts {
83+
now := time.Now()
84+
arn := instance{
85+
Account: opts.Account,
86+
Region: opts.Region,
87+
InstanceID: *_host.HostId,
88+
}.ARN()
89+
90+
set.firstSeen[arn] = now
91+
}
92+
return true
93+
})
94+
return set, errors.Wrapf(err, "couldn't describe DedicatedHosts for %q in %q", opts.Account, opts.Region)
95+
}
96+
97+
type host struct {
98+
Account string
99+
Region string
100+
HostID string
101+
}
102+
103+
func (h host) ARN() string {
104+
return fmt.Sprintf("arn:aws:ec2:%s:%s:host/%s", h.Region, h.Account, h.HostID)
105+
}
106+
107+
func (h host) ResourceKey() string {
108+
return h.ARN()
109+
}

aws-janitor/resources/list.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ var RegionalTypeList = []Type{
104104
TargetGroups{},
105105
KeyPairs{},
106106
S3Bucket{},
107+
DedicatedHosts{},
107108
}
108109

109110
// Non-regional AWS resource types, in dependency order

0 commit comments

Comments
 (0)