Skip to content

Commit

Permalink
Merge pull request #88 from hortonworks/CB-19052
Browse files Browse the repository at this point in the history
CB-19052 Check AWS resources without group if they belong to a CF stack
  • Loading branch information
Bajzathd authored Feb 3, 2023
2 parents 7e9c72f + 43c5e68 commit 3bc64b8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
42 changes: 28 additions & 14 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (p awsProvider) GetStacks() ([]*types.Stack, error) {
elbClients := p.getElbClientsByRegion()
ec2Clients, _ := p.getEc2AndCTClientsByRegion()
cloudWatchClients := p.getCloudWatchClientsByRegion()
nativeStacks, nativeError := getNativeStacks(ec2Clients, elbClients, cloudWatchClients)
nativeStacks, nativeError := getNativeStacks(ec2Clients, elbClients, cloudWatchClients, cfStacks)
if nativeError != nil {
return nil, nativeError
}
Expand Down Expand Up @@ -1123,7 +1123,7 @@ type AwsNativeStack struct {
Tags types.Tags
}

func getNativeStacks(ec2Clients map[string]ec2Client, elbClients map[string]elbClient, cloudWatchClients map[string]cloudWatchClient) ([]*types.Stack, error) {
func getNativeStacks(ec2Clients map[string]ec2Client, elbClients map[string]elbClient, cloudWatchClients map[string]cloudWatchClient, cfStacks []*types.Stack) ([]*types.Stack, error) {
stackChan := make(chan AwsNativeStack, 5)
wg := sync.WaitGroup{}
wg.Add(len(ec2Clients))
Expand Down Expand Up @@ -1321,33 +1321,47 @@ func getNativeStacks(ec2Clients map[string]ec2Client, elbClients map[string]elbC
}
}

cfStackGroups := []string{}
for _, cfStack := range cfStacks {
if group, ok := cfStack.Tags[ctx.ResourceGroupingLabel]; ok {
cfStackGroups = append(cfStackGroups, group)
}
}
log.Debugf("[AWS] CF stack groups in region %s: %s", region, strings.Join(cfStackGroups, ","))

if len(loadBalancersByGroup) != 0 {
for group, loadBalancers := range loadBalancersByGroup {
lbNames := []string{}
for _, lb := range loadBalancers {
lbNames = append(lbNames, *lb.LoadBalancerName)
if !utils.IsAnyEquals(group, cfStackGroups...) {
lbNames := []string{}
for _, lb := range loadBalancers {
lbNames = append(lbNames, *lb.LoadBalancerName)
}
log.Warnf("[AWS] Loadbalancers %s with group %s does not have a stack in region %s", strings.Join(lbNames, ","), group, region)
}
log.Warnf("[AWS] Loadbalancers %s with group %s does not have a stack", strings.Join(lbNames, ","), group)
}
}

if len(elasticIpsByGroup) != 0 {
for group, ips := range elasticIpsByGroup {
ipAddresses := []string{}
for _, ip := range ips {
ipAddresses = append(ipAddresses, *ip.PublicIp)
if !utils.IsAnyEquals(group, cfStackGroups...) {
ipAddresses := []string{}
for _, ip := range ips {
ipAddresses = append(ipAddresses, *ip.PublicIp)
}
log.Warnf("[AWS] Elastic IPs %s with group %s does not have a stack in region %s", strings.Join(ipAddresses, ","), group, region)
}
log.Warnf("[AWS] Elastic IPs %s with group %s does not have a stack", strings.Join(ipAddresses, ","), group)
}
}

if len(securityGroupsByGroup) != 0 {
for group, securityGroups := range securityGroupsByGroup {
securityGroupIds := []string{}
for _, sg := range securityGroups {
securityGroupIds = append(securityGroupIds, *sg.GroupId)
if !utils.IsAnyEquals(group, cfStackGroups...) {
securityGroupIds := []string{}
for _, sg := range securityGroups {
securityGroupIds = append(securityGroupIds, *sg.GroupId)
}
log.Warnf("[AWS] Security groups %s with group %s does not have a stack in region %s", strings.Join(securityGroupIds, ","), group, region)
}
log.Warnf("[AWS] Security groups %s with group %s does not have a stack", strings.Join(securityGroupIds, ","), group)
}
}

Expand Down
2 changes: 1 addition & 1 deletion aws/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestGetNativeStacks(t *testing.T) {
go func() {
defer close(operationChannel)

stacks, err := getNativeStacks(ec2Clients, elbClients, cwClients)
stacks, err := getNativeStacks(ec2Clients, elbClients, cwClients, []*types.Stack{})
assert.Nil(t, err)
assert.Equal(t, 1, len(stacks))
stack = stacks[0]
Expand Down

0 comments on commit 3bc64b8

Please sign in to comment.