generated from amazon-archives/__template_Apache-2.0
-
Couldn't load subscription status.
- Fork 278
Prometheus add nodes gauge for SQS mode #1083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f4ae6de
Prometheus add nodes gauge for SQS mode
phuhung273 a3ba27e
add nil check, fix client creation order
phuhung273 d139b86
restore client creation order
phuhung273 aff2a60
EnablePrometheus check
phuhung273 316906e
add e2e test
phuhung273 a95474e
improve test + logging
phuhung273 61dc478
add initMetricErr nil check, e2e remove misleading log
phuhung273 c0fa29c
added cordoned, evicted, message_deleted test
phuhung273 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,98 @@ | ||
| // Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file is distributed | ||
| // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| // express or implied. See the License for the specific language governing | ||
| // permissions and limitations under the License. | ||
|
|
||
| package ec2helper | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/service/ec2" | ||
| "github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
| ) | ||
|
|
||
| type IEC2Helper interface { | ||
| GetInstanceIdsMapByTagKey(tag string) (map[string]bool, error) | ||
| } | ||
|
|
||
| type EC2Helper struct { | ||
| ec2ServiceClient ec2iface.EC2API | ||
| } | ||
|
|
||
| func New(ec2 ec2iface.EC2API) EC2Helper { | ||
| return EC2Helper{ | ||
| ec2ServiceClient: ec2, | ||
| } | ||
| } | ||
|
|
||
| func (h EC2Helper) GetInstanceIdsByTagKey(tag string) ([]string, error) { | ||
| ids := []string{} | ||
| var nextToken string | ||
|
|
||
| for { | ||
| result, err := h.ec2ServiceClient.DescribeInstances(&ec2.DescribeInstancesInput{ | ||
| Filters: []*ec2.Filter{ | ||
phuhung273 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| Name: aws.String("tag-key"), | ||
| Values: []*string{aws.String(tag)}, | ||
| }, | ||
| }, | ||
| NextToken: &nextToken, | ||
| }) | ||
|
|
||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if result == nil || result.Reservations == nil { | ||
| return nil, fmt.Errorf("failed to describe instances") | ||
| } | ||
|
|
||
| for _, reservation := range result.Reservations { | ||
| if reservation.Instances == nil { | ||
| continue | ||
| } | ||
| for _, instance := range reservation.Instances { | ||
| if instance == nil || instance.InstanceId == nil { | ||
| continue | ||
| } | ||
| ids = append(ids, *instance.InstanceId) | ||
| } | ||
| } | ||
|
|
||
| if result.NextToken == nil { | ||
| break | ||
| } | ||
| nextToken = *result.NextToken | ||
| } | ||
|
|
||
| return ids, nil | ||
| } | ||
|
|
||
| func (h EC2Helper) GetInstanceIdsMapByTagKey(tag string) (map[string]bool, error) { | ||
phuhung273 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| idMap := map[string]bool{} | ||
| ids, err := h.GetInstanceIdsByTagKey(tag) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if ids == nil { | ||
phuhung273 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return nil, fmt.Errorf("failed to describe instances") | ||
| } | ||
|
|
||
| for _, id := range ids { | ||
| idMap[id] = true | ||
| } | ||
|
|
||
| return idMap, nil | ||
| } | ||
This file contains hidden or 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 @@ | ||
| // Copyright 2016-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"). You may | ||
| // not use this file except in compliance with the License. A copy of the | ||
| // License is located at | ||
| // | ||
| // http://aws.amazon.com/apache2.0/ | ||
| // | ||
| // or in the "license" file accompanying this file. This file is distributed | ||
| // on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
| // express or implied. See the License for the specific language governing | ||
| // permissions and limitations under the License. | ||
|
|
||
| package ec2helper_test | ||
phuhung273 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/aws/aws-node-termination-handler/pkg/ec2helper" | ||
| h "github.com/aws/aws-node-termination-handler/pkg/test" | ||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/service/ec2" | ||
| ) | ||
|
|
||
| const ( | ||
| instanceId1 = "i-1" | ||
| instanceId2 = "i-2" | ||
| ) | ||
|
|
||
| func TestGetInstanceIdsByTagKey(t *testing.T) { | ||
| ec2Mock := h.MockedEC2{ | ||
| DescribeInstancesResp: getDescribeInstancesResp(), | ||
| } | ||
| ec2Helper := ec2helper.New(ec2Mock) | ||
| instanceIds, err := ec2Helper.GetInstanceIdsByTagKey("myNTHManagedTag") | ||
| h.Ok(t, err) | ||
|
|
||
| h.Equals(t, 2, len(instanceIds)) | ||
| h.Equals(t, instanceId1, instanceIds[0]) | ||
| h.Equals(t, instanceId2, instanceIds[1]) | ||
| } | ||
|
|
||
| func TestGetInstanceIdsMapByTagKey(t *testing.T) { | ||
| ec2Mock := h.MockedEC2{ | ||
| DescribeInstancesResp: getDescribeInstancesResp(), | ||
| } | ||
| ec2Helper := ec2helper.New(ec2Mock) | ||
| instanceIdsMap, err := ec2Helper.GetInstanceIdsMapByTagKey("myNTHManagedTag") | ||
| h.Ok(t, err) | ||
|
|
||
| _, exist := instanceIdsMap[instanceId1] | ||
| h.Equals(t, true, exist) | ||
| _, exist = instanceIdsMap[instanceId2] | ||
| h.Equals(t, true, exist) | ||
| _, exist = instanceIdsMap["non-existent instance id"] | ||
| h.Equals(t, false, exist) | ||
| } | ||
|
|
||
| func getDescribeInstancesResp() ec2.DescribeInstancesOutput { | ||
| return ec2.DescribeInstancesOutput{ | ||
| Reservations: []*ec2.Reservation{ | ||
| { | ||
| Instances: []*ec2.Instance{ | ||
| { | ||
| InstanceId: aws.String(instanceId1), | ||
| }, | ||
| { | ||
| InstanceId: aws.String(instanceId2), | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.