sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/filter/ec2.go (about) 1 /* 2 Copyright 2018 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 filter 18 19 import ( 20 "fmt" 21 22 "github.com/aws/aws-sdk-go/aws" 23 "github.com/aws/aws-sdk-go/service/ec2" 24 25 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 26 ) 27 28 const ( 29 filterNameTagKey = "tag-key" 30 filterNameVpcID = "vpc-id" 31 filterNameState = "state" 32 filterNameVpcAttachment = "attachment.vpc-id" 33 filterAvailabilityZone = "availability-zone" 34 ) 35 36 // EC2 exposes the ec2 sdk related filters. 37 var EC2 = new(ec2Filters) 38 39 type ec2Filters struct{} 40 41 // Cluster returns a filter based on the cluster name. 42 func (ec2Filters) Cluster(clusterName string) *ec2.Filter { 43 return &ec2.Filter{ 44 Name: aws.String(filterNameTagKey), 45 Values: aws.StringSlice([]string{infrav1.ClusterTagKey(clusterName)}), 46 } 47 } 48 49 // Name returns a filter based on the resource name. 50 func (ec2Filters) Name(name string) *ec2.Filter { 51 return &ec2.Filter{ 52 Name: aws.String("tag:Name"), 53 Values: aws.StringSlice([]string{name}), 54 } 55 } 56 57 // ClusterOwned returns a filter using the Cluster API per-cluster tag where 58 // the resource is owned. 59 func (ec2Filters) ClusterOwned(clusterName string) *ec2.Filter { 60 return &ec2.Filter{ 61 Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterTagKey(clusterName))), 62 Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleOwned)}), 63 } 64 } 65 66 // ClusterShared returns a filter using the Cluster API per-cluster tag where 67 // the resource is shared. 68 func (ec2Filters) ClusterShared(clusterName string) *ec2.Filter { 69 return &ec2.Filter{ 70 Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterTagKey(clusterName))), 71 Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleShared)}), 72 } 73 } 74 75 // ProviderRole returns a filter using cluster-api-provider-aws role tag. 76 func (ec2Filters) ProviderRole(role string) *ec2.Filter { 77 return &ec2.Filter{ 78 Name: aws.String(fmt.Sprintf("tag:%s", infrav1.NameAWSClusterAPIRole)), 79 Values: aws.StringSlice([]string{role}), 80 } 81 } 82 83 // ProviderOwned returns a filter using the cloud provider tag where the resource is owned. 84 func (ec2Filters) ProviderOwned(clusterName string) *ec2.Filter { 85 return &ec2.Filter{ 86 Name: aws.String(fmt.Sprintf("tag:%s", infrav1.ClusterAWSCloudProviderTagKey(clusterName))), 87 Values: aws.StringSlice([]string{string(infrav1.ResourceLifecycleOwned)}), 88 } 89 } 90 91 // VPC returns a filter based on the id of the VPC. 92 func (ec2Filters) VPC(vpcID string) *ec2.Filter { 93 return &ec2.Filter{ 94 Name: aws.String(filterNameVpcID), 95 Values: aws.StringSlice([]string{vpcID}), 96 } 97 } 98 99 // VPCAttachment returns a filter based on the vpc id attached to the resource. 100 func (ec2Filters) VPCAttachment(vpcID string) *ec2.Filter { 101 return &ec2.Filter{ 102 Name: aws.String(filterNameVpcAttachment), 103 Values: aws.StringSlice([]string{vpcID}), 104 } 105 } 106 107 // Available returns a filter based on the state being available. 108 func (ec2Filters) Available() *ec2.Filter { 109 return &ec2.Filter{ 110 Name: aws.String(filterNameState), 111 Values: aws.StringSlice([]string{"available"}), 112 } 113 } 114 115 // NATGatewayStates returns a filter based on the list of states passed in. 116 func (ec2Filters) NATGatewayStates(states ...string) *ec2.Filter { 117 return &ec2.Filter{ 118 Name: aws.String("state"), 119 Values: aws.StringSlice(states), 120 } 121 } 122 123 // InstanceStates returns a filter based on the list of states passed in. 124 func (ec2Filters) InstanceStates(states ...string) *ec2.Filter { 125 return &ec2.Filter{ 126 Name: aws.String("instance-state-name"), 127 Values: aws.StringSlice(states), 128 } 129 } 130 131 // VPCStates returns a filter based on the list of states passed in. 132 func (ec2Filters) VPCStates(states ...string) *ec2.Filter { 133 return &ec2.Filter{ 134 Name: aws.String("state"), 135 Values: aws.StringSlice(states), 136 } 137 } 138 139 // SubnetStates returns a filter based on the list of states passed in. 140 func (ec2Filters) SubnetStates(states ...string) *ec2.Filter { 141 return &ec2.Filter{ 142 Name: aws.String("state"), 143 Values: aws.StringSlice(states), 144 } 145 } 146 147 func (ec2Filters) AvailabilityZone(zone string) *ec2.Filter { 148 return &ec2.Filter{ 149 Name: aws.String(filterAvailabilityZone), 150 Values: aws.StringSlice([]string{zone}), 151 } 152 } 153 154 func (ec2Filters) IgnoreLocalZones() *ec2.Filter { 155 return &ec2.Filter{ 156 Name: aws.String("opt-in-status"), 157 Values: aws.StringSlice([]string{"opt-in-not-required"}), 158 } 159 }