-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GROW-662: Capture K8 related sub-counts for AWS (#9)
- Loading branch information
Showing
5 changed files
with
219 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/****************************************************************************** | ||
Cloud Resource Counter | ||
File: spot.go | ||
Summary: Provides a count of all Spot EC2 instances. | ||
******************************************************************************/ | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
color "github.com/logrusorgru/aurora" | ||
) | ||
|
||
// SpotInstances retrieves the count of all EC2 spot instances | ||
// either for all regions (allRegions is true) or the region | ||
// associated with the session. | ||
// This method gives status back to the user via the supplied | ||
// ActivityMonitor instance. | ||
func EC2K8SubInstances(sf ServiceFactory, am ActivityMonitor, allRegions bool) int { | ||
// Indicate activity | ||
am.StartAction("Retrieving EC2 K8 related VMs Sub-instance counts") | ||
|
||
// Should we get the counts for all regions? | ||
instanceCount := 0 | ||
if allRegions { | ||
// Get the list of all enabled regions for this account | ||
regionsSlice := GetEC2Regions(sf.GetEC2InstanceService(""), am) | ||
|
||
// Loop through all of the regions | ||
for _, regionName := range regionsSlice { | ||
// Get the EC2 counts for a specific region | ||
instanceCount += ec2K8SubInstancesForSingleRegion(sf.GetEC2InstanceService(regionName), am) | ||
} | ||
} else { | ||
// Get the EC2 counts for the region selected by this session | ||
instanceCount = ec2K8SubInstancesForSingleRegion(sf.GetEC2InstanceService(""), am) | ||
} | ||
|
||
// Indicate end of activity | ||
am.EndAction("OK (%d)", color.Bold(instanceCount)) | ||
|
||
return instanceCount | ||
} | ||
|
||
func ec2K8SubInstancesForSingleRegion(ec2is *EC2InstanceService, am ActivityMonitor) int { | ||
// Indicate activity | ||
am.Message(".") | ||
|
||
// Construct our input to find ONLY RUNNING EC2 instances that also have the "aws:eks:cluster-name" tag | ||
input := &ec2.DescribeInstancesInput{ | ||
Filters: []*ec2.Filter{ | ||
{ | ||
Name: aws.String("tag-key"), | ||
Values: []*string{ | ||
aws.String("aws:eks:cluster-name"), | ||
}, | ||
}, | ||
{ | ||
Name: aws.String("instance-state-name"), | ||
Values: []*string{ | ||
aws.String("running"), | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Invoke our service | ||
instanceCount := 0 | ||
err := ec2is.InspectInstances(input, func(dio *ec2.DescribeInstancesOutput, lastPage bool) bool { | ||
// Loop through each reservation | ||
for _, reservation := range dio.Reservations { | ||
// We assume that the AWS Service has properly filtered the list of returned instances | ||
instanceCount += len(reservation.Instances) | ||
} | ||
|
||
return true | ||
}) | ||
|
||
// Check for error | ||
am.CheckError(err) | ||
|
||
return instanceCount | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/****************************************************************************** | ||
Cloud Resource Counter | ||
File: ec2_k8_subcount_test.go | ||
Summary: The Unit Test for EC2 K8 sub-count. | ||
******************************************************************************/ | ||
|
||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/expel-io/aws-resource-counter/mock" | ||
) | ||
|
||
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | ||
// Unit Test for EC2 K8 sub-count. | ||
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= | ||
|
||
func TestEC2K8SubInstances(t *testing.T) { | ||
// Describe all of our test cases: 1 failure and 4 success cases | ||
cases := []struct { | ||
RegionName string | ||
AllRegions bool | ||
ExpectedCount int | ||
ExpectError bool | ||
}{ | ||
{ | ||
RegionName: "us-east-1", | ||
ExpectedCount: 1, | ||
}, { | ||
RegionName: "us-east-2", | ||
ExpectedCount: 0, | ||
}, { | ||
RegionName: "af-south-1", | ||
ExpectedCount: 0, | ||
}, { | ||
RegionName: "undefined-region", | ||
ExpectError: true, | ||
}, { | ||
AllRegions: true, | ||
ExpectedCount: 1, | ||
}, | ||
} | ||
|
||
// Loop through each test case | ||
for _, c := range cases { | ||
// Create our fake service factory | ||
sf := fakeEC2ServiceFactory{ | ||
RegionName: c.RegionName, | ||
DRResponse: ec2Regions, | ||
} | ||
|
||
// Create a mock activity monitor | ||
mon := &mock.ActivityMonitorImpl{} | ||
|
||
// Invoke our EC K8 Subcount Instances function | ||
actualCount := EC2K8SubInstances(sf, mon, c.AllRegions) | ||
|
||
// Did we expect an error? | ||
if c.ExpectError { | ||
// Did it fail to arrive? | ||
if !mon.ErrorOccured { | ||
t.Error("Expected an error to occur, but it did not... :^(") | ||
} | ||
} else if mon.ErrorOccured { | ||
t.Errorf("Unexpected error occurred: %s", mon.ErrorMessage) | ||
} else if actualCount != c.ExpectedCount { | ||
t.Errorf("Error: EC K8 SubcountInstances returned %d; expected %d", actualCount, c.ExpectedCount) | ||
} else if mon.ProgramExited { | ||
t.Errorf("Unexpected Exit: The program unexpected exited with status code=%d", mon.ExitCode) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters