Skip to content

Commit ab7c727

Browse files
authored
Merge pull request #169 from YoungJinJung/feat/add-resource-tag-ebs
add resource tag for ebs
2 parents d09fe38 + bc0dcd3 commit ab7c727

File tree

3 files changed

+81
-17
lines changed

3 files changed

+81
-17
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ S3_RELEASE_PATH ?= s3://$(RELEASE_BUCKET)/releases/$(VERSION)
2424
S3_RELEASE_LATEST ?= s3://$(RELEASE_BUCKET)/releases/latest
2525
S3_BLEEDING_EDGE_LATEST ?= s3://$(RELEASE_BUCKET)/edge/latest
2626
S3_EXPERIMENTAL_LATEST ?= s3://$(RELEASE_BUCKET)/experimental/latest
27-
VERSION = 2.1.1
27+
VERSION = 2.1.2
2828

2929
GCP_ONLY ?= false
3030
GCP_PROJECT ?= goployer

pkg/aws/ec2.go

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ func (e EC2Client) ValidateSecurityGroupsConfig(securityGroups []*string, primar
358358
}
359359

360360
// CreateNewLaunchTemplate creates a new launch template
361-
func (e EC2Client) CreateNewLaunchTemplate(name, ami, instanceType, keyName, iamProfileName, userdata string, ebsOptimized, mixedInstancePolicyEnabled bool, securityGroups []*string, blockDevices []*ec2.LaunchTemplateBlockDeviceMappingRequest, instanceMarketOptions *schemas.InstanceMarketOptions, detailedMonitoringEnabled bool, primaryENI *schemas.ENIConfig, secondaryENIs []*schemas.ENIConfig) error {
361+
func (e EC2Client) CreateNewLaunchTemplate(name, ami, instanceType, keyName, iamProfileName, userdata string, ebsOptimized, mixedInstancePolicyEnabled bool, securityGroups []*string, blockDevices []*ec2.LaunchTemplateBlockDeviceMappingRequest, instanceMarketOptions *schemas.InstanceMarketOptions, detailedMonitoringEnabled bool, primaryENI *schemas.ENIConfig, secondaryENIs []*schemas.ENIConfig, tags []string) error {
362362
// Validate security group configuration
363363
if err := e.ValidateSecurityGroupsConfig(securityGroups, primaryENI, secondaryENIs); err != nil {
364364
return err
@@ -385,6 +385,26 @@ func (e EC2Client) CreateNewLaunchTemplate(name, ami, instanceType, keyName, iam
385385
LaunchTemplateName: aws.String(name),
386386
}
387387

388+
// Add resource tags if provided
389+
if len(tags) > 0 {
390+
var tagSpecs []*ec2.LaunchTemplateTagSpecificationRequest
391+
var ec2Tags []*ec2.Tag
392+
for _, tag := range tags {
393+
parts := strings.Split(tag, "=")
394+
if len(parts) == 2 {
395+
ec2Tags = append(ec2Tags, &ec2.Tag{
396+
Key: aws.String(parts[0]),
397+
Value: aws.String(parts[1]),
398+
})
399+
}
400+
}
401+
tagSpecs = append(tagSpecs, &ec2.LaunchTemplateTagSpecificationRequest{
402+
ResourceType: aws.String("volume"),
403+
Tags: ec2Tags,
404+
})
405+
input.LaunchTemplateData.SetTagSpecifications(tagSpecs)
406+
}
407+
388408
if len(blockDevices) > 0 {
389409
input.LaunchTemplateData.SetBlockDeviceMappings(blockDevices)
390410
}
@@ -539,7 +559,7 @@ func (e EC2Client) MakeBlockDevices(blocks []schemas.BlockDevice) []*autoscaling
539559

540560
for _, block := range blocks {
541561
enabledEBSEncrypted := block.Encrypted
542-
562+
Logger.Infof("Encrypt ebs %t", enabledEBSEncrypted)
543563
var ebsDevice *autoscaling.Ebs
544564

545565
if enabledEBSEncrypted {
@@ -556,6 +576,7 @@ func (e EC2Client) MakeBlockDevices(blocks []schemas.BlockDevice) []*autoscaling
556576
DeleteOnTermination: aws.Bool(block.DeleteOnTermination),
557577
}
558578
}
579+
Logger.Infof("ebs %s", ebsDevice)
559580

560581
if len(block.SnapshotID) > 0 {
561582
if !isValidSnapshotID(block.SnapshotID) {
@@ -569,6 +590,7 @@ func (e EC2Client) MakeBlockDevices(blocks []schemas.BlockDevice) []*autoscaling
569590
DeviceName: aws.String(block.DeviceName),
570591
Ebs: ebsDevice,
571592
}
593+
Logger.Infof("tmp %s", tmp)
572594

573595
if block.VolumeType == "io1" || block.VolumeType == "io2" {
574596
tmp.Ebs.Iops = aws.Int64(block.Iops)
@@ -590,18 +612,16 @@ func (e EC2Client) MakeLaunchTemplateBlockDeviceMappings(blocks []schemas.BlockD
590612
var LaunchTemplateEbsBlockDevice *ec2.LaunchTemplateEbsBlockDeviceRequest
591613

592614
if enabledEBSEncrypted {
593-
if len(block.KmsAlias) > 0 {
594-
keyId, err := e.getKmsKeyIdByAlias(block.KmsAlias)
595-
if err != nil {
596-
Logger.Fatal(fmt.Sprintf("Error: %s", err.Error()))
597-
}
598-
LaunchTemplateEbsBlockDevice = &ec2.LaunchTemplateEbsBlockDeviceRequest{
599-
VolumeSize: aws.Int64(block.VolumeSize),
600-
VolumeType: aws.String(block.VolumeType),
601-
Encrypted: aws.Bool(enabledEBSEncrypted),
602-
KmsKeyId: aws.String(keyId),
603-
DeleteOnTermination: aws.Bool(block.DeleteOnTermination),
604-
}
615+
keyId, err := e.getKmsKeyIdByAlias(block.KmsAlias)
616+
if err != nil {
617+
Logger.Fatal(fmt.Sprintf("Error: %s", err.Error()))
618+
}
619+
LaunchTemplateEbsBlockDevice = &ec2.LaunchTemplateEbsBlockDeviceRequest{
620+
VolumeSize: aws.Int64(block.VolumeSize),
621+
VolumeType: aws.String(block.VolumeType),
622+
Encrypted: aws.Bool(enabledEBSEncrypted),
623+
KmsKeyId: aws.String(keyId),
624+
DeleteOnTermination: aws.Bool(block.DeleteOnTermination),
605625
}
606626
} else {
607627
LaunchTemplateEbsBlockDevice = &ec2.LaunchTemplateEbsBlockDeviceRequest{

pkg/deployer/deployer.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -589,14 +589,14 @@ func (d *Deployer) Deploy(config schemas.Config, region schemas.RegionConfig) er
589589
if err != nil {
590590
return err
591591
}
592-
593592
if d.SecurityGroup[region.Region] != nil {
594593
securityGroups = append(securityGroups, d.SecurityGroup[region.Region])
595594
d.Logger.Debugf("additional security group applied to %s: %s", newAsgName, *d.SecurityGroup[region.Region])
596595
}
596+
launchTemplateTags := d.GenerateLaunchTemplateTags(newAsgName, d.Stack.Stack, config.ExtraTags, region.Region)
597597

598598
blockDevices := client.EC2Service.MakeLaunchTemplateBlockDeviceMappings(d.Stack.BlockDevices)
599-
d.Logger.Debugf("additional blockDevice information %s", blockDevices[0].Ebs.String())
599+
d.Logger.Debugf("additional blokcDevice information %s", blockDevices)
600600

601601
ebsOptimized := d.Stack.EbsOptimized
602602

@@ -631,6 +631,7 @@ func (d *Deployer) Deploy(config schemas.Config, region schemas.RegionConfig) er
631631
region.DetailedMonitoringEnabled,
632632
region.PrimaryENI,
633633
region.SecondaryENIs,
634+
launchTemplateTags,
634635
)
635636

636637
if err != nil {
@@ -785,6 +786,49 @@ func (d *Deployer) CompareWithCurrentCapacity(forceManifestCapacity bool, region
785786
return d.Stack.Capacity
786787
}
787788

789+
// GenerateLaunchTemplateTags creates tag list for launch template
790+
func (d *Deployer) GenerateLaunchTemplateTags(asgName, stack string, extraTags, region string) []string {
791+
var ret []string
792+
var keyList []string
793+
794+
// Add tags from AwsConfig
795+
for _, tag := range d.AwsConfig.Tags {
796+
if strings.Contains(tag, "=") {
797+
arr := strings.Split(tag, "=")
798+
keyList = append(keyList, arr[0])
799+
ret = append(ret, tag)
800+
}
801+
}
802+
803+
// Add basic tags
804+
basicTags := []string{
805+
fmt.Sprintf("Name=%s", asgName),
806+
fmt.Sprintf("stack=%s_%s", stack, strings.ReplaceAll(region, "-", "")),
807+
fmt.Sprintf("app=%s", d.AwsConfig.Name),
808+
}
809+
810+
for _, tag := range basicTags {
811+
arr := strings.Split(tag, "=")
812+
key := arr[0]
813+
if !tool.IsStringInArray(key, keyList) {
814+
ret = append(ret, tag)
815+
keyList = append(keyList, key)
816+
}
817+
}
818+
819+
// Add extraTags
820+
if len(extraTags) > 0 && strings.Contains(extraTags, ",") {
821+
ts := strings.Split(extraTags, ",")
822+
for _, s := range ts {
823+
if strings.Contains(strings.TrimSpace(s), "=") {
824+
ret = append(ret, strings.TrimSpace(s))
825+
}
826+
}
827+
}
828+
829+
return ret
830+
}
831+
788832
// GenerateTags creates tag list for autoscaling group
789833
func (d *Deployer) GenerateTags(asgName, stack string, extraTags, ansibleExtraVars, region string) []*autoscaling.Tag {
790834
var ret []*autoscaling.Tag

0 commit comments

Comments
 (0)