-
Notifications
You must be signed in to change notification settings - Fork 551
/
instance.go
64 lines (53 loc) · 2 KB
/
instance.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
/*
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 converters
import (
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1alpha2"
)
// SDKToInstance converts an EC2 instance type to the CAPA
// instance type.
// Note: This does not return a complete instance, as rootVolumeSize
// can not be determined via the output of EC2.DescribeInstances.
func SDKToInstance(v *ec2.Instance) *infrav1.Instance {
i := &infrav1.Instance{
ID: aws.StringValue(v.InstanceId),
State: infrav1.InstanceState(*v.State.Name),
Type: aws.StringValue(v.InstanceType),
SubnetID: aws.StringValue(v.SubnetId),
ImageID: aws.StringValue(v.ImageId),
SSHKeyName: v.KeyName,
PrivateIP: v.PrivateIpAddress,
PublicIP: v.PublicIpAddress,
ENASupport: v.EnaSupport,
EBSOptimized: v.EbsOptimized,
}
// Extract IAM Instance Profile name from ARN
// TODO: Handle this comparison more safely, perhaps by querying IAM for the
// instance profile ARN and comparing to the ARN returned by EC2
if v.IamInstanceProfile != nil && v.IamInstanceProfile.Arn != nil {
split := strings.Split(aws.StringValue(v.IamInstanceProfile.Arn), "instance-profile/")
if len(split) > 1 && split[1] != "" {
i.IAMProfile = split[1]
}
}
for _, sg := range v.SecurityGroups {
i.SecurityGroupIDs = append(i.SecurityGroupIDs, *sg.GroupId)
}
if len(v.Tags) > 0 {
i.Tags = TagsToMap(v.Tags)
}
return i
}