Skip to content

Commit

Permalink
Merge pull request #9520 from rifelpet/cloudmock-cleanup
Browse files Browse the repository at this point in the history
Cloudmock cleanup - preparation for EC2 tag-on-create
  • Loading branch information
k8s-ci-robot authored Jul 8, 2020
2 parents ab1b5fc + b1e7704 commit 856d581
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 53 deletions.
6 changes: 5 additions & 1 deletion cloudmock/aws/mockec2/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ type MockEC2 struct {

InternetGateways map[string]*ec2.InternetGateway

LaunchTemplates map[string]*ec2.ResponseLaunchTemplateData
launchTemplateNumber int
LaunchTemplates map[string]*launchTemplateInfo

NatGateways map[string]*ec2.NatGateway

Expand Down Expand Up @@ -98,6 +99,9 @@ func (m *MockEC2) All() map[string]interface{} {
for id, o := range m.InternetGateways {
all[id] = o
}
for id, o := range m.LaunchTemplates {
all[id] = o
}
for id, o := range m.NatGateways {
all[id] = o
}
Expand Down
5 changes: 4 additions & 1 deletion cloudmock/aws/mockec2/dhcpoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ func (m *MockEC2) CreateDhcpOptions(request *ec2.CreateDhcpOptionsInput) (*ec2.C
}

n := len(m.DhcpOptions) + 1
id := fmt.Sprintf("dopt-%d", n)

dhcpOptions := &ec2.DhcpOptions{
DhcpOptionsId: s(fmt.Sprintf("dopt-%d", n)),
DhcpOptionsId: s(id),
}

for _, o := range request.DhcpConfigurations {
Expand All @@ -147,6 +148,8 @@ func (m *MockEC2) CreateDhcpOptions(request *ec2.CreateDhcpOptionsInput) (*ec2.C
}
m.DhcpOptions[*dhcpOptions.DhcpOptionsId] = dhcpOptions

m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeDhcpOptions)...)

copy := *dhcpOptions
copy.Tags = m.getTags(ec2.ResourceTypeDhcpOptions, *dhcpOptions.DhcpOptionsId)
return &ec2.CreateDhcpOptionsOutput{DhcpOptions: &copy}, nil
Expand Down
4 changes: 4 additions & 0 deletions cloudmock/aws/mockec2/internetgateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,20 @@ func (m *MockEC2) CreateInternetGateway(request *ec2.CreateInternetGatewayInput)
klog.Infof("CreateInternetGateway: %v", request)

id := m.allocateId("igw")
tags := tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeInternetGateway)

igw := &ec2.InternetGateway{
InternetGatewayId: s(id),
Tags: tags,
}

if m.InternetGateways == nil {
m.InternetGateways = make(map[string]*ec2.InternetGateway)
}
m.InternetGateways[id] = igw

m.addTags(id, tags...)

response := &ec2.CreateInternetGatewayOutput{
InternetGateway: igw,
}
Expand Down
53 changes: 35 additions & 18 deletions cloudmock/aws/mockec2/launch_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
)

type launchTemplateInfo struct {
data *ec2.ResponseLaunchTemplateData
name *string
}

