generated from kubernetes/kubernetes-template-project
/
launch_configs.go
103 lines (82 loc) · 3 KB
/
launch_configs.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
/*
Copyright 2019 The Kubernetes Authors.
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 resources
import (
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// LaunchConfigurations: http://docs.aws.amazon.com/sdk-for-go/api/service/autoscaling/#AutoScaling.DescribeLaunchConfigurations
type LaunchConfigurations struct{}
func (LaunchConfigurations) MarkAndSweep(opts Options, set *Set) error {
logger := logrus.WithField("options", opts)
svc := autoscaling.New(opts.Session, aws.NewConfig().WithRegion(opts.Region))
var toDelete []*launchConfiguration // Paged call, defer deletion until we have the whole list.
pageFunc := func(page *autoscaling.DescribeLaunchConfigurationsOutput, _ bool) bool {
for _, lc := range page.LaunchConfigurations {
l := &launchConfiguration{
arn: aws.StringValue(lc.LaunchConfigurationARN),
name: aws.StringValue(lc.LaunchConfigurationName),
}
// No tags?
if set.Mark(opts, l, lc.CreatedTime, nil) {
logger.Warningf("%s: deleting %T: %s", l.ARN(), lc, l.name)
if !opts.DryRun {
toDelete = append(toDelete, l)
}
}
}
return true
}
if err := svc.DescribeLaunchConfigurationsPages(&autoscaling.DescribeLaunchConfigurationsInput{}, pageFunc); err != nil {
return err
}
for _, lc := range toDelete {
deleteReq := &autoscaling.DeleteLaunchConfigurationInput{
LaunchConfigurationName: aws.String(lc.name),
}
if _, err := svc.DeleteLaunchConfiguration(deleteReq); err != nil {
logger.Warningf("%s: delete failed: %v", lc.ARN(), err)
}
}
return nil
}
func (LaunchConfigurations) ListAll(opts Options) (*Set, error) {
c := autoscaling.New(opts.Session, aws.NewConfig().WithRegion(opts.Region))
set := NewSet(0)
input := &autoscaling.DescribeLaunchConfigurationsInput{}
err := c.DescribeLaunchConfigurationsPages(input, func(lcs *autoscaling.DescribeLaunchConfigurationsOutput, isLast bool) bool {
now := time.Now()
for _, lc := range lcs.LaunchConfigurations {
arn := launchConfiguration{
arn: aws.StringValue(lc.LaunchConfigurationARN),
name: aws.StringValue(lc.LaunchConfigurationName),
}.ARN()
set.firstSeen[arn] = now
}
return true
})
return set, errors.Wrapf(err, "couldn't list launch configurations for %q in %q", opts.Account, opts.Region)
}
type launchConfiguration struct {
arn string
name string
}
func (lc launchConfiguration) ARN() string {
return lc.arn
}
func (lc launchConfiguration) ResourceKey() string {
return lc.ARN()
}