-
Notifications
You must be signed in to change notification settings - Fork 560
/
ec2.go
158 lines (134 loc) · 4.73 KB
/
ec2.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
/*
Copyright 2018 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 filter
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha3"
)
const (
filterNameTagKey = "tag-key"
filterNameVpcID = "vpc-id"
filterNameState = "state"
filterNameVpcAttachment = "attachment.vpc-id"
filterAvailabilityZone = "availability-zone"
)
// EC2 exposes the ec2 sdk related filters.
var EC2 = new(ec2Filters)
type ec2Filters struct{}
// Cluster returns a filter based on the cluster name.
func (ec2Filters) Cluster(clusterName string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(filterNameTagKey),
Values: aws.StringSlice([]string{infrav1.ClusterTagKey(clusterName)}),
}
}
// Name returns a filter based on the resource name.
func (ec2Filters) Name(name string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("tag:Name"),
Values: aws.StringSlice([]string{name}),
}
}
// ClusterOwned returns a filter using the Cluster API per-cluster tag where
// the resource is owned
func (ec2Filters) ClusterOwned(clusterName string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterTagKey(clusterName))),
Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleOwned)}),
}
}
// ClusterShared returns a filter using the Cluster API per-cluster tag where
// the resource is shared.
func (ec2Filters) ClusterShared(clusterName string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterTagKey(clusterName))),
Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleShared)}),
}
}
// ProviderRole returns a filter using cluster-api-provider-aws role tag.
func (ec2Filters) ProviderRole(role string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", infrav1.NameAWSClusterAPIRole)),
Values: aws.StringSlice([]string{role}),
}
}
// ProviderOwned returns a filter using the cloud provider tag where the resource is owned.
func (ec2Filters) ProviderOwned(clusterName string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterAWSCloudProviderTagKey(clusterName))),
Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleOwned)}),
}
}
// VPC returns a filter based on the id of the VPC.
func (ec2Filters) VPC(vpcID string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(filterNameVpcID),
Values: aws.StringSlice([]string{vpcID}),
}
}
// VPCAttachment returns a filter based on the vpc id attached to the resource.
func (ec2Filters) VPCAttachment(vpcID string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(filterNameVpcAttachment),
Values: aws.StringSlice([]string{vpcID}),
}
}
// Available returns a filter based on the state being available.
func (ec2Filters) Available() *ec2.Filter {
return &ec2.Filter{
Name: aws.String(filterNameState),
Values: aws.StringSlice([]string{"available"}),
}
}
// NATGatewayStates returns a filter based on the list of states passed in.
func (ec2Filters) NATGatewayStates(states ...string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("state"),
Values: aws.StringSlice(states),
}
}
// InstanceStates returns a filter based on the list of states passed in.
func (ec2Filters) InstanceStates(states ...string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("instance-state-name"),
Values: aws.StringSlice(states),
}
}
// VPCStates returns a filter based on the list of states passed in.
func (ec2Filters) VPCStates(states ...string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("state"),
Values: aws.StringSlice(states),
}
}
// SubnetStates returns a filter based on the list of states passed in.
func (ec2Filters) SubnetStates(states ...string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String("state"),
Values: aws.StringSlice(states),
}
}
func (ec2Filters) AvailabilityZone(zone string) *ec2.Filter {
return &ec2.Filter{
Name: aws.String(filterAvailabilityZone),
Values: aws.StringSlice([]string{zone}),
}
}
func (ec2Filters) IgnoreLocalZones() *ec2.Filter {
return &ec2.Filter{
Name: aws.String("opt-in-status"),
Values: aws.StringSlice([]string{"opt-in-not-required"}),
}
}