Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ec2: include attributes in instance observation, fix reconciling tags #2027

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions apis/ec2/manualv1alpha1/instances_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ type InstanceObservation struct {
// +optional
CPUOptons *CPUOptionsRequest `json:"cpuOptions,omitempty"`
// +optional
DisableAPITermination *bool `json:"disableAPITermination,omitempty"`
// +optional
EBSOptimized *bool `json:"ebsOptimized,omitempty"`
// +optional
ElasticGPUAssociations []ElasticGPUAssociation `json:"elasticGpuAssociation,omitempty"`
Expand All @@ -330,6 +332,8 @@ type InstanceObservation struct {
// +optional
InstanceID *string `json:"instanceId,omitempty"`
// +optional
InstanceInitiatedShutdownBehavior *string `json:"instanceInitiatedShutdownBehavior,omitempty"`
// +optional
InstanceLifecycle string `json:"instanceLifecyle"`
// Supported instance family when set instanceInterruptionBehavior to hibernate
// C3, C4, C5, M4, M5, R3, R4
Expand Down Expand Up @@ -382,8 +386,10 @@ type InstanceObservation struct {
// +optional
SubnetID *string `json:"subnetId,omitempty"`
// +optional
Tags []Tag `json:"tags,omitempty"`
VirtualizationType string `json:"virualizationType"`
Tags []Tag `json:"tags,omitempty"`
// +optional
UserData *string `json:"userData,omitempty"`
VirtualizationType string `json:"virualizationType"`
// +optional
VPCID *string `json:"vpcId,omitempty"`
}
Expand Down
15 changes: 15 additions & 0 deletions apis/ec2/manualv1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package/crds/ec2.aws.crossplane.io_instances.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,8 @@ spec:
- coreCount
- threadsPerCore
type: object
disableAPITermination:
type: boolean
ebs:
type: string
ebsOptimized:
Expand Down Expand Up @@ -1491,6 +1493,8 @@ spec:
type: string
instanceId:
type: string
instanceInitiatedShutdownBehavior:
type: string
instanceLifecyle:
type: string
instanceType:
Expand Down Expand Up @@ -1886,6 +1890,8 @@ spec:
- value
type: object
type: array
userData:
type: string
virualizationType:
type: string
vpcId:
Expand Down
37 changes: 36 additions & 1 deletion pkg/clients/ec2/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,44 @@ func IsInstanceUpToDate(spec manualv1alpha1.InstanceParameters, instance types.I
if pointer.StringValue(spec.UserData) != attributeValue(attributes.UserData) {
return false
}

// Tags
existingTags := map[string]string{}
for _, t := range instance.Tags {
existingTags[*t.Key] = *t.Value
}
for _, t := range spec.Tags {
// A tag is missing from the instance or the value does match the expected value.
value, ok := existingTags[t.Key]
if !ok {
return false
}
if value != t.Value {
return false
}
}

return CompareGroupIDs(spec.SecurityGroupIDs, instance.SecurityGroups)
}

// GenerateInstanceObservation is used to produce manualv1alpha1.InstanceObservation from
// a []ec2.Instance.
func GenerateInstanceObservation(i types.Instance) manualv1alpha1.InstanceObservation {
func GenerateInstanceObservation(i types.Instance, attributes *ec2.DescribeInstanceAttributeOutput) manualv1alpha1.InstanceObservation {
var disableAPITermination *bool = nil
if attributes.DisableApiTermination != nil {
disableAPITermination = attributes.DisableApiTermination.Value
}

var instanceInitiatedShutdownBehavior *string = nil
if attributes.InstanceInitiatedShutdownBehavior != nil {
instanceInitiatedShutdownBehavior = attributes.InstanceInitiatedShutdownBehavior.Value
}

var userData *string = nil
if attributes.UserData != nil {
userData = attributes.UserData.Value
}

return manualv1alpha1.InstanceObservation{
AmiLaunchIndex: i.AmiLaunchIndex,
Architecture: string(i.Architecture),
Expand All @@ -93,6 +125,7 @@ func GenerateInstanceObservation(i types.Instance) manualv1alpha1.InstanceObserv
CapacityReservationSpecification: GenerateCapacityReservationSpecResponse(i.CapacityReservationSpecification),
ClientToken: i.ClientToken,
CPUOptons: GenerateCPUOptionsRequest(i.CpuOptions),
DisableAPITermination: disableAPITermination,
EBSOptimized: i.EbsOptimized,
ElasticGPUAssociations: GenerateElasticGPUAssociation(i.ElasticGpuAssociations),
ElasticInferenceAcceleratorAssociations: GenerateElasticInferenceAcceleratorAssociation(i.ElasticInferenceAcceleratorAssociations),
Expand All @@ -102,6 +135,7 @@ func GenerateInstanceObservation(i types.Instance) manualv1alpha1.InstanceObserv
IAMInstanceProfile: GenerateIAMInstanceProfile(i.IamInstanceProfile),
ImageID: i.ImageId,
InstanceID: i.InstanceId,
InstanceInitiatedShutdownBehavior: instanceInitiatedShutdownBehavior,
InstanceLifecycle: string(i.InstanceLifecycle),
InstanceType: string(i.InstanceType),
KernelID: i.KernelId,
Expand Down Expand Up @@ -130,6 +164,7 @@ func GenerateInstanceObservation(i types.Instance) manualv1alpha1.InstanceObserv
StateTransitionReason: i.StateTransitionReason,
SubnetID: i.SubnetId,
Tags: GenerateTags(i.Tags),
UserData: userData,
VirtualizationType: string(i.VirtualizationType),
VPCID: i.VpcId,
}
Expand Down
136 changes: 74 additions & 62 deletions pkg/clients/ec2/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,58 +32,59 @@ const (
managedKind = "instance.ec2.aws.crossplane.io"
managedProviderConfig = "example"

arch = "x86_64"
assocID = "assocId"
assocState = "assocState"
attachmentID = "attachId"
blockDeviceName = "/dev/xvda"
capacityReservationID = "capResId"
clientToken = "clientToken"
cpuCredits = "cpuCredits"
description = "desc"
elasticInferAccARN = "inferArn"
elasticInferAccID = "inferId"
elasticInferAccState = "inferState"
gpuID = "gpuId"
gpuType = "gpuType"
groupID = "groupId"
groupName = "groupName"
hostID = "hostId"
hostGroupARN = "hostGroupArn"
iamARN = "iamArn"
iamID = "iamId"
imageID = "imageId"
interfaceType = "intType"
ipOwnerID = "ipOwnerId"
ipv6Address = "ipv6Address"
ipv6Prefix = "ipv6Prefix"
kernelID = "kernelId"
keyName = "keyName"
launchTemplateID = "launchTemplateId"
launchTemplateName = "launchTemplateName"
licenseConfig = "licenseConfig"
macAddress = "macAddress"
outpostARN = "outpostArn"
placementAff = "affinity"
privateDNSName = "privDnsName"
privateIPAddress = "privIpAddress"
productCodeID = "productCodeId"
publicDNSName = "publicDnsName"
publicIPAddress = "publicIp"
ramDiskID = "ramDiskId"
rootDeviceName = "rootDeviceName"
snapshotID = "snapshotId"
spotInstanceReqID = "spotInstacneId"
spotMarketType = "spotMarketType"
spreadDomain = "spreadDomain"
sriovNetSupport = "sriovNetSupport"
stateReason = "stateReason"
tagResourceType = "instance"
tagsKey = "key"
tagsVal = "value"
userData = "userData"
volumeID = "volId"
volumeType = "gp2"
arch = "x86_64"
assocID = "assocId"
assocState = "assocState"
attachmentID = "attachId"
blockDeviceName = "/dev/xvda"
capacityReservationID = "capResId"
clientToken = "clientToken"
cpuCredits = "cpuCredits"
description = "desc"
elasticInferAccARN = "inferArn"
elasticInferAccID = "inferId"
elasticInferAccState = "inferState"
gpuID = "gpuId"
gpuType = "gpuType"
groupID = "groupId"
groupName = "groupName"
hostID = "hostId"
hostGroupARN = "hostGroupArn"
iamARN = "iamArn"
iamID = "iamId"
imageID = "imageId"
instanceInitiatedShutdownBehavior = "stop"
interfaceType = "intType"
ipOwnerID = "ipOwnerId"
ipv6Address = "ipv6Address"
ipv6Prefix = "ipv6Prefix"
kernelID = "kernelId"
keyName = "keyName"
launchTemplateID = "launchTemplateId"
launchTemplateName = "launchTemplateName"
licenseConfig = "licenseConfig"
macAddress = "macAddress"
outpostARN = "outpostArn"
placementAff = "affinity"
privateDNSName = "privDnsName"
privateIPAddress = "privIpAddress"
productCodeID = "productCodeId"
publicDNSName = "publicDnsName"
publicIPAddress = "publicIp"
ramDiskID = "ramDiskId"
rootDeviceName = "rootDeviceName"
snapshotID = "snapshotId"
spotInstanceReqID = "spotInstacneId"
spotMarketType = "spotMarketType"
spreadDomain = "spreadDomain"
sriovNetSupport = "sriovNetSupport"
stateReason = "stateReason"
tagResourceType = "instance"
tagsKey = "key"
tagsVal = "value"
userData = "userData"
volumeID = "volId"
volumeType = "gp2"
)

func TestGenerateInstanceConditions(t *testing.T) {
Expand Down Expand Up @@ -195,8 +196,9 @@ func TestGenerateDescribeInstancesByExternalTags(t *testing.T) {

func TestGenerateInstanceObservation(t *testing.T) {
cases := map[string]struct {
in types.Instance
out manualv1alpha1.InstanceObservation
in types.Instance
attributes ec2.DescribeInstanceAttributeOutput
out manualv1alpha1.InstanceObservation
}{
"AllFilled": {
in: types.Instance{
Expand Down Expand Up @@ -368,6 +370,13 @@ func TestGenerateInstanceObservation(t *testing.T) {
VirtualizationType: types.VirtualizationTypeHvm,
VpcId: aws.String(vpcID),
},
attributes: ec2.DescribeInstanceAttributeOutput{
DisableApiTermination: &types.AttributeBooleanValue{Value: aws.Bool(true)},
InstanceInitiatedShutdownBehavior: &types.AttributeValue{Value: aws.String(instanceInitiatedShutdownBehavior)},
KernelId: &types.AttributeValue{Value: aws.String(kernelID)},
RamdiskId: &types.AttributeValue{Value: aws.String(ramDiskID)},
UserData: &types.AttributeValue{Value: aws.String(userData)},
},
out: manualv1alpha1.InstanceObservation{
AmiLaunchIndex: aws.Int32(0),
Architecture: arch,
Expand All @@ -394,8 +403,9 @@ func TestGenerateInstanceObservation(t *testing.T) {
CoreCount: aws.Int32(1),
ThreadsPerCore: aws.Int32(1),
},
EBSOptimized: aws.Bool(false),
EnaSupport: aws.Bool(false),
DisableAPITermination: aws.Bool(true),
EBSOptimized: aws.Bool(false),
EnaSupport: aws.Bool(false),
ElasticGPUAssociations: []manualv1alpha1.ElasticGPUAssociation{
{
ElasticGPUAssociationID: aws.String(assocID),
Expand All @@ -420,11 +430,12 @@ func TestGenerateInstanceObservation(t *testing.T) {
ARN: aws.String(iamARN),
ID: aws.String(iamID),
},
ImageID: aws.String(imageID),
InstanceID: aws.String(instanceID),
InstanceLifecycle: string(types.InstanceLifecycleTypeScheduled),
InstanceType: string(types.InstanceTypeM1Small),
KernelID: aws.String(kernelID),
ImageID: aws.String(imageID),
InstanceID: aws.String(instanceID),
InstanceInitiatedShutdownBehavior: aws.String(instanceInitiatedShutdownBehavior),
InstanceLifecycle: string(types.InstanceLifecycleTypeScheduled),
InstanceType: string(types.InstanceTypeM1Small),
KernelID: aws.String(kernelID),
Licenses: []manualv1alpha1.LicenseConfigurationRequest{
{
LicenseConfigurationARN: aws.String(licenseConfig),
Expand Down Expand Up @@ -530,6 +541,7 @@ func TestGenerateInstanceObservation(t *testing.T) {
Value: tagsVal,
},
},
UserData: aws.String(userData),
VirtualizationType: string(types.VirtualizationTypeHvm),
VPCID: aws.String(vpcID),
},
Expand All @@ -538,7 +550,7 @@ func TestGenerateInstanceObservation(t *testing.T) {

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
r := GenerateInstanceObservation(tc.in)
r := GenerateInstanceObservation(tc.in, &tc.attributes)
if diff := cmp.Diff(tc.out, r); diff != "" {
t.Errorf("GenerateInstanceObservation(...): -want, +got:\n%s", diff)
}
Expand Down
Loading
Loading