Skip to content

Commit b339737

Browse files
committed
Added Capella support and configurations.
1 parent 0ad2110 commit b339737

41 files changed

Lines changed: 3584 additions & 68 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package awscontrol
2+
3+
import (
4+
"context"
5+
6+
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
7+
"github.com/pkg/errors"
8+
"go.uber.org/zap"
9+
)
10+
11+
type LocalInstanceController struct {
12+
Logger *zap.Logger
13+
}
14+
15+
type LocalInstanceInfo struct {
16+
Region string
17+
InstanceID string
18+
}
19+
20+
func (c *LocalInstanceController) Identify(ctx context.Context) (*LocalInstanceInfo, error) {
21+
imdsClient := imds.New(imds.Options{})
22+
23+
instanceIdentity, err := imdsClient.GetInstanceIdentityDocument(ctx, nil)
24+
if err != nil {
25+
if errors.Is(err, context.DeadlineExceeded) {
26+
return nil, errors.New("must be running within an ec2 instance")
27+
}
28+
29+
return nil, errors.Wrap(err, "failed to load instance identity data")
30+
}
31+
32+
c.Logger.Info("instance identity loaded", zap.Any("identity", instanceIdentity))
33+
34+
return &LocalInstanceInfo{
35+
Region: instanceIdentity.Region,
36+
InstanceID: instanceIdentity.InstanceID,
37+
}, nil
38+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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

Comments
 (0)