// DescribeLaunchTemplates mocks the describing the launch templates
func (m *MockEC2) DescribeLaunchTemplates(request *ec2.DescribeLaunchTemplatesInput) (*ec2.DescribeLaunchTemplatesOutput, error) {
m.mutex.Lock()
Expand All @@ -34,9 +39,9 @@ func (m *MockEC2) DescribeLaunchTemplates(request *ec2.DescribeLaunchTemplatesIn
return o, nil
}

for name := range m.LaunchTemplates {
for _, ltInfo := range m.LaunchTemplates {
o.LaunchTemplates = append(o.LaunchTemplates, &ec2.LaunchTemplate{
LaunchTemplateName: aws.String(name),
LaunchTemplateName: ltInfo.name,
})
}

Expand All @@ -54,16 +59,17 @@ func (m *MockEC2) DescribeLaunchTemplateVersions(request *ec2.DescribeLaunchTemp
return o, nil
}

lt, found := m.LaunchTemplates[aws.StringValue(request.LaunchTemplateName)]
if !found {
return o, nil
for id, ltInfo := range m.LaunchTemplates {
if aws.StringValue(ltInfo.name) != aws.StringValue(request.LaunchTemplateName) {
continue
}
o.LaunchTemplateVersions = append(o.LaunchTemplateVersions, &ec2.LaunchTemplateVersion{
DefaultVersion: aws.Bool(true),
LaunchTemplateId: aws.String(id),
LaunchTemplateData: ltInfo.data,
LaunchTemplateName: request.LaunchTemplateName,
})
}
o.LaunchTemplateVersions = append(o.LaunchTemplateVersions, &ec2.LaunchTemplateVersion{
DefaultVersion: aws.Bool(true),
LaunchTemplateData: lt,
LaunchTemplateName: request.LaunchTemplateName,
})

return o, nil
}

Expand All @@ -72,11 +78,15 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
m.mutex.Lock()
defer m.mutex.Unlock()

m.launchTemplateNumber++
n := m.launchTemplateNumber
id := fmt.Sprintf("lt-%d", n)

if m.LaunchTemplates == nil {
m.LaunchTemplates = make(map[string]*ec2.ResponseLaunchTemplateData)
m.LaunchTemplates = make(map[string]*launchTemplateInfo)
}
if m.LaunchTemplates[aws.StringValue(request.LaunchTemplateName)] != nil {
return nil, fmt.Errorf("duplicate LaunchTemplateName %s", aws.StringValue(request.LaunchTemplateName))
if m.LaunchTemplates[id] != nil {
return nil, fmt.Errorf("duplicate LaunchTemplateName %s", id)
}
resp := &ec2.ResponseLaunchTemplateData{
DisableApiTermination: request.LaunchTemplateData.DisableApiTermination,
Expand All @@ -88,9 +98,10 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
SecurityGroups: request.LaunchTemplateData.SecurityGroups,
UserData: request.LaunchTemplateData.UserData,
}
m.LaunchTemplates[aws.StringValue(request.LaunchTemplateName)] = resp

// @GOD DAMN AWS request vs response .. fu@@@@@ .. so much typing!!#@#@#
m.LaunchTemplates[id] = &launchTemplateInfo{
data: resp,
name: request.LaunchTemplateName,
}

if request.LaunchTemplateData.Monitoring != nil {
resp.Monitoring = &ec2.LaunchTemplatesMonitoring{Enabled: request.LaunchTemplateData.Monitoring.Enabled}
Expand Down Expand Up @@ -169,6 +180,8 @@ func (m *MockEC2) CreateLaunchTemplate(request *ec2.CreateLaunchTemplateInput) (
})
}
}
m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeLaunchTemplate)...)

return &ec2.CreateLaunchTemplateOutput{}, nil
}

Expand All @@ -182,7 +195,11 @@ func (m *MockEC2) DeleteLaunchTemplate(request *ec2.DeleteLaunchTemplateInput) (
if m.LaunchTemplates == nil {
return o, nil
}
delete(m.LaunchTemplates, aws.StringValue(request.LaunchTemplateName))
for id, lt := range m.LaunchTemplates {
if aws.StringValue(lt.name) == aws.StringValue(request.LaunchTemplateName) {
delete(m.LaunchTemplates, id)
}
}

return o, nil
}
9 changes: 7 additions & 2 deletions cloudmock/aws/mockec2/natgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ func (m *MockEC2) CreateNatGatewayWithId(request *ec2.CreateNatGatewayInput, id
m.mutex.Lock()
defer m.mutex.Unlock()

tags := tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeNatgateway)

ngw := &ec2.NatGateway{
NatGatewayId: s(id),
SubnetId: request.SubnetId,
Tags: tags,
}

