Skip to content

Commit 9a6d9af

Browse files
committed
Move tags to cloudformation stack
Signed-off-by: Mustafa Abdelrahman <[email protected]>
1 parent 25df506 commit 9a6d9af

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

aws/adapter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ func (a *Adapter) UpdateTargetGroupsAndAutoScalingGroups(stacks []*Stack, proble
702702
// All the required resources (listeners and target group) are created in a
703703
// transactional fashion.
704704
// Failure to create the stack causes it to be deleted automatically.
705-
func (a *Adapter) CreateStack(certificateARNs []string, scheme, securityGroup, owner, sslPolicy, ipAddressType, wafWebACLID string, cwAlarms CloudWatchAlarmList, loadBalancerType string, http2 bool) (string, error) {
705+
func (a *Adapter) CreateStack(certificateARNs []string, scheme, securityGroup, owner, sslPolicy, ipAddressType, wafWebACLID string, cwAlarms CloudWatchAlarmList, loadBalancerType string, http2 bool, stackTags map[string]string) (string, error) {
706706
certARNs := make(map[string]time.Time, len(certificateARNs))
707707
for _, arn := range certificateARNs {
708708
certARNs[arn] = time.Time{}
@@ -754,7 +754,7 @@ func (a *Adapter) CreateStack(certificateARNs []string, scheme, securityGroup, o
754754
httpRedirectToHTTPS: a.httpRedirectToHTTPS,
755755
nlbCrossZone: a.nlbCrossZone,
756756
http2: http2,
757-
tags: a.stackTags,
757+
tags: mergeTags(a.stackTags, stackTags),
758758
internalDomains: a.internalDomains,
759759
denyInternalDomains: a.denyInternalDomains,
760760
denyInternalDomainsResponse: denyResp{
@@ -767,7 +767,7 @@ func (a *Adapter) CreateStack(certificateARNs []string, scheme, securityGroup, o
767767
return createStack(a.cloudformation, spec)
768768
}
769769

770-
func (a *Adapter) UpdateStack(stackName string, certificateARNs map[string]time.Time, scheme, securityGroup, owner, sslPolicy, ipAddressType, wafWebACLID string, cwAlarms CloudWatchAlarmList, loadBalancerType string, http2 bool) (string, error) {
770+
func (a *Adapter) UpdateStack(stackName string, certificateARNs map[string]time.Time, scheme, securityGroup, owner, sslPolicy, ipAddressType, wafWebACLID string, cwAlarms CloudWatchAlarmList, loadBalancerType string, http2 bool, stackTags map[string]string) (string, error) {
771771
if _, ok := SSLPolicies[sslPolicy]; !ok {
772772
return "", fmt.Errorf("invalid SSLPolicy '%s' defined", sslPolicy)
773773
}
@@ -810,7 +810,7 @@ func (a *Adapter) UpdateStack(stackName string, certificateARNs map[string]time.
810810
httpRedirectToHTTPS: a.httpRedirectToHTTPS,
811811
nlbCrossZone: a.nlbCrossZone,
812812
http2: http2,
813-
tags: a.stackTags,
813+
tags: mergeTags(a.stackTags, stackTags),
814814
internalDomains: a.internalDomains,
815815
denyInternalDomains: a.denyInternalDomains,
816816
denyInternalDomainsResponse: denyResp{

aws/cf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Stack struct {
3838
TargetGroupARNs []string
3939
WAFWebACLID string
4040
CertificateARNs map[string]time.Time
41-
tags map[string]string
41+
Tags map[string]string
4242
}
4343

4444
// IsComplete returns true if the stack status is a complete state.
@@ -513,7 +513,7 @@ func mapToManagedStack(stack *cloudformation.Stack) *Stack {
513513
LoadBalancerType: parameters[parameterLoadBalancerTypeParameter],
514514
HTTP2: http2,
515515
CertificateARNs: certificateARNs,
516-
tags: tags,
516+
Tags: tags,
517517
OwnerIngress: ownerIngress,
518518
status: aws.StringValue(stack.StackStatus),
519519
statusReason: aws.StringValue(stack.StackStatusReason),

aws/cf_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ func TestFindManagedStacks(t *testing.T) {
478478
"cert-arn": {},
479479
},
480480
TargetGroupARNs: []string{"tg-arn"},
481-
tags: map[string]string{
481+
Tags: map[string]string{
482482
kubernetesCreatorTag: DefaultControllerID,
483483
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
484484
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -494,7 +494,7 @@ func TestFindManagedStacks(t *testing.T) {
494494
"cert-arn": {},
495495
},
496496
TargetGroupARNs: []string{"tg-arn"},
497-
tags: map[string]string{
497+
Tags: map[string]string{
498498
kubernetesCreatorTag: DefaultControllerID,
499499
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
500500
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -510,7 +510,7 @@ func TestFindManagedStacks(t *testing.T) {
510510
"cert-arn": {},
511511
},
512512
TargetGroupARNs: []string{"tg-arn", "http-tg-arn"},
513-
tags: map[string]string{
513+
Tags: map[string]string{
514514
kubernetesCreatorTag: DefaultControllerID,
515515
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
516516
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -522,7 +522,7 @@ func TestFindManagedStacks(t *testing.T) {
522522
{
523523
Name: "managed-stack-not-ready",
524524
CertificateARNs: map[string]time.Time{},
525-
tags: map[string]string{
525+
Tags: map[string]string{
526526
kubernetesCreatorTag: DefaultControllerID,
527527
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
528528
},
@@ -558,7 +558,7 @@ func TestFindManagedStacks(t *testing.T) {
558558
CertificateARNs: map[string]time.Time{
559559
"cert-arn": {},
560560
},
561-
tags: map[string]string{
561+
Tags: map[string]string{
562562
kubernetesCreatorTag: DefaultControllerID,
563563
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
564564
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -595,7 +595,7 @@ func TestFindManagedStacks(t *testing.T) {
595595
CertificateARNs: map[string]time.Time{
596596
"cert-arn": {},
597597
},
598-
tags: map[string]string{
598+
Tags: map[string]string{
599599
kubernetesCreatorTag: DefaultControllerID,
600600
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
601601
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -644,7 +644,7 @@ func TestFindManagedStacks(t *testing.T) {
644644
DNSName: "example-notready.com",
645645
TargetGroupARNs: []string{"tg-arn"},
646646
CertificateARNs: map[string]time.Time{},
647-
tags: map[string]string{
647+
Tags: map[string]string{
648648
kubernetesCreatorTag: DefaultControllerID,
649649
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
650650
targetGroupsArnsTag: "dGctYXJu", // "tg-arn"
@@ -657,7 +657,7 @@ func TestFindManagedStacks(t *testing.T) {
657657
DNSName: "example.com",
658658
TargetGroupARNs: []string{"tg-arn"},
659659
CertificateARNs: map[string]time.Time{},
660-
tags: map[string]string{
660+
Tags: map[string]string{
661661
kubernetesCreatorTag: DefaultControllerID,
662662
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
663663
targetGroupsArnsTag: "dGctYXJu", // "tg-arn"
@@ -738,7 +738,7 @@ func TestGetStack(t *testing.T) {
738738
"cert-arn": {},
739739
},
740740
TargetGroupARNs: []string{"tg-arn"},
741-
tags: map[string]string{
741+
Tags: map[string]string{
742742
kubernetesCreatorTag: DefaultControllerID,
743743
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
744744
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),
@@ -779,7 +779,7 @@ func TestGetStack(t *testing.T) {
779779
"cert-arn": {},
780780
},
781781
TargetGroupARNs: []string{"tg-arn", "tg-http-arn"},
782-
tags: map[string]string{
782+
Tags: map[string]string{
783783
kubernetesCreatorTag: DefaultControllerID,
784784
clusterIDTagPrefix + "test-cluster": resourceLifecycleOwned,
785785
certificateARNTagPrefix + "cert-arn": time.Time{}.Format(time.RFC3339),

aws/fake/cf.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (m *CFClient) DescribeStacks(in *cloudformation.DescribeStacksInput) (*clou
6969
}
7070

7171
func (m *CFClient) CreateStack(params *cloudformation.CreateStackInput) (*cloudformation.CreateStackOutput, error) {
72+
print("\n ======== CreateStack ======== \n")
7273
m.tagCreationHistory = append(m.tagCreationHistory, params.Tags)
7374
m.paramCreationHistory = append(m.paramCreationHistory, params.Parameters)
7475
m.templateCreationHistory = append(m.templateCreationHistory, *params.TemplateBody)

worker.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,12 @@ func createStack(awsAdapter *aws.Adapter, lb *loadBalancer, problems *problem.Li
534534

535535
log.Infof("Creating stack for certificates %q / ingress %q", certificates, lb.ingresses)
536536

537-
stackId, err := awsAdapter.CreateStack(certificates, lb.scheme, lb.securityGroup, lb.Owner(), lb.sslPolicy, lb.ipAddressType, lb.wafWebACLID, lb.cwAlarms, lb.loadBalancerType, lb.http2)
537+
tags := make(map[string]string)
538+
if lb.stack != nil && lb.stack.Tags != nil {
539+
tags = lb.stack.Tags
540+
}
541+
542+
stackId, err := awsAdapter.CreateStack(certificates, lb.scheme, lb.securityGroup, lb.Owner(), lb.sslPolicy, lb.ipAddressType, lb.wafWebACLID, lb.cwAlarms, lb.loadBalancerType, lb.http2, tags)
538543
if err != nil {
539544
if isAlreadyExistsError(err) {
540545
lb.stack, err = awsAdapter.GetStack(stackId)
@@ -554,7 +559,12 @@ func updateStack(awsAdapter *aws.Adapter, lb *loadBalancer, problems *problem.Li
554559

555560
log.Infof("Updating %q stack for %d certificates / %d ingresses", lb.scheme, len(certificates), len(lb.ingresses))
556561

557-
stackId, err := awsAdapter.UpdateStack(lb.stack.Name, certificates, lb.scheme, lb.securityGroup, lb.Owner(), lb.sslPolicy, lb.ipAddressType, lb.wafWebACLID, lb.cwAlarms, lb.loadBalancerType, lb.http2)
562+
tags := make(map[string]string)
563+
if lb.stack != nil && lb.stack.Tags != nil {
564+
tags = lb.stack.Tags
565+
}
566+
567+
stackId, err := awsAdapter.UpdateStack(lb.stack.Name, certificates, lb.scheme, lb.securityGroup, lb.Owner(), lb.sslPolicy, lb.ipAddressType, lb.wafWebACLID, lb.cwAlarms, lb.loadBalancerType, lb.http2, tags)
558568
if isNoUpdatesToBePerformedError(err) {
559569
log.Debugf("Stack(%q) is already up to date", certificates)
560570
} else if err != nil {

worker_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"crypto/x509"
66
"encoding/json"
7+
"fmt"
78
"io"
89
"net/http/httptest"
910
"os"
@@ -527,9 +528,9 @@ func TestResourceConversionOneToOne(tt *testing.T) {
527528
t.Error(problems.Errors())
528529
}
529530

530-
assert.Equal(t, len(clientCF.GetTagCreationHistory()), len(tags))
531+
assert.Equal(t, len(clientCF.GetTagCreationHistory()), len(tags), fmt.Sprintf("got %v, expected %v", tags, clientCF.GetTagCreationHistory()))
531532
assert.Equal(t, len(clientCF.GetParamCreationHistory()), len(params))
532-
assert.Equal(t, len(clientCF.GetTemplateCreationHistory()), len(templates))
533+
assert.Equal(t, len(clientCF.GetTemplateCreationHistory()), len(templates), fmt.Sprintf("got %v, expected %v", templates, clientCF.GetTemplateCreationHistory()))
533534

534535
// This loop is necessary because assert.ElementsMatch only do set-style comparison
535536
// for the first level of the array. So for nested arrays it would not behave like expected.

0 commit comments

Comments
 (0)