-
Notifications
You must be signed in to change notification settings - Fork 75
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
Add aws support #640
Open
rabunkosar-dd
wants to merge
11
commits into
cloudprober:master
Choose a base branch
from
rabunkosar-dd:add_aws_support
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add aws support #640
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f98c266
add AWS support for some services
rabunkosar-dd 87c7f26
init maps
rabunkosar-dd dbceb92
unit tests for ec2
rabunkosar-dd 525c8f7
add elasticache test and bug fix
rabunkosar-dd e45c79a
Merge branch 'master' into add_aws_support
rabunkosar-dd 2e116af
split rds and ec API calls
rabunkosar-dd 48b6e87
remove test files
rabunkosar-dd 2a06fbe
update comments
rabunkosar-dd 02cad60
remove AWS resource discovery for a separate PR
rabunkosar-dd 6785310
address comments
rabunkosar-dd 46069f8
update the comment
rabunkosar-dd File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,148 @@ | ||
// Copyright 2017-2023 The Cloudprober Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License 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 aws implements a AWS (Amazon Web Services) resources provider for | ||
ResourceDiscovery server. | ||
|
||
See ResourceTypes variable for the list of supported resource types. | ||
|
||
AWS provider is configured through a protobuf based config file | ||
(proto/config.proto). Example config: | ||
|
||
{ | ||
aws_instances {} | ||
} | ||
*/ | ||
|
||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
configpb "github.com/cloudprober/cloudprober/internal/rds/aws/proto" | ||
pb "github.com/cloudprober/cloudprober/internal/rds/proto" | ||
serverconfigpb "github.com/cloudprober/cloudprober/internal/rds/server/proto" | ||
"github.com/cloudprober/cloudprober/logger" | ||
|
||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
// DefaultProviderID is the povider id to use for this provider if a provider | ||
// id is not configured explicitly. | ||
const DefaultProviderID = "aws" | ||
|
||
// ResourceTypes declares resource types supported by the AWS provider. | ||
var ResourceTypes = struct { | ||
EC2Instances, ElastiCaches, RDS string | ||
}{ | ||
"ec2_instances", | ||
"elasticaches", | ||
"rds", | ||
} | ||
|
||
type lister interface { | ||
listResources(req *pb.ListResourcesRequest) ([]*pb.Resource, error) | ||
} | ||
|
||
// Provider implements a AWS provider for a ResourceDiscovery server. | ||
type Provider struct { | ||
listers map[string]lister | ||
} | ||
|
||
// ListResources returns the list of resources based on the given request. | ||
func (p *Provider) ListResources(req *pb.ListResourcesRequest) (*pb.ListResourcesResponse, error) { | ||
lr := p.listers[req.GetResourcePath()] | ||
if lr == nil { | ||
return nil, fmt.Errorf("unknown resource type: %s", req.GetResourcePath()) | ||
} | ||
|
||
resources, err := lr.listResources(req) | ||
return &pb.ListResourcesResponse{Resources: resources}, err | ||
} | ||
|
||
func initAWSProject(c *configpb.ProviderConfig, l *logger.Logger) (map[string]lister, error) { | ||
resourceLister := make(map[string]lister) | ||
|
||
// Enable EC2 instances lister if configured. | ||
if c.GetEc2Instances() != nil { | ||
lr, err := newEC2InstancesLister(c.GetEc2Instances(), c.GetRegion(), l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resourceLister[ResourceTypes.EC2Instances] = lr | ||
} | ||
|
||
// Enable ElastiCaches lister if configured. | ||
if c.GetElasticaches() != nil { | ||
lr, err := newElastiCacheLister(c.GetElasticaches(), c.GetRegion(), l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resourceLister[ResourceTypes.ElastiCaches] = lr | ||
} | ||
|
||
// Enable RDS (AWS) if configured. | ||
if c.GetRds() != nil { | ||
lr, err := newRdsLister(c.GetRds(), c.GetRegion(), l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
resourceLister[ResourceTypes.RDS] = lr | ||
} | ||
|
||
return resourceLister, nil | ||
} | ||
|
||
// New creates a AWS provider for RDS server, based on the provided config. | ||
func New(c *configpb.ProviderConfig, l *logger.Logger) (*Provider, error) { | ||
resourceLister, err := initAWSProject(c, l) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &Provider{ | ||
listers: resourceLister, | ||
}, nil | ||
} | ||
|
||
// DefaultProviderConfig is a convenience function that builds and returns a | ||
// basic AWS provider config based on the given parameters. | ||
func DefaultProviderConfig(resTypes map[string]string, reEvalSec int) *serverconfigpb.Provider { | ||
c := &configpb.ProviderConfig{} | ||
|
||
for k := range resTypes { | ||
switch k { | ||
case ResourceTypes.EC2Instances: | ||
c.Ec2Instances = &configpb.EC2Instances{ | ||
ReEvalSec: proto.Int32(int32(reEvalSec)), | ||
} | ||
|
||
case ResourceTypes.ElastiCaches: | ||
c.Elasticaches = &configpb.ElastiCaches{ | ||
ReEvalSec: proto.Int32(int32(reEvalSec)), | ||
} | ||
|
||
case ResourceTypes.RDS: | ||
c.Rds = &configpb.RDS{ | ||
ReEvalSec: proto.Int32(int32(reEvalSec)), | ||
} | ||
} | ||
} | ||
|
||
return &serverconfigpb.Provider{ | ||
Id: proto.String(DefaultProviderID), | ||
Config: &serverconfigpb.Provider_AwsConfig{AwsConfig: c}, | ||
} | ||
} |
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,100 @@ | ||
// Copyright 2019 The Cloudprober Authors. | ||
// | ||
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd put the latest year in the copyright headers of new *.go files. |
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License 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 aws | ||
|
||
import ( | ||
"testing" | ||
|
||
serverconfigpb "github.com/cloudprober/cloudprober/internal/rds/server/proto" | ||
) | ||
|
||
func testAWSConfig(t *testing.T, pc *serverconfigpb.Provider, awsInstances bool, rdsConfig, elasticCachesConfig string, reEvalSec int) { | ||
t.Helper() | ||
|
||
if pc.GetId() != DefaultProviderID { | ||
t.Errorf("pc.GetId()=%s, wanted=%s", pc.GetId(), DefaultProviderID) | ||
} | ||
c := pc.GetAwsConfig() | ||
|
||
if !awsInstances { | ||
if c.GetEc2Instances() != nil { | ||
t.Errorf("c.GetEc2Instances()=%v, wanted=nil", c.GetEc2Instances()) | ||
} | ||
} else { | ||
if c.GetEc2Instances() == nil { | ||
t.Fatal("c.GetGceInstances() is nil, wanted=not-nil") | ||
} | ||
if c.GetEc2Instances().GetReEvalSec() != int32(reEvalSec) { | ||
t.Errorf("AWS instance reEvalSec=%d, wanted=%d", c.GetEc2Instances().GetReEvalSec(), reEvalSec) | ||
} | ||
} | ||
|
||
// Verify that RDS config is set correctly. | ||
if rdsConfig == "" { | ||
if c.GetRds() != nil { | ||
t.Errorf("c.GetRds()=%v, wanted=nil", c.GetRds()) | ||
} | ||
} else { | ||
if c.GetRds() == nil { | ||
t.Fatalf("c.GetRds()=nil, wanted=not-nil") | ||
} | ||
if c.GetRds().GetReEvalSec() != int32(reEvalSec) { | ||
t.Errorf("RDS config reEvalSec=%d, wanted=%d", c.GetRds().GetReEvalSec(), reEvalSec) | ||
} | ||
} | ||
|
||
// Verify that Elasticache is set correctly. | ||
if elasticCachesConfig == "" { | ||
if c.GetElasticaches() != nil { | ||
t.Errorf("c.GetElasticaches()=%v, wanted=nil", c.GetElasticaches()) | ||
} | ||
} else { | ||
if c.GetElasticaches() == nil { | ||
t.Fatalf("c.GetElasticaches()=nil, wanted=not-nil") | ||
} | ||
if c.GetElasticaches().GetReEvalSec() != int32(reEvalSec) { | ||
t.Errorf("Elasticaches config reEvalSec=%d, wanted=%d", c.GetElasticaches().GetReEvalSec(), reEvalSec) | ||
} | ||
} | ||
} | ||
|
||
func TestDefaultProviderConfig(t *testing.T) { | ||
resTypes := map[string]string{ | ||
ResourceTypes.EC2Instances: "", | ||
} | ||
|
||
c := DefaultProviderConfig(resTypes, 10) | ||
testAWSConfig(t, c, true, "", "", 10) | ||
|
||
// Elasticache and RDS | ||
testElastiCacheConfig := "elasticaches" | ||
testRDSConfig := "rds" | ||
|
||
resTypes = map[string]string{ | ||
ResourceTypes.ElastiCaches: testElastiCacheConfig, | ||
ResourceTypes.RDS: testRDSConfig, | ||
} | ||
c = DefaultProviderConfig(resTypes, 10) | ||
testAWSConfig(t, c, false, testRDSConfig, testElastiCacheConfig, 10) | ||
|
||
// EC2 instances, RTC and pub-sub | ||
resTypes = map[string]string{ | ||
ResourceTypes.EC2Instances: "", | ||
ResourceTypes.ElastiCaches: testElastiCacheConfig, | ||
ResourceTypes.RDS: testRDSConfig, | ||
} | ||
c = DefaultProviderConfig(resTypes, 10) | ||
testAWSConfig(t, c, true, testRDSConfig, testElastiCacheConfig, 10) | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand why this file is included in this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's imports tool, placing the internal package in the top section, seems like a good change but I can remove if you want
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, actually flag is in the middle for a reason. It's required for Google's importing tools to work properly. Please drop this file.