if request.AllocationId != nil {
Expand All @@ -57,6 +60,8 @@ func (m *MockEC2) CreateNatGatewayWithId(request *ec2.CreateNatGatewayInput, id
}
m.NatGateways[*ngw.NatGatewayId] = ngw

m.addTags(id, tags...)

copy := *ngw

return &ec2.CreateNatGatewayOutput{
Expand Down Expand Up @@ -128,7 +133,7 @@ func (m *MockEC2) DescribeNatGateways(request *ec2.DescribeNatGatewaysInput) (*e
}
default:
if strings.HasPrefix(*filter.Name, "tag:") {
match = m.hasTag(ResourceTypeNatGateway, *ngw.NatGatewayId, filter)
match = m.hasTag(ec2.ResourceTypeNatgateway, *ngw.NatGatewayId, filter)
} else {
return nil, fmt.Errorf("unknown filter name: %q", *filter.Name)
}
Expand All @@ -145,7 +150,7 @@ func (m *MockEC2) DescribeNatGateways(request *ec2.DescribeNatGatewaysInput) (*e
}

copy := *ngw
copy.Tags = m.getTags(ResourceTypeNatGateway, id)
copy.Tags = m.getTags(ec2.ResourceTypeNatgateway, id)
ngws = append(ngws, &copy)
}

Expand Down
7 changes: 3 additions & 4 deletions cloudmock/aws/mockec2/routetable.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ func (m *MockEC2) AddRouteTable(rt *ec2.RouteTable) {
if m.RouteTables == nil {
m.RouteTables = make(map[string]*ec2.RouteTable)
}
for _, tag := range rt.Tags {
m.addTag(*rt.RouteTableId, tag)
}
rt.Tags = nil

m.addTags(*rt.RouteTableId, rt.Tags...)

m.RouteTables[*rt.RouteTableId] = rt
}

Expand Down
7 changes: 6 additions & 1 deletion cloudmock/aws/mockec2/securitygroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,23 @@ func (m *MockEC2) CreateSecurityGroup(request *ec2.CreateSecurityGroupInput) (*e

m.securityGroupNumber++
n := m.securityGroupNumber
id := fmt.Sprintf("sg-%d", n)
tags := tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeSecurityGroup)

sg := &ec2.SecurityGroup{
GroupName: request.GroupName,
GroupId: s(fmt.Sprintf("sg-%d", n)),
GroupId: s(id),
VpcId: request.VpcId,
Description: request.Description,
Tags: tags,
}
if m.SecurityGroups == nil {
m.SecurityGroups = make(map[string]*ec2.SecurityGroup)
}
m.SecurityGroups[*sg.GroupId] = sg

m.addTags(id, tags...)

response := &ec2.CreateSecurityGroupOutput{
GroupId: sg.GroupId,
}
Expand Down
2 changes: 2 additions & 0 deletions cloudmock/aws/mockec2/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ func (m *MockEC2) CreateSubnetWithId(request *ec2.CreateSubnetInput, id string)
main: *subnet,
}

m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeSubnet)...)

response := &ec2.CreateSubnetOutput{
Subnet: subnet,
}
Expand Down
44 changes: 25 additions & 19 deletions cloudmock/aws/mockec2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ import (
"k8s.io/klog"
)

const (
// Not (yet?) in aws-sdk-go
ResourceTypeNatGateway = "nat-gateway"
ResourceTypeAddress = "elastic-ip"
)

func (m *MockEC2) CreateTagsRequest(*ec2.CreateTagsInput) (*request.Request, *ec2.CreateTagsOutput) {
panic("Not implemented")
}
Expand All @@ -49,15 +43,13 @@ func (m *MockEC2) CreateTags(request *ec2.CreateTagsInput) (*ec2.CreateTagsOutpu

for _, v := range request.Resources {
resourceId := *v
for _, tag := range request.Tags {
m.addTag(resourceId, tag)
}
m.addTags(resourceId, request.Tags...)
}
response := &ec2.CreateTagsOutput{}
return response, nil
}

