|
| 1 | +package awscontrol |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 7 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "go.uber.org/zap" |
| 11 | +) |
| 12 | + |
| 13 | +type PrivateEndpointsController struct { |
| 14 | + Logger *zap.Logger |
| 15 | + Region string |
| 16 | + Credentials aws.Credentials |
| 17 | +} |
| 18 | + |
| 19 | +func (c *PrivateEndpointsController) ec2Client() *ec2.Client { |
| 20 | + return ec2.New(ec2.Options{ |
| 21 | + Region: c.Region, |
| 22 | + Credentials: aws.CredentialsProviderFunc(func(ctx context.Context) (aws.Credentials, error) { |
| 23 | + return c.Credentials, nil |
| 24 | + }), |
| 25 | + }) |
| 26 | +} |
| 27 | + |
| 28 | +type CreateVPCEndpointOptions struct { |
| 29 | + ClusterID string |
| 30 | + ServiceName string |
| 31 | + InstanceID string |
| 32 | +} |
| 33 | + |
| 34 | +type CreateVPCEndpointResult struct { |
| 35 | + EndpointID string |
| 36 | +} |
| 37 | + |
| 38 | +func (c *PrivateEndpointsController) CreateVPCEndpoint(ctx context.Context, opts *CreateVPCEndpointOptions) (*CreateVPCEndpointResult, error) { |
| 39 | + ec2Client := c.ec2Client() |
| 40 | + |
| 41 | + describeResp, err := ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{ |
| 42 | + InstanceIds: []string{opts.InstanceID}, |
| 43 | + }) |
| 44 | + if err != nil { |
| 45 | + return nil, errors.Wrap(err, "failed to describe local instance") |
| 46 | + } |
| 47 | + |
| 48 | + var instances []types.Instance |
| 49 | + for _, reservation := range describeResp.Reservations { |
| 50 | + instances = append(instances, reservation.Instances...) |
| 51 | + } |
| 52 | + |
| 53 | + if len(instances) == 0 { |
| 54 | + return nil, errors.Wrap(err, "failed to find local instance") |
| 55 | + } |
| 56 | + |
| 57 | + instance := instances[0] |
| 58 | + vpcID := *instance.VpcId |
| 59 | + subnetID := *instance.SubnetId |
| 60 | + |
| 61 | + vpcEpResp, err := ec2Client.CreateVpcEndpoint(ctx, &ec2.CreateVpcEndpointInput{ |
| 62 | + ServiceName: aws.String(opts.ServiceName), |
| 63 | + VpcId: aws.String(vpcID), |
| 64 | + SubnetIds: []string{subnetID}, |
| 65 | + VpcEndpointType: types.VpcEndpointTypeInterface, |
| 66 | + TagSpecifications: []types.TagSpecification{ |
| 67 | + { |
| 68 | + ResourceType: types.ResourceTypeVpcEndpoint, |
| 69 | + Tags: []types.Tag{ |
| 70 | + { |
| 71 | + Key: aws.String("Name"), |
| 72 | + Value: aws.String("cbdc2_" + opts.ClusterID), |
| 73 | + }, |
| 74 | + { |
| 75 | + Key: aws.String("Cbdc2ClusterId"), |
| 76 | + Value: aws.String(opts.ClusterID), |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | + if err != nil { |
| 83 | + return nil, errors.Wrap(err, "failed to create vpc endpoint") |
| 84 | + } |
| 85 | + |
| 86 | + return &CreateVPCEndpointResult{ |
| 87 | + EndpointID: *vpcEpResp.VpcEndpoint.VpcEndpointId, |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +func (c *PrivateEndpointsController) EnableVPCEndpointPrivateDNS(ctx context.Context, vpceID string) error { |
| 92 | + ec2Client := c.ec2Client() |
| 93 | + |
| 94 | + _, err := ec2Client.ModifyVpcEndpoint(ctx, &ec2.ModifyVpcEndpointInput{ |
| 95 | + VpcEndpointId: aws.String(vpceID), |
| 96 | + PrivateDnsEnabled: aws.Bool(true), |
| 97 | + }) |
| 98 | + if err != nil { |
| 99 | + return errors.Wrap(err, "failed to modify vpc endpoint") |
| 100 | + } |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func (c *PrivateEndpointsController) Cleanup(ctx context.Context) error { |
| 106 | + ec2Client := c.ec2Client() |
| 107 | + |
| 108 | + endpoints, err := ec2Client.DescribeVpcEndpoints(ctx, &ec2.DescribeVpcEndpointsInput{}) |
| 109 | + if err != nil { |
| 110 | + return errors.Wrap(err, "failed to list vpc endpoints") |
| 111 | + } |
| 112 | + |
| 113 | + var endpointIdsToRemove []string |
| 114 | + |
| 115 | + for _, endpoint := range endpoints.VpcEndpoints { |
| 116 | + hasTag := func(tagName string) bool { |
| 117 | + for _, tag := range endpoint.Tags { |
| 118 | + if *tag.Key == tagName { |
| 119 | + return true |
| 120 | + } |
| 121 | + } |
| 122 | + return false |
| 123 | + } |
| 124 | + |
| 125 | + if !hasTag("Cbdc2ClusterId") { |
| 126 | + continue |
| 127 | + } |
| 128 | + |
| 129 | + if endpoint.State != "rejected" && endpoint.State != "failed" { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + endpointIdsToRemove = append(endpointIdsToRemove, *endpoint.VpcEndpointId) |
| 134 | + } |
| 135 | + |
| 136 | + c.Logger.Info("found vpc endpoints to remove", zap.Strings("endpoint-ids", endpointIdsToRemove)) |
| 137 | + |
| 138 | + if len(endpointIdsToRemove) > 0 { |
| 139 | + _, err = ec2Client.DeleteVpcEndpoints(ctx, &ec2.DeleteVpcEndpointsInput{ |
| 140 | + VpcEndpointIds: endpointIdsToRemove, |
| 141 | + }) |
| 142 | + if err != nil { |
| 143 | + return errors.Wrap(err, "failed to remove endpoints") |
| 144 | + } |
| 145 | + |
| 146 | + c.Logger.Info("removed endpoints") |
| 147 | + } |
| 148 | + |
| 149 | + return nil |
| 150 | +} |
0 commit comments