11package stack
22
33import (
4- "context "
4+ "regexp "
55 "strings"
66
7- "github.com/aws/aws-sdk-go-v2/aws"
8- "github.com/aws/aws-sdk-go-v2/service/cloudformation"
97 "github.com/aws/aws-sdk-go-v2/service/cloudformation/types"
10- "github.com/xh3b4sd/tracer"
118)
129
13- var status = map [types.ResourceStatus ]float64 {
14- types .ResourceStatusCreateFailed : 0.0 ,
15- types .ResourceStatusDeleteFailed : 0.0 ,
16- types .ResourceStatusDeleteSkipped : 0.0 ,
17- types .ResourceStatusExportFailed : 0.0 ,
18- types .ResourceStatusExportRollbackComplete : 0.0 ,
19- types .ResourceStatusExportRollbackFailed : 0.0 ,
20- types .ResourceStatusExportRollbackInProgress : 0.0 ,
21- types .ResourceStatusImportFailed : 0.0 ,
22- types .ResourceStatusImportRollbackComplete : 0.0 ,
23- types .ResourceStatusImportRollbackFailed : 0.0 ,
24- types .ResourceStatusImportRollbackInProgress : 0.0 ,
25- types .ResourceStatusRollbackComplete : 0.0 ,
26- types .ResourceStatusRollbackFailed : 0.0 ,
27- types .ResourceStatusRollbackInProgress : 0.0 ,
28- types .ResourceStatusUpdateFailed : 0.0 ,
29- types .ResourceStatusUpdateRollbackComplete : 0.0 ,
30- types .ResourceStatusUpdateRollbackFailed : 0.0 ,
31- types .ResourceStatusUpdateRollbackInProgress : 0.0 ,
32-
33- types .ResourceStatusCreateInProgress : 0.5 ,
34- types .ResourceStatusDeleteInProgress : 0.5 ,
35- types .ResourceStatusExportInProgress : 0.5 ,
36- types .ResourceStatusImportInProgress : 0.5 ,
37- types .ResourceStatusUpdateInProgress : 0.5 ,
38-
39- types .ResourceStatusCreateComplete : 1.0 ,
40- types .ResourceStatusDeleteComplete : 1.0 ,
41- types .ResourceStatusExportComplete : 1.0 ,
42- types .ResourceStatusImportComplete : 1.0 ,
43- types .ResourceStatusUpdateComplete : 1.0 ,
44- }
10+ var (
11+ // exp defines a regular expression for the stack names of CloudFormation
12+ // stacks. Note that there is no precise definition for the amount of
13+ // characters that a random hash suffix for the stack names ought to look
14+ // like. So we are testing against at least 10 characters, but also accept
15+ // e.g. 13 or 14.
16+ //
17+ // server-test-FargateStack-QGXQ9XZ4J44K
18+ //
19+ exp = regexp .MustCompile (`-[A-Z0-9]{10,}$` )
20+
21+ status = map [types.StackStatus ]float64 {
22+ types .StackStatusCreateFailed : 0.0 ,
23+ types .StackStatusDeleteFailed : 0.0 ,
24+ types .StackStatusImportRollbackComplete : 0.0 ,
25+ types .StackStatusImportRollbackFailed : 0.0 ,
26+ types .StackStatusImportRollbackInProgress : 0.0 ,
27+ types .StackStatusRollbackComplete : 0.0 ,
28+ types .StackStatusRollbackFailed : 0.0 ,
29+ types .StackStatusRollbackInProgress : 0.0 ,
30+ types .StackStatusUpdateFailed : 0.0 ,
31+ types .StackStatusUpdateRollbackComplete : 0.0 ,
32+ types .StackStatusUpdateRollbackCompleteCleanupInProgress : 0.0 ,
33+ types .StackStatusUpdateRollbackFailed : 0.0 ,
34+ types .StackStatusUpdateRollbackInProgress : 0.0 ,
35+
36+ types .StackStatusCreateInProgress : 0.5 ,
37+ types .StackStatusDeleteInProgress : 0.5 ,
38+ types .StackStatusImportInProgress : 0.5 ,
39+ types .StackStatusReviewInProgress : 0.5 ,
40+ types .StackStatusUpdateCompleteCleanupInProgress : 0.5 ,
41+ types .StackStatusUpdateInProgress : 0.5 ,
42+
43+ types .StackStatusCreateComplete : 1.0 ,
44+ types .StackStatusDeleteComplete : 1.0 ,
45+ types .StackStatusImportComplete : 1.0 ,
46+ types .StackStatusUpdateComplete : 1.0 ,
47+ }
48+ )
4549
4650type stack struct {
4751 // hlt is the CloudFormation stack status, either 0.0, 0.5 or 1.0.
@@ -57,65 +61,57 @@ type stack struct {
5761// assigned the stack status 0.5, otherwise the unhealthy stack status 0 is
5862// assigned.
5963func (h * Handler ) stack (det []detail ) ([]stack , error ) {
60- var err error
61-
6264 var sta []stack
65+
6366 for _ , x := range det {
64- var inp * cloudformation. DescribeStackResourcesInput
67+ var tag string
6568 {
66- inp = & cloudformation.DescribeStackResourcesInput {
67- StackName : aws .String (x .arn ),
68- }
69+ tag = staTag (x .nam )
6970 }
7071
71- var out * cloudformation. DescribeStackResourcesOutput
72- {
73- out , err = h . cfc . DescribeStackResources ( context . Background (), inp )
74- if err != nil {
75- return nil , tracer . Mask ( err )
76- }
77- }
72+ if tag == "" {
73+ h . log . Log (
74+ "level" , "warning" ,
75+ "message" , "skipping instrumentation for CloudFormation stack" ,
76+ "reason" , "CloudFormation stack name pattern is unrecognizable" ,
77+ "name" , x . nam ,
78+ )
7879
79- for _ , y := range out .StackResources {
80- var tag string
8180 {
82- tag = staTag (* y .LogicalResourceId )
83- }
84-
85- if tag == "" {
86- h .log .Log (
87- "level" , "warning" ,
88- "message" , "skipping instrumentation for CloudFormation stack" ,
89- "reason" , "CloudFormation stack name pattern is unrecognizable" ,
90- "name" , * y .LogicalResourceId ,
91- )
92-
93- {
94- continue
95- }
96- }
97-
98- var hlt float64
99- {
100- hlt = status [y .ResourceStatus ]
81+ continue
10182 }
83+ }
10284
103- sta = append (sta , stack {
104- hlt : hlt ,
105- lab : tag ,
106- })
85+ var hlt float64
86+ {
87+ hlt = status [x .sta ]
10788 }
89+
90+ sta = append (sta , stack {
91+ hlt : hlt ,
92+ lab : tag ,
93+ })
10894 }
10995
11096 return sta , nil
11197}
11298
99+ // rooSta identifies whether the given CloudFormation stack name is considered a
100+ // root stack name.
101+ func rooSta (arn string ) bool {
102+ return ! exp .MatchString (arn )
103+ }
104+
113105func staTag (nam string ) string {
114106 for k , v := range mapping {
115107 if strings .Contains (nam , k ) {
116108 return v
117109 }
118110 }
119111
112+ if rooSta (nam ) {
113+ return "root"
114+ }
115+
120116 return ""
121117}
0 commit comments