func (m *MockEC2) addTag(resourceId string, tag *ec2.Tag) {
func (m *MockEC2) addTags(resourceId string, tags ...*ec2.Tag) {
resourceType := ""
if strings.HasPrefix(resourceId, "subnet-") {
resourceType = ec2.ResourceTypeSubnet
Expand All @@ -70,24 +62,27 @@ func (m *MockEC2) addTag(resourceId string, tag *ec2.Tag) {
} else if strings.HasPrefix(resourceId, "igw-") {
resourceType = ec2.ResourceTypeInternetGateway
} else if strings.HasPrefix(resourceId, "nat-") {
resourceType = ResourceTypeNatGateway
resourceType = ec2.ResourceTypeNatgateway
} else if strings.HasPrefix(resourceId, "dopt-") {
resourceType = ec2.ResourceTypeDhcpOptions
} else if strings.HasPrefix(resourceId, "rtb-") {
resourceType = ec2.ResourceTypeRouteTable
} else if strings.HasPrefix(resourceId, "eipalloc-") {
resourceType = ResourceTypeAddress
resourceType = ec2.ResourceTypeElasticIp
} else if strings.HasPrefix(resourceId, "lt-") {
resourceType = ec2.ResourceTypeLaunchTemplate
} else {
klog.Fatalf("Unknown resource-type in create tags: %v", resourceId)
}

t := &ec2.TagDescription{
Key: tag.Key,
Value: tag.Value,
ResourceId: s(resourceId),
ResourceType: s(resourceType),
for _, tag := range tags {
t := &ec2.TagDescription{
Key: tag.Key,
Value: tag.Value,
ResourceId: s(resourceId),
ResourceType: s(resourceType),
}
m.Tags = append(m.Tags, t)
}
m.Tags = append(m.Tags, t)
}

func (m *MockEC2) DescribeTagsRequest(*ec2.DescribeTagsInput) (*request.Request, *ec2.DescribeTagsOutput) {
Expand Down Expand Up @@ -227,3 +222,14 @@ func SortTags(tags []*ec2.Tag) {
}
sort.SliceStable(tags, func(i, j int) bool { return keys[i] < keys[j] })
}

func tagSpecificationsToTags(specifications []*ec2.TagSpecification, resourceType string) []*ec2.Tag {
tags := make([]*ec2.Tag, 0)
for _, specification := range specifications {
if aws.StringValue(specification.ResourceType) != resourceType {
continue
}
tags = append(tags, specification.Tags...)
}
return tags
}
11 changes: 4 additions & 7 deletions cloudmock/aws/mockec2/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@ func (m *MockEC2) CreateVolume(request *ec2.CreateVolumeInput) (*ec2.Volume, err
}

n := len(m.Volumes) + 1
id := fmt.Sprintf("vol-%d", n)

volume := &ec2.Volume{
VolumeId: s(fmt.Sprintf("vol-%d", n)),
VolumeId: s(id),
AvailabilityZone: request.AvailabilityZone,
Encrypted: request.Encrypted,
Iops: request.Iops,
Expand All @@ -49,19 +50,15 @@ func (m *MockEC2) CreateVolume(request *ec2.CreateVolumeInput) (*ec2.Volume, err
VolumeType: request.VolumeType,
}

for _, tags := range request.TagSpecifications {
for _, tag := range tags.Tags {
m.addTag(*volume.VolumeId, tag)
}
}
if m.Volumes == nil {
m.Volumes = make(map[string]*ec2.Volume)
}
m.Volumes[*volume.VolumeId] = volume

m.addTags(id, tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeVolume)...)

copy := *volume
copy.Tags = m.getTags(ec2.ResourceTypeVolume, *volume.VolumeId)

// TODO: a few fields
// // Information about the volume attachments.
// Attachments []*VolumeAttachment `locationName:"attachmentSet" locationNameList:"item" type:"list"`
Expand Down
5 changes: 5 additions & 0 deletions cloudmock/aws/mockec2/vpcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,14 @@ func (m *MockEC2) CreateVpcWithId(request *ec2.CreateVpcInput, id string) (*ec2.
m.mutex.Lock()
defer m.mutex.Unlock()

tags := tagSpecificationsToTags(request.TagSpecifications, ec2.ResourceTypeVpc)

vpc := &vpcInfo{
main: ec2.Vpc{
VpcId: s(id),
CidrBlock: request.CidrBlock,
IsDefault: aws.Bool(false),
Tags: tags,
},
attributes: ec2.DescribeVpcAttributeOutput{
EnableDnsHostnames: &ec2.AttributeBooleanValue{Value: aws.Bool(true)},
Expand All @@ -72,6 +75,8 @@ func (m *MockEC2) CreateVpcWithId(request *ec2.CreateVpcInput, id string) (*ec2.
}
m.Vpcs[id] = vpc

m.addTags(id, tags...)

response := &ec2.CreateVpcOutput{
Vpc: &vpc.main,
}
Expand Down
Loading

0 comments on commit 856d581

Please sign in to comment.